Runner
The execution runtime from adk-runner that orchestrates agent execution.
Overview
The Runner manages the complete lifecycle of agent execution:
- Session management (create/retrieve sessions)
- Memory injection (search and inject relevant memories)
- Artifact handling (scoped artifact access)
- Event streaming (process and forward events)
- Agent transfers (handle multi-agent handoffs)
Installation
[dependencies]
adk-runner = "0.2.0"
RunnerConfig
Configure the runner with required services:
use adk_runner::{Runner, RunnerConfig};
use adk_session::InMemorySessionService;
use adk_artifact::InMemoryArtifactService;
use std::sync::Arc;
let config = RunnerConfig {
app_name: "my_app".to_string(),
agent: Arc::new(my_agent),
session_service: Arc::new(InMemorySessionService::new()),
artifact_service: Some(Arc::new(InMemoryArtifactService::new())),
memory_service: None,
run_config: None,
};
let runner = Runner::new(config)?;
Configuration Fields
| Field | Type | Required | Description |
|---|---|---|---|
app_name | String | Yes | Application identifier |
agent | Arc<dyn Agent> | Yes | Root agent to execute |
session_service | Arc<dyn SessionService> | Yes | Session storage backend |
artifact_service | Option<Arc<dyn ArtifactService>> | No | Artifact storage |
memory_service | Option<Arc<dyn Memory>> | No | Long-term memory |
run_config | Option<RunConfig> | No | Execution options |
Running Agents
Execute an agent with user input:
use adk_core::Content;
use futures::StreamExt;
let user_content = Content::new("user").with_text("Hello!");
let mut stream = runner.run(
"user-123".to_string(),
"session-456".to_string(),
user_content,
).await?;
while let Some(event) = stream.next().await {
match event {
Ok(e) => {
if let Some(content) = e.content() {
for part in &content.parts {
if let Some(text) = part.text() {
print!("{}", text);
}
}
}
}
Err(e) => eprintln!("Error: {}", e),
}
}
Execution Flow
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Runner.run() β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 1. Session Retrieval β
β β
β SessionService.get(app_name, user_id, session_id) β
β β Creates new session if not exists β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 2. Agent Selection β
β β
β Check session state for active agent β
β β Use root agent or transferred agent β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 3. Context Creation β
β β
β InvocationContext with: β
β - Session (mutable) β
β - Artifacts (scoped to session) β
β - Memory (if configured) β
β - Run config β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 4. Agent Execution β
β β
β agent.run(ctx) β EventStream β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 5. Event Processing β
β β
β For each event: β
β - Update session state β
β - Handle transfers β
β - Forward to caller β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 6. Session Save β
β β
β SessionService.append_event(session, events) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
InvocationContext
The context provided to agents during execution:
pub trait InvocationContext: CallbackContext {
/// The agent being executed
fn agent(&self) -> Arc<dyn Agent>;
/// Memory service (if configured)
fn memory(&self) -> Option<Arc<dyn Memory>>;
/// Current session
fn session(&self) -> &dyn Session;
/// Execution configuration
fn run_config(&self) -> &RunConfig;
/// Signal end of invocation
fn end_invocation(&self);
/// Check if invocation has ended
fn ended(&self) -> bool;
}
RunConfig
Execution options:
pub struct RunConfig {
/// Streaming mode for responses
pub streaming_mode: StreamingMode,
}
pub enum StreamingMode {
/// No streaming, return complete response
None,
/// Server-Sent Events (default)
SSE,
/// Bidirectional streaming (realtime)
Bidi,
}
Note: Additional fields like
max_turnsandinclude_historyare planned for future releases.
Agent Transfers
The Runner handles multi-agent transfers automatically:
// In an agent's tool or callback
if should_transfer {
// Set transfer in event actions
ctx.set_actions(EventActions {
transfer_to_agent: Some("specialist_agent".to_string()),
..Default::default()
});
}
The Runner will:
- Detect the transfer request in the event
- Find the target agent in sub_agents
- Update session state with new active agent
- Continue execution with the new agent
Integration with Launcher
The Launcher uses Runner internally:
// Launcher creates Runner with default services
Launcher::new(agent)
.app_name("my_app")
.run()
.await?;
// Equivalent to:
let runner = Runner::new(RunnerConfig {
app_name: "my_app".to_string(),
agent,
session_service: Arc::new(InMemorySessionService::new()),
artifact_service: Some(Arc::new(FileArtifactService::new("./artifacts")?)),
memory_service: None,
run_config: None,
})?;
Custom Runner Usage
For advanced scenarios, use Runner directly:
use adk_runner::{Runner, RunnerConfig};
use adk_session::DatabaseSessionService;
use adk_artifact::S3ArtifactService;
use adk_memory::QdrantMemoryService;
// Production configuration
let config = RunnerConfig {
app_name: "production_app".to_string(),
agent: my_agent,
session_service: Arc::new(DatabaseSessionService::new(db_pool)),
artifact_service: Some(Arc::new(S3ArtifactService::new(s3_client))),
memory_service: Some(Arc::new(QdrantMemoryService::new(qdrant_client))),
run_config: None, // Uses default SSE streaming
};
let runner = Runner::new(config)?;
// Use in HTTP handler
async fn chat_handler(runner: &Runner, request: ChatRequest) -> Response {
let stream = runner.run(
request.user_id,
request.session_id,
request.content,
).await?;
// Stream events to client
Response::sse(stream)
}
Previous: β Core Types | Next: Launcher β