Key points
This post is a comprehensive analysis of context engineering — the discipline of managing all tokens that enter an LLM's context window during inference — as the successor to prompt engineering.
1. From prompt engineering to context engineering
- LLMs are stateless: every API call starts fresh. Early workarounds (giant system prompts) hit an "attention budget" wall — attention has \(O(n^2)\) complexity, and stuffing context triggers context rot (hallucinations, ignored instructions), even with 2M-token windows like Gemini 1.5 Pro.
- Anthropic defines context engineering as curating/maintaining/optimizing all tokens entering the context window. The metaphor: prompt engineering writes a recipe; context engineering runs the kitchen's supply chain so the model has *exactly* the right ingredients at inference time.
- Key contrast: prompt engineering optimizes a single interaction's wording; context engineering is systems architecture over the full information flow (memory, tools, RAG, user profiles), designed for scale and lifecycle iteration.
- Context rot: retrieval accuracy degrades as distractors accumulate, especially semantically similar ones (e.g., multiple quarters of financial data competing for attention).
- Lost-in-the-middle: models show a U-shaped performance curve — strong at the start and end of context, weak in the middle. Best practices: put system instructions first, user query last; use dynamic re-ranking (e.g., LangChain's LongContextReorder) to place high-relevance documents at both ends.
- Every token costs money *and* attention bandwidth; compression (summarization, key-info extraction) improves signal-to-noise ratio.
- Sliding window / intelligent truncation: simple, low latency, but loses long-range dependencies.
- Recursive summarization: background LLM compresses old turns into a summary prepended to recent turns; preserves semantic thread.
- Structured note-taking: agent maintains an external XML/JSON file re-injected each turn as a high-fidelity external brain (Anthropic-recommended).
- Production concerns: hot-path latency (Redis/Firestore), strict per-tenant isolation via ACLs, and PII scrubbing before persistence (e.g., Google Model Armor) for GDPR/CCPA compliance.
- Memory vs RAG: RAG serves *world knowledge* (shared, static, authoritative docs); memory serves *user knowledge* (isolated, high-frequency updates). Good agents need both.
- Taxonomy: semantic memory (facts/preferences), episodic memory (timestamped event recall), and procedural memory — learned skills/workflows. Examples: Voyager stores successful Minecraft behaviors as callable code skills; Reflexion stores self-reflections on failures as negative constraints. Procedural memory can even transfer from strong models (GPT-4) to weaker ones.
- Memory ETL pipeline: ingestion → LLM-based extraction → consolidation (dedup, timestamp-based conflict resolution, synthesis of high-level generalizations, per Stanford Generative Agents) → vector/graph storage.
- Retrieval scoring: relevance (embedding similarity) × recency (exponential decay) × importance (LLM-assigned at write time). Google ADK also promotes "Memory-as-a-Tool": agents decide when to write/read memories themselves.
- MCP (Model Context Protocol) — Anthropic's "USB-C for AI": Client-Host-Server architecture with stdio or HTTP-SSE transports. Exposes three primitives: Resources (read-only data), Tools (executable functions), Prompts (templates). Write once, integrate everywhere — solves the N×M integration problem.
- A2A — Google's agent-to-agent protocol: agent discovery via metadata cards, task delegation through standard
/runinterfaces for distributed agent federations. - Anthropic: Effective context engineering for AI agents
- Lost in the Middle (MIT Press / TACL)
- Model Armor | Google Cloud
- Voyager: An Open-Ended Embodied Agent with LLMs
- Agent procedural memory (arXiv) · Lost-in-the-Middle mitigations (arXiv)
- Google ADK vs LangGraph (ZenML)
2. Context physics: why long contexts fail
3. Session layer (working memory)
Sessions are the agent's "workbench" — a container of time-ordered events (user input, model replies, tool calls/outputs) plus structured state. Management strategies:
4. Memory architecture (long-term cognition)
5. Frameworks: Google ADK vs LangGraph
| Aspect | Google ADK | LangGraph | | :-- | :-- | :-- | | Metaphor | Event pipeline, "context as compiled view" | State machine / graph (StateGraph) | | Loops | Implicit via LoopAgent | Native, explicit (enables Reflexion-style self-correction) | | Multi-agent | A2A protocol (HTTP/REST, service-oriented) | Subgraphs, in-process orchestration | | Extras | Deep Google Cloud/Gemini integration | Checkpointing, human-in-the-loop, time-travel debugging |
6. Protocols
7. Security and governance
Threat vectors: (direct and indirect) prompt injection via retrieved docs/memories, context poisoning (deliberate ingestion of false content into memory), and leakage of secrets/PII through long-term memory. Defense-in-depth with Google Cloud Model Armor: input sanitization, output auditing, configurable confidence thresholds, PII templates, and full audit logging.
Conclusion
Context engineering marks AI development's move from "alchemy" (prompt crafting) to industrial engineering: sessions as workbench, memory as archive, MCP as connector, security as moat. The frontier is metacognition — agents that evaluate and manage their own memories and self-author their procedural scripts, extended to multimodal contexts.
#### Notable references cited