Context Engineering
Master the art of managing what goes into a language model’s context window — from tokens and system prompts to tool calls and memory strategies.
1 Context Engineering
Master the art of managing what goes into a language model’s context window — from tokens and system prompts to tool calls and memory strategies.
Source: latentpatterns.com/courses/context-engineering
1.1 Who This Book Is For
Software developers, ML engineers, and architects who build applications on top of LLMs — especially agent-based systems. You should be comfortable with API calls to language models and have experience (even brief) with tools like Claude Code, Cursor, Copilot, or LangChain.
1.2 Why “Context Engineering”?
The term was popularized in June 2025 by Tobi Lutke (Shopify CEO) — “the art of providing all the context for the task to be plausibly solvable by the LLM” — and Andrej Karpathy, who elaborated: “context engineering is the delicate art and science of filling the context window with just the right information for the next step.” It supersedes “prompt engineering” because in production systems, the prompt is the least of your concerns. The real work is managing everything else that goes into context: tool definitions, retrieved documents, conversation history, project conventions, and compaction strategies.
1.3 What You’ll Learn
The context window is the single most important resource in any LLM application. It determines what the model knows, how well it reasons, and when it fails. Most developers treat it as a black box. This book treats it as a system to be engineered.
By the end, you’ll be able to:
- Calculate the real (not advertised) context budget for any model
- Design system prompts, project context, and tool sets for maximum signal-per-token
- Architect agent systems that stay in the “smart zone” across long sessions
- Use sub-agents, message passing, and fresh-context patterns to scale beyond single-window limits
- Apply memory management principles from systems programming to context management
1.4 Chapters
1.4.1 Part I: Foundations
| Chapter | Topic | Sections |
|---|---|---|
| 1. Tokens and Inference | How LLMs consume text and why inference is stateless | What Are Tokens? Inference Is Stateless |
| 2. The Real Size of Your Context Window | Why advertised context lengths are marketing, not engineering | Marketing vs. Reality Why Models Fail at Length The Smart Zone and the Dumb Zone Measuring Your Context Budget |
1.4.2 Part II: The Messages Array
| Chapter | Topic | Sections |
|---|---|---|
| 3. Anatomy of the Messages Array | The slot-by-slot structure of what goes into context | System Prompt Harness Prompt Project Context Files MCP Servers and Tools Your Prompt |
| 4. Dynamic Allocation — Tool Calling | How tool calls grow context and what it costs | Tool Calls as Memory Allocations A Real Agent Session |
1.4.3 Part III: Scaling Patterns
| Chapter | Topic | Sections |
|---|---|---|
| 5. Sub-Agents — Managed Runtimes for AI | Context isolation through disposable child windows | Not About Personas The Test Runner Problem Designing Boundaries |
| 6. Message Passing — The Erlang OTP of AI | Context windows as actors with message-passing semantics | Context Windows Are Actors Designing the Message Protocol Supervision and Failure |
1.4.4 Part IV: Architecture
| Chapter | Topic | Sections |
|---|---|---|
| 7. The Ralph Wiggum Loop | Crash-only agent design — fresh-context iteration for autonomous coding | What Is It? The Three-Phase Funnel Specs and Plans Backpressure Greenfield vs. Brownfield Failure Modes |
| 8. Context Management Strategies | Memory management for LLMs — allocation, compaction, and beyond | The malloc Without free Why Compaction Is Dangerous Better Strategies |
1.5 The Central Insight
Treat the context window as you would treat memory in a systems programming language: a finite, precious resource that requires deliberate management.
The techniques in this book — from token budgeting to sub-agent isolation to supervision trees — are the context engineering equivalent of the memory management discipline that separates reliable systems from fragile ones.
1.6 How to Read This Book
- Sequential: Chapters build on each other. Chapter 1’s token economics inform Chapter 2’s budget calculations, which inform Chapter 3’s slot analysis, and so on.
- Reference: Each chapter is self-contained enough to revisit independently. Cross-references link to prerequisite concepts.
- Practical: Every concept is tied to concrete numbers, real examples, and actionable patterns. No theory without application.
- PDF: The full book is available as a PDF download for offline reading.
1.7 Credits
Written by Claude (Anthropic), directed and edited by a human author. Outline from Latent Patterns. See publication history for details.
Licensed under CC BY-ND 4.0.
1.8 References and Further Reading
1.8.1 Papers
- Hsieh, C.-P., et al. (2024). “RULER: What’s the Real Context Size of Your Long-Context Language Models?” arXiv:2404.06654
- Liu, N. F., et al. (2023). “Lost in the Middle: How Language Models Use Long Contexts.” arXiv:2307.03172
- Lewis, P., et al. (2020). “Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks.” arXiv:2005.11401
- Packer, C., et al. (2023). “MemGPT: Towards LLMs as Operating Systems.” arXiv:2310.08560
- Paulsen, T. (2025). “Context Is What You Need: The Maximum Effective Context Window.” arXiv:2509.21361
- Chroma Research. (2025). “Context Rot.” research.trychroma.com/context-rot
1.8.2 Books
- Armstrong, J. (2007). Programming Erlang: Software for a Concurrent World. Pragmatic Bookshelf.
- Kernighan, B. & Ritchie, D. (1988). The C Programming Language, 2nd Ed. Prentice Hall.
- Brooks, F. (1975). The Mythical Man-Month. Addison-Wesley.
1.8.3 Articles and Blog Posts
- Anthropic. (2025). “Effective Context Engineering for AI Agents.”
- Anthropic. (2025). “How We Built Our Multi-Agent Research System.”
- Huntley, G. (2025). “Ralph Wiggum as a ‘Software Engineer.’”
- Farr, C. (2025). “The Ralph Wiggum Playbook.”
- Manus. (2025). “Context Engineering for AI Agents.”
- Cognition. (2025). “Don’t Build Multi-Agents.”
- Schmid, P. (2025). “Single vs Multi-Agent System?”
- Martin, L. (2025). “Context Engineering for Agents.”