Skip to main content

Agent

Trait Agent 

Source
pub trait Agent: Send + Sync {
Show 19 methods // Required methods fn name(&self) -> &str; fn description(&self) -> &str; fn sub_agents(&self) -> &[Arc<dyn Agent>]; fn run<'life0, 'async_trait>( &'life0 self, ctx: Arc<dyn InvocationContext>, ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<Event, AdkError>> + Send>>, AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait; // Provided methods fn interaction_mode(&self) -> AgentInteractionMode { ... } fn supports_agent_transfer(&self) -> bool { ... } fn capabilities(&self) -> AgentCapabilities { ... } fn cancel_remote_task<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, _task_id: &'life1 str, _acting: &'life2 ActingContext, ) -> Pin<Box<dyn Future<Output = Result<(), AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, Self: 'async_trait { ... } fn profile(&self) -> AgentProfile { ... } fn topology(&self) -> Option<AgentTopology> { ... } fn configure_run(&self, _agent_name: &str, _config: &mut RunConfig) { ... } fn transfer_targets_for(&self, _agent_name: &str) -> Option<Vec<String>> { ... } fn strict_transfer_policy(&self) -> bool { ... } fn govern_transfer<'life0, 'life1, 'async_trait>( &'life0 self, _request: &'life1 AgentTransferRequest, ) -> Pin<Box<dyn Future<Output = Result<AgentTransferDecision, AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait { ... } fn allows_transfer_to_parent(&self) -> bool { ... } fn allows_transfer_to_peers(&self) -> bool { ... } fn close<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait { ... } fn prewarm<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait { ... } fn screen_user_input<'life0, 'life1, 'async_trait>( &'life0 self, content: &'life1 Content, ) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait { ... }
}
Expand description

Core traits and types.

Always available regardless of feature flags. Includes:

  • Agent - The fundamental trait for all agents
  • Tool / Toolset - For extending agents with capabilities
  • Session / State - For managing conversation context
  • Event - For streaming agent responses
  • AdkError / Result - Unified error handling The fundamental trait for all ADK agents.

Every agent — whether a simple LLM wrapper, a multi-step workflow, or a composite orchestrator — implements this trait. The runtime invokes run with an InvocationContext and consumes the returned EventStream.

Required Methods§

Source

fn name(&self) -> &str

Returns the unique name of this agent.

Source

fn description(&self) -> &str

Returns a human-readable description of this agent’s purpose.

Source

fn sub_agents(&self) -> &[Arc<dyn Agent>]

Returns the child agents managed by this agent.

Source

fn run<'life0, 'async_trait>( &'life0 self, ctx: Arc<dyn InvocationContext>, ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<Event, AdkError>> + Send>>, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Executes the agent and returns a stream of events.

Provided Methods§

Source

fn interaction_mode(&self) -> AgentInteractionMode

Returns the agent’s primary interaction pattern.

Existing agents remain request/response by default. Realtime agents override this without introducing a new atomic agent kind or changing composition semantics.

Source

fn supports_agent_transfer(&self) -> bool

Whether this agent participates in LLM-driven agent transfer and may be resumed directly across conversation turns.

When a session persists across turns, the runner inspects history to decide which agent should handle the next user message. LLM-based and custom agents return the default true, so the runner can hand a new turn back to whichever agent responded last.

Deterministic workflow agents (sequential, parallel, loop, conditional) override this to return false. Their sub-agents must not be resumed individually: doing so would skip the workflow’s other sub-agents on subsequent turns. Returning false makes the runner resume the workflow root instead, so every sub-agent runs again on each turn.

Source

fn capabilities(&self) -> AgentCapabilities

Declares the execution-plane capabilities this agent supports.

The default remains compatible with existing custom agents: transfer, shared-state, cancellation, and request metadata follow the established Agent and InvocationContext contracts. Runtime tool injection and exact-call relationship confirmation are opt-in because an implementation must actively consume those facilities.

Source

fn cancel_remote_task<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, _task_id: &'life1 str, _acting: &'life2 ActingContext, ) -> Pin<Box<dyn Future<Output = Result<(), AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, Self: 'async_trait,

Cancels task_id, a task this agent started on another agent, for acting: the task that waited on it was cancelled, so the input it waits for will never come.

The default does nothing: only an agent that starts tasks elsewhere, such as an A2A remote agent, has any to cancel.

§Errors

Returns an error when the other agent cannot be reached or refuses the cancel.

Source

fn profile(&self) -> AgentProfile

What the agent tells callers about itself when they list its skills, such as an A2A agent card: its kind, its fixed instructions and its tools.

The default is a AgentKind::Custom agent with nothing more to say. LlmAgent and the workflow agents describe themselves.

Source

fn topology(&self) -> Option<AgentTopology>

Returns portable composition metadata when this agent owns an explicit topology.

Leaf agents and legacy composites return None. The default keeps this additive API backward compatible for existing Agent implementations.

Source

fn configure_run(&self, _agent_name: &str, _config: &mut RunConfig)

Applies agent-composition policy to a run before the runtime creates the invocation context for agent_name.

Composite agents can use this hook to inject invocation-scoped tools, constrain transfer targets, or tighten depth and concurrency limits. The default is a no-op, preserving the behavior of existing agents.

Source

fn transfer_targets_for(&self, _agent_name: &str) -> Option<Vec<String>>

Returns the exact handoff targets allowed for agent_name, when this agent owns an explicit transfer topology.

None asks the runtime to retain its legacy parent/peer discovery. Some(vec![]) explicitly forbids handoff from the named agent.

Source

fn strict_transfer_policy(&self) -> bool

Whether transfer-policy violations should fail the run.

Legacy agent trees return false, so missing targets and exceeded depth retain their historical warn-and-stop behavior. Validated composites return true to make topology violations observable errors.

Source

fn govern_transfer<'life0, 'life1, 'async_trait>( &'life0 self, _request: &'life1 AgentTransferRequest, ) -> Pin<Box<dyn Future<Output = Result<AgentTransferDecision, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Applies asynchronous policy to an otherwise valid transfer.

Runner calls this after exact target validation and before control moves to the target. Ordinary agents allow transfers, preserving legacy behavior; validated composites can attach authorization, lifecycle hooks, audit logging, or other policy without teaching Runner their schema.

Source

fn allows_transfer_to_parent(&self) -> bool

Whether this agent may hand control back to the agent that lists it as a sub-agent.

The runner checks every transfer an agent’s events request, whether its model or one of its tools set EventActions::transfer_to_agent, against the agents it may transfer to: its sub-agents, its parent when this returns true, and its parent’s other sub-agents when allows_transfer_to_peers returns true. Defaults to true.

Source

fn allows_transfer_to_peers(&self) -> bool

Whether this agent may hand control to the other sub-agents of its parent.

See allows_transfer_to_parent. Defaults to true.

Source

fn close<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Releases the resources this agent and its sub-agents hold, such as toolset connections.

adk_runner::Runner::close calls this on its root agent when the application shuts down. The default closes each of sub_agents; an agent that owns toolsets or plugins closes them, then its sub-agents.

§Example
use adk_core::{Agent, EventStream, InvocationContext, Result};
use async_trait::async_trait;
use std::sync::Arc;

struct Indexer {
    connection: tokio::sync::Mutex<Option<String>>,
}

#[async_trait]
impl Agent for Indexer {
    fn name(&self) -> &str {
        "indexer"
    }

    fn description(&self) -> &str {
        "indexes documents"
    }

    fn sub_agents(&self) -> &[Arc<dyn Agent>] {
        &[]
    }

    async fn close(&self) {
        self.connection.lock().await.take();
    }

    async fn run(&self, _ctx: Arc<dyn InvocationContext>) -> Result<EventStream> {
        Ok(Box::pin(futures::stream::empty()))
    }
}
Source

fn prewarm<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Readies what this agent’s first turn would otherwise start, such as its model’s connection and its toolsets’ servers, so the turn starts without that wait.

adk_runner::Runner::prewarm calls this on its root agent. It sends no prompt and runs no tool. The default prewarms each of sub_agents; an agent that owns a model or toolsets readies them, then its sub-agents.

§Errors

Returns the first error a model or toolset gave; a turn would meet the same error.

§Example
use adk_core::{Agent, EventStream, InvocationContext, Result};
use async_trait::async_trait;
use std::sync::Arc;

struct Leaf;

#[async_trait]
impl Agent for Leaf {
    fn name(&self) -> &str {
        "leaf"
    }
    fn description(&self) -> &str {
        "does nothing"
    }
    fn sub_agents(&self) -> &[Arc<dyn Agent>] {
        &[]
    }
    async fn run(&self, _ctx: Arc<dyn InvocationContext>) -> Result<EventStream> {
        Ok(Box::pin(futures::stream::empty()))
    }
}

Leaf.prewarm().await?;
Source

fn screen_user_input<'life0, 'life1, 'async_trait>( &'life0 self, content: &'life1 Content, ) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Screens a user message with the input guardrails of this agent and of every agent below it.

The runner calls this on its root agent before it saves a user message, and saves only what it returns, so the session and every later model request hold only the screened message. It also screens, before any model call, a saved user message that carries no EventActions::input_screened marker. Ok(None) means no agent in the tree screens input; Ok(Some(content)) is the screened message, transformed when a guardrail redacts it.

The default screens the message with each of sub_agents in order. An agent with input guardrails applies its own first, then its sub-agents’. An agent that wraps another agent without listing it in sub_agents delegates to it.

§Example
use adk_core::{Agent, Content, EventStream, InvocationContext, Result};
use async_trait::async_trait;
use std::sync::Arc;

/// Runs `inner` without listing it as a sub-agent.
struct Wrapper {
    inner: Arc<dyn Agent>,
}

#[async_trait]
impl Agent for Wrapper {
    fn name(&self) -> &str {
        "wrapper"
    }

    fn description(&self) -> &str {
        "runs its inner agent"
    }

    fn sub_agents(&self) -> &[Arc<dyn Agent>] {
        &[]
    }

    async fn screen_user_input(&self, content: &Content) -> Result<Option<Content>> {
        self.inner.screen_user_input(content).await
    }

    async fn run(&self, ctx: Arc<dyn InvocationContext>) -> Result<EventStream> {
        self.inner.run(ctx).await
    }
}
§Errors

Returns the guardrail’s error when it blocks the message, which is then neither saved nor sent to a model.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§