Agent.run()
Learn how to run an agent and get the response.
The Agent.run()
function runs the agent and generates a response, either as a RunResponse
object or a stream of RunResponse
objects.
Many of our examples use agent.print_response()
which is a helper utility to print the response in the terminal. It uses agent.run()
under the hood.
Here’s how to run your agent. The response is captured in the response
and response_stream
variables.
stream=True
to return a stream of RunResponse
objects.RunResponse
The Agent.run()
function returns either a RunResponse
object or an Iterator[RunResponse]
when stream=True
. It has the following attributes:
Understanding Metrics
For a detailed explanation of how metrics are collected and used, please refer to the Metrics Documentation.
RunResponse Attributes
Attribute | Type | Default | Description |
---|---|---|---|
content | Any | None | Content of the response. |
content_type | str | "str" | Specifies the data type of the content. |
context | List[MessageContext] | None | The context added to the response for RAG. |
event | str | RunEvent.run_response.value | Event type of the response. |
event_data | Dict[str, Any] | None | Data associated with the event. |
messages | List[Message] | None | A list of messages included in the response. |
metrics | Dict[str, Any] | None | Usage metrics of the run. |
model | str | None | The model used in the run. |
run_id | str | None | Run Id. |
agent_id | str | None | Agent Id for the run. |
session_id | str | None | Session Id for the run. |
tools | List[Dict[str, Any]] | None | List of tools provided to the model. |
images | List[Image] | None | List of images the model produced. |
videos | List[Video] | None | List of videos the model produced. |
audio | List[Audio] | None | List of audio snippets the model produced. |
response_audio | ModelResponseAudio | None | The model’s raw response in audio. |
created_at | int | - | Unix timestamp of the response creation. |
extra_data | RunResponseExtraData | None | Extra data containing optional fields like references , add_messages , history , reasoning_steps , and reasoning_messages . |
Streaming Intermediate Steps
Throughout the execution of an agent, multiple events take place, and we provide these events in real-time for enhanced agent transparency.
You can enable streaming of intermediate steps by setting stream_intermediate_steps=True
.
Event Types
The following events are sent by the Agent.run()
and Agent.arun()
functions depending on agent’s configuration:
Event Type | Description |
---|---|
RunStarted | Indicates the start of a run |
RunResponse | Contains the model’s response text as individual chunks |
RunCompleted | Signals successful completion of the run |
RunError | Indicates an error occurred during the run |
RunCancelled | Signals that the run was cancelled |
ToolCallStarted | Indicates the start of a tool call |
ToolCallCompleted | Signals completion of a tool call. This also contains the tool call results. |
ReasoningStarted | Indicates the start of the agent’s reasoning process |
ReasoningStep | Contains a single step in the reasoning process |
ReasoningCompleted | Signals completion of the reasoning process |
UpdatingMemory | Indicates that the agent is updating its memory |
WorkflowStarted | Indicates the start of a workflow |
WorkflowCompleted | Signals completion of a workflow |
You can see this behavior in action in our Playground.