Context Engineering
Context engineering refers to the set of strategies for curating and maintaining the optimal set of tokens during LLM inference.
Definition · At a glance · How it works
Definition
Context refers to the set of tokens included when sampling from a large-language model (LLM). Context engineering refers to the set of strategies for curating and maintaining the optimal set of tokens (information) during LLM inference, including other information that may land there outside of the prompts. Prompt engineering refers to methods for writing and organizing LLM instructions for optimal outcomes.
Anthropic views context engineering as the natural progression of prompt engineering. Context engineering is the art and science of curating what will go into the limited context window from that constantly evolving universe of possible information. Writing a prompt is a discrete task, whereas context engineering is iterative: the curation phase happens each time a decision is made about what to pass to the model.
At a glance
An agent running in a loop generates data that could be relevant to its next inference turn, and this information needs cyclic refinement. The source's Claude Code compaction example shows how a conversation nearing the context limit becomes context the agent can continue using.
How it works
Studies on needle-in-a-haystack style benchmarking have uncovered the concept of context rot: as the number of tokens in the context window increases, the model's ability to accurately recall information from that context decreases. Like humans, who have limited working memory capacity, LLMs have an "attention budget" that they draw on when parsing large volumes of context. Every new token introduced depletes this attention budget by some amount, which increases the need to carefully curate the tokens available to the LLM.
LLMs are based on the transformer architecture, which enables every token to attend to every other token across the entire context. For n tokens, this attention results in n² pairwise relationships. Tools allow agents to operate with their environment and pull in new, additional context as they work.
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, rather than pre-processing all relevant data up front. Claude Code, Anthropic's agentic coding solution, uses this approach to perform complex data analysis over large databases. In that approach, the model can write targeted queries, store results, and use Bash commands to analyze large volumes of data without ever loading the full data objects into context.
Runtime exploration is slower than retrieving pre-computed data.
Example
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. In Claude Code, compaction is implemented by passing the message history to the model to summarize and compress the most critical details. In that Claude Code example, the model preserves architectural decisions, unresolved bugs, and implementation details while discarding redundant tool outputs or messages.
The agent can then continue with this compressed context plus the 5 most recently accessed files. Compaction depends on selecting what to keep and what to discard, since overly aggressive compaction can result in the loss of subtle but critical context whose importance only becomes apparent later. 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. In the Claude playing Pokémon example, the agent reads its own notes after context resets and continues multi-hour training sequences or dungeon explorations.
Sources
This page is written from the official sources below. Open them to check the original text.
- Official bloghttps://www.anthropic.com/research/effective-context-engineering-for-ai-agentsFetched
Verified ·
This page is written by AI from the official sources above and checked automatically. This site has not tested it. Found a mistake? Tell us.