Context assembly transforms retrieved document chunks and a user query into structured context that a language model can process. It bridges semantic search and coherent prompt construction by formatting, deduplicating, and organizing retrieved information. section 12.2.5
A production-oriented assembly flow should:
- Load the persistent vector index with the same embedding model, dimensions, normalization, and model version used during indexing. section 12.2.5
- Expose retrieval through a standardized interface, then combine document contents with clear separators for prompt construction. section 12.2.5
- Monitor reasoning paths, token allocation, context utilization, and failures across retrieval, assembly, and generation. section 12.4.4
Policy question prompt
For a question such as “What’s our vacation policy?”, assemble a prompt containing the relevant handbook sections followed by the user’s question: “Based on these policy sections: [chunk 1], [chunk 2], [chunk 3], answer: What’s our vacation policy?” section 12.2.5
Listing 12.10 RAG query processing with retrieval and context
query = "How do I rotate encryption keys?" #1
docs = retriever.invoke(query) #2
context = "\n\n".join( #3
[doc.page_content for doc in docs]) #3
Shows the minimal retrieval-to-context operation: invoke the retriever, extract page content, and join documents into a single context string. section 12.2.5
