Agent.run() or Agent.arun(). The execution flow:
- The agent builds context to send to the model (system message, user message, chat history, user memories, session state, and other relevant inputs).
- The agent sends this context to the model.
- The model responds with either a message or a tool call.
- If the model makes a tool call, the agent executes it and returns results to the model.
- The model processes the updated context, repeating this loop until it produces a final message without tool calls.
- The agent returns this final response to the caller.
Basic Execution
Agent.run() returns a RunOutput object, or a stream of RunOutputEvent objects when stream=True:
Run Input
Theinput parameter can be a string, list, dictionary, message, Pydantic model, or list of messages:
Run Output
Agent.run() returns a RunOutput object when not streaming. Core attributes:
run_id: The ID of the run.agent_id: The ID of the agent.agent_name: The name of the agent.session_id: The ID of the session.user_id: The ID of the user.content: The response content.content_type: The type of content. For structured output, this is the class name of the Pydantic model.reasoning_content: The reasoning content.messages: The list of messages sent to the model.metrics: The metrics of the run. See Metrics.model: The model used for the run.
Streaming
Setstream=True to return an iterator of RunOutputEvent objects:
Streaming Events
By default, onlyRunContent events (model responses) are streamed.
To stream all events (tool calls, reasoning, memory updates, etc.), set stream_events=True:
Handling Events
Process events as they arrive:Run events expose each step of the run as it happens. Use them for live UI updates and debugging.
Event Types
Events yielded byAgent.run() and Agent.arun(), depending on agent configuration:
Core Events
Control Flow Events
Tool Events
Reasoning Events
Memory Events
Session Summary Events
Pre-Hook Events
Post-Hook Events
Parser Model Events
Output Model Events
Model Request Events
Compression Events
Followup Events
Custom Events
Create custom events by extendingCustomEvent:
Specify Run User and Session
Passuser_id and session_id to associate a run with a specific user and session:
Passing Images / Audio / Video / Files
Pass media viaimages, audio, videos, or files parameters:
Passing Output Schema
Pass an output schema for structured output:Pausing and Continuing a Run
An agent run can be paused for human-in-the-loop flows. Continue execution withAgent.continue_run().
See Human-in-the-Loop for more details.
Cancelling a Run
Cancel a run withAgent.cancel_run().
See Cancelling a Run for more details.
Background Execution
Run agents in the background withAgent.arun(background=True). The agent continues running even if the client disconnects. Combine with stream=True for resumable SSE streaming with automatic event buffering and reconnection.
See Background Execution for polling, resumable streaming, and the /resume endpoint.