Context engineering is the practice of deciding which information goes into a language model’s context window at each step, and keeping that set small and useful. Anthropic defines context as the set of tokens included when sampling from a model, and context engineering as the strategies for curating and maintaining the best set of those tokens during inference, including information that lands there outside the prompt.
Context engineering vs prompt engineering
Prompt engineering is about writing and organizing instructions, mainly system prompts. Anthropic describes context engineering as its natural progression: an agent that runs over many turns has to manage system instructions, tools, Model Context Protocol (MCP) servers, external data and message history together.
The other difference is timing. Writing a prompt is a discrete task. Anthropic says context engineering is iterative, because curation happens every time the system decides what to pass to the model.
| Prompt engineering | Context engineering | |
|---|---|---|
| Scope | Instructions, especially the system prompt | System prompt, tools, examples, retrieved data, message history, memory |
| When | Written up front, revised between runs | Decided again at every step of an agent loop |
| Main question | What should the instructions say? | What should the model see right now? |
Andrej Karpathy, as quoted by LangChain, called it the delicate art and science of filling the context window with just the right information for the next step. LangChain compares the window to RAM: limited working memory that something has to curate.
Why it matters
More context is not automatically better. Anthropic reports context rot: as the number of tokens grows, the model’s ability to recall information from the window decreases. It describes this as a performance gradient rather than a hard cliff, and treats context as a finite “attention budget” that each added token draws down.
LangChain notes that long-running agents collect tool feedback until the window overflows, cost and latency balloon, or quality drops. It cites Drew Breunig’s four failure modes:
| Failure | Meaning |
|---|---|
| Poisoning | A hallucination makes it into the context |
| Distraction | The context overwhelms what the model learned in training |
| Confusion | Superfluous context influences the response |
| Clash | Parts of the context disagree |
Waiting for bigger windows does not remove the problem. Anthropic expects windows of all sizes to face context pollution and relevance concerns for the foreseeable future. Manus lists three pain points of very long inputs: observations such as web pages and PDFs can exceed the limit, performance degrades beyond a certain length, and long inputs stay expensive even with prefix caching.
The parts of context
Anthropic recommends aiming for the smallest set of high-signal tokens that makes the desired outcome most likely. In practice that means working on each component:
- System prompt. Write at the right altitude: specific enough to guide behavior, flexible enough to give the model heuristics, and not a pile of brittle if-else rules. Anthropic adds that minimal does not mean short, and suggests starting with a minimal prompt, then adding instructions based on failures found in testing.
- Tools. Anthropic calls bloated tool sets with ambiguous choices one of the most common failure modes: if a human engineer cannot say which tool fits a situation, the agent will not do better. Tools should also return token-efficient output.
- Examples. Anthropic advises a few diverse, canonical examples instead of a laundry list of edge cases.
- Message history and tool results. These grow every turn and are the main target of the techniques below.
Techniques
LangChain groups common strategies into four buckets:
| Strategy | What it means |
|---|---|
| Write | Save context outside the window, for example in scratchpads or memory |
| Select | Pull the relevant context back into the window |
| Compress | Keep only the tokens needed for the task |
| Isolate | Split context up, for example across agents, sandboxes or state fields |
Anthropic’s post and the teams below fill in what each looks like in practice.
Retrieve just in time
Instead of loading everything up front, Anthropic describes agents that keep lightweight identifiers such as file paths, stored queries and links, and load data with tools at runtime. The trade-off is speed: runtime exploration is slower than precomputed retrieval, and it needs good tools and heuristics. Claude Code takes a hybrid route, according to Anthropic: CLAUDE.md files are put into context up front, while glob and grep fetch other files on demand. A file such as AGENTS.md plays the up-front role for coding agents.
Compact long conversations
Anthropic defines compaction as summarizing a conversation that is nearing the window limit and starting a new window with the summary. The hard part is choosing what to keep, since overly aggressive compaction can drop details whose importance shows up later. Anthropic suggests tuning the compaction prompt on complex agent traces: maximize recall first, then improve precision.
The Claude cookbook separates this from tool-result clearing. Compaction summarizes the whole window, so some detail is lost. Clearing replaces old tool results with a placeholder and keeps the record that the call happened, and if the agent needs the data again it can call the tool again. Anthropic’s docs call server-side compaction the primary strategy for most long-running conversations, with context editing for finer control.
OpenAI’s Agents SDK cookbook compares two ways to handle history. Trimming to the last N turns is deterministic and adds no latency, but forgets older context abruptly. Summarizing keeps long-range memory, but can lose detail or carry a bad fact forward.
Keep notes outside the window
Anthropic describes structured note-taking as the agent regularly writing notes to memory outside the context window and pulling them back in later, like a to-do list or a NOTES.md file. Manus treats the file system as context and makes its compression restorable: a web page’s content can be dropped if the URL stays, and a document’s content can be dropped if its path stays in the sandbox.
Manus also rewrites a todo list as a task proceeds, which pushes the objectives to the end of the context. Tasks there average about 50 tool calls, and the team says this guards against drifting off-topic.
Split work across sub-agents
Anthropic explains that sub-agents can work with clean context windows on focused tasks and return only a condensed summary, often 1,000 to 2,000 tokens, even if they used tens of thousands of tokens exploring. The lead agent then works from summaries rather than raw search results.
Choosing among them
Anthropic suggests matching the technique to the task:
| Technique | Fits |
|---|---|
| Compaction | Tasks with extensive back-and-forth |
| Note-taking | Iterative work with clear milestones |
| Multi-agent | Complex research where parallel exploration pays off |
Production lessons from Manus
Manus, an agent product, published lessons from rebuilding its framework four times, in July 2025. It stresses that they are patterns that worked for it, not universal truths.
- Protect the KV-cache. Manus calls the cache hit rate the single most important metric for a production agent. Its average input-to-output ratio is about 100:1, so cached prefixes matter. It advises a stable prompt prefix (no second-precision timestamp at the start), append-only context with deterministic serialization, and explicit cache breakpoints when the provider needs them.
- Mask tools instead of removing them. Changing tool definitions mid-task invalidates the cache and can confuse the model about earlier calls, so Manus restricts choices by masking during decoding.
- Keep errors in. Failed actions and stack traces stay in context so the model shifts away from repeating them.
- Avoid uniform history. A context full of similar action-observation pairs makes the model copy the pattern, so Manus adds small amounts of structured variation.
Testing context changes
Context changes are easy to make and hard to judge by eye. Anthropic says to find failure modes through testing before adding instructions, and LangChain points to testing an agent and tracking its context usage. For measuring whether a change helped, see How to evaluate an AI agent.