Skip to content

engineer@aine:~/book/chapters$ cat 03_context-engineering-fundamentals.md

CHAPTER 03Context Engineering Fundamentals

Context engineering is the discipline underneath everything else in the book. This chapter gives you a taxonomy of what goes into a context window, how agents discover context on their own, and how to keep a long session from rotting.

  • 38 sections
  • 10k words
  • ~46 min
  • 2 figures
key takeaways5 points
  • 01Context is everything the model receives, not just your prompt.
  • 02The context window is finite and models don't process it uniformly.
  • 03Context rot accumulates across long sessions.
  • 04The only mandatory context component is the user's query. Everything else is a choice.
  • 05Context engineering has two failure modes: too little focus, and too much distraction.

inside this chapter

  • From Prompt Engineering to Context Engineering
  • Practice
  • The Core Problem: Right Context, Right Time, Limited Window
  • The attention problem
  • The lost-in-the-middle phenomenon
  • Context rot
  • The human working-memory parallel
  • Practice
  • The right-time problem
  • The two failure modes
  • Mandatory and Optional Context Components
  • A Taxonomy of Context Components
  • System prompts
  • User input and user-provided context
  • Rules, style guides, and constraints
  • Skills
  • Tools
  • Custom agents
  • Environment context: runtime and metadata
  • Conversation history and memory management
  • Practice
  • The Context Assembly Process
  • Autonomous Context Discovery: How Agents Explore Before They Write
  • Code indexing and semantic search
  • CLI-based exploration
  • Language servers and symbolic code understanding
  • What autonomous discovery does and doesn't replace
  • Practice
  • The Agent Runtime Pipeline: From Request to Action
  • Context assembly, planning, and tool execution
  • The iterative loop: reason, act, observe, adjust
  • The Golden Balance: Too Little Focus, Too Much Distraction
  • Practice
  • Avoiding Context Rot
  • Session management
  • Token efficiency for large payloads
  • Practice
  • Summary

figures

fig 3-1
How context accumulates across a session. The static foundation is established before the first message; dynamic content (like tool calls, tool results, and follow-up messages) fills in as the session progresses.
How context accumulates across a session. The static foundation is established before the first message; dynamic content (like tool calls, tool results, and follow-up messages) fills in as the session progresses.
fig 3-2
Each iteration of the agent loop adds tool results to the context window. The context grows with every action, and managing that growth is part of what context engineering addresses.
Each iteration of the agent loop adds tool results to the context window. The context grows with every action, and managing that growth is part of what context engineering addresses.

vocabulary introduced here

man aine →

read this chapter

Chapter 3 is in the Early Release.

The code ANSE2026 gives you 30 days of free access to the O'Reilly platform, which covers this book and everything else on it.