Context assembly & prompt design
Turn retrieved chunks into a prompt that fits the budget and that the model actually uses well.
Retrieval gives you candidates. Assembly decides what the model sees. Set a token budget for context, remove duplicates, and include the source id, title and date with each passage so the model can cite and weigh them.
Models tend to use information near the start and end of a long context better than information in the middle, so put the strongest evidence first and consider repeating the key instruction at the end. More context is not better: extra weak passages can pull the model off the right answer.
State the contract in the prompt: answer only from the provided sources, cite by id, say so when the sources do not contain the answer, and prefer newer sources when they conflict. Keep the instruction block stable across calls so providers can cache it.
Key points
- Budget tokens for context and drop duplicates and weak passages.
- Include source id, title and date with every passage.
- Place the strongest evidence early. Do not bury it in the middle.
- Spell out the contract: sources only, cite, admit gaps, prefer recent.
Answer these out loud
- 1You have 6,000 tokens of context budget and 14 candidate chunks. Describe how you choose and order them.
- 2Two retrieved passages contradict each other. What instruction and metadata let the model handle it?
Use this from an agent
Any MCP client, including Grok, can read this lesson. See how to connect.
curl https://engineeros-delta.vercel.app/api/v1/topics/rag-context-assembly