Agents
Agent
dataclass
Bases: Generic[TContext]
An agent is an AI model configured with instructions, tools, guardrails, handoffs and more.
We strongly recommend passing instructions
, which is the "system prompt" for the agent. In
addition, you can pass description
, which is a human-readable description of the agent, used
when the agent is used inside tools/handoffs.
Agents are generic on the context type. The context is a (mutable) object you create. It is passed to tool functions, handoffs, guardrails, etc.
Source code in src/agents/agent.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
|
instructions
class-attribute
instance-attribute
instructions: (
str
| Callable[
[RunContextWrapper[TContext], Agent[TContext]],
MaybeAwaitable[str],
]
| None
) = None
The instructions for the agent. Will be used as the "system prompt" when this agent is invoked. Describes what the agent should do, and how it responds.
Can either be a string, or a function that dynamically generates instructions for the agent. If you provide a function, it will be called with the context and the agent instance. It must return a string.
handoff_description
class-attribute
instance-attribute
A description of the agent. This is used when the agent is used as a handoff, so that an LLM knows what it does and when to invoke it.
handoffs
class-attribute
instance-attribute
Handoffs are sub-agents that the agent can delegate to. You can provide a list of handoffs, and the agent can choose to delegate to them if relevant. Allows for separation of concerns and modularity.
model
class-attribute
instance-attribute
model: str | Model | None = None
The model implementation to use when invoking the LLM.
By default, if not set, the agent will use the default model configured in
model_settings.DEFAULT_MODEL
.
model_settings
class-attribute
instance-attribute
model_settings: ModelSettings = field(
default_factory=ModelSettings
)
Configures model-specific tuning parameters (e.g. temperature, top_p).
tools
class-attribute
instance-attribute
tools: list[Tool] = field(default_factory=list)
A list of tools that the agent can use.
input_guardrails
class-attribute
instance-attribute
input_guardrails: list[InputGuardrail[TContext]] = field(
default_factory=list
)
A list of checks that run in parallel to the agent's execution, before generating a response. Runs only if the agent is the first agent in the chain.
output_guardrails
class-attribute
instance-attribute
output_guardrails: list[OutputGuardrail[TContext]] = field(
default_factory=list
)
A list of checks that run on the final output of the agent, after generating a response. Runs only if the agent produces a final output.
output_type
class-attribute
instance-attribute
The type of the output object. If not provided, the output will be str
.
hooks
class-attribute
instance-attribute
hooks: AgentHooks[TContext] | None = None
A class that receives callbacks on various lifecycle events for this agent.
clone
clone(**kwargs: Any) -> Agent[TContext]
Make a copy of the agent, with the given arguments changed. For example, you could do:
Source code in src/agents/agent.py
as_tool
as_tool(
tool_name: str | None,
tool_description: str | None,
custom_output_extractor: Callable[
[RunResult], Awaitable[str]
]
| None = None,
) -> Tool
Transform this agent into a tool, callable by other agents.
This is different from handoffs in two ways: 1. In handoffs, the new agent receives the conversation history. In this tool, the new agent receives generated input. 2. In handoffs, the new agent takes over the conversation. In this tool, the new agent is called as a tool, and the conversation is continued by the original agent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool_name
|
str | None
|
The name of the tool. If not provided, the agent's name will be used. |
required |
tool_description
|
str | None
|
The description of the tool, which should indicate what it does and when to use it. |
required |
custom_output_extractor
|
Callable[[RunResult], Awaitable[str]] | None
|
A function that extracts the output from the agent. If not provided, the last message from the agent will be used. |
None
|
Source code in src/agents/agent.py
get_system_prompt
async
get_system_prompt(
run_context: RunContextWrapper[TContext],
) -> str | None
Get the system prompt for the agent.