liveBook · Manning

Agent class

As explained in Build an AI Agent (From Scratch)

The Agent class is the book's abstract programming interface for representing an agent loop.

The Agent class is an orchestration interface for a tool-using agent loop. It coordinates an LLM, tools, execution state, and information flow so the agent can solve problems step by step. section 1.5.6 section 4.6

Its core execution flow consists of:

  • Initializing with an LlmClient, BaseTool instances, instructions, a maximum step count, and an optional name. section 4.6.1
  • Registering tools through _setup_tools(), which provides an extension point for adding or modifying default tools. section 4.6.1
  • Creating an ExecutionContext in run() and repeating step() until the task completes or max_steps prevents an infinite loop. section 4.6
  • Performing each think–act cycle by having think() request the LLM’s next action and act() execute requested tools. section 4.6

Tool callbacks extend act() without changing the basic loop. Before-callbacks can intercept a call and supply an early result; after-callbacks can replace a tool result. Both synchronous and asynchronous callbacks are supported, and processing stops at the first non-None callback result. section 5.5.2

Figure 4.4 Agent implementation road map showing the five core methods and their execution flow. The numbered sequence illustrates how the components work together to solve problems.

Trace the five-method execution flow from tool setup through the repeated think–act cycle. section 4.6

The interface can also return structured output through typed Pydantic models instead of free-form text, making agent results predictable for applications. section 4.6

How Build an AI Agent (From Scratch) covers it

How deep it goes
Descends through constructor, tool setup, run, step, think, and act, then extends execution with callbacks and structured output.
Examples
Uses five core methods, a max_steps safety limit, tool callbacks, and typed Pydantic results as implementation examples.
What it assumes
Assumes ExecutionContext, BaseTool, and LlmClient, while assembling their roles into the complete agent loop.
What you can do after
Build it from scratch as a multistep tool-using agent with interception callbacks and predictable typed results.
How it connects
Connects ExecutionContext, tool abstraction, LLM communication, ReAct, callbacks, retrieval, and structured output.

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.