# What context engineering is Context engineering is the practice of deciding what goes into an agent's context window. Practically, it manages the model's working memory: system prompts, conversation history, retrieved documents, tool outputs, and state. Every token competes for attention, so including the right things matters for multi-step and long-horizon tasks.
# Why agents that work in sandbox fail in production As context grows, important signals get buried—this effect is called context rot. Errors fall into four repeatable failure modes:
- Context poisoning: an untrusted or incorrect document enters the window and the agent treats it as truth, producing persistent errors.
- Context distraction: low-value or noisy history dominates attention, crowding out important facts.
- Context confusion: poorly structured inputs cause tool misuse or wrong API calls.
- Context clash: contradictory sources force the agent to hedge or pick one arbitrarily.
Each failure mode maps to a concrete fix in the core strategy set below.
# Core strategies: Write, Select, Compress, Isolate These four tactics are the practical toolbox for production agents.
Store bulk or long-term state outside the immediate context window. Use scratchpads and structured memory types—semantic (facts), episodic (events), procedural (how-to)—so state survives even when the window fills.
Decide what actually enters the window through retrieval. Rank and filter aggressively, use metadata to prefer recent or authoritative sources, and limit item counts. Irrelevant retrieved data is often worse than none.
Save tokens while preserving meaning using compaction, summarisation, contextual compression, or gist representations. Compression trades fine-grained detail for token budget, so measure baseline behavior before aggressive compaction.
Break complex flows into focused sub-contexts. Use subagents, state partitioning, sandboxed execution, or quarantined context to ensure each component sees only the signals it needs. Isolation reduces confusion and lowers poisoning risk.
# Advanced techniques (2026 practices) Adopt spec-first development: write formal specs for agent behavior and context contracts. Use intentional compaction patterns and scoped subagents for separate concerns. Build state-based context tiers where tiers map to volatility and trust. Design for reasoning-aware context (type-like boundaries for instructions, evidence, and tool outputs) and add self-refinement loops so agents iteratively fix context errors.
# Measuring context effectiveness Move beyond token counts. Useful measures include:
- Output-variance testing: change context items and measure output changes to find brittle dependencies.
- A/B context testing: compare alternate retrieval and compression strategies against task success rates.
- Probe-based evaluation: test recall and continuation with targeted probes for critical facts.
- Effective context length: how far back the agent reliably uses information in practice.
For multi-agent systems, test context flow: verify each agent receives correctly filtered and formatted context at handoff points, and that compression or summarisation preserves intent.
# Practical checklist
- Validate and source-tag retrieved documents before passing them in.
- Limit retrieved items and rank by metadata such as freshness and authority.
- Keep tool metadata and output schemas explicit so agents treat tool outputs correctly.
- Partition state so sensitive or untrusted data never reaches the main context without validation.