liveBook · Manning

Context engineering

As explained in Build an AI Agent (From Scratch)

The discipline of designing which information is provided to a language model, when it is provided, and how it is represented for task completion.

Context engineering is the practice of giving a language model the information it needs for a task, at the right time and in the right form. A prompt is only part of that context: the model may also receive conversation history, tool results, and retrieved documents. In an agent, those inputs change after every tool call. The design problem is therefore not just what to say in a system prompt, but what to add, retain, remove, or keep outside the model’s context window. section 1.5 section 1.5.2

Five strategies organize those decisions:

  • Generate: add model-produced plans or reflections that help structure later work. section 1.5.4
  • Retrieve: bring in needed information through searches, queries, files, or document retrieval. section 1.5.4
  • Write: save useful information outside the context, such as memories, intermediate results, or code. section 1.5.4
  • Reduce: summarize, filter, or delete information so the context stays focused rather than accumulating unnecessary content. section 1.5.4
  • Isolate: give separate environments or specialized agents distinct tasks instead of forcing everything into one context. section 1.5.4

A practical agent separates stored execution state from the request sent to the model. ExecutionContext holds the state used by the agent and its tools; LlmRequest contains the curated subset needed for one model call. When a tool runs, its result returns to execution state. The next request is another selection and formatting decision—not an obligation to send everything stored. This boundary makes missing information and excessive context easier to investigate. section 4.2.2 section 1.0

Keep tool details in a workspace

Suppose an agent can run Python, read files, and use additional skills. Including every tool definition in every prompt would fill the context with instructions that may not matter to the current task. Instead, keep core tools such as execute_python and bash_tool available, place detailed skill instructions in workspace files, and let the agent read a skill document when needed. Code generated for a one-time task can run through execute_python. This arrangement controls when tool information enters context and keeps execution results that need no model judgment out of it. section 8.5.5 section 8.6

Figure 4.3 Information flow in the agent architecture. ExecutionContext sits at the center, managing all execution state. Arrows show (1) context to LlmRequest to LlmClient for outgoing LLM calls, (2) LlmResponse back to context for incoming responses, and (3) bidirectional flow between context and tools for tool execution.
Shape

Description automatically generated with low confidence

The information-flow diagram distinguishes all execution state from the curated request sent to the model, and shows how tool results return to state. section 4.2.2

How Build an AI Agent (From Scratch) covers it

How deep it goes
Defines five strategies and shows an execution-state-to-request boundary, while leaving detailed strategy implementations to later components.
Examples
Uses a curated LlmRequest and workspace-based skill documents to illustrate selective disclosure of state and tool instructions.
What it assumes
Starts with LLMs and a basic agent loop before adding tools and context-management components incrementally.
What you can do after
Build it from scratch by selecting model-visible state, then adding retrieval, memory, planning, and workspaces to an agent loop.
How it connects
Connects prompt engineering with tool use, retrieval, memory, planning, workspaces, and multi-agent systems.

Also explained in

Compare how 2 books explain context engineering →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build an AI Agent (From Scratch). The section links point to the parts of the book it draws on.