Glossary
Context pruning
Removing messages, tool outputs or retrieved text from an LLM's input when they are no longer relevant, so the model sees only what the current step needs.
Context pruning is the removal of material from a language model’s input that is irrelevant, outdated or redundant for the current step: old tool outputs, superseded drafts, failed attempts, duplicate passages or tool definitions the agent will not use. Unlike summarisation, pruning does not rewrite what remains; it deletes items whole and leaves the rest verbatim. It is one form of context compression, and usually the cheapest and safest one, because most of what accumulates in an agent’s context is easy to identify as finished with. The hard cases are items whose relevance depends on a question that has not been asked yet, and a pruning policy needs a way to recover them.
Why prune context
An agent running in a loop adds to its input on every step. Each tool call returns a result, each result is appended, and by the twentieth step the model may be reading tens of thousands of tokens, most of which served one earlier decision. The context window is the hard ceiling, but cost, latency and accuracy suffer long before it is reached. Every token is billed on every call, and research consistently finds that model accuracy falls as input grows, a pattern known as context rot.
Irrelevant material also misleads. Context distraction describes a model that latches on to a long, vivid block of old content and repeats earlier actions instead of moving on. Context poisoning describes an error, such as a wrong intermediate conclusion or a hallucination, that stays in the input and keeps being treated as fact. Removing the item removes the influence. A field guide to context failures catalogues these patterns.
Pruning sits alongside the other tools of context engineering. Context compression is the umbrella term; summarisation rewrites, pruning deletes, and context offloading moves material somewhere retrievable. A sound design usually prunes first, offloads what might be needed again, and summarises only what is left.
What gets pruned
Item | Typical size | Pruning approach |
|---|---|---|
Tool outputs | Hundreds to tens of thousands of tokens | Keep the latest result per tool, or a one-line outcome after use |
Failed attempts and errors | Small to medium | Drop once a later attempt succeeds, keep a note of what failed |
Superseded drafts | Medium to large | Keep only the current version |
Retrieved passages | 500 to 3,000 tokens per batch | Keep only passages above a relevance threshold |
Tool definitions | Tens to thousands of tokens | Expose only the tools relevant to the current task |
Old conversation turns | Grows without limit | Drop chatter, keep durable facts and decisions |
Tool outputs
Tool results are usually the largest and least durable part of an agent’s context. A file read, a web page, a database query or a test log is useful for the decision it informs and rarely afterwards. A common rule keeps full outputs for the last few steps and replaces older ones with a short line, such as “read config.yaml (142 lines)” or “tests: 3 failed, see run-14.log”. If the agent needs the detail again, it can call the tool again or read the offloaded copy. Agent tools exposed through an MCP server can return very large payloads, so this rule matters more as an agent’s toolset grows.
Tool definitions
Tool schemas are sent with every call. An agent connected to fifty tools may carry several thousand tokens of definitions it will never use for the current task, and a long tool list also makes the model more likely to pick the wrong tool. Pruning here means loading tools per task or per stage: search tools while researching, editing tools while writing. Tool use quality tends to improve as the list shrinks.
Retrieved passages
Retrieval returns candidates with relevance scores, and the tail of a ranked list is often noise. Reranking, commonly with a cross-encoder, produces a more reliable order so the tail can be cut with confidence. Deduplication removes near-identical passages from different sources, and metadata filtering excludes whole classes of documents, such as outdated versions, before they are scored. Sending three right passages beats sending ten likely ones; top-K is the wrong default argues the point, and why reranking can’t fix a bad first pass explains its limit.
Conversation turns
Conversation is the hardest category, because a single message can mix pleasantries that can go with a constraint that must stay. Rule-based pruning by age, such as keeping the last twenty messages, is simple and loses early durable facts. Better approaches separate the two: extract facts and decisions into a store that persists, then prune the raw turns freely.
How pruning decisions are made
There are three broad ways to decide what to remove, and most systems combine them.
Rules. Fixed policies by type and age: keep the last three tool outputs, drop errors once resolved, cap retrieved passages at five. Rules are cheap, predictable and easy to debug. They cannot tell an old item that still matters from one that does not.
Relevance scoring. Score each item against the current goal or query, with embeddings, a reranker or a small model, and keep what clears a threshold. This adapts to the task. It costs a scoring pass per step, and the threshold needs tuning. Recency weighting is often added so that, between two equally relevant items, the newer one wins.
Model-directed pruning. Let the agent itself mark items as finished, or give it a tool that removes or collapses parts of its own context. This uses the model’s understanding of the task and can be surprisingly effective in long-running agents. It also adds tokens and steps, and a model can prune something it later needs.
Whatever the method, pruning works best when the agent state is structured rather than one long transcript. If the agent keeps a plan, a list of open questions and a set of findings as separate, named sections, pruning becomes a question of which section an item belongs to, and the transcript can be cut aggressively.
Where pruning goes wrong
The main failure is deleting something a later step needs. A user mentions in turn three that their account is on the EU region; by turn forty, age-based pruning has removed it and the agent queries the wrong region. Because nothing errors, the failure is silent. Test for it deliberately with long sessions that ask late questions about early details, a variant of needle in a haystack testing.
The second failure is pruning that breaks coherence. Removing a tool result but leaving the model’s reasoning about it, or removing a question but leaving its answer, produces a context the model cannot make sense of. Prune in matched units: a tool call with its result, a question with its answer. A third failure is invisibility: without agent tracing that records what was pruned at each step, it is hard to work out why an agent forgot something. Good agent observability logs the pruning decisions alongside the model calls.
The safeguard against all three is the same: never make pruning the only copy. If the full item is written to storage before it leaves the context, a mistaken prune costs one retrieval rather than a wrong answer.
A worked example
A research agent compiles a market report over sixty steps. It searches the web, reads around forty pages and drafts sections as it goes. Without pruning, its input passes 150,000 tokens by step thirty, responses slow down, and it begins re-reading pages it has already summarised.
With a pruning policy, each page read is reduced after use to its title, URL and three extracted findings, while the full text is saved to the agent’s workspace. Only the current section’s draft stays in context, and earlier sections are replaced by their headings. Search tools are dropped from the tool list once the writing stage begins. The input stays between 10,000 and 20,000 tokens for the whole run. When the agent needs a figure from a page read at step twelve, it searches its saved notes and finds it.
Where Exabase fits
Exabase provides the storage and retrieval that make aggressive pruning safe, and it applies pruning-style selection inside its own retrieval. It does not decide what your agent removes from its prompt; that logic stays in your agent loop.
Exabase Memory extracts facts, preferences and events from conversations so raw turns can be pruned without losing what they established. When facts change, M-1 resolves the contradiction, so a superseded fact does not keep returning to the context the way it would from a transcript. Memory search uses hybrid retrieval with recency bias, and its rerankThreshold parameter drops the lowest-scoring share of reranked results, which is pruning applied at retrieval time. The retrieving memories guide shows the parameters.
For documents and tool outputs, an agent can save what it reads as notes or files in a Base and search them later with Deep Search, which returns sub-document passages and has a precision setting from 0 to 1 to cut the tail of loosely related results. The searching resources guide covers it, and Workers can keep that store tidy on a schedule, pruning or flagging stale content in the Base itself.
Together these let an agent carry a few hundred relevant tokens per call instead of its whole history. The reduce token costs and RAG accuracy solutions explain the effect on cost and answer quality, the research agents use case shows the pattern in practice, and the best reranking models and best RAG infrastructure roundups compare the wider options.
Frequently asked questions
What is the difference between context pruning and context compression?
Context pruning is one kind of context compression. Pruning removes whole items and leaves the rest unchanged; compression also includes summarising, rewriting and offloading material to make the input smaller.
What should be pruned first?
Old tool outputs. They are usually the largest items in an agent’s context and the least likely to be needed again, and the agent can re-run the tool or read a saved copy if it does need them.
Can pruning make an agent less accurate?
Yes, if it removes something a later step needs. Saving pruned items to searchable storage before removing them turns that risk into one extra retrieval, and testing long sessions with late questions about early details catches policies that cut too much.
Is truncation the same as pruning?
Truncation is the simplest form of pruning: drop everything older than a cut-off. It is easy to implement and tends to lose the durable facts that appear early in a conversation, so most agents combine it with a memory store.
Should the model decide what to prune?
It can, and in long tasks a model that marks its own finished work often prunes well. Combine it with simple rules for predictable cases, such as old tool outputs, and log every decision so mistakes can be traced.
How does Exabase support context pruning?
By keeping what is pruned retrievable. Exabase Memory holds the facts from pruned conversation turns, and Deep Search finds saved documents and tool outputs again, so an agent can keep its prompt small without losing information.
Related pages: context compression, context offloading, context distraction, context poisoning, context rot, reranking, reduce token costs, a field guide to context failures, short-term memory