Context engineering: fewer tokens, stronger signal for your agents
TL;DR
Context is a limited attention budget that thins as it grows. Keep a small, high-signal set of tokens, retrieve just in time, and for long tasks use compaction, structured notes or sub-agents.
Prompt engineering asks "how do I phrase the instruction well?" When you build agents that run over many turns, the bigger question is: at each turn, what should be in the model's context? System prompt, tool definitions, tool results, conversation history, external documents… they all accumulate and compete for attention. Anthropic calls this skill context engineering in a September 2025 engineering post from its Applied AI team. This post summarises the key ideas and how to apply them, both as a Claude Code user and as someone building agents.
Why context is a scarce resource
- Context rot: "needle in a haystack" tests show that as the token count grows, the model's ability to accurately recall information from its context declines.
- Attention budget: each new token draws down some attention. Transformers let every token attend to every other token (n² relationships), so longer context gets more diluted.
- The decline is a gentle slope, not a cliff: long contexts still work, but precision drops gradually.
The post's guiding principle: find the smallest set of high-signal tokens that maximises the chance of the desired outcome.
The parts of context
| Part | Common mistake | Recommendation |
|---|---|---|
| System prompt | Too rigid (if-else everywhere) or too vague | Write at the "right altitude": clear, sectioned with XML/Markdown; start minimal and add based on observed failures |
| Tools | A bloated tool set with overlapping functions | Each tool clearly scoped, little overlap, compact results. If an engineer can't tell which tool to use, neither can the agent |
| Examples | A long list of edge cases crammed in | A few diverse, canonical examples |
| Data | Preloading everything that might be relevant | Just-in-time retrieval |
Just-in-time: keep "pointers", not data
Instead of preloading all documents, the agent keeps lightweight references (file paths, queries, links) and uses tools to read when needed. Claude Code is the example: it analyses large data with targeted queries, head and tail, rather than reading whole files. Folder names, naming conventions and modification times are all signals that help the agent guess which files matter. The tradeoff: runtime exploration is slower. Claude Code takes a hybrid approach: CLAUDE.md is loaded up front, everything else is found with glob/grep.
Three techniques for long-horizon tasks
- Compaction: when nearly full, summarise the conversation and restart with the summary. Claude Code keeps key decisions, unresolved bugs, implementation details and recently accessed files, and drops redundant tool output. The lightest form is clearing old tool results.
- Structured note-taking: the agent writes progress to files outside context (to-do lists,
NOTES.md) and reads them back when needed. The post's example: Claude playing Pokémon keeps notes on maps and strategy across thousands of steps. - Sub-agent architecture: a lead agent coordinates while sub-agents work in clean contexts; they may use tens of thousands of tokens but return only a summary of about 1,000–2,000 tokens.
Choosing a technique: compaction for long back-and-forth conversations; note-taking for iterative development with clear milestones; multi-agent for parallel research and analysis.
Risks and caveats
- Over-aggressive compaction loses subtle details; the post advises optimising for recall first, then for conciseness.
- Multi-agent setups use more total tokens; only worth it when the work genuinely splits.
- Smarter models need less "engineering", so avoid building overly complex systems for temporary problems.
How to apply it
- Dev (using Claude Code): Delegate exploration to subagents, write plans to files,
/clearbetween unrelated tasks, and use@fileinstead of pasting whole folders. - Tech Lead (building agents): Measure tokens per component (system, tools, history); make tools return compact, paginated results; design in note-taking and compaction from the start.
- PM: When specifying an AI feature, define clearly "what the agent needs to know at each step". That is your context specification.
Sources & references
- Effective context engineering for AI agents — Anthropic Engineering