Stopping Context Window Growth from Raising Per-Turn Cost in Long AI Agent Sessions
Per-turn cost rises in long agent sessions because every turn resends accumulated history, tool outputs and retrieved content. Keep it bounded by layering controls: cap tool output and retrieved content at entry, clear stale results, compact older history, move durable facts to external memory, reuse a stable prompt prefix, and isolate sub-task context. Record every reduction so you can still show what the agent was given.
A sequence for implementing bounded context
- 1
1. Set backstops
Apply per-user rate limits at a gateway or middleware, hard spending limits and billing alerts, and loop limits. Unbounded loops are a recognized cost risk and these limits work regardless of context technique.
- 2
2. Measure growth
Record input tokens per turn and the share contributed by history, tool output and retrieved content. This shows which technique matters first.
- 3
3. Bound entry
Cap tool output size and the number and total size of retrieved chunks. Restrict context to the minimum content needed for the task.
- 4
4. Clear and compact
Replace stale tool results with placeholders, then add compaction on a defined trigger, with pinned instructions excluded.
- 5
5. Externalize and isolate
Move durable facts to scoped external memory and delegate bounded subtasks to separate contexts that return compact results.
- 6
6. Stabilize the prefix
Order the prompt so stable content comes first and changes rarely, then use prompt caching where your provider supports it.
Per-turn records to design in before rollout
- What was trimmed, cleared or replaced, and which rule triggered it
- The summary or placeholder that replaced the original content
- What was redacted or blocked, and the policy decision behind it
- The input the model actually received on that turn, or a reference to rebuild it
- Tool calls, arguments and outcomes, including calls the policy refused
- Memory writes and reads, scoped to user and session, with integrity checks
Why per-turn token consumption grows in agent loops
A model call is stateless. In a multi-turn agent loop, the runtime rebuilds the full input on every turn: system prompt, tool definitions, the conversation so far, each tool call and its result, and any retrieved documents. The agent's work on turn 40 therefore pays for everything that happened on turns 1 to 39. Input tokens, latency and per-turn cost rise with session length even when each new step is small.
Tool output is usually the largest contributor. A single query result, file read or web fetch can exceed all the reasoning text around it, and once it is in the history it is resent on every later turn whether or not it is still relevant. Retrieved content behaves the same way. Oversized retrieved chunks also carry a quality risk: a long retrieved document can push the system prompt out of the model's effective attention window, which is why limits on the number and total size of chunks are recommended.
Treat this as an architecture and lifecycle problem. No single technique bounds context without a trade-off, so the practical question is which combination you can apply, in what order, and with what record of what each step removed.
Context management techniques compared
The techniques below are ordered roughly by implementation effort and risk. Each changes what the model sees, so each needs an evaluation against your own task set before rollout.
- Truncation or sliding window: Lowest effort. Drops the oldest turns. Failure mode: early constraints, instructions or decisions disappear without notice. Pin the system prompt and standing requirements outside the window.
- Tool-output limits and clearing: Cap result size at entry and replace stale results with a short placeholder. Targets the largest source of growth. Failure mode: the agent needs a cleared result later and must call the tool again, or proceeds without it.
- Summarization and compaction: Replaces older history with a model-written summary. Keeps continuity at lower token cost. Failure mode: lossy summaries omit details, flatten uncertainty or introduce errors that persist for the rest of the session.
- External memory and retrieval: Stores facts and artifacts outside the prompt and retrieves on demand. Bounds context well. Adds retrieval quality risk, a write path to validate, and a scoping requirement between users and sessions.
- Prompt caching: Reuses a stable, repeated prefix so repeated input is processed more cheaply. It reduces the cost of resending, not the size of the context. Edits earlier in the prompt generally reduce reuse, so summaries that rewrite the prefix can work against it. Confirm your provider's documented limits.
- Sub-agent context isolation: Delegates a bounded task to a separate context and returns only a compact result. Keeps the parent small. Failure mode: the returned result drops detail the parent needs, and multi-agent designs raise cost exposure if loops are unbounded.
Failure modes and how to evaluate them
Context reduction fails quietly. The agent keeps running, but it may have lost a constraint stated in turn three, a tool result that justified a later action, or an instruction that was only ever given once. Design for this explicitly.
Separate content that must never be reduced from content that can be. Standing instructions, policy constraints and user-stated requirements belong in a pinned section that truncation and summarization do not touch. Everything else is a candidate for clearing or compaction.
Evaluate with your own tasks. Run the same long-session scenarios with and without each technique, and check whether early constraints are still honored, whether the agent repeats tool calls it already made, and whether final outputs match. Include cases where a fact from early in the session is needed late. Compare latency and token counts alongside accuracy so a saving is not bought with silent degradation.
Treat tool output and retrieved content as untrusted until validated, including content that has been summarized. A summary of an injected instruction is still an injected instruction, now with less visible provenance. Scan retrieved chunks for injection patterns before they enter context, and validate and sanitize data before it is written to agent memory.
What a regulated enterprise must preserve
Every reduction technique answers the question "what does the model see?" differently from the full history. A regulated enterprise also has to answer the later question: what was the agent given, and what did it do? If a tool result was cleared, a history was summarized or a field was redacted, the record should show that, not just the final prompt.
Logging all agent decisions, tool calls and outcomes is the baseline. It does not by itself capture reductions, so that needs separate design. For each turn, retain what was removed or replaced, the rule or trigger that caused it, the summary text that replaced the original, and a reference to the original content under your retention policy where policy allows. Where content was redacted because it was sensitive, keep the fact and basis of the redaction rather than the sensitive value.
Stored memory should be trustworthy as a record, so use integrity checks on long-term memory and isolate memory and context between users and sessions. Shared or insufficiently scoped context can expose sensitive information from one task, user or agent to another, and isolation should be enforced by design rather than by prompt wording.
Runtime policy enforcement helps at the entry point. Trussed enforces policy in the execution path on individual interactions and tool calls, applying allow, redact or block decisions at runtime, not reviewed after the fact. That can keep sensitive or oversized tool output out of the context before it accumulates, and the enforcement itself generates audit-ready evidence of what the agent was permitted to do and what it did. It does not replace the agent runtime's own record of how it summarized or compacted history, and it is not end-to-end tracing of an agent workflow.
Where context accumulates and where to bound it
Entry
Cap, screen and redact tool output and retrieved chunks before they join the context.
In session
Clear stale tool results and compact older history on a defined trigger.
Across sessions
Store durable facts in scoped external memory and retrieve only what the task needs.
Record
Keep a durable note of what was trimmed, summarized or redacted on each turn.
Keep context bounded and the record intact
See how policy enforcement on interactions and tool calls can keep sensitive or oversized output out of agent context and produce audit-ready evidence.
Explore Agent and Tool-Call EnforcementHow does your AI governance program compare?
See where your program has gaps in less than 2 minutes.