What a long-running request is made of
most protected → most disposable
- System promptrole, rules, tools — sent with every request
- Case facts blockamounts, dates, IDs, commitments — never summarised
- Summary of older turnscompaction output — lossy by design
- Recent turnskept verbatim
- Tool resultstrimmed on arrival, cleared when stale
Why long conversations lose things
Every request carries the whole conversation: the system prompt, every message, every tool result and the tool definitions all count toward the context window. As that grows, two separate problems appear. The first is capacity — eventually it will not fit. The second arrives much earlier: the documentation calls it context rot, the tendency for accuracy and recall to degrade as token count grows. Anthropic’s context-engineering guidance describes a model as having a limited attention budget, where every extra token competes for it. A conversation can be well inside the limit and still be too noisy to answer reliably.
The usual response to the capacity problem is summarisation: replace older turns with a shorter account of them. That is necessary, and it is also where critical information disappears. A summary keeps the gist and drops the specifics. “Customer disputes the 3 March charge of $1,240 on card ending 4417 and was promised a callback by Friday” becomes “customer is disputing a charge”. Summarise a summary a few times over a long session and precise values degrade into vague ones — the conversation still reads coherently, which is exactly why nobody notices until the agent gets a number wrong.
| Information type | Survives summarisation? | Protect it by |
|---|---|---|
| General goal, tone, topic | Usually | Nothing special needed |
| Exact amounts, dates, order or account IDs | Often not | A structured case facts block |
| Promises and decisions already made | Often not | Recording them as facts when they happen |
| What the user said they expect | Rarely verbatim | Quoting it into the facts block |
| Raw tool output from twenty turns ago | Doesn’t need to | Trimming it, or clearing it once used |
Pattern 1: keep a case facts block outside the history
The most dependable technique is also the simplest. Extract the transactional facts into a structured block as they arrive — customer, account, amounts, dates, order numbers, what has been promised, what is still open — and send that block with every request, outside the part of the conversation that gets summarised. The system prompt is a natural home for it, because it is sent separately on every call. Summaries can then be as short as they like: the facts the task depends on are never inside them.
case = { # updated by your code as facts arrive
"customer_id": "C-88213",
"disputed_charge": {"amount": "1240.00", "currency": "USD", "date": "2026-03-03"},
"card_last4": "4417",
"promised": ["callback by Fri 2026-03-13"],
"open_questions": ["merchant has not replied"],
}
response = client.beta.messages.create(
betas=["compact-2026-01-12"],
model=MODEL,
max_tokens=2048,
system=f"{BASE_PROMPT}\n<case_facts>{json.dumps(case)}</case_facts>",
messages=messages,
context_management={"edits": [{
"type": "compact_20260112",
# Replaces the default summary prompt entirely, so say what to keep.
"instructions": "Summarise the conversation. Preserve every decision, "
"commitment and unresolved question. Do not call tools.",
}]},
)
messages.append({"role": "assistant", "content": response.content})Two details in that example matter. Server-side compaction returns a compaction block; you append the whole response as usual, and on later requests the API ignores everything before that block. And the instructions field replaces the default summarisation prompt rather than adding to it — the documentation recommends saying what the summary must retain and telling the model not to call tools during the summary step.
Pattern 2: keep noise out in the first place
Much of what fills a long agent session is tool output. An order lookup might return forty fields when the task needs four; a search might return ten pages when one paragraph matters. Every one of those tokens is carried on every later request. Trim tool results to the fields the task needs before appending them — in your tool handler, or in a post-tool hook if you are on the Agent SDK (hooks are covered in 1.5).
For results that were useful once and are now stale, the API offers context editing. The clear_tool_uses_20250919 strategy clears older tool results once a trigger threshold is passed, keeps the most recent few, and replaces each cleared result with placeholder text so Claude knows something was removed. exclude_tools protects tools whose output must stay. Anthropic’s context-engineering post describes clearing old tool results as one of the lightest-touch ways to reclaim context: once a result has been used, the agent rarely needs the raw version again.
A tool result, before and after trimming
Raw tool resultjson
{
"order_id": "A-1042",
"status": "delivered",
"delivered_at": "2026-03-02",
"total": "89.00",
"warehouse": "DC-7",
"picker_id": "u-5521",
"carrier_raw": { ...40 lines },
"audit_log": [ ...120 lines ],
"marketing_flags": { ... },
"tax_breakdown": { ... }
}What the agent receivesjson
{
"order_id": "A-1042",
"status": "delivered",
"delivered_at": "2026-03-02",
"total": "89.00"
}Pattern 3: put important material where it is read best
Position matters in a long prompt. Anthropic’s long-context guidance is to put long documents at the top and the question or instructions at the end; in their tests, queries at the end improved answer quality by up to 30%, most of all with several documents. Wrap each document in its own tagged block with its source as metadata, and for long-document tasks ask Claude to quote the relevant passages before answering. When you assemble many results for a final step, lead with a short list of the key findings and give each section a clear heading, rather than hoping a detail buried in the middle of a long block is noticed.
Which tool for which problem?
- Exact facts get lostCase facts blockoutside summarised history
- Old tool output piling upTrim, then clear
clear_tool_uses_…+exclude_tools - Whole conversation too longCompactionwith instructions on what to keep
- Must survive a new sessionMemory or notes filememory tool, NOTES.md, CLAUDE.md
Memory outside the window
Some state must outlive the context window altogether — a multi-day task, an agent that may be interrupted. Anthropic’s guidance calls this structured note-taking: the agent writes progress to a file outside the conversation and reads it back later. The API’s memory tool formalises it: Claude issues file commands against a /memories directory that your application stores, and when used with context editing Claude is warned before results are cleared so it can save what it needs first. The trade-off is that memory is only as good as what the agent chose to write, so critical facts should still be written deterministically by your code rather than left to the model’s judgement.
Traps the wrong answers are built from
| Tempting but wrong | Do this instead |
|---|---|
| Relying on progressive summarisation to keep amounts, dates and IDs | Extract them into a structured case facts block sent with every request. |
| Appending full tool responses to history | Trim to the fields the task needs before they enter context; clear stale results. |
| Solving forgetfulness with a bigger context window | Curate context — a larger window delays overflow but not context rot. |
| Custom compaction instructions that only say “summarise” | Name what must be retained; the instructions replace the default prompt. |
| Burying the key question or finding in the middle of long input | Documents first, question last, key findings summarised up front with headings. |
You should now be able to
- Identify which information types are lost by summarisation and protect them explicitly.
- Design a case facts block that persists outside summarised conversation history.
- Trim verbose tool output and configure tool result clearing for long agent runs.
- Choose between compaction, context editing, memory and
/clearfor a given problem. - Order long inputs so documents come first and the query and key findings are easy to find.
- Say what survives compaction in Claude Code and where lasting rules should live.