Context Engineering Tricks for More Precise RAG

By Carlos Montiel | Enterprise AI Specialist
Leer en español →
Published: 2026-07-28 | By: Carlos Montiel | Reading time: ~5 minutes

A RAG system that retrieves the right documents but presents them poorly to the model gets the same result as one that retrieves badly: imprecise answers. Retrieval is half the problem -- how you structure what's retrieved is the other half, and it's the half fewer teams take care of.

The "lost in the middle" problem

Language models don't pay uniform attention to all of their context. The consistent evidence across the industry is that information at the beginning and end of the context gets processed more faithfully than what sits in the middle. If your RAG stuffs in 10 documents and the critical fact is in document number 6, the model is more likely to miss it than if it were in document 1 or 10.

The practical implication: it's not enough to retrieve the right documents by raw relevance -- the **order** in which you present them matters. Put the most relevant document (per your reranker) at the beginning and end, not buried in the middle of a long list.

def order_for_context(ranked_documents): # The most relevant go to the extremes, not all in simple # descending order n = len(ranked_documents) order = [None] * n for i, doc in enumerate(ranked_documents): if i % 2 == 0: order[i // 2] = doc # fills from the start else: order[n - 1 - i // 2] = doc # fills from the end return order

Reranking: vector retrieval isn't enough

Embedding similarity search retrieves "semantically similar" documents, but that's not the same as "the documents that actually answer the question." A reranking step -- a model (or even the same LLM) that reorders by true relevance to the specific query -- drastically reduces the noise that reaches the final context.

# Typical pattern: broad retrieval, narrow reranking candidates = vector_search(query, k=30) # broad recall, cheap top_k = rerank(query, candidates, top_k=5) # precision, more expensive but # over 30 candidates, not # the whole index

The "broad recall + narrow reranking" pattern is cheaper and more precise than raising the vector retrieval's `k` directly, because the reranker operates over a small, already pre-filtered set.

Structure the context, don't concatenate it raw

A common mistake: concatenating retrieved chunks as plain text, with no marker for where each source starts and ends. This keeps the model from distinguishing where each claim comes from, hurting both precision and the ability to cite sources.

structured_context = "\n\n".join( f'\n{doc.text}\n' for doc in retrieved_documents ) prompt = f""" Answer the question using only the provided documents. Cite the document id that backs each claim. {structured_context} Question: {user_question} """

Delimiting each source with XML or markdown isn't cosmetic -- it measurably improves the model's ability to correctly attribute each fact to its origin, and makes it possible to ask for verifiable citations.

Use native citations when the model supports them

Some models support a native citations mode: instead of asking the model to "cite the source" in free text (which can hallucinate the citation), the system automatically returns which exact document fragment backs each part of the response.

response = client.messages.create( model="claude-opus-4-8", max_tokens=1024, messages=[{ "role": "user", "content": [ { "type": "document", "source": {"type": "text", "media_type": "text/plain", "data": document_text}, "citations": {"enabled": True}, }, {"type": "text", "text": user_question}, ], }], ) for block in response.content: if block.type == "text" and block.citations: for citation in block.citations: print(citation.cited_text, citation.document_title)

This eliminates an entire class of hallucination: the "invented" citation that sounds plausible but doesn't correspond to the real text.

Deduplicate before dumping everything into the context

It's common for retrieval to return multiple chunks saying essentially the same thing (the same fact repeated across different sections of a document, or in near-duplicate documents). Feeding all three into the context doesn't improve the answer -- it inflates tokens and increases the surface for contradictions if the versions have slightly different nuances.

Simple deduplication by similarity across retrieved chunks, before building the final prompt, usually shrinks context size without losing coverage.

Cache retrieved context when it's reusable

If multiple users ask about the same base document, or the same user asks several questions over the same retrieved context in a session, that context block is a direct candidate for `cache_control` -- exactly the same principle from the prompt caching article, applied specifically to RAG-retrieved content instead of the system prompt.

messages = [{ "role": "user", "content": [ { "type": "text", "text": structured_context, # the block of retrieved documents "cache_control": {"type": "ephemeral"}, }, {"type": "text", "text": user_question}, # varies, no cache_control ], }]

Measure retrieval precision separately from generation precision

The most common final mistake: when RAG fails, people assume it's the model generating a bad answer. Before touching the prompt, verify whether the problem is that retrieval never brought back the right document in the first place. An evaluation set with questions whose correct answer you know, and the document that contains it identified beforehand, lets you measure separately:

- **Retrieval recall:** was the correct document among the retrieved ones? - **Generation precision:** given that the correct document was present, did the model use it well?

Mixing these two metrics is the number one reason teams "fix" the prompt when the real problem was in the search index.

Carlos Montiel
Enterprise AI Solutions Architect
Specialist in LLMs, Agents, and Orchestration
guatemalia.com/en/#contact · info@guatemalia.com

Need to implement AI at your company?

Carlos Montiel is an enterprise AI solutions architect. He implements LLMs, Agents, RAG, and orchestrators for companies across Guatemala and Latin America. Reach out for a consultation.

Contact Carlos Montiel

info@guatemalia.com