Start by decomposing the request
Before changing any setting, measure input tokens per request by stage. The table below shows where tokens enter a RAG request, from retrieval through the final model call.
| Stage | What it adds to the request |
|---|---|
| Retrieval | Number and size of passages returned for the query. |
| Prompt assembly | System prompt, templates, and formatting added around the content. |
| Conversation state | Prior turns carried into each new request. |
| Tool and agent outputs | Results appended to context during multi-step runs. |
| Model call | Final input and output token counts that drive cost and latency. |
Common sources of context bloat and application-level controls
Once you know which stage dominates, apply pipeline fixes. Each control carries a tradeoff.
Retrieval: top-k and reranking
Most reductions come from retrieving less, and retrieving better. Lowering top-k is the simplest change, but it trades recall for cost: if the relevant passage ranked sixth, it is now gone. A reranker lets you retrieve broadly, then pass only the best few passages to the model. It adds a processing step and latency of its own, so the net effect should be measured rather than assumed.
Chunking and duplicate passages
Chunking strategy sets the floor for how much text each retrieved item carries. Oversized chunks bring irrelevant text along with the answer, while very small chunks can lose the surrounding meaning and push you to retrieve more of them. Overlap between chunks is a frequent source of duplicate passages, which deduplication or maximal-marginal-relevance style selection can remove.
Compression, summarization, and history truncation
Context compression and summarization shrink what remains, at the risk of dropping details the model needed. History truncation is predictable and cheap, while summarizing older turns preserves more intent but adds a model call and a place where errors can enter.
Enforcing context policy at runtime
After pipeline fixes are in place, enforce token budgets and context limits in a central runtime layer that logs what entered the model context. This keeps behavior consistent across applications and teams.
Telemetry and audit data to capture per request
- Request ID, timestamp, model identifier, and policy version applied
- Application, team, and user or service identity for attribution
- Input tokens split by system prompt, history, retrieved passages, and tool outputs
- Retrieval details: passages returned, passages kept, and their document identifiers
- Policy decisions: limits checked, truncation applied, requests blocked or flagged
- The assembled context or a reference to it, with a defined retention period and access controls