Context Engineering for Agents
How to design effective context for AI agents — memory, tool results, and orchestration.
Official Anthropic docs →Context is everything
The quality of an AI agent isn't just about the model — it's about what information you put in the context window. Bad context produces bad agents. The Anthropic engineering team breaks down exactly what should go in the context and in what order: system instructions first, then relevant memory, tool definitions, and conversation history. The arrangement and quality of this context determines agent performance more than any other factor.
System instructions design
System instructions set the foundation for agent behavior. They should define the agent's role, available tools, constraints, and expected workflow patterns. Keep them focused and avoid contradictory instructions. The most effective system prompts are structured with clear sections: identity, capabilities, rules, and output format expectations.
Memory management
Agents need to remember what they've done across steps. Strategies include compressing conversation history, deciding what to keep vs summarize, and structuring memory so the agent can retrieve what it needs without filling the context window. Use structured formats (JSON) for state data and unstructured text for progress notes. Git provides excellent state tracking across sessions.
Tool result handling
When an agent calls a tool and gets a result back, how you format that result matters significantly. Present tool results in a way that helps the model decide what to do next — rather than getting confused or repeating itself. Include relevant metadata, truncate overly long results, and structure the output to highlight actionable information.
Orchestration patterns
Different ways to structure multi-step workflows include: linear chains (step-by-step), branching logic (conditional paths), loops with exit conditions, and human-in-the-loop patterns. Each has trade-offs around reliability, cost, and latency. For complex tasks, encourage the agent to plan before acting, verify results, and track progress in structured files.
Multi-context window workflows
For tasks spanning multiple context windows: use the first window to set up a framework (write tests, create scripts), have the model write tests in structured formats, set up quality-of-life tools like init scripts, and provide verification tools so the agent can check correctness without human feedback. Consider starting with a fresh context window rather than compaction — Claude's latest models are effective at discovering state from the filesystem.