Context Engineering for Agents
Table of contents
- Key takeaways
- What is context engineering?
- Why is the context window a scarce resource?
- How is context managed: compaction, summarisation and selective retrieval?
- What is context rot and why does long context fail?
- What do long-running agents need?
- Frequently asked questions
- How does context engineering differ from prompt engineering?
- How much context is too much?
- Does context engineering replace RAG?
- Conclusion
- Sources
Context engineering is the craft of deciding what information enters a model's window at each step of an agent. Beyond prompt engineering, it manages the whole set of tokens: instructions, tools, memory and history. Its goal is the smallest possible set of high-signal tokens that still completes the task.
Context engineering is the discipline of deciding, at each step of an agent, which information occupies the model’s context window. It is the evolution of prompt engineering: writing a good instruction is no longer enough, because an agent accumulates history, tool outputs and memory that all compete for limited space. In this guide you will see what context engineering is, why the window is a scarce resource, how information is compacted and retrieved selectively, what context rot is and which practices keep a long-running agent reliable. The same explanation is available in Spanish.
Key takeaways
- Context engineering is, in Anthropic’s words, the set of strategies for "curating and maintaining the optimal set of tokens during model inference": instructions, tools, external data and message history.
- The context window is a scarce resource: because of the Transformer’s quadratic attention, every added token drains signal from the rest, so the goal is the smallest possible set of high-signal tokens.
- Chroma Research’s Context Rot study (July 2025) tested 18 frontier models and every one degrades monotonically as input grows, with 30 to 50 % drops well before reaching the advertised limit.
- The three core techniques are compaction (summarise and reinitialise the history), selective retrieval (load data only when needed) and persistent notes (external memory outside the window).
- For Cognition, context engineering is "effectively the #1 job of engineers building AI agents": without the right context, even the most capable model fails the task.
What is context engineering?
Context engineering is the practice of curating and maintaining the optimal set of tokens a model sees at the moment it generates its next step. Anthropic defined it precisely in its engineering post of 29 September 2025, framing it as the successor to prompt engineering. The difference is one of scope. A prompt is text you write once; an agent’s context is everything that enters the window on each turn: the system message, the tool definitions, the retrieved documents, the agent’s memory and the full conversation history, which grows with every call.
That distinction matters because an agent does not make a single call but dozens or hundreds in a loop. With each iteration it accumulates tool results, intermediate messages and observations, and the window fills with material competing for the model’s attention. Context engineering is the set of decisions about what to keep, what to summarise and what to discard at each step. As Anthropic’s own team puts it, "good context engineering means finding the smallest possible set of high-signal tokens that maximise the likelihood of some desired outcome".
Why is the context window a scarce resource?
Although 2026 models advertise windows of hundreds of thousands or even a million tokens, treating them as an infinite bucket is a mistake. The reason is architectural: the Transformer’s attention builds relationships between every pair of tokens, a cost that grows quadratically. Every new token you add splits the model’s attention across more elements, so the signal per token drops. Anthropic calls this an attention budget: a finite resource with diminishing marginal returns.
The empirical evidence is compelling. The Lost in the Middle work by Liu and colleagues, published in 2023, showed that models make better use of information placed at the start and end of the context, and that performance falls noticeably when the relevant fact sits in the middle: a U-shaped curve. Two years later, Chroma Research’s Context Rot study extended the finding to 18 frontier models and found that all of them degrade in accuracy as the input grows, with drops of 30 to 50 % well before the advertised window is full. In million-token models the effect is often noticeable around 300,000 or 400,000 tokens. The practical conclusion is clear: filling the window does not improve the agent, it hurts it.
How is context managed: compaction, summarisation and selective retrieval?
Faced with a scarce resource, context engineering applies three complementary techniques that you can combine in a single agent.
- Compaction: when the history approaches a token threshold, the conversation is summarised and the loop restarts with that compressed version. Claude Code, for instance, keeps architectural decisions and unresolved bugs while discarding already-redundant tool outputs, and keeps the five most recently accessed files at hand.
- Selective (just-in-time) retrieval: instead of preloading every document, the agent stores lightweight identifiers (file paths, queries, links) and only loads the content when it needs it. This is where vector databases fit, returning only the fragments relevant to the current query.
- Persistent notes: the agent writes its progress to memory outside the window, for example a
NOTES.mdfile, and rereads it when needed. This way state survives compactions without permanently occupying context.
A bounded-context loop takes very few lines of pseudocode. The idea is to measure the size before each step and compact as soon as the budget is exceeded:
BUDGET = 8000 # tokens reserved for the live history
def agent_step(history, task):
if count_tokens(history) > BUDGET:
summary = model.summarise(history, keep=["decisions", "errors"])
history = [summary] + history[-4:] # summary plus the last turns
context = build_context(task, history, notes="NOTES.md")
return model.answer(context)
The key detail is on the summary line: it does not compress everything equally but keeps decisions and errors (the high signal) while discarding the verbose tool outputs (the low signal). That criterion of what deserves a token is the heart of context engineering.
What is context rot and why does long context fail?
Context rot is the measurable degradation of a model’s performance as the input length grows, even when the window is far from full. The term was popularised by Chroma Research’s report of the same name in July 2025, by Kelly Hong, Anton Troynikov and Jeff Huber. Its finding is uncomfortable for anyone who trusts giant windows: more input tokens produce worse outputs, and no model keeps uniform accuracy across its entire advertised window.
The causes combine. There is the positional bias of Lost in the Middle, which buries information in the middle. There is signal dilution: important instructions get lost among thousands of noise tokens. And there is interference between similar data, which confuses the model when the context holds contradictory or near-duplicate fragments. Cognition’s team, in its June 2025 post, frames it from a design angle: it argues that sharing the full context between steps and avoiding conflicting implicit decisions is "effectively the #1 job of engineers building AI agents", which is why it advises against splitting a task among sub-agents that do not share their history.
What do long-running agents need?
An agent that works for hours (reviewing a repository, researching a topic, processing a queue) overflows any window unless it manages its context actively. These practices keep it reliable:
- Write state outside the window. Use a notes file or a database as long-term memory and bring in only what each step needs.
- Compact with judgement, not by age. When summarising, keep decisions, constraints and pending errors; discard tool noise.
- Retrieve on demand. Store lightweight references and load the content at the moment of use, not before.
- Put what matters at the edges. Place critical instructions at the start and the immediate goal at the end, where the model attends best.
- Exploit the cache. Structuring the prompt with a stable prefix lets you reuse prompt caching to cut costs and latency between steps.
- Delegate to sub-agents that return summaries. A specialised sub-agent can explore and return a short summary, of one to two thousand tokens, instead of dumping its whole trace into the main agent.
With these rules, an agent can run thousands of steps without its window degrading, because on each turn it sees only what it truly needs.
Frequently asked questions
How does context engineering differ from prompt engineering?
Prompt engineering optimises an instruction text you write once and that stays more or less fixed. Context engineering manages the whole set of tokens the model sees at each step of an agent, including the growing history, the tool outputs and the retrieved memory. A single-turn chatbot only needs the former; an agent that makes dozens of calls needs the latter, because the context changes constantly.
How much context is too much?
There is no fixed number, but the useful rule is that less is usually more. The Context Rot study observed accuracy drops of 30 to 50 % well before the window was full, and in million-token models the degradation shows from around 300,000. In practice it pays to keep the live history well below the advertised limit and offload the rest to external memory, rather than trusting a huge window to solve it on its own.
Does context engineering replace RAG?
No, it integrates it. Retrieval-augmented generation (RAG) is one of context engineering’s tools: it brings in exactly the relevant fragments at the right moment, instead of preloading whole documents. Context engineering is the wider framework that decides when to retrieve, when to summarise, when to write notes and what to discard at each step of the agent.
Conclusion
Context engineering acknowledges an uncomfortable truth about AI agents: the context window is a scarce resource, not an infinite bucket, and filling it degrades the model rather than helping it. Against that, the discipline offers a set of concrete tactics, compact with judgement, retrieve on demand, write persistent notes and place what matters at the edges, with one shared goal: the smallest set of high-signal tokens that solves the task. The natural next step is to instrument your agent to measure how many tokens it spends at each step and start trimming the noise before context rot appears.
Sources: [1] Effective context engineering for AI agents, Anthropic[1], [2] Lost in the Middle: How Language Models Use Long Contexts[2], [3] Context Rot: How Increasing Input Tokens Impacts LLM Performance, Chroma Research[3], [4] Don’t Build Multi-Agents, Cognition[4].