AI Agent Context Window: How to Reduce Token Usage
In a long agent conversation, old lookups often fill much of what the model rereads before each reply. Clear the ones the agent can fetch again.
Naresh Tank- 9 min read
Key takeaways
- An AI agent's model reads the whole conversation again before every reply, so the context window fills up as the conversation grows, and each reply sends more tokens than the last.
- In agents that call tools, tool results are often the biggest part of that growth. Clearing old ones is one of the lightest ways to reduce token usage: the conversation history stays, and the agent can fetch the data again.
- Keep the latest tool results, the results the agent used to choose its tools, data it keeps going back to, and the result of any tool that changes something.
- A cap on conversation turns is simple and predictable, but it drops whatever the person said early on. Use it only where each topic stands alone.
- In Avesta AgentOS, context management is off by default and set per agent: three rules that clear tool results at different moments, a cap on turns, and a choice of which tools each clearing rule touches.
The fortieth message in a long chat with an order-tracking agent sends far more text to its AI model than the first one did. The model, the part that reads and writes, remembers nothing between replies. So the agent sends the whole conversation every time, and all of it has to fit in the model's context window: the most text the model can read at once.
In an agent like this, much of that text is tool results: the raw data that comes back when the agent asks another system for something. That might be an order, a shipment or a customer record. One result can run to thousands of tokens, which are the small chunks of text a model reads and is billed by.
Clearing old tool results means leaving them out of what the agent sends to the model, while the chat itself stays whole. It is one of the lightest fixes. The hard part is choosing which ones to clear, and when: an agent that answers with one lookup needs a different rule from one working through a long, multi-part task. Our earlier post shows how much clearing saved in one of our agents.
What is an AI agent context window?
An AI agent context window is the most text its model can read in one request. It holds the agent's instructions, its tool descriptions, the conversation so far and every tool result in it, measured in tokens. In a long conversation it fills up, so each reply costs more and the model gets worse at finding what it needs.
Anthropic's guide to context windows describes the build-up: each request contains "all previous conversation history plus the current user message".
In the agent harness, the software around the model, this is the part that decides what the model gets to see.
Keep what the current step needs. Clear what the agent can fetch again.
What happens when the context window is full?
Three things get worse as it fills, and the last one stops the conversation.
The cost rises. Every reply resends everything before it, so a conversation's token usage grows faster than its length.
Accuracy slips. Chroma's context rot study tested 18 models in July 2025. It found their performance "grows increasingly unreliable as input length grows", even on simple tasks. A model reading an hour of old lookups has more to sort through to find the one fact that matters now.
Then the request fails. As of December 2026, once what the agent sends is longer than the window, Anthropic's API rejects it as "prompt is too long", on every model. A retry won't help, as our post on LLM fallback strategy explains: the conversation has to get shorter, or move to a model that takes longer input.

- Before clearing: the instructions, the tool descriptions, the conversation, three old tool results and the latest one all go to the model.
- After clearing: each old result becomes a one-line note. The conversation, the tool calls and the latest result stay as they were.
How do you reduce token usage in a long agent conversation?
There are five common ways. They differ in what they cost you, so start with the light ones.
| Method | What it removes | What it costs you | Use it when |
|---|---|---|---|
| Clear old tool results | The raw output of earlier tool calls | The agent must call the tool again if it needs the data | The agent calls tools that fetch data it can look up again |
| Remove large tool arguments | What the agent sent to a tool, such as a full document | The agent no longer sees what it wrote | Tools that take long text, like writing or saving a document |
| Cap conversation turns | Whole turns beyond the last N | Anything the person said early on | Long chats where each topic stands alone |
| Summarise the conversation | The old conversation, rewritten shorter | Detail lost in the summary, plus a model call to write it | Long tasks near the limit, where the thread must carry on |
| Notes outside the window | Old detail, once the key facts are saved elsewhere | Something has to decide what to save and read it back | Facts that must last for the whole task |
Anthropic's context engineering guide calls the first one tool result clearing. It describes it as "one of the safest lightest touch forms of compaction", Anthropic's term for shrinking a long conversation so the agent can carry on.
In September 2025, Anthropic reported a 100-turn web search test of its own clearing feature, which it calls context editing. Clearing stale tool calls and results let agents finish work that would otherwise have failed, "while reducing token consumption by 84%".
Which tool results should an agent keep?
An agent works in steps: in each one, the model reads everything so far, then calls a tool or answers. One reply to a person can take several steps.
Clearing everything at once would break the step in progress and force repeat fetches, so four kinds of result are worth keeping.
- The latest results. The agent is reasoning about them right now. Clear only what came before the current step.
- Results the agent used to choose its tools. If the agent searched for the right tool or skill, clearing that search leaves it unsure which tools it has.
- Data the agent keeps going back to. Every re-fetch is another tool call, and more waiting for the person. If the agent needs the same result again and again, clearing it may cost more than it saves, so consider leaving that tool out.
- The result of any tool that changes something. "Call the tool again" is fine for a lookup. For a refund, it isn't.
Leave tools that change something out of every clearing rule.
Arguments need the same thought. When an agent saves a two-page report, the whole report travels as the tool's input. It stays in the conversation after the save, so clearing that input can save as much as clearing a large result.
How we handle context window management in Avesta AgentOS
Avesta AgentOS is the platform we build and run AI agents on. Context management is a tab on each agent, off by default.
1. From one switch to rules per moment
Our first version was a single switch per agent that cleared every tool result in earlier messages, whatever the tool.
A few weeks later we moved to rules for when to clear, what to clear and which tools, then added a cap on turns. The rules can run together or one at a time.

- Within one reply (Sanitize Previous Turn; despite the name, it works on steps). From the agent's third step, results from earlier steps are cleared and the latest step's stay. Results it used to find its tools and skills are never cleared. Use it for agents that make many tool calls per message.
- When the next plan step starts (Sanitize Completed Step, only for agents with planning on). Once the next plan step is marked in progress, results from the work before it are cleared. Use it for long multi-step tasks.
- On each new message (Sanitize Previous Messages). Before the model reads a new message, tool results in all earlier messages are cleared, searches and actions included. Use it for long chats full of new lookups, leaving out tools that change something.
- Past a set number of turns (Limit Conversation History). Only the last N turns are sent; a turn is one message from the person plus the reply, and the default is 10. Use it where each topic stands alone.
We run only the turn cap on a textile manufacturer's WhatsApp order agent. Each dealer keeps one long chat, so token usage grew with every message. Its tool results are short, so clearing them would save little.
A cap of 10 turns stops each reply resending the whole chat. The dealer is identified outside the chat, so the agent loses nothing it needs and behaves as before.
2. The person still sees everything
Clearing changes only the copy the model reads. The stored conversation isn't touched, so the person still sees every result. In the model's copy, a cleared result reads: "This tool-call result has already been saved. If you need the data again, you can call the tool again."
3. Results, arguments, or both, tool by tool
Each of the three clearing rules can remove results, arguments, or both. For results, you pick all tools or only selected ones, so you can clear the lookups and keep what an action returned.
For arguments, you pick the tools and which inputs to remove, such as a document's text; the tool's name and other inputs stay.
When is clearing tool results not enough?
Clearing assumes the old data can be fetched again, or isn't needed. Sometimes neither is true.
- When the agent needs facts from old results. If step 12 depends on a figure from step 2, clearing step 2 loses it. Have the agent write the fact down first; our compaction post shows a short notes block that does this.
- When one result is too big on its own. Clearing old results doesn't help a single 200-page document. Search or split it so the agent reads only the part it needs.
- When the conversation is short. A support agent that answers in two or three turns rarely comes near its limit. Leave context management off and keep one less thing to test.
- When the early turns carry the details. A cap on turns drops the account number the person gave in turn one. Clear tool results instead.
The bottom line
In an agent that calls tools, the context window often fills with what the agent fetched more than with what people typed. So start by finding where the tokens go.
In Avesta AgentOS, the traces show each turn's tokens and cost, and each step's input and output tokens, plus cached tokens and cost where they're recorded. Then decide which tool results the next step really needs, and let the rest go. To see what a long conversation costs per completed job, read our post on AI agent cost per task.
What share of your agent's longest conversation is tool results it could fetch again?
Want AI agents that keep long conversations lean without losing the thread? See how Avesta AgentOS manages the context window, agent by agent.
Get started: hello@avestalabs.ai
Frequently asked questions
- How big is an AI agent's context window?
- It depends on the model. As of December 2026, Anthropic's documentation lists context windows of up to 1 million tokens, depending on the model. A bigger window lets a conversation run longer before a request fails, but it doesn't keep the model accurate: studies of context rot show recall getting less reliable as the input grows.
- What is tool result clearing?
- Tool result clearing removes the raw output of earlier tool calls from what the model reads next, and leaves a short note in its place. The tool call itself stays, so the model can see that the lookup happened and what it asked for. Only the data that came back is gone, and the agent can ask for it again.
- Does clearing old tool results make an AI agent forget?
- It forgets the details of the cleared result, not that the lookup happened. If it needs the data again, it can call the tool again. That works for lookups. For facts the agent must carry forward, such as a figure it found ten steps ago, ask it to write the fact into its reply or a short note before the result is cleared.
- Does prompt caching reduce context window usage?
- No. Prompt caching makes repeated input cheaper: as of December 2026, reading from Anthropic's cache costs a tenth of the normal input price on many Claude models. But cached text still counts toward the context window. Anthropic's documentation says caching changes what you pay for those tokens, not whether they count.
- Does clearing old tool results break the prompt cache?
- Yes, each time something is cleared. Prompt caching reuses the unchanged start of a request, so clearing earlier content invalidates the cache from that point, and the next request pays to write it again. After that, the trimmed version is what gets sent, so later replies can read it from the cache until the next clearing. Anthropic's documentation advises clearing enough each time to make that cost worthwhile.
Share this article




