# Effective Context Engineering for AI Agents

URL:: https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents
Author:: anthropic.com

## AI-Generated Summary
Context engineering is about choosing the smallest, most relevant set of information to feed an LLM so it stays focused and effective. For long tasks, agents use techniques like compaction, structured notes, and sub-agents to avoid context overload. Treating context as a scarce resource is key to building reliable, autonomous AI agents.
## Highlights
> **Context engineering** refers to the set of strategies for curating and maintaining the optimal set of tokens (information) during LLM inference, including all the other information that may land there outside of the prompts. ([View Highlight](https://read.readwise.io/read/01m3293fwfe8z5yq804ph9pff1))
> as we move towards engineering more capable agents that operate over multiple turns of inference and longer time horizons, we need strategies for managing the entire context state (system instructions, tools, [Model Context Protocol](https://modelcontextprotocol.io/docs/getting-started/intro) (MCP), external data, message history, etc).
> An agent running in a loop generates more and more data that *could* be relevant for the next turn of inference, and this information must be cyclically refined. Context engineering is the [art and science](https://x.com/karpathy/status/1937902205765607626?lang=en) of curating what will go into the limited context window from that constantly evolving universe of possible information. ([View Highlight](https://read.readwise.io/read/01m3296qvq8rfxch9gyq5r0x8n))
>  ([View Highlight](https://read.readwise.io/read/01m32975retkpfqs6e8ww49n41))
> Despite their speed and ability to manage larger and larger volumes of data, we’ve observed that LLMs, like humans, lose focus or experience confusion at a certain point. Studies on needle-in-a-haystack style benchmarking have uncovered the concept of [context rot](https://research.trychroma.com/context-rot): as the number of tokens in the context window increases, the model’s ability to accurately recall information from that context decreases. ([View Highlight](https://read.readwise.io/read/01m32986drykakskcr195x94sy))
> Context, therefore, must be treated as a finite resource with diminishing marginal returns. Like humans, who have [limited working memory capacity](https://journals.sagepub.com/doi/abs/10.1177/0963721409359277), LLMs have an “attention budget” that they draw on when parsing large volumes of context. ([View Highlight](https://read.readwise.io/read/01m3298g2ctcerqn6n6c82pq70))
> Given that LLMs are constrained by a finite attention budget, *good* context engineering means finding the *smallest* *possible* set of high-signal tokens that maximize the likelihood of some desired outcome. ([View Highlight](https://read.readwise.io/read/01m329arcvsjnr7c8vvaysf5nz))
> **Tools** allow agents to operate with their environment and pull in new, additional context as they work. Because tools define the contract between agents and their information/action space, it’s extremely important that tools promote efficiency, both by returning information that is token efficient and by encouraging efficient agent behaviors. ([View Highlight](https://read.readwise.io/read/01m329cfje2ehztg3ar4wc24ky))
> One of the most common failure modes we see is bloated tool sets that cover too much functionality or lead to ambiguous decision points about which tool to use. ([View Highlight](https://read.readwise.io/read/01m329cxj5mvh7z1yr14akrqb6))
> a [simple definition](https://simonwillison.net/2025/Sep/18/agents/) for agents: LLMs autonomously using tools in a loop. ([View Highlight](https://read.readwise.io/read/01m329ef8jv7q3rwvqa06a1149))
> Today, many AI-native applications employ some form of embedding-based pre-inference time retrieval to surface important context for the agent to reason over. ([View Highlight](https://read.readwise.io/read/01m329f31k331cwex2gcwfhs4w))
> Rather than pre-processing all relevant data up front, agents built with the “just in time” approach maintain lightweight identifiers (file paths, stored queries, web links, etc.) and use these references to dynamically load data into context at runtime using tools. ([View Highlight](https://read.readwise.io/read/01m329fhzedfpd5vfvdfwg6tq5))
> This approach mirrors human cognition: we generally don’t memorize entire corpuses of information, but rather introduce external organization and indexing systems like file systems, inboxes, and bookmarks to retrieve relevant information on demand. ([View Highlight](https://read.readwise.io/read/01m329gdbvgbptx0tyrvekqhmg))
> Letting agents navigate and retrieve data autonomously also enables progressive disclosure—in other words, allows agents to incrementally discover relevant context through exploration. ([View Highlight](https://read.readwise.io/read/01m329gvnq2s4r6dxjnm8q8qts))
> In certain settings, the most effective agents might employ a hybrid strategy, retrieving some data up front for speed, and pursuing further autonomous exploration at its discretion. The decision boundary for the ‘right’ level of autonomy depends on the task. ([View Highlight](https://read.readwise.io/read/01m329hnkzhsq0fmz1wemmzxas))
> Long-horizon tasks require agents to maintain coherence, context, and goal-directed behavior over sequences of actions where the token count exceeds the LLM’s context window ([View Highlight](https://read.readwise.io/read/01m329jzfmr0gnj3f6q63bptma))
> Compaction is the practice of taking a conversation nearing the context window limit, summarizing its contents, and reinitiating a new context window with the summary. Compaction typically serves as the first lever in context engineering to drive better long-term coherence. At its core, compaction distills the contents of a context window in a high-fidelity manner, enabling the agent to continue with minimal performance degradation. ([View Highlight](https://read.readwise.io/read/01m329kpsene3fr0hyzq14hf9h))
> Structured note-taking, or agentic memory, is a technique where the agent regularly writes notes persisted to memory outside of the context window. These notes get pulled back into the context window at later times. ([View Highlight](https://read.readwise.io/read/01m329mm9rmdp562frr0d94v6a))
> This coherence across summarization steps enables long-horizon strategies that would be impossible when keeping all the information in the LLM’s context window alone. ([View Highlight](https://read.readwise.io/read/01m329nt9ngxd9vsc2g2qdcpzt))
> Sub-agent architectures provide another way around context limitations. Rather than one agent attempting to maintain state across an entire project, specialized sub-agents can handle focused tasks with clean context windows. The main agent coordinates with a high-level plan while subagents perform deep technical work or use tools to find relevant information. Each subagent might explore extensively, using tens of thousands of tokens or more, but returns only a condensed, distilled summary of its work (often 1,000-2,000 tokens). ([View Highlight](https://read.readwise.io/read/01m329psxxe3p8avaasc8qx6gt))