Context Engineering for Agentic RAG: Caching and Cost Control
Overview
Single-shot RAG is well understood: retrieve once, generate once, cost is predictable. Agentic RAG behaves differently. The agent decides what to look up, reads the result, decides whether it needs more, and iterates — which means several retrievals per question and a context that grows with every step. The cost profile changes from a fixed amount per query to a variable amount that depends on how many steps the agent takes, and without deliberate control it grows faster than the value it delivers. Context engineering is the discipline that keeps it bounded.


Key takeaways
- Cost is variable per query — it depends on how many retrieval and reasoning steps the agent takes.
- KV cache growth binds before compute — accumulated context, not model size, exhausts memory.
- Prefix caching is the largest single saving, since agent trajectories share long common prefixes.
- Compact aggressively — summarise retrieved passages once used rather than carrying them verbatim.
- Set explicit budgets — caps on steps, retrievals and total context prevent runaway queries.
Why the cost structure changes
In single-shot RAG the prompt is bounded: a system prompt, a fixed number of retrieved passages, the user question. Cost per query is therefore roughly constant and easy to plan against. In agentic RAG the trajectory is open-ended — the agent may retrieve twice, or six times, reformulating queries and reading results in between.
Two effects follow. Total tokens processed per question rise several-fold, since each step reprocesses the accumulated context plus the new material. And variance rises sharply: a simple question resolves in one step while a hard one takes eight, so capacity planning must consider the tail rather than the mean. Sizing on average step count consistently under-provisions.
KV cache is the binding constraint
Each active agent session holds attention cache for its whole accumulated context. Retrieved passages are long, and an agent that has read four documents carries all of them forward unless something removes them. Concurrency multiplies this: ten simultaneous sessions each holding 30,000 tokens of trajectory is a substantial memory commitment before the model weights are counted.
The observable failure is not a crash but degradation — the serving system evicts cache, recomputes, and latency rises for everyone. Because it presents as general slowness rather than an error, teams often respond by adding GPUs when the correct fix is bounding context. Measuring cache occupancy per session is the diagnostic that distinguishes the two.
Prefix caching
| Technique | What it saves | When it applies | Cost |
|---|---|---|---|
| Prefix KV caching | Recomputation of shared prompt prefix | Stable system prompt and tool definitions | Memory for the cached prefix |
| Session cache reuse | Recomputation of prior steps | Within a single agent trajectory | Memory held for session duration |
| Retrieval result cache | Repeated identical retrievals | Common questions across users | Small; staleness risk |
| Compaction | Cache for superseded steps | After a step’s conclusion is captured | Some information loss |
| Step and retrieval caps | Runaway trajectories | Always | Some queries answered incompletely |
Prefix caching deserves the most attention because it is nearly free in quality terms. Agent prompts begin with the same system instructions, tool schemas and often the same organisational context on every request; caching that prefix’s attention state avoids recomputing thousands of tokens per step. Structuring prompts so stable content comes first and variable content last is what makes the cache hit.
Compaction without losing the thread
Compaction replaces verbatim history with a summary. Done crudely it destroys the agent’s ability to reason about what it has already learned; done well it is the difference between a system that scales and one that does not. Three practices help.
Summarise per step rather than in bulk: when a retrieval result has been used, replace the full passage with a short statement of what it established, retaining a citation so the source can be re-fetched if needed. Keep the original user question and the running plan verbatim, since those anchor the trajectory. And retain the most recent step in full, because the agent’s immediate next decision usually depends on detail rather than summary. The entry-tier version of this pattern is in running local agents on entry hardware.
Retrieval budgets
An agentic system needs explicit limits, and they should be product decisions rather than accidents of implementation. Useful budgets: maximum retrieval calls per question, maximum total context tokens, maximum reasoning steps, and a wall-clock timeout. When a budget is exhausted, the correct behaviour is to answer with what has been gathered and say so, not to fail silently or continue indefinitely.
Setting the numbers is empirical. Instrument a pilot, plot answer quality against step count, and find the point where additional steps stop improving answers — in most enterprise deployments that plateau arrives earlier than expected, often around three to five retrievals. Budgeting just above the plateau captures nearly all the quality at a fraction of the tail cost.
What this means for capacity planning
Three planning consequences. Size KV cache explicitly rather than treating it as a residual, using measured per-session occupancy at your typical trajectory length multiplied by target concurrency. Provision for the tail, since a small fraction of long trajectories consumes a disproportionate share of memory. And separate the retrieval tier from the generation tier so that embedding and search load does not contend with generation for the same accelerators.
The prefill-heavy character of agentic RAG — long contexts processed repeatedly — also makes it a strong candidate for the architecture in disaggregated inference, where prefill and decode are served by different pools. And the decision of when an agent should retrieve at all rather than rely on a long context is covered in long context vs RAG in 2026.
Frequently asked questions
Why is agentic RAG more expensive than single-shot RAG?
Because the agent retrieves several times and reprocesses accumulated context at each step, so total tokens per question rise several-fold. Cost also becomes variable rather than fixed, with a long tail of hard questions consuming disproportionate resources.
What actually runs out first?
KV cache. Each session holds attention state for its whole accumulated trajectory, and concurrency multiplies it. The failure presents as general latency degradation from cache eviction rather than as an error, which is why it is often misdiagnosed as needing more GPUs.
What is prefix caching and how much does it help?
It stores the attention state for the shared beginning of every prompt — system instructions, tool schemas, organisational context — so it is not recomputed each step. It is close to free in quality terms and usually the largest single saving, provided prompts put stable content first.
How should context be compacted without losing quality?
Summarise each retrieval result once used, replacing the passage with what it established plus a citation; keep the original question and running plan verbatim; and retain the most recent step in full, since the next decision usually depends on its detail.
How do I set retrieval budgets?
Empirically. Instrument a pilot and plot answer quality against step count to find where additional steps stop helping — often around three to five retrievals. Cap just above that plateau, and when a budget is exhausted, answer with what was gathered and say so.
Ready to deploy?
Talk to an RDP architect about power, cooling and lead time.