Comprehensive guide to the Agent class and its components
The Agent class is the core component for creating AI agents with customizable behavior, tools, and guardrails. This documentation covers all the key components and configurations available when working with Agents.
A function that takes a run context and a list of tool results, and returns a ToolsToFinalOutputResult. This is used to determine whether tool calls should result in a final output or if the LLM should process the results further.
A dataclass that represents the result of processing tool outputs to determine if they should be the final output.
Whether this is the final output. If False, the LLM will run again and receive the tool call output.
The final output. Can be None if is_final_output is False, otherwise must match the output_type of the agent.
A configuration that specifies which tools should cause the agent to stop running when called.
A list of tool names, any of which will stop the agent from running further. When any of these tools are called, the agent will stop and use the tool's output as the final output.
Configuration for Model Context Protocol (MCP) servers that provide additional tools to the agent.
If True, we will attempt to convert the MCP schemas to strict-mode schemas. This is a best-effort conversion, so some schemas may not be convertible. Defaults to False.
You are expected to manage the lifecycle of MCP servers. Specifically, you must call server.connect() before passing it to the agent, and server.cleanup() when the server is no longer needed.
An agent is an AI model configured with instructions, tools, guardrails, handoffs and more. Agents are generic on the context type which is passed to tool functions, handoffs, guardrails, etc.
The name of the agent.
The instructions for the agent (system prompt). Can be a string or a function that dynamically generates instructions. If a function, it receives the context and agent instance and must return a string.
Description used when the agent is used as a handoff, so the LLM knows when to invoke it.
Sub-agents that this agent can delegate to, allowing for separation of concerns.
The model implementation to use. Defaults to model_settings.DEFAULT_MODEL if not set.
Configures model-specific parameters like temperature and top_p.
Tools that the agent can use.
MCP servers that provide additional tools to the agent.
Configuration for MCP servers.
Checks that run in parallel before generating a response (only for first agent in chain).
Checks that run on the final output after generating a response.
The type of the output object. Defaults to str if not provided.
Receives callbacks on various agent lifecycle events.
Configures how tool use is handled:
Whether to reset tool choice after a tool has been called (prevents infinite loops).
Make a copy of the agent with the given arguments changed.
Transform this agent into a tool callable by other agents.
The name of the tool (defaults to agent name)
Description indicating what it does and when to use it
Function to extract output from agent (defaults to last message)
Get the system prompt for the agent.
Fetches the available tools from the MCP servers.
Gets all agent tools, including MCP tools and function tools.