HANDOFF
Reporting on autonomous software

'Prompt is too long' mid-run: clear tool results before you compact

An agent run failing with 'prompt is too long' has filled the context window. Clearing old tool results cut tokens 84% in a 100-turn Anthropic evaluation.

Reported figures
84%
From Anthropic's post 'Managing context on the Claude Developer Platform', 29 September 2025: context editing reduced token consumption by 84% in a 100-turn web search evaluation. Anthropic's internal evaluation on models of that date; not our measurement.
By , Handoff Published 6 min read

Key takeaways

  • On the Claude API, a request whose input alone exceeds the context window fails with a 400 invalid_request_error, 'prompt is too long', on every model. Tool results, tool definitions and cached tokens all count toward the window.
  • In agent runs, old tool results are usually the bulk of the window. Clearing them reduced token consumption by 84% in Anthropic's 100-turn web search evaluation, and improved task performance by 29%.
  • Anthropic's tool result clearing fires at 100,000 input tokens by default and keeps the last 3 tool uses. Its threshold compaction fires at 150,000 by default, with a minimum of 50,000.
  • Compaction is a billed model call. The summarisation step appears as a separate entry in usage.iterations, and the top-level token counts show only the final iteration, so a cost report that reads only the top level undercounts.

An agent run does not usually die from one huge message. It dies at turn sixty, when the sum of everything it has read finally crosses the line. On the Claude API, that request fails with a 400 invalid_request_error, “prompt is too long”, on every model, according to Anthropic’s context windows documentation read on 13 October 2026.

The fix most teams reach for first is summarising the conversation. In tool-heavy runs, that is often the second-best option and the more expensive one.

What actually fills the window in an agent run?

Everything in the request counts: the system prompt, every message, every tool result, images and documents, and the tool definitions. Cached tokens count too. Anthropic’s documentation is explicit that prompt caching changes what you pay for those tokens, not whether they occupy the window.

In a run that searches, reads files or calls APIs, tool results grow fastest. A page of search results or a file read can be thousands of tokens, and the agent may need it for one step. Every later turn resends it anyway.

Two different failures sit at the limit, and they need different handling.

What the Claude API does when a request reaches the context window. Source: Anthropic context windows and stop reasons documentation, read 13 October 2026.
SituationResultHTTP statusWhat it means for the run
Input alone exceeds the windowinvalid_request_error, prompt is too long400Nothing generated; shrink input before retrying
Input plus max_tokens exceeds the window, Claude 4.5 and newerAccepted; may stop with model_context_window_exceeded200Output truncated; treat as incomplete
Same, on earlier modelsValidation error unless a beta header opts in400Lower max_tokens or shrink input

Current Claude models have a 1M-token window, and older ones 200k. A larger window delays the error; it does not remove it. Long before the hard limit, quality drops as context grows, which is the context rot problem in long agent runs.

Which fix removes the most tokens for the least money?

Clearing old tool results, in most agent runs.

Anthropic reported in September 2025 that context editing, which clears stale tool calls and results, cut token consumption by 84% in a 100-turn web search evaluation and improved task performance by 29%. Combined with a memory tool, so the agent could save what mattered before it was cleared, the improvement was 39%. These are Anthropic’s internal figures on models of that date.

Context management options on the Claude API, with default thresholds and cost side effects. Sources: Anthropic context editing and compaction documentation, read 13 October 2026. The cost column combines documented behaviour with our editorial judgement.
OptionDefault triggerWhat it keepsCost side effect
Tool result clearing, clear_tool_uses_20250919100,000 input tokensLast 3 tool usesBreaks the cached prefix each time it clears
Threshold compaction, compact_20260112150,000 input tokens (min 50,000)A model-written summaryAn extra billed model call over the full history
Your own summariserYou decideWhatever your prompt keepsSame extra call, plus code to maintain

Clearing is cheap per event but not free. Each clearing changes the prefix, so the next request misses the cache. Anthropic’s clear_at_least setting exists so that each clearing removes enough tokens to pay for that miss. If the cache hit rate falls after you turn clearing on, the cache invalidation patterns we covered earlier are the place to look.

exclude_tools keeps results from named tools, and clear_tool_inputs clears the call arguments as well as the results. A tool whose output the agent must cite later belongs on the exclude list.

Why can compaction make the bill look smaller than it is?

Because the summary is written by a model call, and the usage report splits it out.

In Anthropic’s threshold compaction, the summarisation step appears as its own entry in usage.iterations. The documentation’s example shows a compaction iteration of 180,000 input and 3,500 output tokens, followed by the message iteration. The top-level input_tokens and output_tokens reflect only the final message iteration. A cost pipeline that reads only the top level will miss the largest call in that request.

If you track cost per agent task, sum the iterations. Otherwise compaction will look like it made long runs cheaper than it did.

Compaction also loses detail by design. Instructions given early in the run can disappear into the summary. Rules that must survive belong in the system prompt or a re-injected file, not in turn one.

What should a run do when it gets close?

Act before the 400, in this order. The order is our editorial judgement, built on the documented defaults above:

  1. Measure. Track input tokens per turn and alert well below the window, not at it.
  2. Clear old tool results once the run passes a threshold you choose, keeping the most recent few and excluding tools the agent must cite.
  3. Compact only when the conversation itself, not tool output, is what has grown, and count the compaction call in cost.
  4. Move durable facts out of the conversation into memory or state the agent can re-read, which is the trade-off between the four places agent memory can live.

The question for a run that just failed with “prompt is too long”: how many of the tokens in that request were tool results the agent had already used?

Frequently asked questions

What does 'prompt is too long' mean in an AI agent?

It means the full request, including system prompt, tool definitions, every earlier message and every tool result, no longer fits the model's context window. The Claude API rejects it with a 400 error before generating anything.

Should I clear tool results or compact the conversation?

Clear old tool results first when they are most of the window, because it removes bulk without a summarisation call. Compact when the conversation itself, not tool output, is what has grown.

Does context editing break prompt caching?

Yes, when it clears content. Anthropic's documentation says tool result clearing invalidates cached prefixes, and recommends clear_at_least so each clearing removes enough tokens to be worth the cache miss.

Method and sources

  1. Anthropic, Context windows, Claude API documentation, read 13 October 2026. Source of the 400 'prompt is too long' behaviour, the model_context_window_exceeded behaviour on Claude 4.5 and newer, what counts toward the window, the 1M and 200k window sizes, and the higher price above 100,000 prompt tokens on Claude Haiku 5.5.
  2. Anthropic, Context editing, read 13 October 2026. Source of clear_tool_uses_20250919 with its 100,000-token default trigger and default of keeping 3 tool uses, clear_at_least, exclude_tools, clear_tool_inputs and the cache invalidation note.
  3. Anthropic, Compaction at a token threshold, read 13 October 2026. Source of compact_20260112, the 150,000-token default trigger, the 50,000-token minimum, pause_after_compaction and the usage.iterations reporting, including the example compaction iteration of 180,000 input and 3,500 output tokens.
  4. Anthropic, Managing context on the Claude Developer Platform, 29 September 2025. Source of the 29% and 39% performance improvements and the 84% token reduction in a 100-turn web search evaluation. Anthropic's internal evaluation; sample size not published.
  5. The order of fixes in the last section is our editorial judgement. We ran no context management experiment of our own.
CD

, Handoff at Handoff

The Cost desk of Handoff. Every figure here carries a source, a sample size and a date, and we say plainly what we have not measured ourselves.