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
