Agent
The class representing an AI agent 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 handoffDescription
, 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.
Extends
Section titled “Extends”AgentHooks
<TContext
,TOutput
>
Type Parameters
Section titled “Type Parameters”Type Parameter | Default type |
---|---|
|
|
|
Implements
Section titled “Implements”AgentConfiguration
<TContext
,TOutput
>
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new Agent<TContext, TOutput>(config): Agent<TContext, TOutput>
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
|
{ |
‐ |
|
|
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. |
|
|
The warning log would be enabled when multiple output types by handoff agents are detected. |
|
( | |
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. |
|
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. |
|
|
|
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. |
|
A list of Model Context Protocol servers the agent can use. Every time the agent runs, it will include tools from these servers in the list of available tools. NOTE: You are expected to manage the lifecycle of these servers. Specifically, you must call
|
|
|
| |
The model implementation to use when invoking the LLM. By default, if not set, the agent will use the default model configured in modelSettings.defaultModel |
|
Configures model-specific tuning parameters (e.g. temperature, top_p, etc.) |
|
|
|
The name of the agent. |
|
|
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. |
|
|
The type of the output object. If not provided, the output will be a string. |
|
|
Wether to reset the tool choice to the default value after a tool has been called. Defaults
to |
|
|
A list of tools the agent can use. |
|
This lets you configure how tool use is handled.
NOTE: This configuration is specific to |
Returns
Section titled “Returns”Agent
<TContext
, TOutput
>
Overrides
Section titled “Overrides”Properties
Section titled “Properties”handoffDescription
Section titled “handoffDescription”handoffDescription: string;
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.
Implementation of
Section titled “Implementation of”AgentConfiguration
.handoffDescription
handoffs
Section titled “handoffs”handoffs: ( | Agent<any, TOutput> | Handoff<any, TOutput>)[];
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.
Implementation of
Section titled “Implementation of”inputGuardrails
Section titled “inputGuardrails”inputGuardrails: InputGuardrail[];
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.
Implementation of
Section titled “Implementation of”AgentConfiguration
.inputGuardrails
instructions
Section titled “instructions”instructions: string | (runContext, agent) => string | Promise<string>;
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.
Implementation of
Section titled “Implementation of”AgentConfiguration
.instructions
mcpServers
Section titled “mcpServers”mcpServers: MCPServer[];
A list of Model Context Protocol servers the agent can use. Every time the agent runs, it will include tools from these servers in the list of available tools.
NOTE: You are expected to manage the lifecycle of these servers. Specifically, you must call
server.connect()
before passing it to the agent, and server.cleanup()
when the server is
no longer needed.
Implementation of
Section titled “Implementation of”model: | string | Model;
The model implementation to use when invoking the LLM. By default, if not set, the agent will use the default model configured in modelSettings.defaultModel
Implementation of
Section titled “Implementation of”modelSettings
Section titled “modelSettings”modelSettings: ModelSettings;
Configures model-specific tuning parameters (e.g. temperature, top_p, etc.)
Implementation of
Section titled “Implementation of”AgentConfiguration
.modelSettings
name: string;
The name of the agent.
Implementation of
Section titled “Implementation of”outputGuardrails
Section titled “outputGuardrails”outputGuardrails: OutputGuardrail<AgentOutputType<unknown>>[];
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.
Implementation of
Section titled “Implementation of”AgentConfiguration
.outputGuardrails
outputType
Section titled “outputType”outputType: TOutput;
The type of the output object. If not provided, the output will be a string.
Implementation of
Section titled “Implementation of”resetToolChoice
Section titled “resetToolChoice”resetToolChoice: boolean;
Wether to reset the tool choice to the default value after a tool has been called. Defaults
to true
. This ensures that the agent doesn’t enter an infinite loop of tool usage.
Implementation of
Section titled “Implementation of”AgentConfiguration
.resetToolChoice
tools: Tool<TContext>[];
A list of tools the agent can use.
Implementation of
Section titled “Implementation of”toolUseBehavior
Section titled “toolUseBehavior”toolUseBehavior: ToolUseBehavior;
This lets you configure how tool use is handled.
- run_llm_again: The default behavior. Tools are run, and then the LLM receives the results and gets to respond.
- stop_on_first_tool: The output of the frist tool call is used as the final output. This means that the LLM does not process the result of the tool call.
- A list of tool names: The agent will stop running if any of the tools in the list are called. The final output will be the output of the first matching tool call. The LLM does not process the result of the tool call.
- A function: if you pass a function, it will be called with the run context and the list of
tool results. It must return a
ToolsToFinalOutputResult
, which determines whether the tool call resulted in a final output.
NOTE: This configuration is specific to FunctionTools
. Hosted tools, such as file search, web
search, etc. are always processed by the LLM
Implementation of
Section titled “Implementation of”AgentConfiguration
.toolUseBehavior
Accessors
Section titled “Accessors”outputSchemaName
Section titled “outputSchemaName”Get Signature
Section titled “Get Signature”get outputSchemaName(): string
Ouput schema name
Returns
Section titled “Returns”string
Methods
Section titled “Methods”asTool()
Section titled “asTool()”asTool(options): FunctionTool
Transform this agent into a tool, callable by other agents.
This is different from handoffs in two ways:
- In handoffs, the new agent receives the conversation history. In this tool, the new agent receives generated input.
- 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
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
|
{ |
Options for the tool. |
|
( |
A function that extracts the output text from the agent. If not provided, the last message from the agent will be used. |
|
|
The description of the tool, which should indicate what the tool does and when to use it. |
|
|
The name of the tool. If not provided, the name of the agent will be used. |
Returns
Section titled “Returns”A tool that runs the agent and returns the output text.
clone()
Section titled “clone()”clone(config): Agent<TContext, TOutput>
Makes a copy of the agent, with the given arguments changed. For example, you could do:
const newAgent = agent.clone({ instructions: 'New instructions' })
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
|
|
A partial configuration to change. |
Returns
Section titled “Returns”Agent
<TContext
, TOutput
>
A new agent with the given changes.
emit()
Section titled “emit()”emit<K>(type, ...args): boolean
Type Parameters
Section titled “Type Parameters”Type Parameter |
---|
|
Parameters
Section titled “Parameters”Parameter | Type |
---|---|
|
|
… |
|
Returns
Section titled “Returns”boolean
Inherited from
Section titled “Inherited from”getAllTools()
Section titled “getAllTools()”getAllTools(): Promise<Tool<TContext>[]>
ALl agent tools, including the MCPl and function tools.
Returns
Section titled “Returns”Promise
<Tool
<TContext
>[]>
all configured tools
getMcpTools()
Section titled “getMcpTools()”getMcpTools(): Promise<Tool<TContext>[]>
Fetches the available tools from the MCP servers.
Returns
Section titled “Returns”Promise
<Tool
<TContext
>[]>
the MCP powered tools
getSystemPrompt()
Section titled “getSystemPrompt()”getSystemPrompt(runContext): Promise<undefined | string>
Returns the system prompt for the agent.
If the agent has a function as its instructions, this function will be called with the runContext and the agent instance.
Parameters
Section titled “Parameters”Parameter | Type |
---|---|
|
|
Returns
Section titled “Returns”Promise
<undefined
| string
>
off<K>(type, listener): EventEmitter<AgentHookEvents<TContext, TOutput>>
Type Parameters
Section titled “Type Parameters”Type Parameter |
---|
|
Parameters
Section titled “Parameters”Parameter | Type |
---|---|
|
|
|
(… |
Returns
Section titled “Returns”EventEmitter
<AgentHookEvents
<TContext
, TOutput
>>
Inherited from
Section titled “Inherited from”on<K>(type, listener): EventEmitter<AgentHookEvents<TContext, TOutput>>
Type Parameters
Section titled “Type Parameters”Type Parameter |
---|
|
Parameters
Section titled “Parameters”Parameter | Type |
---|---|
|
|
|
(… |
Returns
Section titled “Returns”EventEmitter
<AgentHookEvents
<TContext
, TOutput
>>
Inherited from
Section titled “Inherited from”once()
Section titled “once()”once<K>(type, listener): EventEmitter<AgentHookEvents<TContext, TOutput>>
Type Parameters
Section titled “Type Parameters”Type Parameter |
---|
|
Parameters
Section titled “Parameters”Parameter | Type |
---|---|
|
|
|
(… |
Returns
Section titled “Returns”EventEmitter
<AgentHookEvents
<TContext
, TOutput
>>
Inherited from
Section titled “Inherited from”processFinalOutput()
Section titled “processFinalOutput()”processFinalOutput(output): ResolvedAgentOutput<TOutput>
Processes the final output of the agent.
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
|
|
The output of the agent. |
Returns
Section titled “Returns”ResolvedAgentOutput
<TOutput
>
The parsed out.
toJSON()
Section titled “toJSON()”toJSON(): object
Returns a JSON representation of the agent, which is serializable.
Returns
Section titled “Returns”object
A JSON object containing the agent’s name.
name: string;
create()
Section titled “create()”static create<TOutput, Handoffs>(config): Agent<unknown, TOutput | HandoffsOutputUnion<Handoffs>>
Create an Agent with handoffs and automatically infer the union type for TOutput from the handoff agents’ output types.
Type Parameters
Section titled “Type Parameters”Type Parameter | Default type |
---|---|
|
|
|
[] |
Parameters
Section titled “Parameters”Parameter | Type |
---|---|
|
|
Returns
Section titled “Returns”Agent
<unknown
, TOutput
| HandoffsOutputUnion
<Handoffs
>>