Skip to main content

Overview

BaseAgent is the abstract base class that all agents inherit from. It handles agent lifecycle, parent-child relationships, bus communication, and task coordination. Agents that return a pipeline from build_pipeline() run a Pipecat pipeline. Agents that don’t override build_pipeline() operate purely through bus messages (e.g. coordinators, orchestrators).

Configuration

str
required
Unique name for this agent. Used for bus message routing and registry lookup.
AgentBus
required
The AgentBus for inter-agent communication.
bool
default:"True"
Whether the agent starts active. When False, the agent waits for an activate_agent() call before on_activated fires.
tuple[str, ...] | None
default:"None"
Bridge configuration for receiving pipeline frames from the bus. None means not bridged. An empty tuple () means bridged, accepting frames from all bridges. A tuple of names like ("voice",) means bridged, accepting only frames from those bridges.
tuple[type[Frame], ...] | None
default:"None"
Frame types to exclude from bus forwarding when bridged. Lifecycle frames (StartFrame, EndFrame, CancelFrame, StopFrame) are always excluded.

Properties

bus

The bus instance for agent communication.

active

Whether this agent is currently active.

activation_args

The arguments from the most recent activation, or None if the agent is inactive. The value is cleared when the agent is deactivated.

parent

The name of the parent agent, or None if this is a root agent.

registry

The shared agent registry, if set by a runner.

bridged

Whether this agent is bridged (receives pipeline frames from the bus).

ready

Whether this agent’s pipeline has started and is ready to operate.

started_at

Unix timestamp when this agent became ready, or None if not yet started.

children

The list of child agents added via add_agent().

pipeline_task

The PipelineTask for this agent. Raises RuntimeError if the pipeline task has not been created yet.

task_id

The ID of the task this agent is currently working on, or None if idle.

task_groups

Active task groups launched by this agent, keyed by task_id.

Lifecycle Hooks

Override these methods to react to lifecycle events. Always call super() when overriding.

on_ready

Called once when the agent’s pipeline starts and the agent is ready to operate.

on_finished

Called when the agent’s pipeline has finished.

on_error

Called when a pipeline error occurs. Override to handle errors (e.g. propagate via send_error(), fail a running task, or log and recover).

on_activated

Called when this agent is activated. Override to react to activation.

on_deactivated

Called when this agent is deactivated.

on_agent_ready

Called when another agent is ready to receive messages. For local root agents this fires automatically. For remote agents it fires only for agents watched via watch_agent(). For child agents it fires only on the parent.

on_agent_error

Called when a child agent reports an error.

Agent Management

add_agent

Register a child agent under this parent. The child’s lifecycle (end, cancel) is automatically managed by this parent agent.

activate_agent

Activate an agent by name. The target agent’s on_activated hook will be called with the provided arguments.

deactivate_agent

Deactivate an agent by name. The target agent’s on_deactivated hook will be called.

handoff_to

Hand off to another agent. Deactivates this agent and activates the target. For independent control, use activate_agent() and deactivate_agent() directly.

watch_agent

Request notification when an agent registers. If the agent is already registered, on_agent_ready fires immediately. Otherwise it fires when the agent eventually registers.

end

Request a graceful end of the session.

cancel

Request an immediate cancellation of all agents.

Task Coordination

request_task

Send a task request to a single agent (fire-and-forget). Waits for the agent to be ready before sending the request. Does not wait for the task to complete; use callbacks (on_task_response, on_task_completed) or task() for that. Returns: The generated task_id.

task

Create a single-agent task context manager. Waits for the agent to be ready, sends a task request, and waits for the response on exit. Supports async for inside the block to receive intermediate events. Returns: A TaskContext to use with async with.

request_task_group

Send a task request to multiple agents (fire-and-forget). Waits for all agents to be ready before sending requests. Returns: The generated task_id shared by all agents in the group.

task_group

Create a task group context manager. Waits for agents to be ready, sends task requests, and waits for all responses on exit. Supports async for inside the block to receive intermediate events. Returns: A TaskGroupContext to use with async with.

cancel_task

Cancel a running task group.

request_task_update

Request a progress update from a task agent.

send_task_response

Send a task response back to the requester. After sending, the agent is ready to accept a new task.

send_task_update

Send a progress update to the requester.

send_task_stream_start

Begin streaming task results back to the requester.

send_task_stream_data

Send a streaming chunk to the requester.

send_task_stream_end

End the current stream and mark this agent’s task as complete.

Task Hooks

Override these methods to handle task events. Always call super() when overriding.

on_task_request

Called when this agent receives a task request. Override to perform work. Use send_task_update() to report progress and send_task_response() to return results.

on_task_response

Called when a task agent sends a response. Override to process individual results as they arrive.

on_task_update

Called when a task agent sends a progress update.

on_task_update_requested

Called when the requester asks for a progress update. Override to send back a progress update via send_task_update().

on_task_completed

Called when all agents in a task group have responded.

on_task_error

Called when a task group is cancelled due to a worker error. Fires when a worker responds with ERROR or FAILED status and cancel_on_error is set.

on_task_stream_start

Called when a task agent begins streaming.

on_task_stream_data

Called for each streaming chunk from a task agent.

on_task_stream_end

Called when a task agent finishes streaming.

on_task_cancelled

Called when this agent’s task is cancelled by the requester. Override to clean up resources or stop in-progress work.

Pipeline

build_pipeline

Return this agent’s pipeline. Override to define a processing pipeline. The default returns a no-op pipeline for agents that operate purely through bus messages.

create_pipeline

Assemble the final pipeline from the user pipeline. When bridged, wraps the pipeline with bus edge processors.

build_pipeline_task

Create the PipelineTask for this agent’s pipeline. Override to customize task parameters (e.g. enable interruptions).

queue_frame

Queue a frame into this agent’s pipeline task.

queue_frames

Queue multiple frames into this agent’s pipeline task.

Bus

send_message

Send a message on the bus.

send_error

Report an error on the bus. Child agents send a local-only message to the parent. Root agents broadcast over the network.

on_bus_message

Called for every bus message after built-in lifecycle handling. Override to handle custom message types.