An LLM-powered agent is an autonomous system that combines a backbone LLM with tools to perform tasks for a user. The LLM can express a plan or request a tool call as text; the surrounding agent executes the requested actions and returns their results to the LLM. The model alone does not perform those actions. section 1.2 section 1.0
Finding a croissant
To find the best-value croissant in New York City, an agent can ask its LLM to plan a search for bakeries, prices, and ratings. The LLM requests a web-search tool; the agent executes the request, supplies the result to the LLM, and continues toward an answer. section 1.0 section 1.2.1
A processing loop turns model output into task execution:
- Provide the LLM with the task and descriptions of available tools, including their names and parameters. section 1.0 section 1.2.1
- Use the LLM to plan a step and request any appropriate tool calls. The agent executes those calls outside the model and returns their results. section 1.2.1 section 1.0
- Use the results and prior steps to decide whether to take another step, revise the plan, or return a final result. section 1.2.1 section 4.5
Effective execution depends on the backbone LLM making sensible plans and tool choices. An incorrect initial plan can produce wrong results, timeouts, or inefficient execution; having an agent does not by itself guarantee task success. section 1.2.1 section 1.4
One implementation constructs an LLMAgent from an LLM and Tool objects. Its run() method accepts a Task, creates a TaskHandler, and schedules the loop as a background asyncio.Task. The handler is awaitable and resolves to a TaskResult. Within the loop, get_next_step() and run_step() coordinate decisions and execution using TaskStep and TaskStepResult structures. section 4.5
The handler records a rollout, or trajectory, by appending each sub-step’s chat history. This record makes the agent’s actions and reasoning available for evaluation and improvement. section 4.2.3 section 4.5
A sequence-generating agent
Wrap a next_number Python function as a PydanticFunctionTool, supply it to an LLMAgent, and submit a Task starting at 4. The instruction asks the agent to wait for each actual tool result before continuing. One execution returns 4 → 2 → 1; the handler’s rollout records the intermediate calls. section 4.3
# Included in chapter-notebooks/ch04/ch04.ipynb #A
from pydantic import BaseModel
from llm_agents_from_scratch.tools import PydanticFunctionTool
class AlgoParams(BaseModel):
"""Params for next_number."""
x: int
def next_number(params: AlgoParams) -> int:
"""Generate the next number of the sequence."""
if params.x % 2 == 0: #B
return params.x // 2
return 3 * params.x + 1
# convert our Python function to a BaseTool
tool = PydanticFunctionTool(next_number)The tool defines a Pydantic parameter schema and wraps the sequence function for use by the agent. section 4.3
Agents can be extended with memory to reuse information from earlier executions, or with human-in-the-loop checkpoints to mitigate cascading errors. MCP standardizes access to third-party tools and resources; Agent Skills describes reusable workflows; and A2A standardizes communication between agents. section 1.4 section 1.5
