Course contentsShow
← AI Engineering
Lesson 351 of 1,8869. Retrieval-Augmented Generation (RAG) - BasicsFree lesson

Retrieved Document Formatting

Structuring retrieved chunks with delimiters, numbering, and metadata for optimal LLM comprehension.

Retrieved Document Formatting

What you'll learn: How to structure retrieved chunks so your LLM can easily understand and use them as context.

Why Formatting Matters

You've retrieved the most relevant chunks from your vector database—great! But simply dumping them as raw text into your prompt is like handing someone a pile of unorganized research papers. The LLM needs structure to know what it's looking at and how to use it effectively.

Proper formatting turns messy retrieval results into clean, organized context that maximizes comprehension within your limited context window.

Core Formatting Strategies

Delimiters and Boundaries

Wrap each chunk in clear separators so the LLM knows where one document ends and another begins:

--- Document 1 ---
[chunk content]
--- Document 2 ---
[chunk content]

Numbering for Reference

Number your chunks so the LLM (and you) can cite sources:

[1] The Python dict type uses hash tables internally...
[2] Hash collisions are resolved through open addressing...

Metadata Inclusion

Add relevant metadata you preserved during chunking—source, date, relevance score:

Source: python_docs.pdf | Score: 0.94
Content: The dict.get() method returns None if the key doesn't exist...

The Complete Picture

Well-formatted retrieved context looks like this:

Context Documents (3 results):

[1] Source: api_guide.md | Relevance: 0.96
The authenticate() function requires both username and password parameters...

[2] Source: security_best_practices.pdf | Relevance: 0.89
Always hash passwords using bcrypt before storing in the database...

Key Takeaway: Structure your retrieved chunks with delimiters, numbering, and metadata to help the LLM parse and utilize context effectively—clear formatting dramatically improves generation quality.