RealtimeAgent
Bases: AgentBase
, Generic[TContext]
A specialized agent instance that is meant to be used within a RealtimeSession
to build
voice agents. Due to the nature of this agent, some configuration options are not supported
that are supported by regular Agent
instances. For example:
- model
choice is not supported, as all RealtimeAgents will be handled by the same model
within a RealtimeSession
.
- modelSettings
is not supported, as all RealtimeAgents will be handled by the same model
within a RealtimeSession
.
- outputType
is not supported, as RealtimeAgents do not support structured outputs.
- toolUseBehavior
is not supported, as all RealtimeAgents will be handled by the same model
within a RealtimeSession
.
- voice
can be configured on an Agent
level; however, it cannot be changed after the first
agent within a RealtimeSession
has spoken.
See AgentBase
for base parameters that are shared with Agent
s.
Source code in src/agents/realtime/agent.py
instructions
class-attribute
instance-attribute
instructions: (
str
| Callable[
[
RunContextWrapper[TContext],
RealtimeAgent[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.
handoffs
class-attribute
instance-attribute
handoffs: list[
RealtimeAgent[Any]
| Handoff[TContext, RealtimeAgent[Any]]
] = field(default_factory=list)
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.
hooks
class-attribute
instance-attribute
A class that receives callbacks on various lifecycle events for this agent.
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.
tools
class-attribute
instance-attribute
tools: list[Tool] = field(default_factory=list)
A list of tools that the agent can use.
mcp_servers
class-attribute
instance-attribute
mcp_servers: list[MCPServer] = field(default_factory=list)
A list of Model Context Protocol servers that 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.
mcp_config
class-attribute
instance-attribute
Configuration for MCP servers.
clone
clone(**kwargs: Any) -> RealtimeAgent[TContext]
Make a copy of the agent, with the given arguments changed. For example, you could do:
Source code in src/agents/realtime/agent.py
get_system_prompt
async
get_system_prompt(
run_context: RunContextWrapper[TContext],
) -> str | None
Get the system prompt for the agent.
Source code in src/agents/realtime/agent.py
get_mcp_tools
async
get_mcp_tools(
run_context: RunContextWrapper[TContext],
) -> list[Tool]
Fetches the available tools from the MCP servers.
Source code in src/agents/agent.py
get_all_tools
async
get_all_tools(
run_context: RunContextWrapper[Any],
) -> list[Tool]
All agent tools, including MCP tools and function tools.