# Agent Development Kit (ADK)
> Build powerful multi-agent systems with Agent Development Kit (ADK)
An open-source, code-first toolkit for building, evaluating, and deploying sophisticated AI agents with flexibility and control.
# Build Agents
# Get started
Agent Development Kit (ADK) is designed to empower developers to quickly build, manage, evaluate and deploy AI-powered agents. These quick start guides get you set up and running a simple agent in less than 20 minutes.
- **Python Quickstart**
______________________________________________________________________
Create your first Python ADK agent in minutes.
[Start with Python](https://adk.tools-online.site/get-started/python/index.md)
- **TypeScript Quickstart**
______________________________________________________________________
Create your first TypeScript ADK agent in minutes.
[Start with TypeScript](https://adk.tools-online.site/get-started/typescript/index.md)
- **Go Quickstart**
______________________________________________________________________
Create your first Go ADK agent in minutes.
[Start with Go](https://adk.tools-online.site/get-started/go/index.md)
- **Java Quickstart**
______________________________________________________________________
Create your first Java ADK agent in minutes.
[Start with Java](https://adk.tools-online.site/get-started/java/index.md)
- **Kotlin Quickstart**
______________________________________________________________________
Create your first Kotlin ADK agent in minutes.
[Start with Kotlin](https://adk.tools-online.site/get-started/kotlin/index.md)
- **Agents CLI Quickstart**
______________________________________________________________________
Create your first ADK agent with your coding agent.
[Start with Agents CLI](https://adk.tools-online.site/get-started/agents-cli/index.md)
- **Migrate to ADK**
______________________________________________________________________
Migrate existing agents and workflows to ADK with Agents CLI.
[Migrate to ADK](https://adk.tools-online.site/get-started/migrate/index.md)
To get started, see the [Technical Overview](https://adk.tools-online.site/get-started/about/index.md).
# Agent Development Kit (ADK)
**Build, Evaluate and Deploy agents, seamlessly!**
ADK is designed to empower developers to build, manage, evaluate and deploy AI-powered agents. It provides a robust and flexible environment for creating both conversational and non-conversational agents, capable of handling complex tasks and workflows.
## Core Concepts
ADK is built around a few key primitives and concepts that make it powerful and flexible. Here are the essentials:
- **Agent:** The fundamental worker unit designed for specific tasks. Agents can use language models (`LlmAgent`) for complex reasoning, or act as deterministic controllers of the execution, which are called "[workflow agents](https://adk.tools-online.site/agents/workflow-agents/index.md)" (`SequentialAgent`, `ParallelAgent`, `LoopAgent`).
- **Tool:** Gives agents abilities beyond conversation, letting them interact with external APIs, search information, run code, or call other services.
- **Callbacks:** Custom code snippets you provide to run at specific points in the agent's process, allowing for checks, logging, or behavior modifications.
- **Session Management (`Session` & `State`):** Handles the context of a single conversation (`Session`), including its history (`Events`) and the agent's working memory for that conversation (`State`).
- **Memory:** Enables agents to recall information about a user across *multiple* sessions, providing long-term context (distinct from short-term session `State`).
- **Artifact Management (`Artifact`):** Allows agents to save, load, and manage files or binary data (like images, PDFs) associated with a session or user.
- **Code Execution:** The ability for agents (usually via Tools) to generate and execute code to perform complex calculations or actions.
- **Planning:** An advanced capability where agents can break down complex goals into smaller steps and plan how to achieve them like a ReAct planner.
- **Models:** The underlying LLM that powers `LlmAgent`s, enabling their reasoning and language understanding abilities.
- **Event:** The basic unit of communication representing things that happen during a session (user message, agent reply, tool use), forming the conversation history.
- **Runner:** The engine that manages the execution flow, orchestrates agent interactions based on Events, and coordinates with backend services.
***Note:** Features like Multimodal Streaming, Evaluation, Deployment, Debugging, and Trace are also part of the broader ADK ecosystem, supporting real-time interaction and the development lifecycle.*
## Key Capabilities
ADK offers several key advantages for developers building agentic applications:
1. **Multi-Agent System Design:** Easily build applications composed of multiple, specialized agents arranged hierarchically. Agents can coordinate complex tasks, delegate sub-tasks using LLM-driven transfer or explicit `AgentTool` invocation, enabling modular and scalable solutions.
1. **Rich Tool Ecosystem:** Equip agents with diverse capabilities. ADK supports integrating custom functions (`FunctionTool`), using other agents as tools (`AgentTool`), leveraging built-in functionalities like code execution, and interacting with external data sources and APIs (e.g., Search, Databases). Support for long-running tools allows handling asynchronous operations effectively.
1. **Flexible Orchestration:** Define complex agent workflows using built-in workflow agents (`SequentialAgent`, `ParallelAgent`, `LoopAgent`) alongside LLM-driven dynamic routing. This allows for both predictable pipelines and adaptive agent behavior.
1. **Integrated Developer Tooling:** Develop and iterate locally with ease. ADK includes tools like a command-line interface (CLI) and a Developer UI for running agents, inspecting execution steps (events, state changes), debugging interactions, and visualizing agent definitions.
1. **Native Streaming Support:** Build real-time, interactive experiences with [live and voice agents](https://adk.tools-online.site/live/index.md) that provide native support for bidirectional streaming (text and audio). This integrates seamlessly with underlying capabilities like the [Gemini Live API for the Gemini Developer API](https://ai.google.dev/gemini-api/docs/live) (or for [Agent Platform](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/multimodal-live)), often enabled with simple configuration changes.
1. **Built-in Agent Evaluation:** Assess agent performance systematically. The framework includes tools to create multi-turn evaluation datasets and run evaluations locally (via CLI or the dev UI) to measure quality and guide improvements.
1. **Broad LLM Support:** While optimized for Google's Gemini models, the framework is designed for flexibility, allowing integration with various LLMs (potentially including open-source or fine-tuned models) through its `BaseLlm` interface.
1. **Artifact Management:** Enable agents to handle files and binary data. The framework provides mechanisms (`ArtifactService`, context methods) for agents to save, load, and manage versioned artifacts like images, documents, or generated reports during their execution.
1. **Extensibility and Interoperability:** ADK promotes an open ecosystem. While providing core tools, it allows developers to easily integrate and reuse third-party tools and data connectors.
1. **State and Memory Management:** Automatically handles short-term conversational memory (`State` within a `Session`) managed by the `SessionService`. Provides integration points for longer-term `Memory` services, allowing agents to recall user information across multiple sessions.
## Get Started
- Ready to build your first agent? [Get started](/get-started/)!
# Agents CLI Quickstart for ADK
This guide shows you how to get up and running with Agent Development Kit (ADK) using Agents CLI. You can use the Agents CLI tool set with coding agents like Antigravity, Claude Code, and Codex to build, evaluate, and deploy ADK agents. For more information, see the [Agents CLI](https://google.github.io/agents-cli/) documentation. Before you start, make sure you have the following installed:
- Python 3.11 or later: Agents CLI supports ADK agents in Python
- The [`uv`](https://docs.astral.sh/uv/getting-started/installation/) tool to manage environments and dependencies
- [Node.js](https://nodejs.org/en/download), for installing the skills
- Coding agent such as [Antigravity](https://antigravity.google/), [Claude Code](https://docs.anthropic.com/en/docs/claude-code), or [Codex](https://github.com/openai/codex)
If you want to deploy ADK agents to services like Google Cloud, make sure the following tools are also installed:
- [Google Cloud CLI](https://cloud.google.com/sdk/docs/install)
- [Terraform](https://developer.hashicorp.com/terraform/downloads)
## Installation
Install Agents CLI by running the following command. This step installs the `agents-cli` command, the ADK Python packages, and the ADK skills into any coding agents already on your machine:
```shell
uvx google-agents-cli setup
```
Alternative installation methods
**pipx:**
```shell
pipx install google-agents-cli && agents-cli setup
```
**pip:**
```shell
pip install google-agents-cli && agents-cli setup
```
**Skills only:**
```shell
npx skills add google/agents-cli
```
The installation command is the only one you have to run yourself. Once installed, you can use your coding agent to build and run an ADK agent.
## Authenticate
Agents CLI needs credentials for a generative AI API to run your agents. The simplest option is a Gemini API key from Google AI Studio. Create a key on the [API Keys](https://aistudio.google.com/app/apikey) page, then after you scaffold a project in the next step, open its `.env` file and set:
Update: .env
```text
GEMINI_API_KEY=YOUR_API_KEY
```
Comment out the three `GOOGLE_CLOUD_*` lines in the same file so the SDK uses your key instead of Vertex AI.
Using Google Cloud Agent Platform instead
If you already have a Google Cloud project, Agents CLI picks up your Application Default Credentials:
```shell
gcloud auth application-default login
```
Make sure the `GOOGLE_CLOUD_*` lines in the generated `.env` file are uncommented and set them to your project identifiers. For more information on connecting to Google Cloud services and projects with ADK, see the [Google Cloud setup guide](/get-started/google-cloud/) for ADK.
## Build your agent
Open your coding agent and confirm it can see the skills:
```shell
antigravity # launch from your IDE or terminal
# then verify the Agents CLI skills are listed in your environment
```
```shell
claude
/skills # expect google-agents-cli-* entries in the list
```
```shell
codex
/skills # expect google-agents-cli-* entries in the list
```
Using other coding agents
Agents CLI works with any coding agent that supports [skills](https://agentskills.io/what-are-skills). Most agents list them through a `/skills` command or a settings panel.
Then tell the coding agent what you want to build:
Coding agent prompt
```shell
Use agents-cli to build an agent that turns long text into short
bullet-point summaries
```
Your coding agent activates the `google-agents-cli-workflow` and `google-agents-cli-scaffold` skills, asks clarifying questions about the tools your agent calls, the inputs and outputs you expect, and the success criteria to evaluate against, and then scaffolds the project.
Next, your coding agent uses the `google-agents-cli-adk-code` skill to write your agent into `app/agent.py`. You end up with a working project with the agent code, tests, and an eval dataset in the following file structure:
```text
my-agent/
app/
agent.py # main agent code
fast_api_app.py # server, telemetry, and routes
app_utils/ # session and artifact services
tests/
eval/ # evaluation datasets and metrics
integration/ # end-to-end agent tests
unit/
pyproject.toml # project config and dependencies
agents-cli-manifest.yaml # Agents CLI configuration
Dockerfile # container image for deployment
GEMINI.md # project guidance for coding agents
.env # API keys or project IDs
```
Use this project structure when you plan to test, evaluate, and deploy an agent. If you want a create single-file agent for learning ADK, use the `adk create` command instead.
## Run your agent
Ask your coding agent to start the local playground, or run it yourself:
```console
agents-cli playground
```
This command starts the ADK web interface with hot reload, so your changes are reflected in the project as you edit. You can access the playground at (http://localhost:8080). Select the agent at the upper left corner and paste in a few paragraphs of text. The agent replies with a short bullet-point summary.
## Next: Evaluate and deploy your agent
Now that you have Agents CLI installed and your first agent running, you can evaluate and deploy it with your coding agent using instructions like the following:
- ***"Write evals for this agent and run them"*** to [evaluate your agent](https://google.github.io/agents-cli/guide/evaluation/) against the success criteria you set when you scoped it. Your coding agent grades the results, groups the failures by cause, and tunes the agent's instructions until it passes
- ***"Deploy this to Cloud Run"*** to [deploy your agent](/deploy/agent-runtime/agents-cli/) to Agent Runtime, Cloud Run, or GKE.
- ***"Set up observability infrastructure for my agent"*** to add prompt-response logging and content logs.
For the full walkthrough, including evaluation, deployment, and observability, see the Agents CLI [Tutorial: Build your first agent](https://google.github.io/agents-cli/guide/quickstart-tutorial/).
# Go Quickstart for ADK
This guide shows you how to get up and running with Agent Development Kit for Go. Before you start, make sure you have the following installed:
- Go 1.26.10 or higher
- ADK Go v2.0.0 or later
What's new in ADK Go 2.0
ADK Go 2.0 introduces graph-based workflow agents, parallel and loop execution primitives, and Human-in-the-Loop tool confirmation. See the [ADK 2.0 release page](/2.0/) for the full list of new features and migration guidance.
## Create an agent project
Create an agent project with the following files and directory structure:
```text
my_agent/
agent.go # main agent code
.env # API keys or project IDs
```
Create this project structure using the command line
```console
mkdir my_agent\
type nul > my_agent\agent.go
type nul > my_agent\env.bat
```
```bash
mkdir -p my_agent/ && \
touch my_agent/agent.go && \
touch my_agent/.env
```
### Define the agent code
Create the code for a basic agent that uses the built-in [Google Search tool](/integrations/google-search/). Add the following code to the `my_agent/agent.go` file in your project directory:
my_agent/agent.go
```go
package main
import (
"context"
"log"
"os"
"google.golang.org/adk/v2/agent"
"google.golang.org/adk/v2/agent/llmagent"
"google.golang.org/adk/v2/cmd/launcher"
"google.golang.org/adk/v2/cmd/launcher/full"
"google.golang.org/adk/v2/model/gemini"
"google.golang.org/adk/v2/tool"
"google.golang.org/adk/v2/tool/geminitool"
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
model, err := gemini.NewModel(ctx, "gemini-flash-latest", &genai.ClientConfig{
APIKey: os.Getenv("GOOGLE_API_KEY"),
})
if err != nil {
log.Fatalf("Failed to create model: %v", err)
}
timeAgent, err := llmagent.New(llmagent.Config{
Name: "hello_time_agent",
Model: model,
Description: "Tells the current time in a specified city.",
Instruction: "You are a helpful assistant that tells the current time in a city.",
Tools: []tool.Tool{
geminitool.GoogleSearch{},
},
})
if err != nil {
log.Fatalf("Failed to create agent: %v", err)
}
config := &launcher.Config{
AgentLoader: agent.NewSingleLoader(timeAgent),
}
l := full.NewLauncher()
if err = l.Execute(ctx, config, os.Args[1:]); err != nil {
log.Fatalf("Run failed: %v\n\n%s", err, l.CommandLineSyntax())
}
}
```
### Configure project and dependencies
Initialize your module, add ADK Go 2.0 as a pinned dependency, then let `go mod tidy` resolve the remaining packages based on the `import` statements in your agent code file:
```console
go mod init my-agent/main
go get google.golang.org/adk/v2
go mod tidy
```
### Set your API key
This project uses the Gemini API, which requires an API key. If you don't already have Gemini API key, create a key in Google AI Studio on the [API Keys](https://aistudio.google.com/app/apikey) page.
In a terminal window, write your API key into the `.env` or `env.bat` file of your project to set environment variables:
Update: my_agent/.env
```bash
echo 'export GOOGLE_API_KEY="YOUR_API_KEY"' > .env
```
Update: my_agent/env.bat
```console
echo 'set GOOGLE_API_KEY="YOUR_API_KEY"' > env.bat
```
Update: my_agent/env.bat
```console
echo set GOOGLE_API_KEY="YOUR_API_KEY" > env.bat
```
Using other AI models with ADK
ADK supports the use of many generative AI models. For more information on configuring other models in ADK agents, see [Models & Authentication](/agents/models).
## Run your agent
You can run your ADK agent using the interactive command-line interface you defined or the ADK web user interface provided by the ADK Go command line tool. Both these options allow you to test and interact with your agent.
### Run with command-line interface
Run your agent using the following Go command:
Run from: my_agent/ directory
```console
# Remember to load keys and settings: source .env OR env.bat
go run agent.go
```
### Run with web interface
Run your agent with the ADK web interface using the following Go command:
Run from: my_agent/ directory
```console
# Remember to load keys and settings: source .env OR env.bat
go run agent.go web api webui
```
This command starts a web server with a chat interface for your agent. You can access the web interface at `http://localhost:8080`. Select your agent at the upper left corner and type a request.
Caution: ADK Web for development only
ADK Web is ***not meant for use in production deployments***. You should use ADK Web for development and debugging purposes only.
## Next: Build your agent
Now that you have ADK installed and your first agent running, try building your own agent with our build guides:
- [Build your agent](/tutorials/)
- [Build graph-based workflows](/graphs/)
- [ADK Go workflow agents](/agents/workflow-agents/)
# Connect to Google Cloud and Agent Platform
This guide explains how to connect and authenticate your ADK agents with Google Cloud Platform (GCP) services, models running on Google Cloud Agent Platform, and Agent Platform services.
## Setup Google Cloud Agent Platform
Before attempting to connect an agent with Google Cloud or Agent Platform services, make sure you have completed the following prerequisites:
- Google Cloud Project with the **Agent Platform API** (`aiplatform.googleapis.com`) enabled.
- Install the [gcloud CLI](https://cloud.google.com/sdk/docs/install) tool.
## Google Cloud authentication options
You have a few options for authentication when connecting your ADK agent to Google Cloud, as described in the table below.
| Method | Best Used For | Authentication Mechanism | Environment |
| -------------------------------------------------- | ------------------------------- | -------------------------------------------------- | ---------------------------------------------------------------- |
| [**User Credentials**](#user-credentials) | Local development and testing | Application Default Credentials via `gcloud` | Local workstation |
| [**Service Account**](#service-account) | Production deployment and CI/CD | Google IAM Service Account Key / Workload Identity | Google Cloud (Agent Runtime, Cloud Run, GKE) or external servers |
| [**Express Mode**](#express-mode) | Rapid prototyping and testing | API Key | Local or cloud environments |
| [**Agent Identity**](/integrations/agent-identity) | Production deployment and CI/CD | Google IAM Service Account Key / Workload Identity | Google Cloud (Agent Runtime, Cloud Run, GKE) |
Warning: Protect your credentials
User credentials, service account credentials, and API keys are highly sensitive. Never commit credential files or keys directly to your codebase. Whenever possible use secure secret managers such as [Google Cloud Agent Identity](/integrations/agent-identity/), [Google Cloud Secret Manager](https://cloud.google.com/security/products/secret-manager) or other similar products.
### User credentials for local development
Connect to Google Cloud with user credentials authentication method working with local development environments.
1. Authenticate your local workstation using Application Default Credentials (ADC) *before* running your ADK agent application:
```bash
gcloud auth application-default login
```
1. Set your environment variables to enable Agent Platform and specify your project details:
```console
# Add to ADK code project but NOT source control
GOOGLE_GENAI_USE_ENTERPRISE=TRUE
GOOGLE_CLOUD_PROJECT=your-project-id
GOOGLE_CLOUD_LOCATION=cloud-location # example: us-central1
```
```bash
export GOOGLE_GENAI_USE_ENTERPRISE=TRUE
export GOOGLE_CLOUD_PROJECT="your-project-id"
export GOOGLE_CLOUD_LOCATION="cloud-location" # example: us-central1
```
`GOOGLE_GENAI_USE_ENTERPRISE` was previously `GOOGLE_GENAI_USE_VERTEXAI`
These variable names are equivalent and do the same thing. If you set `GOOGLE_GENAI_USE_ENTERPRISE` and your agent does not connect to Agent Platform, you're on an older ADK version. Use `GOOGLE_GENAI_USE_VERTEXAI` instead, or update to a newer version of ADK.
### Service account for production
When deploying to secure hosted environments, use a service account for connection authentication:
1. Create a [Service Account](https://docs.cloud.google.com/iam/docs/service-account-overview) and grant it the `Agent Platform User` role.
1. Provide the credentials to your agent application according to your deployment strategy:
- **Deployed on Google Cloud (Agent Runtime, Cloud Run, GKE):** The environment automatically provides the credentials. No key file configuration is necessary.
- **Running externally:** Generate a [service account key file](https://cloud.google.com/iam/docs/keys-create-delete#console) (`.json`) and configure the `GOOGLE_APPLICATION_CREDENTIALS` environment variable:
```bash
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account-key.json"
```
Workload Identity option
Instead of the key file, you can also authenticate the service account using [Workload Identity](https://docs.cloud.google.com/kubernetes-engine/docs/how-to/workload-identity).
### Agent Platform express mode for testing
Express Mode offers a simplified, API-key-based setup for prototyping without full gcloud authentication.
1. Sign up for [express mode](https://console.cloud.google.com/expressmode) to get an API key.
1. Set the following environment variables:
```console
# Add to ADK code project but NOT source control
GOOGLE_GENAI_USE_ENTERPRISE=TRUE
GOOGLE_GENAI_API_KEY=PASTE_YOUR_ACTUAL_EXPRESS_MODE_API_KEY_HERE
```
```bash
export GOOGLE_GENAI_USE_ENTERPRISE=TRUE
export GOOGLE_GENAI_API_KEY="PASTE_YOUR_EXPRESS_MODE_API_KEY_HERE"
```
## Google Cloud hosted models
Google Cloud Agent Platform hosts a wide array of AI models you can connect to your ADK agents, including Gemini models, third-party AI models, open weight models, and models custom-tuned for your organization. Once you have connected your ADK agent to Google Cloud and Agent Platform, you have access to AI models to fit your application requirements. Check out these resources to explore and find the model that's right for your project:
- Get more information about using [Gemini models](/agents/models/google-gemini/) with ADK agents.
- Explore third party and custom model options in [Agent Platform hosted models](/agents/models/agent-platform/) for use with ADK agents.
- Find available models and model IDs from Google Cloud in the [Agent Platform](https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/google-models) documentation.
## Additional Google Cloud services connections
Many Google Cloud services provide ADK integrations with authentication helpers for accessing GCP APIs or resources with an ADK agent. For more information, see the following pages:
- [Google Cloud Application Integration](/integrations/application-integration/)
- [BigQuery Toolset](/integrations/bigquery/)
- [BigQuery Agent Analytics](/integrations/bigquery-agent-analytics/)
- [Data Agent](/integrations/data-agent/)
- [Model Armor](/integrations/model-armor/)
# Advanced setup
This page provides detailed installation and configuration instructions for ADK across supported languages. For a guided introduction, start with the [quickstart for your language](/get-started/).
**Create & activate virtual environment**
We recommend creating a virtual Python environment using [venv](https://docs.python.org/3/library/venv.html):
```shell
python3 -m venv .venv
```
Now, you can activate the virtual environment using the appropriate command for your operating system and environment:
```text
# Mac / Linux
source .venv/bin/activate
# Windows CMD:
.venv\Scripts\activate.bat
# Windows PowerShell:
.venv\Scripts\Activate.ps1
```
**Install ADK**
```bash
pip install google-adk
```
(Optional) Verify your installation:
```bash
pip show google-adk
```
**Install ADK and ADK DevTools**
```bash
npm install @google/adk @google/adk-devtools
```
**Prerequisites:** Go 1.26.10 or higher is required for ADK Go v2.7.0 and higher.
**Create a new Go module**
If you are starting a new project, you can create a new Go module:
```shell
go mod init example.com/my-agent
```
**Install ADK Go v2.0.0**
To add ADK Go v2.0.0 to your project, run the following command:
```shell
go get google.golang.org/adk/v2
```
This will add ADK Go v2.0.0 as a dependency to your `go.mod` file.
(Optional) Verify your installation by checking your `go.mod` file for the `google.golang.org/adk/v2` entry.
Still using ADK Go v1.x?
If you are not yet ready to upgrade to v2.0.0, you can continue using the v1.x release line:
```shell
go get google.golang.org/adk@v1
```
See the [ADK 2.0 release page](/2.0/) for upgrade guidance, including breaking changes and migration steps for ADK Go 1.x projects.
You can either use maven or gradle to add the `google-adk` and `google-adk-dev` package.
`google-adk` is the core Java ADK library. Java ADK also comes with a pluggable example SpringBoot server to run your agents seamlessly. This optional package is present as part of `google-adk-dev`.
If you are using maven, add the following to your `pom.xml`:
pom.xml
```xml
4.0.0com.example.agentadk-agents1.0-SNAPSHOT1717UTF-8com.google.adkgoogle-adk1.14.0com.google.adkgoogle-adk-dev1.14.0
```
Here's a [complete pom.xml](https://github.com/google/adk-docs/tree/main/examples/java/cloud-run/pom.xml) file for reference.
If you are using gradle, add the dependency to your build.gradle:
build.gradle
```text
dependencies {
implementation 'com.google.adk:google-adk:1.14.0'
implementation 'com.google.adk:google-adk-dev:1.14.0'
}
```
You should also configure Gradle to pass `-parameters` to `javac`. (Alternatively, use `@Schema(name = "...")`).
**Use ADK Kotlin on the JVM**
For Kotlin on the JVM, add the ADK core library and the KSP annotation processor to your `build.gradle.kts`:
build.gradle.kts
```kotlin
plugins {
kotlin("jvm") version "2.1.20"
id("com.google.devtools.ksp") version "2.1.20-2.0.1"
}
dependencies {
implementation("com.google.adk:google-adk-kotlin-core:1.6.0")
ksp("com.google.adk:google-adk-kotlin-processor:1.6.0")
}
```
The KSP processor generates code for the `@Tool` annotation used to register function tools. See the [Kotlin Quickstart](/get-started/kotlin/) for a complete project setup.
# Java Quickstart for ADK
This guide shows you how to get up and running with Agent Development Kit for Java. Before you start, make sure you have the following installed:
- Java 17 or later
- Maven 3.9 or later
## Create an agent project
Create an agent project with the following files and directory structure:
```text
my_agent/
src/main/java/com/example/agent/
HelloTimeAgent.java # main agent code
AgentCliRunner.java # command-line interface
pom.xml # project configuration
.env # API keys or project IDs
```
Create this project structure using the command line
```console
mkdir my_agent\src\main\java\com\example\agent
type nul > my_agent\src\main\java\com\example\agent\HelloTimeAgent.java
type nul > my_agent\src\main\java\com\example\agent\AgentCliRunner.java
type nul > my_agent\pom.xml
type nul > my_agent\.env
```
```bash
mkdir -p my_agent/src/main/java/com/example/agent && \
touch my_agent/src/main/java/com/example/agent/HelloTimeAgent.java && \
touch my_agent/src/main/java/com/example/agent/AgentCliRunner.java && \
touch my_agent/pom.xml my_agent/.env
```
### Define the agent code
Create the code for a basic agent, including a simple implementation of an ADK [Function Tool](/tools-custom/function-tools/), called `getCurrentTime()`. Add the following code to the `HelloTimeAgent.java` file in your project directory:
my_agent/src/main/java/com/example/agent/HelloTimeAgent.java
```java
package com.example.agent;
import com.google.adk.agents.BaseAgent;
import com.google.adk.agents.LlmAgent;
import com.google.adk.tools.Annotations.Schema;
import com.google.adk.tools.FunctionTool;
import java.util.Map;
public class HelloTimeAgent {
public static BaseAgent ROOT_AGENT = initAgent();
private static BaseAgent initAgent() {
return LlmAgent.builder()
.name("hello-time-agent")
.description("Tells the current time in a specified city")
.instruction("""
You are a helpful assistant that tells the current time in a city.
Use the 'getCurrentTime' tool for this purpose.
""")
.model("gemini-flash-latest")
.tools(FunctionTool.create(HelloTimeAgent.class, "getCurrentTime"))
.build();
}
/** Mock tool implementation */
@Schema(description = "Get the current time for a given city")
public static Map getCurrentTime(
@Schema(name = "city", description = "Name of the city to get the time for") String city) {
return Map.of(
"city", city,
"forecast", "The time is 10:30am."
);
}
}
```
Caution: Gemini 3 compatibility
ADK Java v0.3.0 and lower is not compatible with [Gemini 3 Pro Preview](https://ai.google.dev/gemini-api/docs/models#gemini-3-pro) due to thought signature changes for function calling. Use Gemini 2.5 or lower models instead.
### Configure project and dependencies
An ADK agent project requires this dependency in your `pom.xml` project file:
my_agent/pom.xml (partial)
```xml
com.google.adkgoogle-adk1.14.0
```
Update the `pom.xml` project file to include this dependency and additional settings with the following configuration code:
Complete `pom.xml` configuration for project
The following code shows a complete `pom.xml` configuration for this project:
my_agent/pom.xml
```xml
4.0.0com.example.agentadk-agents1.0-SNAPSHOT1717UTF-8com.google.adkgoogle-adk1.14.0com.google.adkgoogle-adk-dev1.14.0
```
### Set your API key
This project uses the Gemini API, which requires an API key. If you don't already have Gemini API key, create a key in Google AI Studio on the [API Keys](https://aistudio.google.com/app/apikey) page.
In a terminal window, write your API key into your `.env` file of your project to set environment variables:
Update: my_agent/.env
```bash
echo 'export GOOGLE_API_KEY="YOUR_API_KEY"' > .env
```
Update: my_agent/env.bat
```console
echo 'set GOOGLE_API_KEY="YOUR_API_KEY"' > env.bat
```
Update: my_agent/env.bat
```console
echo set GOOGLE_API_KEY="YOUR_API_KEY" > env.bat
```
Using other AI models with ADK
ADK supports the use of many generative AI models. For more information on configuring other models in ADK agents, see [Models & Authentication](/agents/models).
### Create an agent command-line interface
Create a `AgentCliRunner.java` class to allow you to run and interact with `HelloTimeAgent` from the command line. This code shows how to create a `RunConfig` object to run the agent and a `Session` object to interact with the running agent.
my_agent/src/main/java/com/example/agent/AgentCliRunner.java
```java
package com.example.agent;
import com.google.adk.agents.RunConfig;
import com.google.adk.events.Event;
import com.google.adk.runner.InMemoryRunner;
import com.google.adk.sessions.Session;
import com.google.genai.types.Content;
import com.google.genai.types.Part;
import io.reactivex.rxjava3.core.Flowable;
import java.util.Scanner;
import static java.nio.charset.StandardCharsets.UTF_8;
public class AgentCliRunner {
public static void main(String[] args) {
RunConfig runConfig = RunConfig.builder().build();
InMemoryRunner runner = new InMemoryRunner(HelloTimeAgent.ROOT_AGENT);
Session session = runner
.sessionService()
.createSession(runner.appName(), "user1234")
.blockingGet();
try (Scanner scanner = new Scanner(System.in, UTF_8)) {
while (true) {
System.out.print("\nYou > ");
String userInput = scanner.nextLine();
if ("quit".equalsIgnoreCase(userInput)) {
break;
}
Content userMsg = Content.fromParts(Part.fromText(userInput));
Flowable events = runner.runAsync(session.userId(), session.id(), userMsg, runConfig);
System.out.print("\nAgent > ");
events.blockingForEach(event -> {
if (event.finalResponse()) {
System.out.println(event.stringifyContent());
}
});
}
}
}
}
```
## Run your agent
You can run your ADK agent using the interactive command-line interface `AgentCliRunner` class you defined or the ADK web user interface provided by the ADK using the `AdkWebServer` class. Both these options allow you to test and interact with your agent.
### Run with command-line interface
Run your agent with the command-line interface `AgentCliRunner` class using the following Maven command:
```console
# Remember to load keys and settings: source .env OR env.bat
mvn compile exec:java -Dexec.mainClass="com.example.agent.AgentCliRunner"
```
### Run with web interface
Run your agent with the ADK web interface using the following Maven command:
```console
# Remember to load keys and settings: source .env OR env.bat
mvn compile exec:java \
-Dexec.mainClass="com.google.adk.web.AdkWebServer" \
-Dexec.args="--adk.agents.source-dir=target --server.port=8000"
```
This command starts a web server with a chat interface for your agent. You can access the web interface at `http://localhost:8000`. Select your agent at the upper left corner and type a request.
Caution: ADK Web for development only
ADK Web is ***not meant for use in production deployments***. You should use ADK Web for development and debugging purposes only.
## Next: Build your agent
Now that you have ADK installed and your first agent running, try building your own agent with our build guides:
- [Build your agent](/tutorials/)
# Kotlin Quickstart for ADK
This guide shows you how to get up and running with Agent Development Kit for Kotlin. Before you start, make sure you have the following installed:
- Java 17 or later
- Gradle 8.0 or later
Building for Android?
This quickstart covers Kotlin on the JVM. If you're building an Android app, complete this quickstart first to learn the agent API, then see [Build ADK agents for Android](https://developer.android.com/ai/adk) for Android-specific project setup and on-device models.
## Create an agent project
Create an agent project with the following files and directory structure:
```text
my_agent/
src/main/kotlin/com/example/agent/
HelloTimeAgent.kt # agent definition + tool
Main.kt # entry point
build.gradle.kts # project configuration
.env # API keys or project IDs
```
Create this project structure using the command line
```console
mkdir my_agent\src\main\kotlin\com\example\agent
type nul > my_agent\src\main\kotlin\com\example\agent\HelloTimeAgent.kt
type nul > my_agent\src\main\kotlin\com\example\agent\Main.kt
type nul > my_agent\build.gradle.kts
type nul > my_agent\.env
```
```bash
mkdir -p my_agent/src/main/kotlin/com/example/agent && \
touch my_agent/src/main/kotlin/com/example/agent/HelloTimeAgent.kt && \
touch my_agent/src/main/kotlin/com/example/agent/Main.kt && \
touch my_agent/build.gradle.kts my_agent/.env
```
### Define the agent code
Create the code for a basic agent, including a simple implementation of an ADK [Function Tool](/tools-custom/function-tools/), called `getCurrentTime()`. Add the following code to the `HelloTimeAgent.kt` file in your project directory:
my_agent/src/main/kotlin/com/example/agent/HelloTimeAgent.kt
```kotlin
package com.example.agent
import com.google.adk.kt.agents.Instruction
import com.google.adk.kt.agents.LlmAgent
import com.google.adk.kt.annotations.Param
import com.google.adk.kt.annotations.Tool
import com.google.adk.kt.models.Gemini
class TimeService {
/** Mock tool implementation */
@Tool
fun getCurrentTime(
@Param("Name of the city to get the time for") city: String
): Map {
return mapOf("city" to city, "time" to "The time is 10:30am.")
}
}
object HelloTimeAgent {
@JvmField
val rootAgent = LlmAgent(
name = "hello_time_agent",
description = "Tells the current time in a specified city.",
model = Gemini(
name = "gemini-flash-latest",
apiKey = System.getenv("GOOGLE_API_KEY")
?: error("GOOGLE_API_KEY environment variable not set."),
),
instruction = Instruction(
"You are a helpful assistant that tells the current time in a city. "
+ "Use the 'getCurrentTime' tool for this purpose."
),
tools = TimeService().generatedTools(),
)
}
```
About `@Tool` and KSP
The `@Tool` annotation marks a function as a tool that the agent can call. At compile time, a KSP (Kotlin Symbol Processing) annotation processor generates the `.generatedTools()` extension function used above. This is a zero-reflection approach to function tool registration. The required KSP plugin and processor dependency are included in the `build.gradle.kts` configuration below.
### Configure project and dependencies
An ADK Kotlin agent project requires the following dependencies in your `build.gradle.kts` project file:
my_agent/build.gradle.kts (partial)
```kotlin
dependencies {
implementation("com.google.adk:google-adk-kotlin-core:1.6.0")
ksp("com.google.adk:google-adk-kotlin-processor:1.6.0")
}
```
Complete `build.gradle.kts` configuration for project
The following code shows a complete `build.gradle.kts` configuration for this project:
my_agent/build.gradle.kts
```kotlin
plugins {
kotlin("jvm") version "2.1.20"
id("com.google.devtools.ksp") version "2.1.20-2.0.1"
application
}
repositories {
mavenCentral()
}
dependencies {
implementation("com.google.adk:google-adk-kotlin-core:1.6.0")
implementation("com.google.adk:google-adk-kotlin-webserver:1.6.0")
ksp("com.google.adk:google-adk-kotlin-processor:1.6.0")
}
kotlin {
jvmToolchain(17)
}
application {
mainClass.set(
project.findProperty("mainClass") as? String
?: "com.example.agent.MainKt"
)
}
tasks.named("run") {
standardInput = System.`in`
}
```
### Set your API key
This project uses the Gemini API, which requires an API key. If you don't already have Gemini API key, create a key in Google AI Studio on the [API Keys](https://aistudio.google.com/app/apikey) page.
In a terminal window, write your API key into your `.env` file of your project to set environment variables:
Update: my_agent/.env
```bash
echo 'export GOOGLE_API_KEY="YOUR_API_KEY"' > .env
```
Update: my_agent/env.bat
```console
echo 'set GOOGLE_API_KEY="YOUR_API_KEY"' > env.bat
```
Update: my_agent/env.bat
```console
echo set GOOGLE_API_KEY="YOUR_API_KEY" > env.bat
```
Using other AI models with ADK
ADK supports the use of many generative AI models. For more information on configuring other models in ADK agents, see [Models & Authentication](/agents/models).
### Create an entry point
Create a `Main.kt` file to run and interact with `HelloTimeAgent` from the command line. `ReplRunner` provides a built-in interactive REPL that handles user input, agent responses, and tool confirmation prompts.
my_agent/src/main/kotlin/com/example/agent/Main.kt
```kotlin
package com.example.agent
import com.google.adk.kt.runners.ReplRunner
fun main() {
ReplRunner(HelloTimeAgent.rootAgent).start()
}
```
## Run your agent
You can run your ADK agent using the interactive command-line REPL or the ADK web user interface provided by `AdkDevServer`. Both options allow you to test and interact with your agent.
### Run with command-line interface
Run your agent with the command-line interface using the Gradle `run` task:
```console
# Remember to load keys and settings: source .env OR env.bat
gradle run
```
The agent starts an interactive session. Type a message and press Enter:
```text
Agent hello_time_agent is ready. Type 'exit' to quit.
You > What time is it in New York?
hello_time_agent > The current time in New York is 10:30am.
You > exit
Exiting agent.
```
### Run with web interface
To run your agent with the ADK web interface, add the webserver dependency to your `build.gradle.kts`:
my_agent/build.gradle.kts (add to dependencies)
```kotlin
dependencies {
implementation("com.google.adk:google-adk-kotlin-core:1.6.0")
implementation("com.google.adk:google-adk-kotlin-webserver:1.6.0")
ksp("com.google.adk:google-adk-kotlin-processor:1.6.0")
}
```
Then create a `WebMain.kt` file alongside your `Main.kt`:
my_agent/src/main/kotlin/com/example/agent/WebMain.kt
```kotlin
package com.example.agent
import com.google.adk.kt.webserver.AdkServerConfig
import com.google.adk.kt.webserver.dev.AdkDevServer
fun main() {
// inMemory() supplies the agent loader and the session and artifact
// services, keeping their state in the process.
val server = AdkDevServer(AdkServerConfig.inMemory(HelloTimeAgent.rootAgent))
println("Starting ADK dev server on http://localhost:8080")
server.start(wait = true)
}
```
Run the web server using the `-PmainClass` property to select the web entry point:
```console
# Remember to load keys and settings: source .env OR env.bat
gradle run -PmainClass=com.example.agent.WebMainKt
```
This command starts a web server with a chat interface for your agent. You can access the web interface at `http://localhost:8080`. Select your agent at the upper left corner and type a request.
Caution: ADK Web for development only
ADK Web is ***not meant for use in production deployments***. You should use ADK Web for development and debugging purposes only. For more information, see ADK [Web Interface](/runtime/web-interface/).
## Next: Build your agent
Now that you have ADK installed and your first agent running, try building your own agent with our build guides:
- [Build your agent](/tutorials/)
- [Build ADK agents for Android](https://developer.android.com/ai/adk)
# Migrate existing agents to ADK
This guide shows you how to migrate an existing agent codebase to Agent Development Kit (ADK) using Agents CLI and your coding agent. Migrating to ADK lets you standardize your agent architecture across multiple languages, use built-in evaluation tools, and deploy directly to Google Cloud.
## Migrate with Agents CLI
Instead of manually rewriting state objects, node graphs, and execution loops line by line, you can use Agents CLI to plan and execute the migration with your coding agent.
Agents CLI installs ADK development skills into coding agents such as Antigravity, Claude Code, Cursor, and Codex. When you open your coding agent in an existing project, it can:
- Analyze your current agent structure, tools, state, and routing rules.
- Map existing components to native ADK classes and graph workflows.
- Propose architecture options with trade-offs.
- Convert tools, agent definitions, and session handling incrementally.
- Generate evaluation datasets to verify behavior before and after migration.
For more information on using Agents CLI, see the [Agents CLI](https://google.github.io/agents-cli/) documentation.
## Prerequisites
Before starting your migration, make sure you have the following installed:
- Python 3.11 or later
- The [`uv`](https://docs.astral.sh/uv/getting-started/installation/) package manager
- A supported coding agent
Install Agents CLI and its ADK skills into your coding agent:
```bash
uvx google-agents-cli setup
```
To verify the installation:
```bash
agents-cli info
```
## Migration workflow
Follow this process to migrate an existing agent to ADK:
1. [Open your coding agent in the existing project](#open-your-coding-agent-in-the-existing-project)
1. [Brainstorm a migration plan](#brainstorm-a-migration-plan)
1. [Map agent patterns to ADK](#map-agent-patterns-to-adk)
1. [Convert code with evaluation](#convert-code-with-evaluation)
1. [Verify and evaluate](#verify-and-evaluate)
### Open your coding agent in the existing project
Open your terminal or IDE in the root directory of your existing agent project, and start your coding agent. Confirm that the agent detects the ADK skills installed by Agents CLI.
### Brainstorm a migration plan
Ask your coding agent to inspect your current codebase and brainstorm the target ADK architecture. Since the agent has ADK Skills loaded through Agents CLI, it understands ADK state management, graph workflows, and orchestration patterns. Use a prompt in your coding agent similar to the following:
Code agent prompt
```text
I want to migrate this existing agent codebase to Google Agent Development Kit (ADK).
Please inspect our current files, state schema, tools, and control flow.
Propose 2-3 target ADK architecture options with trade-offs, and recommend the cleanest approach.
Include an evaluation plan to verify behavior using agents-cli eval.
```
Your coding agent analyzes the following items:
- **Execution flow:** Single tool-calling loop, deterministic graph workflow, dynamic router, or multi-agent team.
- **Tools:** Functions, parameter signatures, docstrings, and external API calls.
- **Memory and retrieval:** Knowledge stores, vector search integrations, or conversational memory.
- **State:** Variables tracked across turns, scratchpad keys, and session storage.
- **Target classes:** Which ADK classes, such as `Agent` or `Workflow`, fit best.
- **Evaluation strategy:** How to convert existing test cases into evaluation datasets to benchmark the migrated agent.
Once you review the proposed approaches, approve the architecture that matches your requirements.
### Map agent patterns to ADK
ADK replaces custom dispatch loops and state handlers with declarative classes and graph workflows. Use the following mapping as a guide during migration:
| Existing pattern | ADK equivalent | Description |
| ----------------------------------- | ------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Custom tool schemas or wrappers | Native Python functions or `FunctionTool` | Plain Python functions with type hints and docstrings. ADK automatically derives tool declarations. |
| Custom agent loops or runners | `Agent` | Declarative agent definition specifying model, instructions, tools, and sub-agents. |
| Memory and retrieval | `BaseMemoryService` implementations and retrieval tools | Built-in memory services (`InMemoryMemoryService`, `VertexAiMemoryBankService`, `VertexAiRagMemoryService`) plus retrieval tools for session and document grounding. |
| State dictionaries or scratchpads | `session.state` via `ToolContext` | Shared, mutable session state accessible inside tools, callbacks, and agent instructions. |
| Multi-agent workflows and pipelines | `google.adk.workflow.Workflow` | Explicit graph nodes with conditional routes, loops, and parallel branching. |
| Multi-agent handoffs | `Agent(sub_agents=[...])` | Hierarchical delegation where a coordinator agent delegates to specialized sub-agents. |
| Remote agent communication | A2A Protocol | Inter-agent communication over HTTP using the Agent-to-Agent standard. |
### Convert code with evaluation
A reliable migration is test-driven. Your coding agent can set up evaluation datasets and test suites alongside the new ADK code to verify that the migrated agent produces the same outcomes as your original implementation.
1. **Set up evaluation test cases:** Have your coding agent convert existing test cases or recorded conversations into evaluation cases under `eval/`.
1. **Port tools and agent logic:** Replace custom dispatch loops and tool wrappers with typed Python functions and an ADK `Agent` or `Workflow`.
```python
# agent.py
from google.adk.agents import Agent
from google.adk.tools import ToolContext
def lookup_customer(customer_id: str) -> str:
"""Retrieve account tier and status for a customer."""
return "Tier: Premium, Status: Active"
def calculate_discount(amount: float, rate: float = 0.1) -> float:
"""Calculate discounted total for a transaction."""
return amount * (1.0 - rate)
root_agent = Agent(
name="customer_support_agent",
model="gemini-flash-latest",
instruction="Assist customers with account inquiries and discounts using your tools.",
tools=[lookup_customer, calculate_discount],
)
```
### Verify and evaluate
Run the evaluation suite to compare the migrated agent against your baseline test cases:
```bash
agents-cli eval run
```
You can also test queries directly or interactively:
```bash
# Test a single prompt
agents-cli run "Look up customer cust_101 and apply a 10% discount on $100."
# Start the interactive web UI
agents-cli playground
```
## Next steps
- Read the [Multi-tool agent tutorial](/tutorials/multi-tool-agent/) to learn more about ADK tool patterns.
- Explore [Graph workflows](/graphs/) for multi-agent routing and state coordination.
- Deploy your agent using the [Deployment guide](/deploy/).
# Python Quickstart for ADK
This guide shows you how to get up and running with Agent Development Kit (ADK) for Python. Before you start, make sure you have the following installed:
- Python 3.10 or later
- `pip` for installing packages
## Installation
Install ADK by running the following command:
```shell
pip install google-adk
```
Recommended: create and activate a Python virtual environment
Create a Python virtual environment:
```shell
python3 -m venv .venv
```
Activate the Python virtual environment:
```console
.venv\Scripts\activate.bat
```
```console
.venv\Scripts\Activate.ps1
```
```bash
source .venv/bin/activate
```
## Create an agent project
Run the `adk create` command to start a new agent project.
```shell
adk create my_agent
```
### Explore the agent project
The created agent project has the following structure, with the `agent.py` file containing the main control code for the agent.
```text
my_agent/
agent.py # main agent code
.env # API keys or project IDs
__init__.py
```
## Update your agent project
The `agent.py` file contains a `root_agent` definition which is the only required element of an ADK agent. You can also define tools for the agent to use. Update the generated `agent.py` code to include a `get_current_time` tool for use by the agent, as shown in the following code:
```python
from google.adk.agents.llm_agent import Agent
# Mock tool implementation
def get_current_time(city: str) -> dict:
"""Returns the current time in a specified city."""
return {"status": "success", "city": city, "time": "10:30 AM"}
root_agent = Agent(
model='gemini-flash-latest',
name='root_agent',
description="Tells the current time in a specified city.",
instruction="You are a helpful assistant that tells the current time in cities. Use the 'get_current_time' tool for this purpose.",
tools=[get_current_time],
)
```
### Set your API key
This project uses the Gemini API, which requires an API key. If you don't already have Gemini API key, create a key in Google AI Studio on the [API Keys](https://aistudio.google.com/app/apikey) page.
In a terminal window, write your API key into an `.env` file as an environment variable:
Update: my_agent/.env
```bash
echo 'GOOGLE_API_KEY="YOUR_API_KEY"' > .env
```
Update: my_agent/.env
```console
echo 'GOOGLE_API_KEY="YOUR_API_KEY"' > .env
```
Update: my_agent/.env
```console
echo GOOGLE_API_KEY="YOUR_API_KEY" > .env
```
Using other AI models with ADK
ADK supports the use of many generative AI models. For more information on configuring other models in ADK agents, see [Models & Authentication](/agents/models).
## Run your agent
You can run your ADK agent with an interactive command-line interface using the `adk run` command or the ADK web user interface provided by the ADK using the `adk web` command. Both these options allow you to test and interact with your agent.
### Run with command-line interface
Run your agent using the `adk run` command-line tool.
```console
adk run my_agent
```
### Run with web interface
The ADK framework provides web interface you can use to test and interact with your agent. You can start the web interface using the following command:
```console
adk web --port 8000
```
Note
Run this command from the **parent directory** that contains your `my_agent/` folder. For example, if your agent is inside `agents/my_agent/`, run `adk web` from the `agents/` directory.
This command starts a web server with a chat interface for your agent. You can access the web interface at `http://localhost:8000`. Select the agent at the upper left corner and type a request.
Caution: ADK Web for development only
ADK Web is ***not meant for use in production deployments***. You should use ADK Web for development and debugging purposes only.
## Next: Build your agent
Now that you have ADK installed and your first agent running, try building your own agent with our build guides:
- [Build your agent](/tutorials/)
# TypeScript Quickstart for ADK
This guide shows you how to get up and running with Agent Development Kit for TypeScript. Before you start, make sure you have the following installed:
- Node.js 24.13.0 or later
- Node Package Manager (npm) 11.8.0 or later
## Create an agent project
Create an empty `my-agent` directory for your project:
```text
my-agent/
```
Create this project structure using the command line
```bash
mkdir -p my-agent/
```
```console
mkdir my-agent
```
### Configure project and dependencies
Use the `npm` tool to install and configure dependencies for your project, including the package file, ADK TypeScript main library, and developer tools. Run the following commands from your `my-agent/` directory to create the `package.json` file and install the project dependencies:
```console
cd my-agent/
# initialize a project as an ES module
npm init --yes
npm pkg set type="module"
npm pkg set main="agent.ts"
# install ADK libraries
npm install @google/adk
# install dev tools as a dev dependency
npm install -D @google/adk-devtools
```
### Define the agent code
Create the code for a basic agent, including a simple implementation of an ADK [Function Tool](/tools-custom/function-tools/), called `get_current_time`. Create an `agent.ts` file in your project directory and add the following code:
my-agent/agent.ts
```typescript
import {FunctionTool, LlmAgent} from '@google/adk';
import {z} from 'zod';
/* Mock tool implementation */
const getCurrentTime = new FunctionTool({
name: 'get_current_time',
description: 'Returns the current time in a specified city.',
parameters: z.object({
city: z.string().describe("The name of the city for which to retrieve the current time."),
}),
execute: ({city}) => {
return {status: 'success', report: `The current time in ${city} is 10:30 AM`};
},
});
export const rootAgent = new LlmAgent({
name: 'hello_time_agent',
model: 'gemini-flash-latest',
description: 'Tells the current time in a specified city.',
instruction: `You are a helpful assistant that tells the current time in a city.
Use the 'get_current_time' tool for this purpose.`,
tools: [getCurrentTime],
});
```
### Set your API key
This project uses the Gemini API, which requires an API key. If you don't already have Gemini API key, create a key in Google AI Studio on the [API Keys](https://aistudio.google.com/app/apikey) page.
In a terminal window, write your API key into your `.env` file of your project to set environment variables:
Update: my-agent/.env
```bash
echo 'GEMINI_API_KEY="YOUR_API_KEY"' > .env
```
Update: my-agent/.env
```console
echo 'GEMINI_API_KEY="YOUR_API_KEY"' > .env
```
Update: my-agent/.env
```console
echo GEMINI_API_KEY="YOUR_API_KEY" > .env
```
Using other AI models with ADK
ADK supports the use of many generative AI models. For more information on configuring other models in ADK agents, see [Models & Authentication](/agents/models).
## Run your agent
You can run your ADK agent with the `@google/adk-devtools` library as an interactive command-line interface using the `run` command or the ADK web user interface using the `web` command. Both these options allow you to test and interact with your agent.
### Run with command-line interface
Run your agent with the ADK TypeScript command-line interface tool using the following command:
```console
npx adk run agent.ts
```
### Run with web interface
Run your agent with the ADK web interface using the following command:
```console
npx adk web
```
This command starts a web server with a chat interface for your agent. You can access the web interface at `http://localhost:8000`. Select your agent at the upper right corner and type a request.
Caution: ADK Web for development only
ADK Web is ***not meant for use in production deployments***. You should use ADK Web for development and debugging purposes only.
## Next: Build your agent
Now that you have ADK installed and your first agent running, try building your own agent with our build guides:
- [Build your agent](/tutorials/)
# Build your agent with ADK
Get started with the Agent Development Kit (ADK) through our collection of practical guides. These tutorials are designed in a simple, progressive, step-by-step fashion, introducing you to different ADK features and capabilities.
This approach allows you to learn and build incrementally – starting with foundational concepts and gradually tackling more advanced agent development techniques. You'll explore how to apply these features effectively across various use cases, equipping you to build your own sophisticated agentic applications with ADK. Explore our collection below and happy building:
- **Multi-tool agent**
______________________________________________________________________
Create a workflow that uses multiple tools.
[Build a multi-tool agent](/tutorials/multi-tool-agent/)
- **Agent team**
______________________________________________________________________
Build a multi-agent workflow including agent delegation, session management, and safety callbacks.
[Build an agent team](/tutorials/agent-team/)
- **Streaming agent**
______________________________________________________________________
Create an agent for handling streamed content.
[Build a streaming agent](https://adk.tools-online.site/live/get-started/index.md)
- **Discover sample agents**
______________________________________________________________________
Discover sample agents for retail, travel, customer service, and more!
[Discover adk-samples](https://github.com/google/adk-samples)
# Build Your First Intelligent Agent Team: A Progressive Weather Bot with ADK
This tutorial extends from the [Multi-tool agent](/tutorials/multi-tool-agent/) project. Now, you're ready to dive deeper and construct a more sophisticated, **multi-agent system**.
We'll embark on building a **Weather Bot agent team**, progressively layering advanced features onto a simple foundation. Starting with a single agent that can look up weather, we will incrementally add capabilities like:
- Leveraging different AI models (Gemini, GPT, Claude).
- Designing specialized sub-agents for distinct tasks (like greetings and farewells).
- Enabling intelligent delegation between agents.
- Giving agents memory using persistent session state.
- Implementing crucial safety guardrails using callbacks.
**Why a Weather Bot Team?**
This use case, while seemingly simple, provides a practical and relatable canvas to explore core ADK concepts essential for building complex, real-world agentic applications. You'll learn how to structure interactions, manage state, ensure safety, and orchestrate multiple AI "brains" working together.
**What is ADK Again?**
As a reminder, ADK is a Python framework designed to streamline the development of applications powered by Large Language Models (LLMs). It offers robust building blocks for creating agents that can reason, plan, utilize tools, interact dynamically with users, and collaborate effectively within a team.
**In this advanced tutorial, you will master:**
- ✅ **Tool Definition & Usage:** Crafting Python functions (`tools`) that grant agents specific abilities (like fetching data) and instructing agents on how to use them effectively.
- ✅ **Multi-LLM Flexibility:** Configuring agents to utilize various leading LLMs (Gemini, GPT-4o, Claude Sonnet) via LiteLLM integration, allowing you to choose the best model for each task.
- ✅ **Agent Delegation & Collaboration:** Designing specialized sub-agents and enabling automatic routing (`auto flow`) of user requests to the most appropriate agent within a team.
- ✅ **Session State for Memory:** Utilizing `Session State` and `ToolContext` to enable agents to remember information across conversational turns, leading to more contextual interactions.
- ✅ **Safety Guardrails with Callbacks:** Implementing `before_model_callback` and `before_tool_callback` to inspect, modify, or block requests/tool usage based on predefined rules, enhancing application safety and control.
**End State Expectation:**
By completing this tutorial, you will have built a functional multi-agent Weather Bot system. This system will not only provide weather information but also handle conversational niceties, remember the last city checked, and operate within defined safety boundaries, all orchestrated using ADK.
**Prerequisites:**
- ✅ **Solid understanding of Python programming.**
- ✅ **Familiarity with Large Language Models (LLMs), APIs, and the concept of agents.**
- ❗ **Crucially: Completion of the ADK Quickstart tutorial(s) or equivalent foundational knowledge of ADK basics (Agent, Runner, SessionService, basic Tool usage).** This tutorial builds directly upon those concepts.
- ✅ **API Keys** for the LLMs you intend to use (e.g., Google AI Studio for Gemini, OpenAI Platform, Anthropic Console).
______________________________________________________________________
**Note on Execution Environment:**
This tutorial is structured for interactive notebook environments like Google Colab, Colab Enterprise, or Jupyter notebooks. Please keep the following in mind:
- **Running Async Code:** Notebook environments handle asynchronous code differently. You'll see examples using `await` (suitable when an event loop is already running, common in notebooks) or `asyncio.run()` (often needed when running as a standalone `.py` script or in specific notebook setups). The code blocks provide guidance for both scenarios.
- **Manual Runner/Session Setup:** The steps involve explicitly creating `Runner` and `SessionService` instances. This approach is shown because it gives you fine-grained control over the agent's execution lifecycle, session management, and state persistence.
**Alternative: Using ADK's Built-in Tools (Web UI / CLI / API Server)**
If you prefer a setup that handles the runner and session management automatically using ADK's standard tools, you can find the [equivalent code](https://github.com/google/adk-docs/tree/main/examples/python/tutorial/agent_team/adk_tutorial) for that purpose. That version is designed to be run directly with commands like `adk web` (for a web UI), `adk run` (for CLI interaction), or `adk api_server` (to expose an API). Please follow the `README.md` instructions provided in that alternative resource.
______________________________________________________________________
**Ready to build your agent team? Let's dive in!**
> **Note:** This tutorial works with adk version 1.0.0 and above
```python
# @title Step 0: Setup and Installation
# Install ADK and LiteLLM for multi-model support
!pip install google-adk -q
!pip install "litellm>=1.84" -q
print("Installation complete.")
```
```python
# @title Import necessary libraries
import os
import asyncio
from google.adk.agents import Agent
from google.adk.models.lite_llm import LiteLlm # For multi-model support
from google.adk.sessions import InMemorySessionService
from google.adk.runners import Runner
from google.genai import types # For creating message Content/Parts
import warnings
# Ignore all warnings
warnings.filterwarnings("ignore")
import logging
logging.basicConfig(level=logging.ERROR)
print("Libraries imported.")
```
```python
# @title Configure API Keys (Replace with your actual keys!)
# --- IMPORTANT: Replace placeholders with your real API keys ---
# Gemini API Key (Get from Google AI Studio: https://aistudio.google.com/app/apikey)
os.environ["GOOGLE_API_KEY"] = "YOUR_GOOGLE_API_KEY" # <--- REPLACE
# [Optional]
# OpenAI API Key (Get from OpenAI Platform: https://platform.openai.com/api-keys)
os.environ['OPENAI_API_KEY'] = 'YOUR_OPENAI_API_KEY' # <--- REPLACE
# [Optional]
# Anthropic API Key (Get from Anthropic Console: https://console.anthropic.com/settings/keys)
os.environ['ANTHROPIC_API_KEY'] = 'YOUR_ANTHROPIC_API_KEY' # <--- REPLACE
# --- Verify Keys (Optional Check) ---
print("API Keys Set:")
print(f"Google API Key set: {'Yes' if os.environ.get('GOOGLE_API_KEY') and os.environ['GOOGLE_API_KEY'] != 'YOUR_GOOGLE_API_KEY' else 'No (REPLACE PLACEHOLDER!)'}")
print(f"OpenAI API Key set: {'Yes' if os.environ.get('OPENAI_API_KEY') and os.environ['OPENAI_API_KEY'] != 'YOUR_OPENAI_API_KEY' else 'No (REPLACE PLACEHOLDER!)'}")
print(f"Anthropic API Key set: {'Yes' if os.environ.get('ANTHROPIC_API_KEY') and os.environ['ANTHROPIC_API_KEY'] != 'YOUR_ANTHROPIC_API_KEY' else 'No (REPLACE PLACEHOLDER!)'}")
# Configure ADK to use API keys directly (not Agent Platform for this multi-model setup)
os.environ["GOOGLE_GENAI_USE_ENTERPRISE"] = "False"
# @markdown **Security Note:** It's best practice to manage API keys securely (e.g., using Colab Secrets or environment variables) rather than hardcoding them directly in the notebook. Replace the placeholder strings above.
```
```python
# --- Define Model Constants for easier use ---
# More supported models can be referenced here: https://ai.google.dev/gemini-api/docs/models#model-variations
MODEL_GEMINI_FLASH = "gemini-flash-latest"
# More supported models can be referenced here: https://docs.litellm.ai/docs/providers/openai#openai-chat-completion-models
MODEL_GPT_4O = "openai/gpt-4.1" # You can also try: gpt-4.1-mini, gpt-4o etc.
# More supported models can be referenced here: https://docs.litellm.ai/docs/providers/anthropic
MODEL_CLAUDE_SONNET = "claude-sonnet-4-6" # You can also try: claude-opus-4-6, etc
print("\nEnvironment configured.")
```
______________________________________________________________________
## Step 1: Your First Agent - Basic Weather Lookup
Let's begin by building the fundamental component of our Weather Bot: a single agent capable of performing a specific task – looking up weather information. This involves creating two core pieces:
1. **A Tool:** A Python function that equips the agent with the *ability* to fetch weather data.
1. **An Agent:** The AI "brain" that understands the user's request, knows it has a weather tool, and decides when and how to use it.
______________________________________________________________________
**1. Define the Tool (`get_weather`)**
In ADK, **Tools** are the building blocks that give agents concrete capabilities beyond just text generation. They are typically regular Python functions that perform specific actions, like calling an API, querying a database, or performing calculations.
Our first tool will provide a *mock* weather report. This allows us to focus on the agent structure without needing external API keys yet. Later, you could easily swap this mock function with one that calls a real weather service.
**Key Concept: Docstrings are Crucial!** The agent's LLM relies heavily on the function's **docstring** to understand:
- *What* the tool does.
- *When* to use it.
- *What arguments* it requires (`city: str`).
- *What information* it returns.
**Best Practice:** Write clear, descriptive, and accurate docstrings for your tools. This is essential for the LLM to use the tool correctly.
```python
# @title Define the get_weather Tool
def get_weather(city: str) -> dict:
"""Retrieves the current weather report for a specified city.
Args:
city (str): The name of the city (e.g., "New York", "London", "Tokyo").
Returns:
dict: A dictionary containing the weather information.
Includes a 'status' key ('success' or 'error').
If 'success', includes a 'report' key with weather details.
If 'error', includes an 'error_message' key.
"""
print(f"--- Tool: get_weather called for city: {city} ---") # Log tool execution
city_normalized = city.lower().replace(" ", "") # Basic normalization
# Mock weather data
mock_weather_db = {
"newyork": {"status": "success", "report": "The weather in New York is sunny with a temperature of 25°C."},
"london": {"status": "success", "report": "It's cloudy in London with a temperature of 15°C."},
"tokyo": {"status": "success", "report": "Tokyo is experiencing light rain and a temperature of 18°C."},
}
if city_normalized in mock_weather_db:
return mock_weather_db[city_normalized]
else:
return {"status": "error", "error_message": f"Sorry, I don't have weather information for '{city}'."}
# Example tool usage (optional test)
print(get_weather("New York"))
print(get_weather("Paris"))
```
______________________________________________________________________
**2. Define the Agent (`weather_agent`)**
Now, let's create the **Agent** itself. An `Agent` in ADK orchestrates the interaction between the user, the LLM, and the available tools.
We configure it with several key parameters:
- `name`: A unique identifier for this agent (e.g., "weather_agent_v1").
- `model`: Specifies which LLM to use (e.g., `MODEL_GEMINI_FLASH`). We'll start with a specific Gemini model.
- `description`: A concise summary of the agent's overall purpose. This becomes crucial later when other agents need to decide whether to delegate tasks to *this* agent.
- `instruction`: Detailed guidance for the LLM on how to behave, its persona, its goals, and specifically *how and when* to utilize its assigned `tools`.
- `tools`: A list containing the actual Python tool functions the agent is allowed to use (e.g., `[get_weather]`).
**Best Practice:** Provide clear and specific `instruction` prompts. The more detailed the instructions, the better the LLM can understand its role and how to use its tools effectively. Be explicit about error handling if needed.
**Best Practice:** Choose descriptive `name` and `description` values. These are used internally by ADK and are vital for features like automatic delegation (covered later).
```python
# @title Define the Weather Agent
# Use one of the model constants defined earlier
AGENT_MODEL = MODEL_GEMINI_FLASH # Starting with Gemini
weather_agent = Agent(
name="weather_agent_v1",
model=AGENT_MODEL, # Can be a string for Gemini or a LiteLlm object
description="Provides weather information for specific cities.",
instruction="You are a helpful weather assistant. "
"When the user asks for the weather in a specific city, "
"use the 'get_weather' tool to find the information. "
"If the tool returns an error, inform the user politely. "
"If the tool is successful, present the weather report clearly.",
tools=[get_weather], # Pass the function directly
)
print(f"Agent '{weather_agent.name}' created using model '{AGENT_MODEL}'.")
```
______________________________________________________________________
**3. Setup Runner and Session Service**
To manage conversations and execute the agent, we need two more components:
- `SessionService`: Responsible for managing conversation history and state for different users and sessions. The `InMemorySessionService` is a simple implementation that stores everything in memory, suitable for testing and simple applications. It keeps track of the messages exchanged. We'll explore state persistence more in Step 4.
- `Runner`: The engine that orchestrates the interaction flow. It takes user input, routes it to the appropriate agent, manages calls to the LLM and tools based on the agent's logic, handles session updates via the `SessionService`, and yields events representing the progress of the interaction.
```python
# @title Setup Session Service and Runner
# --- Session Management ---
# Key Concept: SessionService stores conversation history & state.
# InMemorySessionService is simple, non-persistent storage for this tutorial.
session_service = InMemorySessionService()
# Define constants for identifying the interaction context
APP_NAME = "weather_tutorial_app"
USER_ID = "user_1"
SESSION_ID = "session_001" # Using a fixed ID for simplicity
# Create the specific session where the conversation will happen
session = await session_service.create_session(
app_name=APP_NAME,
user_id=USER_ID,
session_id=SESSION_ID
)
print(f"Session created: App='{APP_NAME}', User='{USER_ID}', Session='{SESSION_ID}'")
# --- OR ---
# Uncomment the following lines if running as a standard Python script (.py file):
# from google.adk.sessions import Session
#
# async def init_session(app_name:str,user_id:str,session_id:str) -> Session:
# session = await session_service.create_session(
# app_name=app_name,
# user_id=user_id,
# session_id=session_id
# )
# print(f"Session created: App='{app_name}', User='{user_id}', Session='{session_id}'")
# return session
#
# session = asyncio.run(init_session(APP_NAME,USER_ID,SESSION_ID))
# --- Runner ---
# Key Concept: Runner orchestrates the agent execution loop.
runner = Runner(
agent=weather_agent, # The agent we want to run
app_name=APP_NAME, # Associates runs with our app
session_service=session_service # Uses our session manager
)
print(f"Runner created for agent '{runner.agent.name}'.")
```
______________________________________________________________________
**4. Interact with the Agent**
We need a way to send messages to our agent and receive its responses. Since LLM calls and tool executions can take time, ADK's `Runner` operates asynchronously.
We'll define an `async` helper function (`call_agent_async`) that:
1. Takes a user query string.
1. Packages it into the ADK `Content` format.
1. Calls `runner.run_async`, providing the user/session context and the new message.
1. Iterates through the **Events** yielded by the runner. Events represent steps in the agent's execution (e.g., tool call requested, tool result received, intermediate LLM thought, final response).
1. Identifies and prints the **final response** event using `event.is_final_response()`.
**Why `async`?** Interactions with LLMs and potentially tools (like external APIs) are I/O-bound operations. Using `asyncio` allows the program to handle these operations efficiently without blocking execution.
```python
# @title Define Agent Interaction Function
from google.genai import types # For creating message Content/Parts
async def call_agent_async(query: str, runner, user_id, session_id):
"""Sends a query to the agent and prints the final response."""
print(f"\n>>> User Query: {query}")
# Prepare the user's message in ADK format
content = types.Content(role='user', parts=[types.Part(text=query)])
final_response_text = "Agent did not produce a final response." # Default
# Key Concept: run_async executes the agent logic and yields Events.
# We iterate through events to find the final answer.
async for event in runner.run_async(user_id=user_id, session_id=session_id, new_message=content):
# You can uncomment the line below to see *all* events during execution
# print(f" [Event] Author: {event.author}, Type: {type(event).__name__}, Final: {event.is_final_response()}, Content: {event.content}")
# Key Concept: is_final_response() marks the concluding message for the turn.
if event.is_final_response():
if event.content and event.content.parts:
# Assuming text response in the first part
final_response_text = event.content.parts[0].text
elif event.actions and event.actions.escalate: # Handle potential errors/escalations
final_response_text = f"Agent escalated: {event.error_message or 'No specific message.'}"
# Add more checks here if needed (e.g., specific error codes)
break # Stop processing events once the final response is found
print(f"<<< Agent Response: {final_response_text}")
```
______________________________________________________________________
**5. Run the Conversation**
Finally, let's test our setup by sending a few queries to the agent. We wrap our `async` calls in a main `async` function and run it using `await`.
Watch the output:
- See the user queries.
- Notice the `--- Tool: get_weather called... ---` logs when the agent uses the tool.
- Observe the agent's final responses, including how it handles the case where weather data isn't available (for Paris).
```python
# @title Run the Initial Conversation
# We need an async function to await our interaction helper
async def run_conversation():
await call_agent_async("What is the weather like in London?",
runner=runner,
user_id=USER_ID,
session_id=SESSION_ID)
await call_agent_async("How about Paris?",
runner=runner,
user_id=USER_ID,
session_id=SESSION_ID) # Expecting the tool's error message
await call_agent_async("Tell me the weather in New York",
runner=runner,
user_id=USER_ID,
session_id=SESSION_ID)
# Execute the conversation using await in an async context (like Colab/Jupyter)
await run_conversation()
# --- OR ---
# Uncomment the following lines if running as a standard Python script (.py file):
# import asyncio
# if __name__ == "__main__":
# try:
# asyncio.run(run_conversation())
# except Exception as e:
# print(f"An error occurred: {e}")
```
______________________________________________________________________
Congratulations! You've successfully built and interacted with your first ADK agent. It understands the user's request, uses a tool to find information, and responds appropriately based on the tool's result.
In the next step, we'll explore how to easily switch the underlying Language Model powering this agent.
## Step 2: Going Multi-Model with LiteLLM [Optional]
In Step 1, we built a functional Weather Agent powered by a specific Gemini model. While effective, real-world applications often benefit from the flexibility to use *different* Large Language Models (LLMs). Why?
- **Performance:** Some models excel at specific tasks (e.g., coding, reasoning, creative writing).
- **Cost:** Different models have varying price points.
- **Capabilities:** Models offer diverse features, context window sizes, and fine-tuning options.
- **Availability/Redundancy:** Having alternatives ensures your application remains functional even if one provider experiences issues.
ADK makes switching between models seamless through its integration with the [**LiteLLM**](https://github.com/BerriAI/litellm) library. LiteLLM acts as a consistent interface to over 100 different LLMs.
**In this step, we will:**
1. Learn how to configure an ADK `Agent` to use models from providers like OpenAI (GPT) and Anthropic (Claude) using the `LiteLlm` wrapper.
1. Define, configure (with their own sessions and runners), and immediately test instances of our Weather Agent, each backed by a different LLM.
1. Interact with these different agents to observe potential variations in their responses, even when using the same underlying tool.
______________________________________________________________________
**1. Import `LiteLlm`**
We imported this during the initial setup (Step 0), but it's the key component for multi-model support:
```python
# @title 1. Import LiteLlm
from google.adk.models.lite_llm import LiteLlm
```
**2. Define and Test Multi-Model Agents**
Instead of passing only a model name string (which defaults to Google's Gemini models), we wrap the desired model identifier string within the `LiteLlm` class.
- **Key Concept: `LiteLlm` Wrapper:** The `LiteLlm(model="provider/model_name")` syntax tells ADK to route requests for this agent through the LiteLLM library to the specified model provider.
Make sure you have configured the necessary API keys for OpenAI and Anthropic in Step 0. We'll use the `call_agent_async` function (defined earlier, which now accepts `runner`, `user_id`, and `session_id`) to interact with each agent immediately after its setup.
Each block below will:
- Define the agent using a specific LiteLLM model (`MODEL_GPT_4O` or `MODEL_CLAUDE_SONNET`).
- Create a *new, separate* `InMemorySessionService` and session specifically for that agent's test run. This keeps the conversation histories isolated for this demonstration.
- Create a `Runner` configured for the specific agent and its session service.
- Immediately call `call_agent_async` to send a query and test the agent.
**Best Practice:** Use constants for model names (like `MODEL_GPT_4O`, `MODEL_CLAUDE_SONNET` defined in Step 0) to avoid typos and make code easier to manage.
**Error Handling:** We wrap the agent definitions in `try...except` blocks. This prevents the entire code cell from failing if an API key for a specific provider is missing or invalid, allowing the tutorial to proceed with the models that *are* configured.
First, let's create and test the agent using OpenAI's GPT-4o.
```python
# @title Define and Test GPT Agent
# Make sure 'get_weather' function from Step 1 is defined in your environment.
# Make sure 'call_agent_async' is defined from earlier.
# --- Agent using GPT-4o ---
weather_agent_gpt = None # Initialize to None
runner_gpt = None # Initialize runner to None
try:
weather_agent_gpt = Agent(
name="weather_agent_gpt",
# Key change: Wrap the LiteLLM model identifier
model=LiteLlm(model=MODEL_GPT_4O),
description="Provides weather information (using GPT-4o).",
instruction="You are a helpful weather assistant powered by GPT-4o. "
"Use the 'get_weather' tool for city weather requests. "
"Clearly present successful reports or polite error messages based on the tool's output status.",
tools=[get_weather], # Re-use the same tool
)
print(f"Agent '{weather_agent_gpt.name}' created using model '{MODEL_GPT_4O}'.")
# InMemorySessionService is simple, non-persistent storage for this tutorial.
session_service_gpt = InMemorySessionService() # Create a dedicated service
# Define constants for identifying the interaction context
APP_NAME_GPT = "weather_tutorial_app_gpt" # Unique app name for this test
USER_ID_GPT = "user_1_gpt"
SESSION_ID_GPT = "session_001_gpt" # Using a fixed ID for simplicity
# Create the specific session where the conversation will happen
session_gpt = await session_service_gpt.create_session(
app_name=APP_NAME_GPT,
user_id=USER_ID_GPT,
session_id=SESSION_ID_GPT
)
print(f"Session created: App='{APP_NAME_GPT}', User='{USER_ID_GPT}', Session='{SESSION_ID_GPT}'")
# Create a runner specific to this agent and its session service
runner_gpt = Runner(
agent=weather_agent_gpt,
app_name=APP_NAME_GPT, # Use the specific app name
session_service=session_service_gpt # Use the specific session service
)
print(f"Runner created for agent '{runner_gpt.agent.name}'.")
# --- Test the GPT Agent ---
print("\n--- Testing GPT Agent ---")
# Ensure call_agent_async uses the correct runner, user_id, session_id
await call_agent_async(query = "What's the weather in Tokyo?",
runner=runner_gpt,
user_id=USER_ID_GPT,
session_id=SESSION_ID_GPT)
# --- OR ---
# Uncomment the following lines if running as a standard Python script (.py file):
# import asyncio
# if __name__ == "__main__":
# try:
# asyncio.run(call_agent_async(query = "What's the weather in Tokyo?",
# runner=runner_gpt,
# user_id=USER_ID_GPT,
# session_id=SESSION_ID_GPT)
# except Exception as e:
# print(f"An error occurred: {e}")
except Exception as e:
print(f"❌ Could not create or run GPT agent '{MODEL_GPT_4O}'. Check API Key and model name. Error: {e}")
```
Next, we'll do the same for Anthropic's Claude Sonnet.
```python
# @title Define and Test Claude Agent
# Make sure 'get_weather' function from Step 1 is defined in your environment.
# Make sure 'call_agent_async' is defined from earlier.
# --- Agent using Claude Sonnet ---
weather_agent_claude = None # Initialize to None
runner_claude = None # Initialize runner to None
try:
weather_agent_claude = Agent(
name="weather_agent_claude",
# Key change: Wrap the LiteLLM model identifier
model=LiteLlm(model=MODEL_CLAUDE_SONNET),
description="Provides weather information (using Claude Sonnet).",
instruction="You are a helpful weather assistant powered by Claude Sonnet. "
"Use the 'get_weather' tool for city weather requests. "
"Analyze the tool's dictionary output ('status', 'report'/'error_message'). "
"Clearly present successful reports or polite error messages.",
tools=[get_weather], # Re-use the same tool
)
print(f"Agent '{weather_agent_claude.name}' created using model '{MODEL_CLAUDE_SONNET}'.")
# InMemorySessionService is simple, non-persistent storage for this tutorial.
session_service_claude = InMemorySessionService() # Create a dedicated service
# Define constants for identifying the interaction context
APP_NAME_CLAUDE = "weather_tutorial_app_claude" # Unique app name
USER_ID_CLAUDE = "user_1_claude"
SESSION_ID_CLAUDE = "session_001_claude" # Using a fixed ID for simplicity
# Create the specific session where the conversation will happen
session_claude = await session_service_claude.create_session(
app_name=APP_NAME_CLAUDE,
user_id=USER_ID_CLAUDE,
session_id=SESSION_ID_CLAUDE
)
print(f"Session created: App='{APP_NAME_CLAUDE}', User='{USER_ID_CLAUDE}', Session='{SESSION_ID_CLAUDE}'")
# Create a runner specific to this agent and its session service
runner_claude = Runner(
agent=weather_agent_claude,
app_name=APP_NAME_CLAUDE, # Use the specific app name
session_service=session_service_claude # Use the specific session service
)
print(f"Runner created for agent '{runner_claude.agent.name}'.")
# --- Test the Claude Agent ---
print("\n--- Testing Claude Agent ---")
# Ensure call_agent_async uses the correct runner, user_id, session_id
await call_agent_async(query = "Weather in London please.",
runner=runner_claude,
user_id=USER_ID_CLAUDE,
session_id=SESSION_ID_CLAUDE)
# --- OR ---
# Uncomment the following lines if running as a standard Python script (.py file):
# import asyncio
# if __name__ == "__main__":
# try:
# asyncio.run(call_agent_async(query = "Weather in London please.",
# runner=runner_claude,
# user_id=USER_ID_CLAUDE,
# session_id=SESSION_ID_CLAUDE)
# except Exception as e:
# print(f"An error occurred: {e}")
except Exception as e:
print(f"❌ Could not create or run Claude agent '{MODEL_CLAUDE_SONNET}'. Check API Key and model name. Error: {e}")
```
Observe the output carefully from both code blocks. You should see:
1. Each agent (`weather_agent_gpt`, `weather_agent_claude`) is created successfully (if API keys are valid).
1. A dedicated session and runner are set up for each.
1. Each agent correctly identifies the need to use the `get_weather` tool when processing the query (you'll see the `--- Tool: get_weather called... ---` log).
1. The *underlying tool logic* remains identical, always returning our mock data.
1. However, the **final textual response** generated by each agent might differ slightly in phrasing, tone, or formatting. This is because the instruction prompt is interpreted and executed by different LLMs (GPT-4o vs. Claude Sonnet).
This step demonstrates the power and flexibility ADK + LiteLLM provide. You can easily experiment with and deploy agents using various LLMs while keeping your core application logic (tools, fundamental agent structure) consistent.
In the next step, we'll move beyond a single agent and build a small team where agents can delegate tasks to each other!
______________________________________________________________________
## Step 3: Building an Agent Team - Delegation for Greetings & Farewells
In Steps 1 and 2, we built and experimented with a single agent focused solely on weather lookups. While effective for its specific task, real-world applications often involve handling a wider variety of user interactions. We *could* keep adding more tools and complex instructions to our single weather agent, but this can quickly become unmanageable and less efficient.
A more robust approach is to build an **Agent Team**. This involves:
1. Creating multiple, **specialized agents**, each designed for a specific capability (e.g., one for weather, one for greetings, one for calculations).
1. Designating a **root agent** (or orchestrator) that receives the initial user request.
1. Enabling the root agent to **delegate** the request to the most appropriate specialized sub-agent based on the user's intent.
**Why build an Agent Team?**
- **Modularity:** Easier to develop, test, and maintain individual agents.
- **Specialization:** Each agent can be fine-tuned (instructions, model choice) for its specific task.
- **Scalability:** Simpler to add new capabilities by adding new agents.
- **Efficiency:** Allows using potentially simpler/cheaper models for simpler tasks (like greetings).
**In this step, we will:**
1. Define simple tools for handling greetings (`say_hello`) and farewells (`say_goodbye`).
1. Create two new specialized sub-agents: `greeting_agent` and `farewell_agent`.
1. Update our main weather agent (`weather_agent_v2`) to act as the **root agent**.
1. Configure the root agent with its sub-agents, enabling **automatic delegation**.
1. Test the delegation flow by sending different types of requests to the root agent.
______________________________________________________________________
**1. Define Tools for Sub-Agents**
First, let's create the simple Python functions that will serve as tools for our new specialist agents. Remember, clear docstrings are vital for the agents that will use them.
```python
# @title Define Tools for Greeting and Farewell Agents
from typing import Optional # Make sure to import Optional
# Ensure 'get_weather' from Step 1 is available if running this step independently.
# def get_weather(city: str) -> dict: ... (from Step 1)
def say_hello(name: Optional[str] = None) -> str:
"""Provides a simple greeting. If a name is provided, it will be used.
Args:
name (str, optional): The name of the person to greet. Defaults to a generic greeting if not provided.
Returns:
str: A friendly greeting message.
"""
if name:
greeting = f"Hello, {name}!"
print(f"--- Tool: say_hello called with name: {name} ---")
else:
greeting = "Hello there!" # Default greeting if name is None or not explicitly passed
print(f"--- Tool: say_hello called without a specific name (name_arg_value: {name}) ---")
return greeting
def say_goodbye() -> str:
"""Provides a simple farewell message to conclude the conversation."""
print(f"--- Tool: say_goodbye called ---")
return "Goodbye! Have a great day."
print("Greeting and Farewell tools defined.")
# Optional self-test
print(say_hello("Alice"))
print(say_hello()) # Test with no argument (should use default "Hello there!")
print(say_hello(name=None)) # Test with name explicitly as None (should use default "Hello there!")
```
______________________________________________________________________
**2. Define the Sub-Agents (Greeting & Farewell)**
Now, create the `Agent` instances for our specialists. Notice their highly focused `instruction` and, critically, their clear `description`. The `description` is the primary information the *root agent* uses to decide *when* to delegate to these sub-agents.
**Best Practice:** Sub-agent `description` fields should accurately and concisely summarize their specific capability. This is crucial for effective automatic delegation.
**Best Practice:** Sub-agent `instruction` fields should be tailored to their limited scope, telling them exactly what to do and *what not* to do (e.g., "Your *only* task is...").
```python
# @title Define Greeting and Farewell Sub-Agents
# If you want to use models other than Gemini, Ensure LiteLlm is imported and API keys are set (from Step 0/2)
# from google.adk.models.lite_llm import LiteLlm
# MODEL_GPT_4O, MODEL_CLAUDE_SONNET etc. should be defined
# Or else, continue to use: model = MODEL_GEMINI_FLASH
# --- Greeting Agent ---
greeting_agent = None
try:
greeting_agent = Agent(
# Using a potentially different/cheaper model for a simple task
model = MODEL_GEMINI_FLASH,
# model=LiteLlm(model=MODEL_GPT_4O), # If you would like to experiment with other models
name="greeting_agent",
instruction="You are the Greeting Agent. Your ONLY task is to provide a friendly greeting to the user. "
"Use the 'say_hello' tool to generate the greeting. "
"If the user provides their name, make sure to pass it to the tool. "
"Do not engage in any other conversation or tasks.",
description="Handles simple greetings and hellos using the 'say_hello' tool.", # Crucial for delegation
tools=[say_hello],
)
print(f"✅ Agent '{greeting_agent.name}' created using model '{greeting_agent.model}'.")
except Exception as e:
print(f"❌ Could not create Greeting agent. Check API Key ({greeting_agent.model}). Error: {e}")
# --- Farewell Agent ---
farewell_agent = None
try:
farewell_agent = Agent(
# Can use the same or a different model
model = MODEL_GEMINI_FLASH,
# model=LiteLlm(model=MODEL_GPT_4O), # If you would like to experiment with other models
name="farewell_agent",
instruction="You are the Farewell Agent. Your ONLY task is to provide a polite goodbye message. "
"Use the 'say_goodbye' tool when the user indicates they are leaving or ending the conversation "
"(e.g., using words like 'bye', 'goodbye', 'thanks bye', 'see you'). "
"Do not perform any other actions.",
description="Handles simple farewells and goodbyes using the 'say_goodbye' tool.", # Crucial for delegation
tools=[say_goodbye],
)
print(f"✅ Agent '{farewell_agent.name}' created using model '{farewell_agent.model}'.")
except Exception as e:
print(f"❌ Could not create Farewell agent. Check API Key ({farewell_agent.model}). Error: {e}")
```
______________________________________________________________________
**3. Define the Root Agent (Weather Agent v2) with Sub-Agents**
Now, we upgrade our `weather_agent`. The key changes are:
- Adding the `sub_agents` parameter: We pass a list containing the `greeting_agent` and `farewell_agent` instances we just created.
- Updating the `instruction`: We explicitly tell the root agent *about* its sub-agents and *when* it should delegate tasks to them.
**Key Concept: Automatic Delegation (Auto Flow)** By providing the `sub_agents` list, ADK enables automatic delegation. When the root agent receives a user query, its LLM considers not only its own instructions and tools but also the `description` of each sub-agent. If the LLM determines that a query aligns better with a sub-agent's described capability (e.g., "Handles simple greetings"), it will automatically generate a special internal action to *transfer control* to that sub-agent for that turn. The sub-agent then processes the query using its own model, instructions, and tools.
**Best Practice:** Ensure the root agent's instructions clearly guide its delegation decisions. Mention the sub-agents by name and describe the conditions under which delegation should occur.
```python
# @title Define the Root Agent with Sub-Agents
# Ensure sub-agents were created successfully before defining the root agent.
# Also ensure the original 'get_weather' tool is defined.
root_agent = None
runner_root = None # Initialize runner
if greeting_agent and farewell_agent and 'get_weather' in globals():
# Let's use a capable Gemini model for the root agent to handle orchestration
root_agent_model = MODEL_GEMINI_FLASH
weather_agent_team = Agent(
name="weather_agent_v2", # Give it a new version name
model=root_agent_model,
description="The main coordinator agent. Handles weather requests and delegates greetings/farewells to specialists.",
instruction="You are the main Weather Agent coordinating a team. Your primary responsibility is to provide weather information. "
"Use the 'get_weather' tool ONLY for specific weather requests (e.g., 'weather in London'). "
"You have specialized sub-agents: "
"1. 'greeting_agent': Handles simple greetings like 'Hi', 'Hello'. Delegate to it for these. "
"2. 'farewell_agent': Handles simple farewells like 'Bye', 'See you'. Delegate to it for these. "
"Analyze the user's query. If it's a greeting, delegate to 'greeting_agent'. If it's a farewell, delegate to 'farewell_agent'. "
"If it's a weather request, handle it yourself using 'get_weather'. "
"For anything else, respond appropriately or state you cannot handle it.",
tools=[get_weather], # Root agent still needs the weather tool for its core task
# Key change: Link the sub-agents here!
sub_agents=[greeting_agent, farewell_agent]
)
print(f"✅ Root Agent '{weather_agent_team.name}' created using model '{root_agent_model}' with sub-agents: {[sa.name for sa in weather_agent_team.sub_agents]}")
else:
print("❌ Cannot create root agent because one or more sub-agents failed to initialize or 'get_weather' tool is missing.")
if not greeting_agent: print(" - Greeting Agent is missing.")
if not farewell_agent: print(" - Farewell Agent is missing.")
if 'get_weather' not in globals(): print(" - get_weather function is missing.")
```
______________________________________________________________________
**4. Interact with the Agent Team**
Now that we've defined our root agent (`weather_agent_team` - *Note: Ensure this variable name matches the one defined in the previous code block, likely `# @title Define the Root Agent with Sub-Agents`, which might have named it `root_agent`*) with its specialized sub-agents, let's test the delegation mechanism.
The following code block will:
1. Define an `async` function `run_team_conversation`.
1. Inside this function, create a *new, dedicated* `InMemorySessionService` and a specific session (`session_001_agent_team`) just for this test run. This isolates the conversation history for testing the team dynamics.
1. Create a `Runner` (`runner_agent_team`) configured to use our `weather_agent_team` (the root agent) and the dedicated session service.
1. Use our updated `call_agent_async` function to send different types of queries (greeting, weather request, farewell) to the `runner_agent_team`. We explicitly pass the runner, user ID, and session ID for this specific test.
1. Immediately execute the `run_team_conversation` function.
We expect the following flow:
1. The "Hello there!" query goes to `runner_agent_team`.
1. The root agent (`weather_agent_team`) receives it and, based on its instructions and the `greeting_agent`'s description, delegates the task.
1. `greeting_agent` handles the query, calls its `say_hello` tool, and generates the response.
1. The "What is the weather in New York?" query is *not* delegated and is handled directly by the root agent using its `get_weather` tool.
1. The "Thanks, bye!" query is delegated to the `farewell_agent`, which uses its `say_goodbye` tool.
```python
# @title Interact with the Agent Team
import asyncio # Ensure asyncio is imported
# Ensure the root agent (e.g., 'weather_agent_team' or 'root_agent' from the previous cell) is defined.
# Ensure the call_agent_async function is defined.
# Check if the root agent variable exists before defining the conversation function
root_agent_var_name = 'root_agent' # Default name from Step 3 guide
if 'weather_agent_team' in globals(): # Check if user used this name instead
root_agent_var_name = 'weather_agent_team'
elif 'root_agent' not in globals():
print("⚠️ Root agent ('root_agent' or 'weather_agent_team') not found. Cannot define run_team_conversation.")
# Assign a dummy value to prevent NameError later if the code block runs anyway
root_agent = None # Or set a flag to prevent execution
# Only define and run if the root agent exists
if root_agent_var_name in globals() and globals()[root_agent_var_name]:
# Define the main async function for the conversation logic.
# The 'await' keywords INSIDE this function are necessary for async operations.
async def run_team_conversation():
print("\n--- Testing Agent Team Delegation ---")
session_service = InMemorySessionService()
APP_NAME = "weather_tutorial_agent_team"
USER_ID = "user_1_agent_team"
SESSION_ID = "session_001_agent_team"
session = await session_service.create_session(
app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID
)
print(f"Session created: App='{APP_NAME}', User='{USER_ID}', Session='{SESSION_ID}'")
actual_root_agent = globals()[root_agent_var_name]
runner_agent_team = Runner( # Or use InMemoryRunner
agent=actual_root_agent,
app_name=APP_NAME,
session_service=session_service
)
print(f"Runner created for agent '{actual_root_agent.name}'.")
# --- Interactions using await (correct within async def) ---
await call_agent_async(query = "Hello there!",
runner=runner_agent_team,
user_id=USER_ID,
session_id=SESSION_ID)
await call_agent_async(query = "What is the weather in New York?",
runner=runner_agent_team,
user_id=USER_ID,
session_id=SESSION_ID)
await call_agent_async(query = "Thanks, bye!",
runner=runner_agent_team,
user_id=USER_ID,
session_id=SESSION_ID)
# --- Execute the `run_team_conversation` async function ---
# Choose ONE of the methods below based on your environment.
# Note: This may require API keys for the models used!
# METHOD 1: Direct await (Default for Notebooks/Async REPLs)
# If your environment supports top-level await (like Colab/Jupyter notebooks),
# it means an event loop is already running, so you can directly await the function.
print("Attempting execution using 'await' (default for notebooks)...")
await run_team_conversation()
# METHOD 2: asyncio.run (For Standard Python Scripts [.py])
# If running this code as a standard Python script from your terminal,
# the script context is synchronous. `asyncio.run()` is needed to
# create and manage an event loop to execute your async function.
# To use this method:
# 1. Comment out the `await run_team_conversation()` line above.
# 2. Uncomment the following block:
"""
import asyncio
if __name__ == "__main__": # Ensures this runs only when script is executed directly
print("Executing using 'asyncio.run()' (for standard Python scripts)...")
try:
# This creates an event loop, runs your async function, and closes the loop.
asyncio.run(run_team_conversation())
except Exception as e:
print(f"An error occurred: {e}")
"""
else:
# This message prints if the root agent variable wasn't found earlier
print("\n⚠️ Skipping agent team conversation execution as the root agent was not successfully defined in a previous step.")
```
______________________________________________________________________
Look closely at the output logs, especially the `--- Tool: ... called ---` messages. You should observe:
- For "Hello there!", the `say_hello` tool was called (indicating `greeting_agent` handled it).
- For "What is the weather in New York?", the `get_weather` tool was called (indicating the root agent handled it).
- For "Thanks, bye!", the `say_goodbye` tool was called (indicating `farewell_agent` handled it).
This confirms successful **automatic delegation**! The root agent, guided by its instructions and the `description`s of its `sub_agents`, correctly routed user requests to the appropriate specialist agent within the team.
You've now structured your application with multiple collaborating agents. This modular design is fundamental for building more complex and capable agent systems. In the next step, we'll give our agents the ability to remember information across turns using session state.
## Step 4: Adding Memory and Personalization with Session State
So far, our agent team can handle different tasks through delegation, but each interaction starts fresh – the agents have no memory of past conversations or user preferences within a session. To create more sophisticated and context-aware experiences, agents need **memory**. ADK provides this through **Session State**.
**What is Session State?**
- It's a Python dictionary (`session.state`) tied to a specific user session (identified by `APP_NAME`, `USER_ID`, `SESSION_ID`).
- It persists information *across multiple conversational turns* within that session.
- Agents and Tools can read from and write to this state, allowing them to remember details, adapt behavior, and personalize responses.
**How Agents Interact with State:**
1. **`ToolContext` (Primary Method):** Tools can accept a `ToolContext` object (automatically provided by ADK for any parameter annotated `ToolContext`, whatever its position). This object gives direct access to the session state via `tool_context.state`, allowing tools to read preferences or save results *during* execution.
1. **`output_key` (Auto-Save Agent Response):** An `Agent` can be configured with an `output_key="your_key"`. ADK will then automatically save the agent's final textual response for a turn into `session.state["your_key"]`.
**In this step, we will enhance our Weather Bot team by:**
1. Using a **new** `InMemorySessionService` to demonstrate state in isolation.
1. Initializing session state with a user preference for `temperature_unit`.
1. Creating a state-aware version of the weather tool (`get_weather_stateful`) that reads this preference via `ToolContext` and adjusts its output format (Celsius/Fahrenheit).
1. Updating the root agent to use this stateful tool and configuring it with an `output_key` to automatically save its final weather report to the session state.
1. Running a conversation to observe how the initial state affects the tool, how manual state changes alter subsequent behavior, and how `output_key` persists the agent's response.
______________________________________________________________________
**1. Initialize New Session Service and State**
To clearly demonstrate state management without interference from prior steps, we'll instantiate a new `InMemorySessionService`. We'll also create a session with an initial state defining the user's preferred temperature unit.
```python
# @title 1. Initialize New Session Service and State
# Import necessary session components
from google.adk.sessions import InMemorySessionService
# Create a NEW session service instance for this state demonstration
session_service_stateful = InMemorySessionService()
print("✅ New InMemorySessionService created for state demonstration.")
# Define a NEW session ID for this part of the tutorial
SESSION_ID_STATEFUL = "session_state_demo_001"
USER_ID_STATEFUL = "user_state_demo"
# Define initial state data - user prefers Celsius initially
initial_state = {
"user_preference_temperature_unit": "Celsius"
}
# Create the session, providing the initial state
session_stateful = await session_service_stateful.create_session(
app_name=APP_NAME, # Use the consistent app name
user_id=USER_ID_STATEFUL,
session_id=SESSION_ID_STATEFUL,
state=initial_state # <<< Initialize state during creation
)
print(f"✅ Session '{SESSION_ID_STATEFUL}' created for user '{USER_ID_STATEFUL}'.")
# Verify the initial state was set correctly
retrieved_session = await session_service_stateful.get_session(app_name=APP_NAME,
user_id=USER_ID_STATEFUL,
session_id = SESSION_ID_STATEFUL)
print("\n--- Initial Session State ---")
if retrieved_session:
print(retrieved_session.state)
else:
print("Error: Could not retrieve session.")
```
______________________________________________________________________
**2. Create State-Aware Weather Tool (`get_weather_stateful`)**
Now, we create a new version of the weather tool. Its key feature is accepting `tool_context: ToolContext` which allows it to access `tool_context.state`. It will read the `user_preference_temperature_unit` and format the temperature accordingly.
- **Key Concept: `ToolContext`** This object is the bridge allowing your tool logic to interact with the session's context, including reading and writing state variables. ADK finds the parameter by its `ToolContext` annotation and injects it automatically, so it can sit at any position in your tool function's signature. The annotated parameter is also hidden from the schema the LLM sees.
- **Best Practice:** When reading from state, use `dictionary.get('key', default_value)` to handle cases where the key might not exist yet, ensuring your tool doesn't crash.
```python
from google.adk.tools.tool_context import ToolContext
def get_weather_stateful(city: str, tool_context: ToolContext) -> dict:
"""Retrieves weather, converts temp unit based on session state."""
print(f"--- Tool: get_weather_stateful called for {city} ---")
# --- Read preference from state ---
preferred_unit = tool_context.state.get("user_preference_temperature_unit", "Celsius") # Default to Celsius
print(f"--- Tool: Reading state 'user_preference_temperature_unit': {preferred_unit} ---")
city_normalized = city.lower().replace(" ", "")
# Mock weather data (always stored in Celsius internally)
mock_weather_db = {
"newyork": {"temp_c": 25, "condition": "sunny"},
"london": {"temp_c": 15, "condition": "cloudy"},
"tokyo": {"temp_c": 18, "condition": "light rain"},
}
if city_normalized in mock_weather_db:
data = mock_weather_db[city_normalized]
temp_c = data["temp_c"]
condition = data["condition"]
# Format temperature based on state preference
if preferred_unit == "Fahrenheit":
temp_value = (temp_c * 9/5) + 32 # Calculate Fahrenheit
temp_unit = "°F"
else: # Default to Celsius
temp_value = temp_c
temp_unit = "°C"
report = f"The weather in {city.capitalize()} is {condition} with a temperature of {temp_value:.0f}{temp_unit}."
result = {"status": "success", "report": report}
print(f"--- Tool: Generated report in {preferred_unit}. Result: {result} ---")
# Example of writing back to state (optional for this tool)
tool_context.state["last_city_checked_stateful"] = city
print(f"--- Tool: Updated state 'last_city_checked_stateful': {city} ---")
return result
else:
# Handle city not found
error_msg = f"Sorry, I don't have weather information for '{city}'."
print(f"--- Tool: City '{city}' not found. ---")
return {"status": "error", "error_message": error_msg}
print("✅ State-aware 'get_weather_stateful' tool defined.")
```
______________________________________________________________________
**3. Redefine Sub-Agents and Update Root Agent**
To ensure this step is self-contained and builds correctly, we first redefine the `greeting_agent` and `farewell_agent` exactly as they were in Step 3. Then, we define our new root agent (`weather_agent_v4_stateful`):
- It uses the new `get_weather_stateful` tool.
- It includes the greeting and farewell sub-agents for delegation.
- **Crucially**, it sets `output_key="last_weather_report"` which automatically saves its final weather response to the session state.
```python
# @title 3. Redefine Sub-Agents and Update Root Agent with output_key
# Ensure necessary imports: Agent, LiteLlm, Runner
from google.adk.agents import Agent
from google.adk.models.lite_llm import LiteLlm
from google.adk.runners import Runner
# Ensure tools 'say_hello', 'say_goodbye' are defined (from Step 3)
# Ensure model constants MODEL_GPT_4O, MODEL_GEMINI_FLASH etc. are defined
# --- Redefine Greeting Agent (from Step 3) ---
greeting_agent = None
try:
greeting_agent = Agent(
model=MODEL_GEMINI_FLASH,
name="greeting_agent",
instruction="You are the Greeting Agent. Your ONLY task is to provide a friendly greeting using the 'say_hello' tool. Do nothing else.",
description="Handles simple greetings and hellos using the 'say_hello' tool.",
tools=[say_hello],
)
print(f"✅ Agent '{greeting_agent.name}' redefined.")
except Exception as e:
print(f"❌ Could not redefine Greeting agent. Error: {e}")
# --- Redefine Farewell Agent (from Step 3) ---
farewell_agent = None
try:
farewell_agent = Agent(
model=MODEL_GEMINI_FLASH,
name="farewell_agent",
instruction="You are the Farewell Agent. Your ONLY task is to provide a polite goodbye message using the 'say_goodbye' tool. Do not perform any other actions.",
description="Handles simple farewells and goodbyes using the 'say_goodbye' tool.",
tools=[say_goodbye],
)
print(f"✅ Agent '{farewell_agent.name}' redefined.")
except Exception as e:
print(f"❌ Could not redefine Farewell agent. Error: {e}")
# --- Define the Updated Root Agent ---
root_agent_stateful = None
runner_root_stateful = None # Initialize runner
# Check prerequisites before creating the root agent
if greeting_agent and farewell_agent and 'get_weather_stateful' in globals():
root_agent_model = MODEL_GEMINI_FLASH # Choose orchestration model
root_agent_stateful = Agent(
name="weather_agent_v4_stateful", # New version name
model=root_agent_model,
description="Main agent: Provides weather (state-aware unit), delegates greetings/farewells, saves report to state.",
instruction="You are the main Weather Agent. Your job is to provide weather using 'get_weather_stateful'. "
"The tool will format the temperature based on user preference stored in state. "
"Delegate simple greetings to 'greeting_agent' and farewells to 'farewell_agent'. "
"Handle only weather requests, greetings, and farewells.",
tools=[get_weather_stateful], # Use the state-aware tool
sub_agents=[greeting_agent, farewell_agent], # Include sub-agents
output_key="last_weather_report" # <<< Auto-save agent's final weather response
)
print(f"✅ Root Agent '{root_agent_stateful.name}' created using stateful tool and output_key.")
# --- Create Runner for this Root Agent & NEW Session Service ---
runner_root_stateful = Runner(
agent=root_agent_stateful,
app_name=APP_NAME,
session_service=session_service_stateful # Use the NEW stateful session service
)
print(f"✅ Runner created for stateful root agent '{runner_root_stateful.agent.name}' using stateful session service.")
else:
print("❌ Cannot create stateful root agent. Prerequisites missing.")
if not greeting_agent: print(" - greeting_agent definition missing.")
if not farewell_agent: print(" - farewell_agent definition missing.")
if 'get_weather_stateful' not in globals(): print(" - get_weather_stateful tool missing.")
```
______________________________________________________________________
**4. Interact and Test State Flow**
Now, let's execute a conversation designed to test the state interactions using the `runner_root_stateful` (associated with our stateful agent and the `session_service_stateful`). We'll use the `call_agent_async` function defined earlier, ensuring we pass the correct runner, user ID (`USER_ID_STATEFUL`), and session ID (`SESSION_ID_STATEFUL`).
The conversation flow will be:
1. **Check weather (London):** The `get_weather_stateful` tool should read the initial "Celsius" preference from the session state initialized in Section 1. The root agent's final response (the weather report in Celsius) should get saved to `state['last_weather_report']` via the `output_key` configuration.
1. **Manually update state:** We will *directly modify* the state stored within the `InMemorySessionService` instance (`session_service_stateful`).
- **Why direct modification?** The `session_service.get_session()` method returns a *copy* of the session. Modifying that copy wouldn't affect the state used in subsequent agent runs. For this testing scenario with `InMemorySessionService`, we access the internal `sessions` dictionary to change the *actual* stored state value for `user_preference_temperature_unit` to "Fahrenheit". *Note: In real applications, state changes are typically triggered by tools or agent logic returning `EventActions(state_delta=...)`, not direct manual updates.*
1. **Check weather again (New York):** The `get_weather_stateful` tool should now read the updated "Fahrenheit" preference from the state and convert the temperature accordingly. The root agent's *new* response (weather in Fahrenheit) will overwrite the previous value in `state['last_weather_report']` due to the `output_key`.
1. **Greet the agent:** Verify that delegation to the `greeting_agent` still works correctly alongside the stateful operations.
1. **Inspect final state:** After the conversation, we retrieve the session one last time (getting a copy) and print its state to confirm the `user_preference_temperature_unit` is indeed "Fahrenheit", observe the final value saved by `output_key` (which will be the previous weather report in this run), and see the `last_city_checked_stateful` value written by the tool.
```python
# @title 4. Interact to Test State Flow and output_key
import asyncio # Ensure asyncio is imported
# Ensure the stateful runner (runner_root_stateful) is available from the previous cell
# Ensure call_agent_async, USER_ID_STATEFUL, SESSION_ID_STATEFUL, APP_NAME are defined
if 'runner_root_stateful' in globals() and runner_root_stateful:
# Define the main async function for the stateful conversation logic.
# The 'await' keywords INSIDE this function are necessary for async operations.
async def run_stateful_conversation():
print("\n--- Testing State: Temp Unit Conversion & output_key ---")
# 1. Check weather (Uses initial state: Celsius)
print("--- Turn 1: Requesting weather in London (expect Celsius) ---")
await call_agent_async(query= "What's the weather in London?",
runner=runner_root_stateful,
user_id=USER_ID_STATEFUL,
session_id=SESSION_ID_STATEFUL
)
# 2. Manually update state preference to Fahrenheit - DIRECTLY MODIFY STORAGE
print("\n--- Manually Updating State: Setting unit to Fahrenheit ---")
try:
# Access the internal storage directly - THIS IS SPECIFIC TO InMemorySessionService for testing
# NOTE: In production with persistent services (Database, VertexAI), you would
# typically update state via agent actions or specific service APIs if available,
# not by direct manipulation of internal storage.
stored_session = session_service_stateful.sessions[APP_NAME][USER_ID_STATEFUL][SESSION_ID_STATEFUL]
stored_session.state["user_preference_temperature_unit"] = "Fahrenheit"
# Optional: You might want to update the timestamp as well if any logic depends on it
# import time
# stored_session.last_update_time = time.time()
print(f"--- Stored session state updated. Current 'user_preference_temperature_unit': {stored_session.state.get('user_preference_temperature_unit', 'Not Set')} ---") # Added .get for safety
except KeyError:
print(f"--- Error: Could not retrieve session '{SESSION_ID_STATEFUL}' from internal storage for user '{USER_ID_STATEFUL}' in app '{APP_NAME}' to update state. Check IDs and if session was created. ---")
except Exception as e:
print(f"--- Error updating internal session state: {e} ---")
# 3. Check weather again (Tool should now use Fahrenheit)
# This will also update 'last_weather_report' via output_key
print("\n--- Turn 2: Requesting weather in New York (expect Fahrenheit) ---")
await call_agent_async(query= "Tell me the weather in New York.",
runner=runner_root_stateful,
user_id=USER_ID_STATEFUL,
session_id=SESSION_ID_STATEFUL
)
# 4. Test basic delegation (should still work)
# The greeting is authored by the delegated sub-agent, not the root agent,
# so output_key does NOT fire: 'last_weather_report' keeps the NY report.
print("\n--- Turn 3: Sending a greeting ---")
await call_agent_async(query= "Hi!",
runner=runner_root_stateful,
user_id=USER_ID_STATEFUL,
session_id=SESSION_ID_STATEFUL
)
# --- Execute the `run_stateful_conversation` async function ---
# Choose ONE of the methods below based on your environment.
# METHOD 1: Direct await (Default for Notebooks/Async REPLs)
# If your environment supports top-level await (like Colab/Jupyter notebooks),
# it means an event loop is already running, so you can directly await the function.
print("Attempting execution using 'await' (default for notebooks)...")
await run_stateful_conversation()
# METHOD 2: asyncio.run (For Standard Python Scripts [.py])
# If running this code as a standard Python script from your terminal,
# the script context is synchronous. `asyncio.run()` is needed to
# create and manage an event loop to execute your async function.
# To use this method:
# 1. Comment out the `await run_stateful_conversation()` line above.
# 2. Uncomment the following block:
"""
import asyncio
if __name__ == "__main__": # Ensures this runs only when script is executed directly
print("Executing using 'asyncio.run()' (for standard Python scripts)...")
try:
# This creates an event loop, runs your async function, and closes the loop.
asyncio.run(run_stateful_conversation())
except Exception as e:
print(f"An error occurred: {e}")
"""
# --- Inspect final session state after the conversation ---
# This block runs after either execution method completes.
print("\n--- Inspecting Final Session State ---")
final_session = await session_service_stateful.get_session(app_name=APP_NAME,
user_id= USER_ID_STATEFUL,
session_id=SESSION_ID_STATEFUL)
if final_session:
# Use .get() for safer access to potentially missing keys
print(f"Final Preference: {final_session.state.get('user_preference_temperature_unit', 'Not Set')}")
print(f"Final Last Weather Report (from output_key): {final_session.state.get('last_weather_report', 'Not Set')}")
print(f"Final Last City Checked (by tool): {final_session.state.get('last_city_checked_stateful', 'Not Set')}")
# Print full state for detailed view
# print(f"Full State Dict: {final_session.state}") # For detailed view
else:
print("\n❌ Error: Could not retrieve final session state.")
else:
print("\n⚠️ Skipping state test conversation. Stateful root agent runner ('runner_root_stateful') is not available.")
```
______________________________________________________________________
By reviewing the conversation flow and the final session state printout, you can confirm:
- **State Read:** The weather tool (`get_weather_stateful`) correctly read `user_preference_temperature_unit` from state, initially using "Celsius" for London.
- **State Update:** The direct modification successfully changed the stored preference to "Fahrenheit".
- **State Read (Updated):** The tool subsequently read "Fahrenheit" when asked for New York's weather and performed the conversion.
- **Tool State Write:** The tool successfully wrote the `last_city_checked_stateful` ("New York" after the second weather check) into the state via `tool_context.state`.
- **Delegation:** The delegation to the `greeting_agent` for "Hi!" functioned correctly even after state modifications.
- **`output_key`:** The `output_key="last_weather_report"` successfully saved the root agent's *final* response for *each turn* where the root agent was the one ultimately responding. In this sequence, the final greeting ("Hello, there!") was generated by the delegated sub-agent rather than the root agent, so `output_key` was not triggered on that final turn, leaving the previous weather report intact in the session state.
- **Final State:** The final check confirms the preference persisted as "Fahrenheit".
You've now successfully integrated session state to personalize agent behavior using `ToolContext`, manually manipulated state for testing `InMemorySessionService`, and observed how `output_key` provides a simple mechanism for saving the agent's last response to state. This foundational understanding of state management is key as we proceed to implement safety guardrails using callbacks in the next steps.
______________________________________________________________________
## Step 5: Adding Safety - Input Guardrail with `before_model_callback`
Our agent team is becoming more capable, remembering preferences and using tools effectively. However, in real-world scenarios, we often need safety mechanisms to control the agent's behavior *before* potentially problematic requests even reach the core Large Language Model (LLM).
ADK provides **Callbacks** – functions that allow you to hook into specific points in the agent's execution lifecycle. The `before_model_callback` is particularly useful for input safety.
**What is `before_model_callback`?**
- It's a Python function you define that ADK executes *just before* an agent sends its compiled request (including conversation history, instructions, and the latest user message) to the underlying LLM.
- **Purpose:** Inspect the request, modify it if necessary, or block it entirely based on predefined rules.
**Common Use Cases:**
- **Input Validation/Filtering:** Check if user input meets criteria or contains disallowed content (like PII or keywords).
- **Guardrails:** Prevent harmful, off-topic, or policy-violating requests from being processed by the LLM.
- **Dynamic Prompt Modification:** Add timely information (e.g., from session state) to the LLM request context just before sending.
**How it Works:**
1. Define a function accepting `callback_context: CallbackContext` and `llm_request: LlmRequest`.
- `callback_context`: Provides access to agent info, session state (`callback_context.state`), etc.
- `llm_request`: Contains the full payload intended for the LLM (`contents`, `config`).
1. Inside the function:
- **Inspect:** Examine `llm_request.contents` (especially the last user message).
- **Modify (Use Caution):** You *can* change parts of `llm_request`.
- **Block (Guardrail):** Return an `LlmResponse` object. ADK will send this response back immediately, *skipping* the LLM call for that turn.
- **Allow:** Return `None`. ADK proceeds to call the LLM with the (potentially modified) request.
**In this step, we will:**
1. Define a `before_model_callback` function (`block_keyword_guardrail`) that checks the user's input for a specific keyword ("BLOCK").
1. Update our stateful root agent (`weather_agent_v4_stateful` from Step 4) to use this callback.
1. Create a new runner associated with this updated agent but using the *same stateful session service* to maintain state continuity.
1. Test the guardrail by sending both normal and keyword-containing requests.
______________________________________________________________________
**1. Define the Guardrail Callback Function**
This function will inspect the last user message within the `llm_request` content. If it finds "BLOCK" (case-insensitive), it constructs and returns an `LlmResponse` to block the flow; otherwise, it returns `None`.
```python
# @title 1. Define the before_model_callback Guardrail
# Ensure necessary imports are available
from google.adk.agents.callback_context import CallbackContext
from google.adk.models.llm_request import LlmRequest
from google.adk.models.llm_response import LlmResponse
from google.genai import types # For creating response content
from typing import Optional
def block_keyword_guardrail(
callback_context: CallbackContext, llm_request: LlmRequest
) -> Optional[LlmResponse]:
"""
Inspects the latest user message for 'BLOCK'. If found, blocks the LLM call
and returns a predefined LlmResponse. Otherwise, returns None to proceed.
"""
agent_name = callback_context.agent_name # Get the name of the agent whose model call is being intercepted
print(f"--- Callback: block_keyword_guardrail running for agent: {agent_name} ---")
# Extract the text from the latest user message in the request history
last_user_message_text = ""
if llm_request.contents:
# Find the most recent message with role 'user'
for content in reversed(llm_request.contents):
if content.role == 'user' and content.parts:
# Assuming text is in the first part for simplicity
if content.parts[0].text:
last_user_message_text = content.parts[0].text
break # Found the last user message text
print(f"--- Callback: Inspecting last user message: '{last_user_message_text[:100]}...' ---") # Log first 100 chars
# --- Guardrail Logic ---
keyword_to_block = "BLOCK"
if keyword_to_block in last_user_message_text.upper(): # Case-insensitive check
print(f"--- Callback: Found '{keyword_to_block}'. Blocking LLM call! ---")
# Optionally, set a flag in state to record the block event
callback_context.state["guardrail_block_keyword_triggered"] = True
print(f"--- Callback: Set state 'guardrail_block_keyword_triggered': True ---")
# Construct and return an LlmResponse to stop the flow and send this back instead
return LlmResponse(
content=types.Content(
role="model", # Mimic a response from the agent's perspective
parts=[types.Part(text=f"I cannot process this request because it contains the blocked keyword '{keyword_to_block}'.")],
)
# Note: You could also set an error_message field here if needed
)
else:
# Keyword not found, allow the request to proceed to the LLM
print(f"--- Callback: Keyword not found. Allowing LLM call for {agent_name}. ---")
return None # Returning None signals ADK to continue normally
print("✅ block_keyword_guardrail function defined.")
```
______________________________________________________________________
**2. Update Root Agent to Use the Callback**
We redefine the root agent, adding the `before_model_callback` parameter and pointing it to our new guardrail function. We'll give it a new version name for clarity.
*Important:* We need to redefine the sub-agents (`greeting_agent`, `farewell_agent`) and the stateful tool (`get_weather_stateful`) within this context if they are not already available from previous steps, ensuring the root agent definition has access to all its components.
```python
# @title 2. Update Root Agent with before_model_callback
# --- Redefine Sub-Agents (Ensures they exist in this context) ---
greeting_agent = None
try:
# Use a defined model constant
greeting_agent = Agent(
model=MODEL_GEMINI_FLASH,
name="greeting_agent", # Keep original name for consistency
instruction="You are the Greeting Agent. Your ONLY task is to provide a friendly greeting using the 'say_hello' tool. Do nothing else.",
description="Handles simple greetings and hellos using the 'say_hello' tool.",
tools=[say_hello],
)
print(f"✅ Sub-Agent '{greeting_agent.name}' redefined.")
except Exception as e:
print(f"❌ Could not redefine Greeting agent. Check Model/API Key ({greeting_agent.model}). Error: {e}")
farewell_agent = None
try:
# Use a defined model constant
farewell_agent = Agent(
model=MODEL_GEMINI_FLASH,
name="farewell_agent", # Keep original name
instruction="You are the Farewell Agent. Your ONLY task is to provide a polite goodbye message using the 'say_goodbye' tool. Do not perform any other actions.",
description="Handles simple farewells and goodbyes using the 'say_goodbye' tool.",
tools=[say_goodbye],
)
print(f"✅ Sub-Agent '{farewell_agent.name}' redefined.")
except Exception as e:
print(f"❌ Could not redefine Farewell agent. Check Model/API Key ({farewell_agent.model}). Error: {e}")
# --- Define the Root Agent with the Callback ---
root_agent_model_guardrail = None
runner_root_model_guardrail = None
# Check all components before proceeding
if greeting_agent and farewell_agent and 'get_weather_stateful' in globals() and 'block_keyword_guardrail' in globals():
# Use a defined model constant
root_agent_model = MODEL_GEMINI_FLASH
root_agent_model_guardrail = Agent(
name="weather_agent_v5_model_guardrail", # New version name for clarity
model=root_agent_model,
description="Main agent: Handles weather, delegates greetings/farewells, includes input keyword guardrail.",
instruction="You are the main Weather Agent. Provide weather using 'get_weather_stateful'. "
"Delegate simple greetings to 'greeting_agent' and farewells to 'farewell_agent'. "
"Handle only weather requests, greetings, and farewells.",
tools=[get_weather_stateful],
sub_agents=[greeting_agent, farewell_agent], # Reference the redefined sub-agents
output_key="last_weather_report", # Keep output_key from Step 4
before_model_callback=block_keyword_guardrail # <<< Assign the guardrail callback
)
print(f"✅ Root Agent '{root_agent_model_guardrail.name}' created with before_model_callback.")
# --- Create Runner for this Agent, Using SAME Stateful Session Service ---
# Ensure session_service_stateful exists from Step 4
if 'session_service_stateful' in globals():
runner_root_model_guardrail = Runner(
agent=root_agent_model_guardrail,
app_name=APP_NAME, # Use consistent APP_NAME
session_service=session_service_stateful # <<< Use the service from Step 4
)
print(f"✅ Runner created for guardrail agent '{runner_root_model_guardrail.agent.name}', using stateful session service.")
else:
print("❌ Cannot create runner. 'session_service_stateful' from Step 4 is missing.")
else:
print("❌ Cannot create root agent with model guardrail. One or more prerequisites are missing or failed initialization:")
if not greeting_agent: print(" - Greeting Agent")
if not farewell_agent: print(" - Farewell Agent")
if 'get_weather_stateful' not in globals(): print(" - 'get_weather_stateful' tool")
if 'block_keyword_guardrail' not in globals(): print(" - 'block_keyword_guardrail' callback")
```
______________________________________________________________________
**3. Interact to Test the Guardrail**
Let's test the guardrail's behavior. We'll use the *same session* (`SESSION_ID_STATEFUL`) as in Step 4 to show that state persists across these changes.
1. Send a normal weather request (should pass the guardrail and execute).
1. Send a request containing "BLOCK" (should be intercepted by the callback).
1. Send a greeting (should pass the root agent's guardrail, be delegated, and execute normally).
```python
# @title 3. Interact to Test the Model Input Guardrail
import asyncio # Ensure asyncio is imported
# Ensure the runner for the guardrail agent is available
if 'runner_root_model_guardrail' in globals() and runner_root_model_guardrail:
# Define the main async function for the guardrail test conversation.
# The 'await' keywords INSIDE this function are necessary for async operations.
async def run_guardrail_test_conversation():
print("\n--- Testing Model Input Guardrail ---")
# Use the runner for the agent with the callback and the existing stateful session ID
# Define a helper lambda for cleaner interaction calls
interaction_func = lambda query: call_agent_async(query,
runner_root_model_guardrail,
USER_ID_STATEFUL, # Use existing user ID
SESSION_ID_STATEFUL # Use existing session ID
)
# 1. Normal request (Callback allows, should use Fahrenheit from previous state change)
print("--- Turn 1: Requesting weather in London (expect allowed, Fahrenheit) ---")
await interaction_func("What is the weather in London?")
# 2. Request containing the blocked keyword (Callback intercepts)
print("\n--- Turn 2: Requesting with blocked keyword (expect blocked) ---")
await interaction_func("BLOCK the request for weather in Tokyo") # Callback should catch "BLOCK"
# 3. Normal greeting (Callback allows root agent, delegation happens)
print("\n--- Turn 3: Sending a greeting (expect allowed) ---")
await interaction_func("Hello again")
# --- Execute the `run_guardrail_test_conversation` async function ---
# Choose ONE of the methods below based on your environment.
# METHOD 1: Direct await (Default for Notebooks/Async REPLs)
# If your environment supports top-level await (like Colab/Jupyter notebooks),
# it means an event loop is already running, so you can directly await the function.
print("Attempting execution using 'await' (default for notebooks)...")
await run_guardrail_test_conversation()
# METHOD 2: asyncio.run (For Standard Python Scripts [.py])
# If running this code as a standard Python script from your terminal,
# the script context is synchronous. `asyncio.run()` is needed to
# create and manage an event loop to execute your async function.
# To use this method:
# 1. Comment out the `await run_guardrail_test_conversation()` line above.
# 2. Uncomment the following block:
"""
import asyncio
if __name__ == "__main__": # Ensures this runs only when script is executed directly
print("Executing using 'asyncio.run()' (for standard Python scripts)...")
try:
# This creates an event loop, runs your async function, and closes the loop.
asyncio.run(run_guardrail_test_conversation())
except Exception as e:
print(f"An error occurred: {e}")
"""
# --- Inspect final session state after the conversation ---
# This block runs after either execution method completes.
# Optional: Check state for the trigger flag set by the callback
print("\n--- Inspecting Final Session State (After Guardrail Test) ---")
# Use the session service instance associated with this stateful session
final_session = await session_service_stateful.get_session(app_name=APP_NAME,
user_id=USER_ID_STATEFUL,
session_id=SESSION_ID_STATEFUL)
if final_session:
# Use .get() for safer access
print(f"Guardrail Triggered Flag: {final_session.state.get('guardrail_block_keyword_triggered', 'Not Set (or False)')}")
print(f"Last Weather Report: {final_session.state.get('last_weather_report', 'Not Set')}") # Should be London weather if successful
print(f"Temperature Unit: {final_session.state.get('user_preference_temperature_unit', 'Not Set')}") # Should be Fahrenheit
# print(f"Full State Dict: {final_session.state}") # For detailed view
else:
print("\n❌ Error: Could not retrieve final session state.")
else:
print("\n⚠️ Skipping model guardrail test. Runner ('runner_root_model_guardrail') is not available.")
```
______________________________________________________________________
Observe the execution flow:
1. **London Weather:** The callback runs for `weather_agent_v5_model_guardrail`, inspects the message, prints "Keyword not found. Allowing LLM call.", and returns `None`. The agent proceeds, calls the `get_weather_stateful` tool (which uses the "Fahrenheit" preference from Step 4's state change), and returns the weather. This response updates `last_weather_report` via `output_key`.
1. **BLOCK Request:** The callback runs again for `weather_agent_v5_model_guardrail`, inspects the message, finds "BLOCK", prints "Blocking LLM call!", sets the state flag, and returns the predefined `LlmResponse`. The agent's underlying LLM is *never called* for this turn. The user sees the callback's blocking message.
1. **Hello Again:** The callback runs for `weather_agent_v5_model_guardrail`, allows the request. The root agent then delegates to `greeting_agent`. *Note: The `before_model_callback` defined on the root agent does NOT automatically apply to sub-agents.* The `greeting_agent` proceeds normally, calls its `say_hello` tool, and returns the greeting.
You have successfully implemented an input safety layer! The `before_model_callback` provides a powerful mechanism to enforce rules and control agent behavior *before* expensive or potentially risky LLM calls are made. Next, we'll apply a similar concept to add guardrails around tool usage itself.
## Step 6: Adding Safety - Tool Argument Guardrail (`before_tool_callback`)
In Step 5, we added a guardrail to inspect and potentially block user input *before* it reached the LLM. Now, we'll add another layer of control *after* the LLM has decided to use a tool but *before* that tool actually executes. This is useful for validating the *arguments* the LLM wants to pass to the tool.
ADK provides the `before_tool_callback` for this precise purpose.
**What is `before_tool_callback`?**
- It's a Python function executed just *before* a specific tool function runs, after the LLM has requested its use and decided on the arguments.
- **Purpose:** Validate tool arguments, prevent tool execution based on specific inputs, modify arguments dynamically, or enforce resource usage policies.
**Common Use Cases:**
- **Argument Validation:** Check if arguments provided by the LLM are valid, within allowed ranges, or conform to expected formats.
- **Resource Protection:** Prevent tools from being called with inputs that might be costly, access restricted data, or cause unwanted side effects (e.g., blocking API calls for certain parameters).
- **Dynamic Argument Modification:** Adjust arguments based on session state or other contextual information before the tool runs.
**How it Works:**
1. Define a function accepting `tool: BaseTool`, `args: Dict[str, Any]`, and `tool_context: ToolContext`.
- `tool`: The tool object about to be called (inspect `tool.name`).
- `args`: The dictionary of arguments the LLM generated for the tool.
- `tool_context`: Provides access to session state (`tool_context.state`), agent info, etc.
1. Inside the function:
- **Inspect:** Examine the `tool.name` and the `args` dictionary.
- **Modify:** Change values within the `args` dictionary *directly*. If you return `None`, the tool runs with these modified args.
- **Block/Override (Guardrail):** Return a **dictionary**. ADK treats this dictionary as the *result* of the tool call, completely *skipping* the execution of the original tool function. The dictionary should ideally match the expected return format of the tool it's blocking.
- **Allow:** Return `None`. ADK proceeds to execute the actual tool function with the (potentially modified) arguments.
**In this step, we will:**
1. Define a `before_tool_callback` function (`block_paris_tool_guardrail`) that specifically checks if the `get_weather_stateful` tool is called with the city "Paris".
1. If "Paris" is detected, the callback will block the tool and return a custom error dictionary.
1. Update our root agent (`weather_agent_v6_tool_guardrail`) to include *both* the `before_model_callback` and this new `before_tool_callback`.
1. Create a new runner for this agent, using the same stateful session service.
1. Test the flow by requesting weather for allowed cities and the blocked city ("Paris").
______________________________________________________________________
**1. Define the Tool Guardrail Callback Function**
This function targets the `get_weather_stateful` tool. It checks the `city` argument. If it's "Paris", it returns an error dictionary that looks like the tool's own error response. Otherwise, it allows the tool to run by returning `None`.
```python
# @title 1. Define the before_tool_callback Guardrail
# Ensure necessary imports are available
from google.adk.tools.base_tool import BaseTool
from google.adk.tools.tool_context import ToolContext
from typing import Optional, Dict, Any # For type hints
def block_paris_tool_guardrail(
tool: BaseTool, args: Dict[str, Any], tool_context: ToolContext
) -> Optional[Dict]:
"""
Checks if 'get_weather_stateful' is called for 'Paris'.
If so, blocks the tool execution and returns a specific error dictionary.
Otherwise, allows the tool call to proceed by returning None.
"""
tool_name = tool.name
agent_name = tool_context.agent_name # Agent attempting the tool call
print(f"--- Callback: block_paris_tool_guardrail running for tool '{tool_name}' in agent '{agent_name}' ---")
print(f"--- Callback: Inspecting args: {args} ---")
# --- Guardrail Logic ---
target_tool_name = "get_weather_stateful" # Match the function name used by FunctionTool
blocked_city = "paris"
# Check if it's the correct tool and the city argument matches the blocked city
if tool_name == target_tool_name:
city_argument = args.get("city", "") # Safely get the 'city' argument
if city_argument and city_argument.lower() == blocked_city:
print(f"--- Callback: Detected blocked city '{city_argument}'. Blocking tool execution! ---")
# Optionally update state
tool_context.state["guardrail_tool_block_triggered"] = True
print(f"--- Callback: Set state 'guardrail_tool_block_triggered': True ---")
# Return a dictionary matching the tool's expected output format for errors
# This dictionary becomes the tool's result, skipping the actual tool run.
return {
"status": "error",
"error_message": f"Policy restriction: Weather checks for '{city_argument.capitalize()}' are currently disabled by a tool guardrail."
}
else:
print(f"--- Callback: City '{city_argument}' is allowed for tool '{tool_name}'. ---")
else:
print(f"--- Callback: Tool '{tool_name}' is not the target tool. Allowing. ---")
# If the checks above didn't return a dictionary, allow the tool to execute
print(f"--- Callback: Allowing tool '{tool_name}' to proceed. ---")
return None # Returning None allows the actual tool function to run
print("✅ block_paris_tool_guardrail function defined.")
```
______________________________________________________________________
**2. Update Root Agent to Use Both Callbacks**
We redefine the root agent again (`weather_agent_v6_tool_guardrail`), this time adding the `before_tool_callback` parameter alongside the `before_model_callback` from Step 5.
*Self-Contained Execution Note:* Similar to Step 5, ensure all prerequisites (sub-agents, tools, `before_model_callback`) are defined or available in the execution context before defining this agent.
```python
# @title 2. Update Root Agent with BOTH Callbacks (Self-Contained)
# --- Ensure Prerequisites are Defined ---
# (Include or ensure execution of definitions for: Agent, LiteLlm, Runner, ToolContext,
# MODEL constants, say_hello, say_goodbye, greeting_agent, farewell_agent,
# get_weather_stateful, block_keyword_guardrail, block_paris_tool_guardrail)
# --- Redefine Sub-Agents (Ensures they exist in this context) ---
greeting_agent = None
try:
# Use a defined model constant
greeting_agent = Agent(
model=MODEL_GEMINI_FLASH,
name="greeting_agent", # Keep original name for consistency
instruction="You are the Greeting Agent. Your ONLY task is to provide a friendly greeting using the 'say_hello' tool. Do nothing else.",
description="Handles simple greetings and hellos using the 'say_hello' tool.",
tools=[say_hello],
)
print(f"✅ Sub-Agent '{greeting_agent.name}' redefined.")
except Exception as e:
print(f"❌ Could not redefine Greeting agent. Check Model/API Key ({greeting_agent.model}). Error: {e}")
farewell_agent = None
try:
# Use a defined model constant
farewell_agent = Agent(
model=MODEL_GEMINI_FLASH,
name="farewell_agent", # Keep original name
instruction="You are the Farewell Agent. Your ONLY task is to provide a polite goodbye message using the 'say_goodbye' tool. Do not perform any other actions.",
description="Handles simple farewells and goodbyes using the 'say_goodbye' tool.",
tools=[say_goodbye],
)
print(f"✅ Sub-Agent '{farewell_agent.name}' redefined.")
except Exception as e:
print(f"❌ Could not redefine Farewell agent. Check Model/API Key ({farewell_agent.model}). Error: {e}")
# --- Define the Root Agent with Both Callbacks ---
root_agent_tool_guardrail = None
runner_root_tool_guardrail = None
if ('greeting_agent' in globals() and greeting_agent and
'farewell_agent' in globals() and farewell_agent and
'get_weather_stateful' in globals() and
'block_keyword_guardrail' in globals() and
'block_paris_tool_guardrail' in globals()):
root_agent_model = MODEL_GEMINI_FLASH
root_agent_tool_guardrail = Agent(
name="weather_agent_v6_tool_guardrail", # New version name
model=root_agent_model,
description="Main agent: Handles weather, delegates, includes input AND tool guardrails.",
instruction="You are the main Weather Agent. Provide weather using 'get_weather_stateful'. "
"Delegate greetings to 'greeting_agent' and farewells to 'farewell_agent'. "
"Handle only weather, greetings, and farewells.",
tools=[get_weather_stateful],
sub_agents=[greeting_agent, farewell_agent],
output_key="last_weather_report",
before_model_callback=block_keyword_guardrail, # Keep model guardrail
before_tool_callback=block_paris_tool_guardrail # <<< Add tool guardrail
)
print(f"✅ Root Agent '{root_agent_tool_guardrail.name}' created with BOTH callbacks.")
# --- Create Runner, Using SAME Stateful Session Service ---
if 'session_service_stateful' in globals():
runner_root_tool_guardrail = Runner(
agent=root_agent_tool_guardrail,
app_name=APP_NAME,
session_service=session_service_stateful # <<< Use the service from Step 4/5
)
print(f"✅ Runner created for tool guardrail agent '{runner_root_tool_guardrail.agent.name}', using stateful session service.")
else:
print("❌ Cannot create runner. 'session_service_stateful' from Step 4/5 is missing.")
else:
print("❌ Cannot create root agent with tool guardrail. Prerequisites missing.")
```
______________________________________________________________________
**3. Interact to Test the Tool Guardrail**
Let's test the interaction flow, again using the same stateful session (`SESSION_ID_STATEFUL`) from the previous steps.
1. Request weather for "New York": Passes both callbacks, tool executes (using Fahrenheit preference from state).
1. Request weather for "Paris": Passes `before_model_callback`. LLM decides to call `get_weather_stateful(city='Paris')`. `before_tool_callback` intercepts, blocks the tool, and returns the error dictionary. Agent relays this error.
1. Request weather for "London": Passes both callbacks, tool executes normally.
```python
# @title 3. Interact to Test the Tool Argument Guardrail
import asyncio # Ensure asyncio is imported
# Ensure the runner for the tool guardrail agent is available
if 'runner_root_tool_guardrail' in globals() and runner_root_tool_guardrail:
# Define the main async function for the tool guardrail test conversation.
# The 'await' keywords INSIDE this function are necessary for async operations.
async def run_tool_guardrail_test():
print("\n--- Testing Tool Argument Guardrail ('Paris' blocked) ---")
# Use the runner for the agent with both callbacks and the existing stateful session
# Define a helper lambda for cleaner interaction calls
interaction_func = lambda query: call_agent_async(query,
runner_root_tool_guardrail,
USER_ID_STATEFUL, # Use existing user ID
SESSION_ID_STATEFUL # Use existing session ID
)
# 1. Allowed city (Should pass both callbacks, use Fahrenheit state)
print("--- Turn 1: Requesting weather in New York (expect allowed) ---")
await interaction_func("What's the weather in New York?")
# 2. Blocked city (Should pass model callback, but be blocked by tool callback)
print("\n--- Turn 2: Requesting weather in Paris (expect blocked by tool guardrail) ---")
await interaction_func("How about Paris?") # Tool callback should intercept this
# 3. Another allowed city (Should work normally again)
print("\n--- Turn 3: Requesting weather in London (expect allowed) ---")
await interaction_func("Tell me the weather in London.")
# --- Execute the `run_tool_guardrail_test` async function ---
# Choose ONE of the methods below based on your environment.
# METHOD 1: Direct await (Default for Notebooks/Async REPLs)
# If your environment supports top-level await (like Colab/Jupyter notebooks),
# it means an event loop is already running, so you can directly await the function.
print("Attempting execution using 'await' (default for notebooks)...")
await run_tool_guardrail_test()
# METHOD 2: asyncio.run (For Standard Python Scripts [.py])
# If running this code as a standard Python script from your terminal,
# the script context is synchronous. `asyncio.run()` is needed to
# create and manage an event loop to execute your async function.
# To use this method:
# 1. Comment out the `await run_tool_guardrail_test()` line above.
# 2. Uncomment the following block:
"""
import asyncio
if __name__ == "__main__": # Ensures this runs only when script is executed directly
print("Executing using 'asyncio.run()' (for standard Python scripts)...")
try:
# This creates an event loop, runs your async function, and closes the loop.
asyncio.run(run_tool_guardrail_test())
except Exception as e:
print(f"An error occurred: {e}")
"""
# --- Inspect final session state after the conversation ---
# This block runs after either execution method completes.
# Optional: Check state for the tool block trigger flag
print("\n--- Inspecting Final Session State (After Tool Guardrail Test) ---")
# Use the session service instance associated with this stateful session
final_session = await session_service_stateful.get_session(app_name=APP_NAME,
user_id=USER_ID_STATEFUL,
session_id= SESSION_ID_STATEFUL)
if final_session:
# Use .get() for safer access
print(f"Tool Guardrail Triggered Flag: {final_session.state.get('guardrail_tool_block_triggered', 'Not Set (or False)')}")
print(f"Last Weather Report: {final_session.state.get('last_weather_report', 'Not Set')}") # Should be London weather if successful
print(f"Temperature Unit: {final_session.state.get('user_preference_temperature_unit', 'Not Set')}") # Should be Fahrenheit
# print(f"Full State Dict: {final_session.state}") # For detailed view
else:
print("\n❌ Error: Could not retrieve final session state.")
else:
print("\n⚠️ Skipping tool guardrail test. Runner ('runner_root_tool_guardrail') is not available.")
```
______________________________________________________________________
Analyze the output:
1. **New York:** The `before_model_callback` allows the request. The LLM requests `get_weather_stateful`. The `before_tool_callback` runs, inspects the args (`{'city': 'New York'}`), sees it's not "Paris", prints "Allowing tool..." and returns `None`. The actual `get_weather_stateful` function executes, reads "Fahrenheit" from state, and returns the weather report. The agent relays this, and it gets saved via `output_key`.
1. **Paris:** The `before_model_callback` allows the request. The LLM requests `get_weather_stateful(city='Paris')`. The `before_tool_callback` runs, inspects the args, detects "Paris", prints "Blocking tool execution!", sets the state flag, and returns the error dictionary `{'status': 'error', 'error_message': 'Policy restriction...'}`. The actual `get_weather_stateful` function is **never executed**. The agent receives the error dictionary *as if it were the tool's output* and formulates a response based on that error message.
1. **London:** Behaves like New York, passing both callbacks and executing the tool successfully. The new London weather report overwrites the `last_weather_report` in the state.
You've now added a crucial safety layer controlling not just *what* reaches the LLM, but also *how* the agent's tools can be used based on the specific arguments generated by the LLM. Callbacks like `before_model_callback` and `before_tool_callback` are essential for building robust, safe, and policy-compliant agent applications.
______________________________________________________________________
## Conclusion: Your Agent Team is Ready!
Congratulations! You've successfully journeyed from building a single, basic weather agent to constructing a sophisticated, multi-agent team using the Agent Development Kit (ADK).
**Let's recap what you've accomplished:**
- You started with a **fundamental agent** equipped with a single tool (`get_weather`).
- You explored ADK's **multi-model flexibility** using LiteLLM, running the same core logic with different LLMs like Gemini, GPT-4o, and Claude.
- You embraced **modularity** by creating specialized sub-agents (`greeting_agent`, `farewell_agent`) and enabling **automatic delegation** from a root agent.
- You gave your agents **memory** using **Session State**, allowing them to remember user preferences (`temperature_unit`) and past interactions (`output_key`).
- You implemented crucial **safety guardrails** using both `before_model_callback` (blocking specific input keywords) and `before_tool_callback` (blocking tool execution based on arguments like the city "Paris").
Through building this progressive Weather Bot team, you've gained hands-on experience with core ADK concepts essential for developing complex, intelligent applications.
**Key Takeaways:**
- **Agents & Tools:** The fundamental building blocks for defining capabilities and reasoning. Clear instructions and docstrings are paramount.
- **Runners & Session Services:** The engine and memory management system that orchestrate agent execution and maintain conversational context.
- **Delegation:** Designing multi-agent teams allows for specialization, modularity, and better management of complex tasks. Agent `description` is key for auto-flow.
- **Session State (`ToolContext`, `output_key`):** Essential for creating context-aware, personalized, and multi-turn conversational agents.
- **Callbacks (`before_model`, `before_tool`):** Powerful hooks for implementing safety, validation, policy enforcement, and dynamic modifications *before* critical operations (LLM calls or tool execution).
- **Flexibility (`LiteLlm`):** ADK empowers you to choose the best LLM for the job, balancing performance, cost, and features.
**Where to Go Next?**
Your Weather Bot team is a great starting point. Here are some ideas to further explore ADK and enhance your application:
1. **Real Weather API:** Replace the `mock_weather_db` in your `get_weather` tool with a call to a real weather API (like OpenWeatherMap, WeatherAPI).
1. **More Complex State:** Store more user preferences (e.g., preferred location, notification settings) or conversation summaries in the session state.
1. **Refine Delegation:** Experiment with different root agent instructions or sub-agent descriptions to fine-tune the delegation logic. Could you add a "forecast" agent?
1. **Advanced Callbacks:**
- Use `after_model_callback` to potentially reformat or sanitize the LLM's response *after* it's generated.
- Use `after_tool_callback` to process or log the results returned by a tool.
- Implement `before_agent_callback` or `after_agent_callback` for agent-level entry/exit logic.
1. **Error Handling:** Improve how the agent handles tool errors or unexpected API responses. Maybe add retry logic within a tool.
1. **Persistent Session Storage:** Consider changing `InMemorySessionService` to one of ADK's persistent implementations, such as `DatabaseSessionService` which is SQLAlchemy-backed and installed with `pip install google-adk[db]`, or `VertexAiSessionService`. For more information, see the [Session](/sessions/session/) page.
1. **Streaming UI:** Integrate your agent team with a web framework (like FastAPI, as shown in the ADK Streaming Quickstart) to create a real-time chat interface.
The Agent Development Kit provides a robust foundation for building sophisticated LLM-powered applications. By mastering the concepts covered in this tutorial – tools, state, delegation, and callbacks – you are well-equipped to tackle increasingly complex agentic systems.
Happy building!
# Coding with AI
You can use AI coding assistants to build agents with Agent Development Kit (ADK). Give your coding agent ADK expertise by installing development skills into your project, or by connecting it to ADK documentation through an MCP server.
- [**Agents CLI in Agent Platform**](#agents-cli): Command-line tool and coding skills for ADK development.
- [**ADK Docs MCP Server**](#adk-docs-mcp-server): Connect your coding tool to ADK documentation through an MCP server.
- [**ADK Docs Index**](#adk-docs-index): Machine-readable documentation files following the `llms.txt` standard.
## Agents CLI
The [Agents CLI](https://google.github.io/agents-cli/) tool set lets you plug ADK agent expertise into your favorite AI-coding environments including Antigravity, Claude Code, Cursor, and other AI coding tools. Install Agents CLI into your current AI-powered development environment to scaffold, build, test, evaluate, and deploy ADK agents. Enable your development environment with these Agents CLI Skills:
- Development lifecycle and coding guidelines
- Project scaffolding
- Evaluation methodology and scoring
- Agent Runtime, Cloud Run, and GKE deployment
- Gemini Enterprise agent publishing
- Trace, logging, and integrations
- Python API quick reference and docs index
To install Agents CLI and set up ADK development skills:
```bash
uvx google-agents-cli setup
```
For more information on installing Agents CLI and using it in your development environment, see the [Agents CLI documentation](https://google.github.io/agents-cli/).
## ADK Docs MCP Server
You can configure your coding tool to search and read ADK documentation using an MCP server. Below are setup instructions for popular tools.
### Antigravity
To add the ADK docs MCP server to [Antigravity](https://antigravity.google/) (requires [`uv`](https://docs.astral.sh/uv/)):
1. Open the MCP store via the **...** (more) menu at the top of the editor's agent panel.
1. Click on **Manage MCP Servers** then **View raw config**.
1. Add the following to `mcp_config.json`:
```json
{
"mcpServers": {
"adk-docs-mcp": {
"command": "uvx",
"args": [
"--from",
"mcpdoc",
"--with",
"mcp<2",
"mcpdoc",
"--urls",
"AgentDevelopmentKit:https://adk.tools-online.site/llms.txt",
"--transport",
"stdio"
]
}
}
}
```
### Claude Code
To add the ADK docs MCP server to [Claude Code](https://code.claude.com/docs/en/overview):
```bash
claude mcp add adk-docs --transport stdio -- uvx --from mcpdoc --with "mcp<2" mcpdoc --urls AgentDevelopmentKit:https://adk.tools-online.site/llms.txt --transport stdio
```
### Cursor
To add the ADK docs MCP server to [Cursor](https://cursor.com/) (requires [`uv`](https://docs.astral.sh/uv/)):
1. Open **Cursor Settings** and navigate to the **Tools & MCP** tab.
1. Click on **New MCP Server**, which will open `mcp.json` for editing.
1. Add the following to `mcp.json`:
```json
{
"mcpServers": {
"adk-docs-mcp": {
"command": "uvx",
"args": [
"--from",
"mcpdoc",
"--with",
"mcp<2",
"mcpdoc",
"--urls",
"AgentDevelopmentKit:https://adk.tools-online.site/llms.txt",
"--transport",
"stdio"
]
}
}
}
```
MCP version setting
The `mcp<2` constraint setting keeps `mcpdoc` compatible with the MCP 1.x FastMCP API it currently uses.
### Other Tools
Any coding tool that supports MCP servers can use the same server configuration shown above. Adapt the JSON example from the Antigravity or Cursor sections for your tool's MCP settings.
## ADK Docs Index
The ADK documentation is available as machine-readable files following the [`llms.txt` standard](https://llmstxt.org/). These files are generated with every documentation update and are always up to date.
| File | Description | URL |
| --------------- | ----------------------------------- | -------------------------------------------------------- |
| `llms.txt` | Documentation index with links | [`adk.dev/llms.txt`](https://adk.tools-online.site/llms.txt) |
| `llms-full.txt` | Full documentation in a single file | [`adk.dev/llms-full.txt`](https://adk.tools-online.site/llms-full.txt) |
# Build a multi-tool agent
Supported in ADKPython v0.1.0TypeScript v0.2.0Go v0.1.0Java v0.1.0Kotlin v0.1.0
This quickstart guides you through installing Agent Development Kit (ADK), setting up a basic agent with multiple tools, and running it locally either in the terminal or in the interactive, browser-based dev UI.
This quickstart assumes a local IDE (VS Code, PyCharm, IntelliJ IDEA, etc.) with Python 3.10+ or Java 17+ and terminal access. This method runs the application entirely on your machine and is recommended for internal development.
## 1. Set up Environment & Install ADK
Create & Activate Virtual Environment (Recommended):
```bash
# Create
python3 -m venv .venv
# Activate (each new terminal)
# macOS/Linux: source .venv/bin/activate
# Windows CMD: .venv\Scripts\activate.bat
# Windows PowerShell: .venv\Scripts\Activate.ps1
```
Install ADK:
```bash
pip install google-adk
```
Create a new project directory, initialize it, and install dependencies:
```bash
mkdir my-adk-agent
cd my-adk-agent
npm init -y
npm install @google/adk @google/adk-devtools
npm install -D typescript
```
Create a `tsconfig.json` file with the following content. This configuration ensures your project correctly handles modern Node.js modules.
tsconfig.json
```json
{
"compilerOptions": {
"target": "es2020",
"module": "nodenext",
"moduleResolution": "nodenext",
"esModuleInterop": true,
"strict": true,
"skipLibCheck": true,
// set to false to allow CommonJS module syntax:
"verbatimModuleSyntax": false
}
}
```
## Create a new Go module
If you are starting a new project, you can create a new Go module:
```bash
mkdir my-adk-agent
cd my-adk-agent
go mod init example.com/my-agent
```
## Install ADK
To add the ADK to your project, run the following command:
```bash
go get google.golang.org/adk/v2
```
This will add the ADK as a dependency to your `go.mod` file.
To install ADK Java and set up the environment, see the [Java Quickstart](/get-started/java/).
To install ADK Kotlin and set up the environment, see the [Kotlin Quickstart](/get-started/kotlin/).
## 2. Create Agent Project
### Project structure
You will need to create the following project structure:
```console
parent_folder/
multi_tool_agent/
__init__.py
agent.py
.env
```
Create the folder `multi_tool_agent`:
```bash
mkdir multi_tool_agent/
```
Note for Windows users
When using ADK on Windows for the next few steps, we recommend creating Python files using File Explorer or an IDE because the following commands (`mkdir`, `echo`) typically generate files with null bytes and/or incorrect encoding.
### `__init__.py`
Now create an `__init__.py` file in the folder:
```shell
echo "from . import agent" > multi_tool_agent/__init__.py
```
Your `__init__.py` should now look like this:
multi_tool_agent/__init__.py
```python
from . import agent
```
### `agent.py`
Create an `agent.py` file in the same folder:
```shell
touch multi_tool_agent/agent.py
```
```shell
type nul > multi_tool_agent/agent.py
```
Copy and paste the following code into `agent.py`:
multi_tool_agent/agent.py
```python
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
from zoneinfo import ZoneInfo
from google.adk.agents import Agent
def get_weather(city: str) -> dict:
"""Retrieves the current weather report for a specified city.
Args:
city (str): The name of the city for which to retrieve the weather report.
Returns:
dict: status and result or error msg.
"""
if city.lower() == "new york":
return {
"status": "success",
"report": (
"The weather in New York is sunny with a temperature of 25 degrees"
" Celsius (77 degrees Fahrenheit)."
),
}
else:
return {
"status": "error",
"error_message": f"Weather information for '{city}' is not available.",
}
def get_current_time(city: str) -> dict:
"""Returns the current time in a specified city.
Args:
city (str): The name of the city for which to retrieve the current time.
Returns:
dict: status and result or error msg.
"""
if city.lower() == "new york":
tz_identifier = "America/New_York"
else:
return {
"status": "error",
"error_message": (
f"Sorry, I don't have timezone information for {city}."
),
}
tz = ZoneInfo(tz_identifier)
now = datetime.datetime.now(tz)
report = (
f'The current time in {city} is {now.strftime("%Y-%m-%d %H:%M:%S %Z%z")}'
)
return {"status": "success", "report": report}
root_agent = Agent(
name="weather_time_agent",
model="gemini-flash-latest",
description=(
"Agent to answer questions about the time and weather in a city."
),
instruction=(
"You are a helpful agent who can answer user questions about the time and weather in a city."
),
tools=[get_weather, get_current_time],
)
```
### `.env`
Create a `.env` file in the same folder:
```shell
touch multi_tool_agent/.env
```
```shell
type nul > multi_tool_agent\.env
```
More instructions about this file are described in the next section on [Set up the model](#set-up-the-model).
You will need to create the following project structure in your `my-adk-agent` directory:
```console
my-adk-agent/
agent.ts
.env
package.json
tsconfig.json
```
### `agent.ts`
Create an `agent.ts` file in your project folder:
```shell
touch agent.ts
```
```shell
type nul > agent.ts
```
Copy and paste the following code into `agent.ts`:
agent.ts
```typescript
import 'dotenv/config';
import { FunctionTool, LlmAgent } from '@google/adk';
import { z } from 'zod';
const getWeather = new FunctionTool({
name: 'get_weather',
description: 'Retrieves the current weather report for a specified city.',
parameters: z.object({
city: z.string().describe('The name of the city for which to retrieve the weather report.'),
}),
execute: ({ city }) => {
if (city.toLowerCase() === 'new york') {
return {
status: 'success',
report:
'The weather in New York is sunny with a temperature of 25 degrees Celsius (77 degrees Fahrenheit).',
};
} else {
return {
status: 'error',
error_message: `Weather information for '${city}' is not available.`,
};
}
},
});
const getCurrentTime = new FunctionTool({
name: 'get_current_time',
description: 'Returns the current time in a specified city.',
parameters: z.object({
city: z.string().describe("The name of the city for which to retrieve the current time."),
}),
execute: ({ city }) => {
let tz_identifier: string;
if (city.toLowerCase() === 'new york') {
tz_identifier = 'America/New_York';
} else {
return {
status: 'error',
error_message: `Sorry, I don't have timezone information for ${city}.`,
};
}
const now = new Date();
const report = `The current time in ${city} is ${now.toLocaleString('en-US', { timeZone: tz_identifier })}`;
return { status: 'success', report: report };
},
});
export const rootAgent = new LlmAgent({
name: 'weather_time_agent',
model: 'gemini-flash-latest',
description: 'Agent to answer questions about the time and weather in a city.',
instruction: 'You are a helpful agent who can answer user questions about the time and weather in a city.',
tools: [getWeather, getCurrentTime],
});
```
### `.env`
Create a `.env` file in the same folder:
```shell
touch .env
```
```shell
type nul > .env
```
More instructions about this file are described in the next section on [Set up the model](#set-up-the-model).
You will need to create the following project structure:
```console
my-adk-agent/
agent.go
.env
go.mod
```
### `agent.go`
Create an `agent.go` file in your project folder:
```bash
touch agent.go
```
```console
type nul > agent.go
```
Copy and paste the following code into `agent.go`:
agent.go
```go
package main
import (
"context"
"log"
"os"
"strings"
"time"
"google.golang.org/genai"
"google.golang.org/adk/v2/agent"
"google.golang.org/adk/v2/agent/llmagent"
"google.golang.org/adk/v2/cmd/launcher"
"google.golang.org/adk/v2/cmd/launcher/full"
"google.golang.org/adk/v2/model/gemini"
"google.golang.org/adk/v2/tool"
"google.golang.org/adk/v2/tool/functiontool"
)
type CityArgs struct {
City string `json:"city"`
}
func main() {
ctx := context.Background()
// 1. Setup the model.
// Note: Authentication is handled via GOOGLE_API_KEY environment variable.
model, err := gemini.NewModel(ctx, "gemini-flash-latest", &genai.ClientConfig{
APIKey: os.Getenv("GOOGLE_API_KEY"),
})
if err != nil {
log.Fatalf("Failed to create model: %v", err)
}
weatherTool, err := functiontool.New[CityArgs, map[string]any](
functiontool.Config{
Name: "get_weather",
Description: "Retrieves the current weather report for a specified city.",
},
func(ctx agent.Context, args CityArgs) (map[string]any, error) {
if strings.EqualFold(args.City, "new york") {
return map[string]any{
"status": "success",
"report": "The weather in New York is sunny with a temperature of 25 degrees Celsius (77 degrees Fahrenheit).",
}, nil
}
return map[string]any{
"status": "error",
"error_message": "Weather information for '" + args.City + "' is not available.",
}, nil
},
)
if err != nil {
log.Fatalf("Failed to create get_weather tool: %v", err)
}
currentTimeTool, err := functiontool.New[CityArgs, map[string]any](
functiontool.Config{
Name: "get_current_time",
Description: "Returns the current time in a specified city.",
},
func(ctx agent.Context, args CityArgs) (map[string]any, error) {
var tzIdentifier string
if strings.EqualFold(args.City, "new york") {
tzIdentifier = "America/New_York"
} else {
return map[string]any{
"status": "error",
"error_message": "Sorry, I don't have timezone information for " + args.City + ".",
}, nil
}
tz, err := time.LoadLocation(tzIdentifier)
if err != nil {
return nil, err
}
now := time.Now().In(tz)
report := "The current time in " + args.City + " is " + now.Format("2006-01-02 15:04:05 MST-0700")
return map[string]any{
"status": "success",
"report": report,
}, nil
},
)
if err != nil {
log.Fatalf("Failed to create get_current_time tool: %v", err)
}
// 2. Define the agent.
a, err := llmagent.New(llmagent.Config{
Name: "weather_time_agent",
Model: model,
Description: "Agent to answer questions about the time and weather in a city.",
Instruction: "You are a helpful agent who can answer user questions about the time and weather in a city.",
Tools: []tool.Tool{
weatherTool,
currentTimeTool,
},
})
if err != nil {
log.Fatalf("Failed to create agent: %v", err)
}
// 3. Configure the launcher and run.
config := &launcher.Config{
AgentLoader: agent.NewSingleLoader(a),
}
l := full.NewLauncher()
if err = l.Execute(ctx, config, os.Args[1:]); err != nil {
log.Fatalf("Run failed: %v\n\n%s", err, l.CommandLineSyntax())
}
}
```
### `.env`
Create a `.env` file in the same folder:
```bash
touch .env
```
```console
type nul > .env
```
Java projects generally feature the following project structure:
```console
project_folder/
├── pom.xml (or build.gradle)
├── src/
├── └── main/
│ └── java/
│ └── agents/
│ └── multitool/
└── test/
```
### Create `MultiToolAgent.java`
Create a `MultiToolAgent.java` source file in the `agents.multitool` package in the `src/main/java/agents/multitool/` directory.
Copy and paste the following code into `MultiToolAgent.java`:
agents/multitool/MultiToolAgent.java
```java
package agents.multitool;
import com.google.adk.agents.BaseAgent;
import com.google.adk.agents.LlmAgent;
import com.google.adk.events.Event;
import com.google.adk.runner.InMemoryRunner;
import com.google.adk.sessions.Session;
import com.google.adk.tools.Annotations.Schema;
import com.google.adk.tools.FunctionTool;
import com.google.genai.types.Content;
import com.google.genai.types.Part;
import io.reactivex.rxjava3.core.Flowable;
import java.nio.charset.StandardCharsets;
import java.text.Normalizer;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.Scanner;
public class MultiToolAgent {
private static String USER_ID = "student";
private static String NAME = "multi_tool_agent";
// The run your agent with Dev UI, the ROOT_AGENT should be a global public static final variable.
public static final BaseAgent ROOT_AGENT = initAgent();
public static BaseAgent initAgent() {
return LlmAgent.builder()
.name(NAME)
.model("gemini-flash-latest")
.description("Agent to answer questions about the time and weather in a city.")
.instruction(
"You are a helpful agent who can answer user questions about the time and weather"
+ " in a city.")
.tools(
FunctionTool.create(MultiToolAgent.class, "getCurrentTime"),
FunctionTool.create(MultiToolAgent.class, "getWeather"))
.build();
}
public static Map getCurrentTime(
@Schema(name = "city",
description = "The name of the city for which to retrieve the current time")
String city) {
String normalizedCity =
Normalizer.normalize(city, Normalizer.Form.NFD)
.trim()
.toLowerCase()
.replaceAll("(\\p{IsM}+|\\p{IsP}+)", "")
.replaceAll("\\s+", "_");
return ZoneId.getAvailableZoneIds().stream()
.filter(zid -> zid.toLowerCase().endsWith("/" + normalizedCity))
.findFirst()
.map(
zid ->
Map.of(
"status",
"success",
"report",
"The current time in "
+ city
+ " is "
+ ZonedDateTime.now(ZoneId.of(zid))
.format(DateTimeFormatter.ofPattern("HH:mm"))
+ "."))
.orElse(
Map.of(
"status",
"error",
"report",
"Sorry, I don't have timezone information for " + city + "."));
}
public static Map getWeather(
@Schema(name = "city",
description = "The name of the city for which to retrieve the weather report")
String city) {
if (city.toLowerCase().equals("new york")) {
return Map.of(
"status",
"success",
"report",
"The weather in New York is sunny with a temperature of 25 degrees Celsius (77 degrees"
+ " Fahrenheit).");
} else {
return Map.of(
"status", "error", "report", "Weather information for " + city + " is not available.");
}
}
public static void main(String[] args) throws Exception {
InMemoryRunner runner = new InMemoryRunner(ROOT_AGENT);
Session session =
runner
.sessionService()
.createSession(NAME, USER_ID)
.blockingGet();
try (Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8)) {
while (true) {
System.out.print("\nYou > ");
String userInput = scanner.nextLine();
if ("quit".equalsIgnoreCase(userInput)) {
break;
}
Content userMsg = Content.fromParts(Part.fromText(userInput));
Flowable events = runner.runAsync(USER_ID, session.id(), userMsg);
System.out.print("\nAgent > ");
events.blockingForEach(event -> System.out.println(event.stringifyContent()));
}
}
}
}
```
Kotlin projects generally feature the following project structure:
```console
project_folder/
├── build.gradle.kts
├── src/
├── └── main/
│ └── kotlin/
│ └── agents/
│ └── multitool/
```
### Create `MultiToolAgent.kt`
Create a `MultiToolAgent.kt` source file in the `src/main/kotlin/agents/multitool/` directory.
Copy and paste the following code into `MultiToolAgent.kt`:
src/main/kotlin/agents/multitool/MultiToolAgent.kt
```kotlin
package agents.multitool
import com.google.adk.kt.agents.Instruction
import com.google.adk.kt.agents.LlmAgent
import com.google.adk.kt.annotations.Param
import com.google.adk.kt.annotations.Tool
import com.google.adk.kt.models.Gemini
import com.google.adk.kt.runners.InMemoryRunner
import com.google.adk.kt.sessions.InMemorySessionService
import com.google.adk.kt.sessions.SessionKey
import com.google.adk.kt.types.Content
import com.google.adk.kt.types.Part
import com.google.adk.kt.types.Role
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking
import java.text.Normalizer
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.util.Scanner
class MultiToolService {
@Tool
fun getCurrentTime(
@Param("The name of the city for which to retrieve the current time") city: String,
): Map {
val normalizedCity =
Normalizer.normalize(city, Normalizer.Form.NFD)
.trim()
.lowercase()
.replace(Regex("(\\p{IsM}+|\\p{IsP}+)"), "")
.replace(Regex("\\s+"), "_")
val zoneId =
ZoneId.getAvailableZoneIds()
.firstOrNull { it.lowercase().endsWith("/$normalizedCity") }
return if (zoneId != null) {
val time =
ZonedDateTime.now(ZoneId.of(zoneId))
.format(DateTimeFormatter.ofPattern("HH:mm"))
mapOf(
"status" to "success",
"report" to "The current time in $city is $time.",
)
} else {
mapOf(
"status" to "error",
"report" to "Sorry, I don't have timezone information for $city.",
)
}
}
@Tool
fun getWeather(
@Param("The name of the city for which to retrieve the weather report") city: String,
): Map {
return if (city.lowercase() == "new york") {
mapOf(
"status" to "success",
"report" to "The weather in New York is sunny with a temperature of " +
"25 degrees Celsius (77 degrees Fahrenheit).",
)
} else {
mapOf(
"status" to "error",
"report" to "Weather information for $city is not available.",
)
}
}
}
fun main() =
runBlocking {
val model = Gemini(name = "gemini-flash-latest")
val agent =
LlmAgent(
name = "multi_tool_agent",
model = model,
description = "Agent to answer questions about the time and weather in a city.",
instruction =
Instruction(
"You are a helpful agent who can answer user questions about the " +
"time and weather in a city.",
),
tools = MultiToolService().generatedTools(),
)
val sessionService = InMemorySessionService()
val runner =
InMemoryRunner(
agent = agent,
appName = "multi_tool_app",
sessionService = sessionService,
)
val userId = "student"
val sessionId = "session_1"
sessionService.createSession(SessionKey("multi_tool_app", userId, sessionId))
val scanner = Scanner(System.`in`)
while (true) {
print("\nYou > ")
val userInput = scanner.nextLine()
if (userInput.lowercase() == "quit") break
val userContent = Content(role = Role.USER, parts = listOf(Part(text = userInput)))
val events =
runner.runAsync(
userId = userId,
sessionId = sessionId,
newMessage = userContent,
).toList()
print("\nAgent > ")
for (event in events) {
event.content?.parts?.forEach { part ->
part.text?.let { print(it) }
}
}
println()
}
}
```
## 3. Set up the model
Your agent's ability to understand user requests and generate responses is powered by a generative AI model or Large Language Model (LLM). This guide uses Gemini models as examples, but ADK is compatible with many AI models from Google and other providers. For more information on available models and how to configure them, see [AI Models for ADK agents](/agents/models/).
### Model connection and authentication
When using an AI model through a service, such as the Gemini API or Gemini Enterprise Agent Platform on Google Cloud, you must provide an API key or authenticate with the service. The most direct way to provide this information is to use environment variables or an `.env` file. The following examples show the most common way to configure an agent for use with the Gemini API or Gemini Enterprise Agent Platform.
```text
# .env configuration file
GOOGLE_API_KEY="PASTE_YOUR_GEMINI_API_KEY_HERE"
```
```text
# .env configuration file
GOOGLE_CLOUD_PROJECT=your-project-id
GOOGLE_CLOUD_LOCATION=location-code # example: us-central1
GOOGLE_GENAI_USE_ENTERPRISE=True
```
For more details on connecting ADK agents to Google Cloud hosted models and services, including Gemini Enterprise Agent Platform, see the [Connect to Google Cloud and Agent Platform](/get-started/google-cloud/) guide.
## 4. Run Your Agent
Using the terminal, navigate to the parent directory of your agent project (e.g. using `cd ..`):
```console
parent_folder/ <-- navigate to this directory
multi_tool_agent/
__init__.py
agent.py
.env
```
There are multiple ways to interact with your agent:
Authentication Setup for Agent Platform Users
If you selected **"Gemini - Google Cloud Agent Platform"** in the previous step, you must authenticate with Google Cloud before launching the dev UI.
Run this command and follow the prompts:
```bash
gcloud auth application-default login
```
**Note:** Skip this step if you're using "Gemini - Google AI Studio".
Run the following command to launch the **dev UI**.
```shell
adk web
```
Caution: ADK Web for development only
ADK Web is ***not meant for use in production deployments***. You should use ADK Web for development and debugging purposes only.
Note for Windows users
When hitting the `_make_subprocess_transport NotImplementedError`, consider using `adk web --no-reload` instead.
**Step 1:** Open the URL provided (usually `http://localhost:8000` or `http://127.0.0.1:8000`) directly in your browser.
**Step 2.** In the top-left corner of the UI, you can select your agent in the dropdown. Select "multi_tool_agent".
Troubleshooting
If you do not see "multi_tool_agent" in the dropdown menu, make sure you are running `adk web` in the **parent folder** of your agent folder (i.e. the parent folder of multi_tool_agent).
**Step 3.** Now you can chat with your agent using the textbox:
**Step 4.** By using the `Events` tab at the left, you can inspect individual function calls, responses and model responses by clicking on the actions:
On the `Events` tab, you can also click the `Trace` button to see the trace logs for each event that shows the latency of each function calls:
**Step 5.** You can also enable your microphone and talk to your agent:
Model support for voice/video streaming
In order to use voice/video streaming in ADK, you will need to use Gemini models that support the Live API. You can find the **model ID(s)** that supports the Gemini Live API in the documentation:
- [Google AI Studio: Gemini Live API](https://ai.google.dev/gemini-api/docs/models#live-api)
- [Agent Platform: Gemini Live API](https://cloud.google.com/vertex-ai/generative-ai/docs/live-api)
You can then replace the `model` string in `root_agent` in the `agent.py` file you created earlier ([jump to section](#agentpy)). Your code should look something like:
```py
root_agent = Agent(
name="weather_time_agent",
model="replace-me-with-model-id", #e.g. gemini-live-2.5-flash-native-audio
...
```
Tip
When using `adk run` you can inject prompts into the agent to start by piping text to the command like so:
```shell
echo "Please start by listing files" | adk run multi_tool_agent
```
Run the following command, to chat with your Weather agent.
```text
adk run multi_tool_agent
```
To exit, use Cmd/Ctrl+C.
`adk api_server` enables you to create a local FastAPI server in a single command, enabling you to test local cURL requests before you deploy your agent.
To learn how to use `adk api_server` for testing, refer to the [documentation on using the API server](/runtime/api-server/).
Using the terminal, navigate to your agent project directory:
```console
my-adk-agent/ <-- navigate to this directory
agent.ts
.env
package.json
tsconfig.json
```
There are multiple ways to interact with your agent:
Run the following command to launch the **dev UI**.
```shell
npx adk web
```
**Step 1:** Open the URL provided (usually `http://localhost:8000` or `http://127.0.0.1:8000`) directly in your browser.
**Step 2.** In the top-left corner of the UI, select your agent from the dropdown. The agents are listed by their filenames, so you should select "agent".
Troubleshooting
If you do not see "agent" in the dropdown menu, make sure you are running `npx adk web` in the directory containing your `agent.ts` file.
**Step 3.** Now you can chat with your agent using the textbox:
**Step 4.** By using the `Events` tab at the left, you can inspect individual function calls, responses and model responses by clicking on the actions:
On the `Events` tab, you can also click the `Trace` button to see the trace logs for each event that shows the latency of each function calls:
Run the following command to chat with your agent.
```text
npx adk run agent.ts
```
To exit, use Cmd/Ctrl+C.
`npx adk api_server` enables you to create a local Express.js server in a single command, enabling you to test local cURL requests before you deploy your agent.
To learn how to use `api_server` for testing, refer to the [documentation on testing](/runtime/api-server/).
Using the terminal, navigate to your agent project directory:
```console
my-adk-agent/ <-- navigate to this directory
agent.go
.env
go.mod
```
There are multiple ways to interact with your agent:
Run the following command to launch the **dev UI**. You must specify which sub-launchers to activate (e.g., `webui`, `api`).
```bash
go run agent.go web webui api
```
**Step 1:** Open the URL provided (usually `http://localhost:8080`) directly in your browser.
**Step 2.** In the top-left corner of the UI, select your agent from the dropdown. It should be "weather_time_agent".
**Step 3.** Now you can chat with your agent using the textbox.
Run the following command to chat with your agent in the terminal.
```bash
go run agent.go console
```
**Note:** If `console` is the first sublauncher in your code (as it is with `full.NewLauncher()`), you can also just run `go run agent.go`.
To exit, use Cmd/Ctrl+C.
Using the terminal, navigate to the parent directory of your agent project (e.g. using `cd ..`):
```console
project_folder/ <-- navigate to this directory
├── pom.xml (or build.gradle)
├── src/
├── └── main/
│ └── java/
│ └── agents/
│ └── multitool/
│ └── MultiToolAgent.java
└── test/
```
Run the following command from the terminal to launch the Dev UI.
**DO NOT change the main class name of the Dev UI server.**
terminal
```console
mvn exec:java \
-Dexec.mainClass="com.google.adk.web.AdkWebServer" \
-Dexec.args="--adk.agents.source-dir=src/main/java" \
-Dexec.classpathScope="compile"
```
**Step 1:** Open the URL provided (usually `http://localhost:8080` or `http://127.0.0.1:8080`) directly in your browser.
**Step 2.** In the top-left corner of the UI, you can select your agent in the dropdown. Select "multi_tool_agent".
Troubleshooting
If you do not see "multi_tool_agent" in the dropdown menu, make sure you are running the `mvn` command at the location where your Java source code is located (usually `src/main/java`).
**Step 3.** Now you can chat with your agent using the textbox:
**Step 4.** You can also inspect individual function calls, responses and model responses by clicking on the actions:
Caution: ADK Web for development only
ADK Web is ***not meant for use in production deployments***. You should use ADK Web for development and debugging purposes only.
With Maven, run the `main()` method of your Java class with the following command:
terminal
```console
mvn compile exec:java -Dexec.mainClass="agents.multitool.MultiToolAgent"
```
With Gradle, the `build.gradle` or `build.gradle.kts` build file should have the following Java plugin in its `plugins` section:
```groovy
plugins {
id('java')
// other plugins
}
```
Then, elsewhere in the build file, at the top-level, create a new task to run the `main()` method of your agent:
```groovy
tasks.register('runAgent', JavaExec) {
classpath = sourceSets.main.runtimeClasspath
mainClass = 'agents.multitool.MultiToolAgent'
}
```
Finally, on the command-line, run the following command:
```console
gradle runAgent
```
Using the terminal, navigate to your agent project directory:
```console
project_folder/ <-- navigate to this directory
├── build.gradle.kts
├── src/
├── └── main/
│ └── kotlin/
│ └── agents/
│ └── multitool/
│ └── MultiToolAgent.kt
```
### Run your Agent
You can run the `main()` method of your Kotlin class using Gradle:
```console
./gradlew run
```
Or if you are using IntelliJ IDEA, you can just click the green run arrow next to the `main()` function.
### 📝 Example prompts to try
- What is the weather in New York?
- What is the time in New York?
- What is the weather in Paris?
- What is the time in Paris?
## 🎉 Congratulations!
You've successfully created and interacted with your first agent using ADK!
______________________________________________________________________
## 🛣️ Next steps
- **Go to the tutorial**: Learn how to add memory, session, state to your agent: [tutorial](/tutorials/).
- **Delve into advanced configuration:** Explore the [setup](/get-started/installation/) section for deeper dives into project structure, configuration, and other interfaces.
- **Understand Core Concepts:** Learn about [agents concepts](/agents/).
# Agents
Supported in ADKPythonTypeScriptGoJava
An ***Agent***, or ***LlmAgent***, in Agent Development Kit (ADK) is a self-contained execution unit designed to act autonomously to achieve specific goals. Agents can perform tasks, interact with users, utilize external tools, and coordinate with other agents. The basic components of an ***Agent*** are an artificial intelligence (AI) model, task instructions, and optionally, a set of tools to be used by the agent. As agent tasks and complexity grow, you can use the ADK development framework to expand them into *workflows*, which allow you to combine and orchestrate multiple agents and code execution tasks.
**Figure 1.** Simple Agents and Agent Workflows in ADK
Building an agent with just a model, instructions, and tools is a great place to start for most developers. As your agent grows in capability and complexity, you are likely to want to break up the capabilities of your agent application in order to better manage its behavior, work within model operating context limits, and modularize your code to keep it manageable. ADK agent ***Workflow*** architectures allow you to evolve an agent from a monolithic structure to more modular code and project structures.
## Grow from single agent to workflows
In ADK, any agent application that has more than one agent or executable *Node* is considered a workflow. ADK does not impose any hard requirements to move from a single-agent architecture to a multi-agent or graph-based ***Workflow*** architecture. You can decide when to make that change based on the needs of your project, or as you discover limitations of a single-agent approach, such as:
- **Instruction following performance:** Beyond a certain length or complexity of a multiple step set of instructions, you may discover that a single agent does not reliably complete all instructions, or perform them with the required level of quality or speed.
- **Context limitations:** You may discover that the amount of data required to perform an agent task exceeds the context window limitations of the AI model you are using.
- **Agent code modularity:** As the complexity and organization of your agent code grows, you may want to break up the agent capabilities to make your code more manageable or enable re-use of agent code for other agent projects.
- **Mixing deterministic and non-deterministic tasks:** As you build agents for solving more complex problems, you may want to design and build agents that interweave the non-deterministic functionality of AI models with deterministic code, rather than relying on non-deterministic AI models to manage the full execution of a task. For more details, see [Graph-based workflows](/graphs/).
For more information about ADK Workflows and agent project architectures, see the [Workflows](/workflows/) section.
## Agent features
The capabilities of ADK agents can be extended and expanded using the following features:
- [**AI models**](/agents/models/): Swap the underlying intelligence of your agents by integrating with generative AI models from Google and other providers.
- [**Pre-built tools and integrations**](/integrations/): Equip your agents with a wide array tools, plugins, and other integrations to interact with the world, including web sites, MCP tools, applications, databases, programming interfaces, and more.
- [**Custom tools**](/tools-custom/): Create your own, task-specific tools for solving specific problems with precision and control.
- [**Artifacts**](/artifacts/): Enable agents to create and manage persistent outputs like files, code, or documents that exist beyond the conversation lifecycle.
- [**Skills**](/skills/): Use prebuilt or custom [Agent Skills](https://agentskills.io/) to extend agent capabilities in a way that works efficiently inside AI context window limits.
- [**Plugins**](/plugins/): Integrate complex, pre-packaged behaviors and third-party services directly into your agent's workflow.
- [**Callbacks**](/callbacks/): Hook into specific events during an agent's execution lifecycle to add logging, monitoring, or custom side-effects without altering core agent logic.
- [**Service registration**](https://adk.tools-online.site/agents/service-registration/index.md): Register custom backend services for sessions, artifacts, memory, and Agent2Agent (A2A) task stores.
## Next Steps
Now that you have an overview of the different agent types available in ADK, dive deeper into how they work and how to use them effectively:
- [**Simple agents:**](/agents/llm-agents/) Explore how to configure agents powered by AI models, including setting instructions, providing tools, and enabling advanced features like planning and code execution.
- [**Managed agents:**](/agents/managed-agents/) Use Google's first-party, out-of-the-box agents (backed by the Managed Agents API) directly in your ADK flows, with built-in server-side tools like web search and code execution.
- [**Graph workflows:**](/graphs/) Discover how to evolve your agents from plain language instructions to composable, reliable execution paths that combine AI reasoning with deterministic code logic.
- [**Multi-agent workflows:**](/workflows/) Explore how to build agent applications that combine multiple agents, execution nodes, a variety of task execution control mechanisms to fit the needs of your project.
- [**Agent optimization:**](/optimize/) Discover methodologies to evaluate, test, and enhance the performance, reliability, and cost-efficiency of your agent applications.
# Build agents with Agent Config
Supported in ADKPython v1.11.0Java v0.3.0
The ADK Agent Config feature lets you build an ADK workflow without writing code. An Agent Config uses a YAML format text file with a brief description of the agent, allowing just about anyone to assemble and run an ADK agent. The following is a simple example of a basic Agent Config definition:
```yaml
name: assistant_agent
model: gemini-flash-latest
description: A helper agent that can answer users' questions.
instruction: You are an agent to help answer users' various questions.
```
You can use Agent Config files to build more complex agents which can incorporate Functions, Tools, Sub-Agents, and more. This page describes how to build and run ADK workflows with the Agent Config feature. For detailed information on the syntax and settings supported by the Agent Config format, see the [Agent Config syntax reference](/api-reference/agentconfig/).
Experimental in Java
The Agent Config feature is experimental in ADK Java. For all languages, see the [known limitations](#known-limitations). We welcome your [feedback](https://github.com/google/adk-java/issues/new?template=feature_request.md&labels=agent%20config)!
## Get started
This section describes how to set up and start building agents with the ADK and the Agent Config feature, including installation setup, building an agent, and running your agent.
### Setup
You need to install the Google Agent Development Kit libraries, and provide an access key for a generative AI model such as Gemini API. This section provides details on what you must install and configure before you can run agents with the Agent Config files.
Note
The `model:` key of an Agent Config takes a model name string, such as a Gemini model name. To use a model that needs constructor arguments, such as `LiteLlm`, construct a `BaseLlm` instance in Python and reference it from the `model_code:` key. For more information about additional functional restrictions, see [Known limitations](#known-limitations).
To set up ADK for use with Agent Config:
1. Install the ADK Python libraries by following the [Installation](/get-started/installation/) instructions. *Python is currently required.* For more information, see the [Known limitations](#known-limitations).
1. Verify that ADK is installed by running the following command in your terminal:
```text
adk --version
```
This command should show the ADK version you have installed.
Tip
If the `adk` command fails to run and the version is not listed in step 2, make sure your Python environment is active. Execute `source .venv/bin/activate` in your terminal on Mac and Linux. For other platform commands, see the [Installation](/get-started/installation/) page.
### Build an agent
You build an agent with Agent Config using the `adk create` command to create the project files for an agent, and then editing the `root_agent.yaml` file it generates for you.
To create an ADK project for use with Agent Config:
1. In your terminal window, run the following command to create a config-based agent:
```text
adk create --type=config my_agent
```
This command generates a `my_agent/` folder, containing a `root_agent.yaml` file and an `.env` file.
1. In the `my_agent/.env` file, set environment variables for your agent to access generative AI models and other services:
1. For Gemini model access through Google API, add a line to the file with your API key:
```text
GOOGLE_GENAI_USE_ENTERPRISE=0
GOOGLE_API_KEY=
```
You can get an API key from the Google AI Studio [API Keys](https://aistudio.google.com/app/apikey) page.
1. For Gemini model access through Google Cloud, add these lines to the file:
```text
GOOGLE_GENAI_USE_ENTERPRISE=1
GOOGLE_CLOUD_PROJECT=
GOOGLE_CLOUD_LOCATION=us-central1
```
For information on creating a Cloud Project, see the Google Cloud docs for [Creating and managing projects](https://cloud.google.com/resource-manager/docs/creating-managing-projects). For more information on connecting to Google Cloud from ADK agents, see [Connect to Google Cloud and Agent Platform](/get-started/google-cloud/).
1. Using text editor, edit the Agent Config file `my_agent/root_agent.yaml`, as shown below:
```text
# yaml-language-server: $schema=https://raw.githubusercontent.com/google/adk-python/refs/heads/main/src/google/adk/agents/config_schemas/AgentConfig.json
name: assistant_agent
model: gemini-flash-latest
description: A helper agent that can answer users' questions.
instruction: You are an agent to help answer users' various questions.
```
You can discover more configuration options for your `root_agent.yaml` agent configuration file by referring to the ADK [samples repository](https://github.com/search?q=repo%3Agoogle%2Fadk-python+path%3A%2F%5Econtributing%5C%2Fsamples%5C%2F%2F+.yaml&type=code) or the [Agent Config syntax](/api-reference/agentconfig/) reference.
### Run the agent
Once you have completed editing your Agent Config, you can run your agent using the web interface, command line terminal execution, or API server mode.
To run your Agent Config-defined agent:
1. In your terminal, navigate to the `my_agent/` directory containing the `root_agent.yaml` file.
1. Type one of the following commands to run your agent:
- `adk web` - Run web UI interface for your agent.
- `adk run` - Run your agent in the terminal without a user interface.
- `adk api_server` - Run your agent as a service that can be used by other applications.
For more information on the ways to run your agent, see [Agent Runtime](/runtime/#ways-to-run-agents). For more information about the ADK command line options, see the [ADK CLI reference](/api-reference/cli/).
### Run programmatically
You can also bypass the CLI and dynamically load and execute a configuration-based agent directly in your code. The utility loads the configuration and instantiates the proper agent class (such as `LlmAgent`) transparently as a `BaseAgent` subclass.
```python
import asyncio
from google.adk.agents import config_agent_utils
from google.adk.runners import Runner
async def main():
# Load the agent directly from the YAML config file
agent = config_agent_utils.from_config("my_agent/root_agent.yaml")
# ...
if __name__ == "__main__":
asyncio.run(main())
```
```java
import com.google.adk.agents.BaseAgent;
import com.google.adk.agents.ConfigAgentUtils;
public class AgentApp {
public static void main(String[] args) throws Exception {
// Load the agent directly from the YAML config file
BaseAgent agent = ConfigAgentUtils.fromConfig("my_agent/root_agent.yaml");
// ...
}
}
```
## Example configs
This section shows examples of Agent Config files to get you started building agents. For additional and more complete examples, see the ADK [samples repository](https://github.com/search?q=repo%3Agoogle%2Fadk-python+path%3A%2F%5Econtributing%5C%2Fsamples%5C%2F%2F+root_agent.yaml&type=code).
### Built-in tool example
The following example uses a built-in ADK tool function for using google search to provide functionality to the agent. This agent automatically uses the search tool to reply to user requests.
```text
# yaml-language-server: $schema=https://raw.githubusercontent.com/google/adk-python/refs/heads/main/src/google/adk/agents/config_schemas/AgentConfig.json
name: search_agent
model: gemini-flash-latest
description: 'an agent whose job it is to perform Google search queries and answer questions about the results.'
instruction: You are an agent whose job is to perform Google search queries and answer questions about the results.
tools:
- name: google_search
```
For more details, see the full code for this sample in the [ADK sample repository](https://github.com/google/adk-python/blob/main/contributing/samples/tools/tool_builtin_config/root_agent.yaml).
### Custom tool example
The following example uses a custom tool built with Python code and listed in the `tools:` section of the config file. The agent uses this tool to check if a list of numbers provided by the user are prime numbers.
```text
# yaml-language-server: $schema=https://raw.githubusercontent.com/google/adk-python/refs/heads/main/src/google/adk/agents/config_schemas/AgentConfig.json
agent_class: LlmAgent
model: gemini-flash-latest
name: prime_agent
description: Handles checking if numbers are prime.
instruction: |
You are responsible for checking whether numbers are prime.
When asked to check primes, you must call the check_prime tool with a list of integers.
Never attempt to determine prime numbers manually.
Return the prime number results to the root agent.
tools:
- name: ma_llm.check_prime
```
For more details, see the full code for this sample in the [ADK sample repository](https://github.com/google/adk-python/blob/main/contributing/samples/multi_agent/multi_agent_llm_config/prime_agent.yaml).
### Sub-agents example
The following example shows an agent defined with two sub-agents in the `sub_agents:` section, and an example tool in the `tools:` section of the config file. This agent determines what the user wants, and delegates to one of the sub-agents to resolve the request. The sub-agents are defined using Agent Config YAML files.
```text
# yaml-language-server: $schema=https://raw.githubusercontent.com/google/adk-python/refs/heads/main/src/google/adk/agents/config_schemas/AgentConfig.json
agent_class: LlmAgent
model: gemini-flash-latest
name: root_agent
description: Learning assistant that provides tutoring in code and math.
instruction: |
You are a learning assistant that helps students with coding and math questions.
You delegate coding questions to the code_tutor_agent and math questions to the math_tutor_agent.
Follow these steps:
1. If the user asks about programming or coding, delegate to the code_tutor_agent.
2. If the user asks about math concepts or problems, delegate to the math_tutor_agent.
3. Always provide clear explanations and encourage learning.
sub_agents:
- config_path: code_tutor_agent.yaml
- config_path: math_tutor_agent.yaml
```
For more details, see the full code for this sample in the [ADK sample repository](https://github.com/google/adk-python/blob/main/contributing/samples/multi_agent/multi_agent_basic_config/root_agent.yaml).
## Deploy agent configs
You can deploy Agent Config agents with [Cloud Run](/deploy/cloud-run/) and [Agent Runtime](/deploy/agent-runtime/), using the same procedure as code-based agents. For more information on how to prepare and deploy Agent Config-based agents, see the [Cloud Run](/deploy/cloud-run/) and [Agent Runtime](/deploy/agent-runtime/) deployment guides.
## Known limitations
The Agent Config feature has the following limitations:
- **Model support:** The `model:` key only accepts a model name string. To use a model that needs constructor arguments, such as `LiteLlm`, construct it in Python and reference it from the `model_code:` key, which takes a mapping whose `name:` sub-key is the fully qualified name of the `BaseLlm` instance:
```text
model_code:
name: my_library.clients.my_litellm
```
The `model:` and `model_code:` keys cannot both be set.
- **Programming language:** The Agent Config feature currently supports Python and Java code for tools and other functionality requiring programming code.
- **ADK Tool support:** The following ADK tools are supported by the Agent Config feature, but *not all tools are fully supported*:
- `google_search`
- `google_maps_grounding`
- `load_artifacts`
- `url_context`
- `exit_loop`
- `preload_memory`
- `get_user_choice`
- `enterprise_web_search`
- `load_web_page`: Requires a fully-qualified path to access web pages.
- `AgentTool`: Allows an agent to call another agent.
- `LongRunningFunctionTool`: Supports long-running functions.
- `McpToolset`: Connects to Model Context Protocol (MCP) servers.
- `ExampleTool`: Provides example-based few-shot learning for tools.
- **Agent Type Support:** The `LangGraphAgent` and `A2aAgent` types are not yet supported.
- **Agent Search:** The `VertexAiSearchTool` is currently supported in Python and Java Agent Configs.
## Next steps
For ideas on what to build, see the [sample agent configs](https://github.com/search?q=repo:google/adk-python+path:/%5Econtributing%5C/samples%5C/.*%5C/root_agent%5C.yaml$/&type=code) in the `adk-python` repository. For detailed information on the syntax and settings supported by the Agent Config format, see the [Agent Config syntax reference](/api-reference/agentconfig/).
# Custom agent template workflows
Supported in ADKPython v0.1.0TypeScript v0.2.0Go v0.1.0Java v0.1.0Kotlin v0.1.0
Custom agents and agent-based workflows allow you to define arbitrary orchestration logic by inheriting directly from `BaseAgent` and implementing your own control flow. This approach allows you to create new execution patterns similar to `SequentialAgent`, `LoopAgent`, and `ParallelAgent`, enabling you to build highly specific and complex agentic workflows.
Alternative: graph-based workflows
Starting in ADK 2.0, agent-based workflows using `BaseAgent` have been superseded
by more flexible workflow structures, including [graph-based workflows](/graphs/) and [dynamic workflows](/graphs/dynamic/). You should evaluate the capabilities of these workflow mechanisms ***before*** building a custom agent for your target workflow.
Advanced Concept
Building custom agents by directly implementing `_run_async_impl`, or its equivalent in other languages, provides powerful control but is more complex than using the predefined `LlmAgent` or `WorkflowAgent` types. We recommend understanding those foundational agent types first before tackling custom orchestration logic.
## Overview
A Custom Agent is essentially any class you create that inherits from `google.adk.agents.BaseAgent` and implements its core execution logic within the `_run_async_impl` asynchronous method. You have complete control over how this method calls other sub-agents, manages state, and handles events.
Note
The specific method name for implementing an agent's core asynchronous logic may vary slightly by SDK language, such as `runAsyncImpl` in Java, `_run_async_impl` in Python, or `runAsyncImpl` in TypeScript. Refer to the language-specific API documentation for details.
### Why build Custom Agents?
After reviewing existing ADK [agent workflow](/workflows/) approaches and architectures, you may want to consider building a custom workflow agent if those mechanisms cannot meet one or more of the following requirements for your project:
- **Conditional Logic:** Executing different sub-agents or taking different paths based on runtime conditions or the results of previous steps.
- **Complex State Management:** Implementing intricate logic for maintaining and updating state throughout the workflow beyond simple sequential passing.
- **External Integrations:** Incorporating calls to external APIs, databases, or custom libraries directly within the orchestration flow control.
- **Dynamic Agent Selection:** Choosing which sub-agent(s) to run next based on dynamic evaluation of the situation or input.
- **Unique Workflow Patterns:** Implementing orchestration logic that doesn't fit the standard sequential, parallel, or loop structures.
## Implementing custom logic
The core of any custom agent is the method where you define its unique asynchronous behavior. This method allows you to orchestrate sub-agents and manage the flow of execution.
The heart of any custom agent is the `_run_async_impl` method. This is where you define its unique behavior.
- **Signature:** `async def _run_async_impl(self, ctx: InvocationContext) -> AsyncGenerator[Event, None]:`
- **Asynchronous Generator:** It must be an `async def` function and return an `AsyncGenerator`. This allows it to `yield` events produced by sub-agents or its own logic back to the runner.
- **`ctx` (InvocationContext):** Provides access to crucial runtime information, most importantly `ctx.session.state`, which is the primary way to share data between steps orchestrated by your custom agent.
- **Live audio and video runs:** The `_run_async_impl` method only covers non-live runs (`run_async()`). To support `run_live()`, also override `_run_live_impl`, which has the same signature. Without that override, live runs raise `NotImplementedError`.
The heart of any custom agent is the `runAsyncImpl` method. This is where you define its unique behavior.
- **Signature:** `async* runAsyncImpl(ctx: InvocationContext): AsyncGenerator`
- **Asynchronous Generator:** It must be an `async` generator function (`async*`).
- **`ctx` (InvocationContext):** Provides access to crucial runtime information, most importantly `ctx.session.state`, which is the primary way to share data between steps orchestrated by your custom agent.
In Go, you create a custom agent by passing a `Run` function to `agent.New` in an `agent.Config`. You can't implement the `agent.Agent` interface directly, because it includes an unexported method. The actual logic is typically a method on your custom agent struct, which you pass as `Run`.
- **Signature:** `Run(ctx agent.InvocationContext) iter.Seq2[*session.Event, error]`
- **Iterator:** The `Run` method returns an iterator (`iter.Seq2`) that yields events and errors. This is the standard way to handle streaming results from an agent's execution.
- **`ctx` (InvocationContext):** The `agent.InvocationContext` provides access to the session, including state, and other crucial runtime information.
- **Session State:** You can access the session state through `ctx.Session().State()`.
The heart of any custom agent is the `runAsyncImpl` method, which you override from `BaseAgent`.
- **Signature:** `protected Flowable runAsyncImpl(InvocationContext ctx)`
- **Reactive Stream (`Flowable`):** It must return an `io.reactivex.rxjava3.core.Flowable`. This `Flowable` represents a stream of events that will be produced by the custom agent's logic, often by combining or transforming multiple `Flowable` from sub-agents.
- **`ctx` (InvocationContext):** Provides access to crucial runtime information, most importantly `ctx.session().state()`, which is a `java.util.concurrent.ConcurrentMap`. This is the primary way to share data between steps orchestrated by your custom agent.
### Key capabilities within the core asynchronous method
1. **Calling Sub-Agents:** You invoke sub-agents (which are typically stored as instance attributes like `self.my_llm_agent`) using their `run_async` method and yield their events:
```python
async for event in self.some_sub_agent.run_async(ctx):
# Optionally inspect or log the event
yield event # Pass the event up
```
1. **Managing State:** Read from and write to the session state dictionary (`ctx.session.state`) to pass data between sub-agent calls or make decisions:
```python
# Read data set by a previous agent
previous_result = ctx.session.state.get("some_key")
# Make a decision based on state
if previous_result == "some_value":
# ... call a specific sub-agent ...
else:
# ... call another sub-agent ...
# Store a result for a later step (often done via a sub-agent's output_key)
# ctx.session.state["my_custom_result"] = "calculated_value"
```
1. **Implementing Control Flow:** Use standard Python constructs (`if`/`elif`/`else`, `for`/`while` loops, `try`/`except`) to create sophisticated, conditional, or iterative workflows involving your sub-agents.
1. **Calling Sub-Agents:** You invoke sub-agents (which are typically stored as instance properties like `this.myLlmAgent`) using their `run` method and yield their events:
```typescript
for await (const event of this.someSubAgent.runAsync(ctx)) {
// Optionally inspect or log the event
yield event; // Pass the event up to the runner
}
```
1. **Managing State:** Read from and write to the session state object (`ctx.session.state`) to pass data between sub-agent calls or make decisions:
```typescript
// Read data set by a previous agent
const previousResult = ctx.session.state['some_key'];
// Make a decision based on state
if (previousResult === 'some_value') {
// ... call a specific sub-agent ...
} else {
// ... call another sub-agent ...
}
// Store a result for a later step (often done via a sub-agent's outputKey)
// ctx.session.state['my_custom_result'] = 'calculated_value';
```
1. **Implementing Control Flow:** Use standard TypeScript/JavaScript constructs (`if`/`else`, `for`/`while` loops, `try`/`catch`) to create sophisticated, conditional, or iterative workflows involving your sub-agents.
1. **Calling Sub-Agents:** You invoke sub-agents by calling their `Run` method.
```go
// Example: Running one sub-agent and yielding its events
for event, err := range someSubAgent.Run(ctx) {
if err != nil {
// Handle or propagate the error
return
}
// Yield the event up to the caller
if !yield(event, nil) {
return
}
}
```
1. **Managing State:** Read from and write to the session state to pass data between sub-agent calls or make decisions.
```go
// The `ctx` (`agent.InvocationContext`) is passed directly to your agent's `Run` function.
// Read data set by a previous agent
previousResult, err := ctx.Session().State().Get("some_key")
if err != nil {
// Handle cases where the key might not exist yet
}
// Make a decision based on state
if val, ok := previousResult.(string); ok && val == "some_value" {
// ... call a specific sub-agent ...
} else {
// ... call another sub-agent ...
}
// Store a result for a later step
if err := ctx.Session().State().Set("my_custom_result", "calculated_value"); err != nil {
// Handle error
}
```
1. **Implementing Control Flow:** Use standard Go constructs (`if`/`else`, `for`/`switch` loops, goroutines, channels) to create sophisticated, conditional, or iterative workflows involving your sub-agents.
1. **Calling Sub-Agents:** You invoke sub-agents (which are typically stored as instance attributes or objects) using their asynchronous run method and return their event streams:
You typically chain `Flowable`s from sub-agents using RxJava operators like `concatWith`, `flatMapPublisher`, or `concatArray`.
```java
// Example: Running one sub-agent
// return someSubAgent.runAsync(ctx);
// Example: Running sub-agents sequentially
Flowable firstAgentEvents = someSubAgent1.runAsync(ctx)
.doOnNext(event -> System.out.println("Event from agent 1: " + event.id()));
Flowable secondAgentEvents = Flowable.defer(() ->
someSubAgent2.runAsync(ctx)
.doOnNext(event -> System.out.println("Event from agent 2: " + event.id()))
);
return firstAgentEvents.concatWith(secondAgentEvents);
```
The `Flowable.defer()` is often used for subsequent stages if their execution depends on the completion or state after prior stages.
1. **Managing State:** Read from and write to the session state to pass data between sub-agent calls or make decisions. The session state is a `java.util.concurrent.ConcurrentMap` obtained via `ctx.session().state()`.
```java
// Read data set by a previous agent
Object previousResult = ctx.session().state().get("some_key");
// Make a decision based on state
if ("some_value".equals(previousResult)) {
// ... logic to include a specific sub-agent's Flowable ...
} else {
// ... logic to include another sub-agent's Flowable ...
}
// Store a result for a later step (often done via a sub-agent's output_key)
// ctx.session().state().put("my_custom_result", "calculated_value");
```
1. **Implementing Control Flow:** Use standard language constructs (`if`/`else`, loops, `try`/`catch`) combined with reactive operators (RxJava) to create sophisticated workflows.
- **Conditional:** `Flowable.defer()` to choose which `Flowable` to subscribe to based on a condition, or `filter()` if you're filtering events within a stream.
- **Iterative:** Operators like `repeat()`, `retry()`, or by structuring your `Flowable` chain to recursively call parts of itself based on conditions (often managed with `flatMapPublisher` or `concatMap`).
## Managing sub-agents and state
Typically, a custom agent orchestrates other agents (like `LlmAgent`, `LoopAgent`, etc.).
- **Initialization:** You usually pass instances of these sub-agents into your custom agent's constructor and store them as instance fields/attributes (e.g., `this.story_generator = story_generator_instance` or `self.story_generator = story_generator_instance`). This makes them accessible within the custom agent's core asynchronous execution logic (such as: `_run_async_impl` method). In Python, `BaseAgent` is a Pydantic model configured with `extra='forbid'`, so declare each sub-agent you keep as an annotated class-level field, such as `story_generator: LlmAgent`, and pass it to `super().__init__(...)`. Assigning an undeclared attribute in `__init__` raises a `ValueError`.
- **Sub Agents List:** When initializing the `BaseAgent` using it's `super()` constructor, you should pass a `sub agents` list. This list tells the ADK framework about the agents that are part of this custom agent's immediate hierarchy. It's important for framework features like lifecycle management, introspection, and potentially future routing capabilities, even if your core execution logic (`_run_async_impl`) calls the agents directly via `self.xxx_agent`. Include the agents that your custom logic directly invokes at the top level.
- **State:** As mentioned, `ctx.session.state` is the standard way sub-agents (especially `LlmAgent`s using `output key`) communicate results back to the orchestrator and how the orchestrator passes necessary inputs down.
## Agent-based workflow primitives
The following sections detail the core ADK primitives—such as agent hierarchy, workflow agents, and interaction mechanisms—that enable you to construct and manage these multi-agent systems effectively. ADK provides core building blocks—primitives—that enable you to structure and manage interactions within your multi-agent system.
Note
The specific parameters or method names for the primitives may vary slightly by SDK language, for example `sub_agents` in Python, and `subAgents` in Java. Refer to the language-specific API documentation for details.
### Agent hierarchy: Parent agents and sub-agents
The foundation for structuring multi-agent systems is the parent-child relationship defined in `BaseAgent`.
- **Establishing Hierarchy:** You create a tree structure by passing a list of agent instances to the `sub_agents` argument when initializing a parent agent. ADK automatically sets the `parent_agent` attribute on each child agent during initialization.
- **Single Parent Rule:** An agent instance can only be added as a sub-agent once. Attempting to assign a second parent will result in a `ValueError`.
- **Importance:** This hierarchy defines the scope for [Workflow Agents](#workflow-agents-as-orchestrators) and influences the potential targets for LLM-Driven Delegation. You can navigate the hierarchy using `agent.parent_agent` or find descendants using `agent.find_agent(name)`.
```python
# Conceptual Example: Defining Hierarchy
from typing import AsyncGenerator
from google.adk.agents import LlmAgent, BaseAgent
from google.adk.agents.invocation_context import InvocationContext
from google.adk.events import Event
# A custom non-LLM agent must implement _run_async_impl; instantiating
# BaseAgent directly raises NotImplementedError when it runs.
class TaskExecutor(BaseAgent):
async def _run_async_impl(
self, ctx: InvocationContext
) -> AsyncGenerator[Event, None]:
yield Event(author=self.name)
# Define individual agents
greeter = LlmAgent(name="Greeter", model="gemini-flash-latest")
task_doer = TaskExecutor(name="TaskExecutor") # Custom non-LLM agent
# Create parent agent and assign children via sub_agents
coordinator = LlmAgent(
name="Coordinator",
model="gemini-flash-latest",
description="I coordinate greetings and tasks.",
sub_agents=[ # Assign sub_agents here
greeter,
task_doer
]
)
# Framework automatically sets:
# assert greeter.parent_agent == coordinator
# assert task_doer.parent_agent == coordinator
```
```typescript
// Conceptual Example: Defining Hierarchy
import { LlmAgent, BaseAgent, InvocationContext } from '@google/adk';
import type { Event, createEventActions } from '@google/adk';
class TaskExecutorAgent extends BaseAgent {
async *runAsyncImpl(context: InvocationContext): AsyncGenerator {
yield {
id: 'event-1',
invocationId: context.invocationId,
author: this.name,
content: { parts: [{ text: 'Task completed!' }] },
actions: createEventActions(),
timestamp: Date.now(),
};
}
async *runLiveImpl(context: InvocationContext): AsyncGenerator {
this.runAsyncImpl(context);
}
}
// Define individual agents
const greeter = new LlmAgent({name: 'Greeter', model: 'gemini-flash-latest'});
const taskDoer = new TaskExecutorAgent({name: 'TaskExecutor'}); // Custom non-LLM agent
// Create parent agent and assign children via subAgents
const coordinator = new LlmAgent({
name: 'Coordinator',
model: 'gemini-flash-latest',
description: 'I coordinate greetings and tasks.',
subAgents: [ // Assign subAgents here
greeter,
taskDoer
],
});
// Framework automatically sets:
// console.assert(greeter.parentAgent === coordinator);
// console.assert(taskDoer.parentAgent === coordinator);
```
```go
import (
"google.golang.org/adk/v2/agent"
"google.golang.org/adk/v2/agent/llmagent"
)
// Conceptual Example: Defining Hierarchy
// Define individual agents
greeter, _ := llmagent.New(llmagent.Config{Name: "Greeter", Model: m})
taskDoer, _ := agent.New(agent.Config{Name: "TaskExecutor"}) // Custom non-LLM agent
// Create parent agent and assign children via sub_agents
coordinator, _ := llmagent.New(llmagent.Config{
Name: "Coordinator",
Model: m,
Description: "I coordinate greetings and tasks.",
SubAgents: []agent.Agent{greeter, taskDoer}, // Assign sub_agents here
})
```
```java
// Conceptual Example: Defining Hierarchy
import com.google.adk.agents.SequentialAgent;
import com.google.adk.agents.LlmAgent;
// Define individual agents
LlmAgent greeter = LlmAgent.builder().name("Greeter").model("gemini-flash-latest").build();
SequentialAgent taskDoer = SequentialAgent.builder().name("TaskExecutor").subAgents(...).build(); // Sequential Agent
// Create parent agent and assign sub_agents
LlmAgent coordinator = LlmAgent.builder()
.name("Coordinator")
.model("gemini-flash-latest")
.description("I coordinate greetings and tasks")
.subAgents(greeter, taskDoer) // Assign sub_agents here
.build();
// Framework automatically sets:
// assert greeter.parentAgent().equals(coordinator);
// assert taskDoer.parentAgent().equals(coordinator);
```
```kotlin
class TaskExecutorAgent : BaseAgent(name = "TaskExecutor") {
override fun runAsyncImpl(context: InvocationContext): Flow {
return flowOf(
Event(
author = name,
content = Content(parts = listOf(Part(text = "Task completed!"))),
),
)
}
}
val greeter = LlmAgent(name = "Greeter", model = model)
val taskDoer = TaskExecutorAgent()
val coordinator =
LlmAgent(
name = "Coordinator",
model = model,
description = "I coordinate greetings and tasks.",
subAgents = listOf(greeter, taskDoer),
)
```
### Workflow agents as orchestrators
ADK includes specialized agents derived from `BaseAgent` that don't perform tasks themselves but orchestrate the execution flow of their `sub_agents`.
Deprecated in Python and TypeScript
In Python and TypeScript, `SequentialAgent`, `ParallelAgent`, and `LoopAgent` are deprecated in favor of `Workflow`: constructing one emits a deprecation warning, and the classes will be removed in a future release. A `Workflow` cannot be used as an `LlmAgent` sub-agent.
- **[`SequentialAgent`](https://adk.tools-online.site/agents/workflow-agents/sequential-agents/index.md):** Executes its `sub_agents` one after another in the order they are listed.
- **Context:** Passes the *same* [`InvocationContext`](https://adk.tools-online.site/runtime/index.md) sequentially, allowing agents to easily pass results via shared state.
```python
# Conceptual Example: Sequential Pipeline
from google.adk.agents import SequentialAgent, LlmAgent
step1 = LlmAgent(name="Step1_Fetch", output_key="data") # Saves output to state['data']
step2 = LlmAgent(name="Step2_Process", instruction="Process data from {data}.")
pipeline = SequentialAgent(name="MyPipeline", sub_agents=[step1, step2])
# When pipeline runs, Step2 can access the state['data'] set by Step1.
```
```typescript
// Conceptual Example: Sequential Pipeline
import { SequentialAgent, LlmAgent } from '@google/adk';
const step1 = new LlmAgent({name: 'Step1_Fetch', outputKey: 'data'}); // Saves output to state['data']
const step2 = new LlmAgent({name: 'Step2_Process', instruction: 'Process data from {data}.'});
const pipeline = new SequentialAgent({name: 'MyPipeline', subAgents: [step1, step2]});
// When pipeline runs, Step2 can access the state['data'] set by Step1.
```
```go
import (
"google.golang.org/adk/v2/agent"
"google.golang.org/adk/v2/agent/llmagent"
"google.golang.org/adk/v2/agent/workflowagents/sequentialagent"
)
// Conceptual Example: Sequential Pipeline
step1, _ := llmagent.New(llmagent.Config{Name: "Step1_Fetch", OutputKey: "data", Model: m}) // Saves output to state["data"]
step2, _ := llmagent.New(llmagent.Config{Name: "Step2_Process", Instruction: "Process data from {data}.", Model: m})
pipeline, _ := sequentialagent.New(sequentialagent.Config{
AgentConfig: agent.Config{Name: "MyPipeline", SubAgents: []agent.Agent{step1, step2}},
})
// When pipeline runs, Step2 can access the state["data"] set by Step1.
```
```java
// Conceptual Example: Sequential Pipeline
import com.google.adk.agents.SequentialAgent;
import com.google.adk.agents.LlmAgent;
LlmAgent step1 = LlmAgent.builder().name("Step1_Fetch").outputKey("data").build(); // Saves output to state.get("data")
LlmAgent step2 = LlmAgent.builder().name("Step2_Process").instruction("Process data from {data}.").build();
SequentialAgent pipeline = SequentialAgent.builder().name("MyPipeline").subAgents(step1, step2).build();
// When pipeline runs, Step2 can access the state.get("data") set by Step1.
```
```kotlin
val step1 = LlmAgent(name = "Step1_Fetch", model = model)
val step2 =
LlmAgent(
name = "Step2_Process",
model = model,
instruction = Instruction("Process data from state."),
)
val pipeline = SequentialAgent(name = "MyPipeline", subAgents = listOf(step1, step2))
```
- **[`ParallelAgent`](https://adk.tools-online.site/agents/workflow-agents/parallel-agents/index.md):** Executes its `sub_agents` in parallel. Events from sub-agents may be interleaved.
- **Context:** Modifies the `InvocationContext.branch` for each child agent (e.g., `ParentBranch.ChildName`), providing a distinct contextual path which can be useful for isolating history in some memory implementations.
- **State:** Despite different branches, all parallel children access the *same shared* `session.state`, enabling them to read initial state and write results (use distinct keys to avoid race conditions).
```python
# Conceptual Example: Parallel Execution
from google.adk.agents import ParallelAgent, LlmAgent
fetch_weather = LlmAgent(name="WeatherFetcher", output_key="weather")
fetch_news = LlmAgent(name="NewsFetcher", output_key="news")
gatherer = ParallelAgent(name="InfoGatherer", sub_agents=[fetch_weather, fetch_news])
# When gatherer runs, WeatherFetcher and NewsFetcher run concurrently.
# A subsequent agent could read state['weather'] and state['news'].
```
```typescript
// Conceptual Example: Parallel Execution
import { ParallelAgent, LlmAgent } from '@google/adk';
const fetchWeather = new LlmAgent({name: 'WeatherFetcher', outputKey: 'weather'});
const fetchNews = new LlmAgent({name: 'NewsFetcher', outputKey: 'news'});
const gatherer = new ParallelAgent({name: 'InfoGatherer', subAgents: [fetchWeather, fetchNews]});
// When gatherer runs, WeatherFetcher and NewsFetcher run concurrently.
// A subsequent agent could read state['weather'] and state['news'].
```
```go
import (
"google.golang.org/adk/v2/agent"
"google.golang.org/adk/v2/agent/llmagent"
"google.golang.org/adk/v2/agent/workflowagents/parallelagent"
)
// Conceptual Example: Parallel Execution
fetchWeather, _ := llmagent.New(llmagent.Config{Name: "WeatherFetcher", OutputKey: "weather", Model: m})
fetchNews, _ := llmagent.New(llmagent.Config{Name: "NewsFetcher", OutputKey: "news", Model: m})
gatherer, _ := parallelagent.New(parallelagent.Config{
AgentConfig: agent.Config{Name: "InfoGatherer", SubAgents: []agent.Agent{fetchWeather, fetchNews}},
})
// When gatherer runs, WeatherFetcher and NewsFetcher run concurrently.
// A subsequent agent could read state["weather"] and state["news"].
```
```java
// Conceptual Example: Parallel Execution
import com.google.adk.agents.LlmAgent;
import com.google.adk.agents.ParallelAgent;
LlmAgent fetchWeather = LlmAgent.builder()
.name("WeatherFetcher")
.outputKey("weather")
.build();
LlmAgent fetchNews = LlmAgent.builder()
.name("NewsFetcher")
.instruction("news")
.build();
ParallelAgent gatherer = ParallelAgent.builder()
.name("InfoGatherer")
.subAgents(fetchWeather, fetchNews)
.build();
// When gatherer runs, WeatherFetcher and NewsFetcher run concurrently.
// A subsequent agent could read state['weather'] and state['news'].
```
```kotlin
val fetchWeather = LlmAgent(name = "WeatherFetcher", model = model)
val fetchNews = LlmAgent(name = "NewsFetcher", model = model)
val gatherer = ParallelAgent(name = "InfoGatherer", subAgents = listOf(fetchWeather, fetchNews))
```
- **[`LoopAgent`](https://adk.tools-online.site/agents/workflow-agents/loop-agents/index.md):** Executes its `sub_agents` sequentially in a loop.
- **Termination:** The loop stops if the optional `max_iterations` is reached, or if any sub-agent returns an [`Event`](https://adk.tools-online.site/events/index.md) with `escalate=True` in its Event Actions.
- **Context & State:** Passes the *same* `InvocationContext` in each iteration, allowing state changes (e.g., counters, flags) to persist across loops.
```python
# Conceptual Example: Loop with Condition
from google.adk.agents import LoopAgent, LlmAgent, BaseAgent
from google.adk.events import Event, EventActions
from google.adk.agents.invocation_context import InvocationContext
from typing import AsyncGenerator
class CheckCondition(BaseAgent): # Custom agent to check state
async def _run_async_impl(self, ctx: InvocationContext) -> AsyncGenerator[Event, None]:
status = ctx.session.state.get("status", "pending")
is_done = (status == "completed")
yield Event(author=self.name, actions=EventActions(escalate=is_done)) # Escalate if done
process_step = LlmAgent(name="ProcessingStep") # Agent that might update state['status']
poller = LoopAgent(
name="StatusPoller",
max_iterations=10,
sub_agents=[process_step, CheckCondition(name="Checker")]
)
# When poller runs, it executes process_step then Checker repeatedly
# until Checker escalates (state['status'] == 'completed') or 10 iterations pass.
```
```typescript
// Conceptual Example: Loop with Condition
import { LoopAgent, LlmAgent, BaseAgent, InvocationContext } from '@google/adk';
import type { Event, createEventActions, EventActions } from '@google/adk';
class CheckConditionAgent extends BaseAgent { // Custom agent to check state
async *runAsyncImpl(ctx: InvocationContext): AsyncGenerator {
const status = ctx.session.state['status'] || 'pending';
const isDone = status === 'completed';
yield createEvent({ author: 'check_condition', actions: createEventActions({ escalate: isDone }) });
}
async *runLiveImpl(ctx: InvocationContext): AsyncGenerator {
// This is not implemented.
}
};
const processStep = new LlmAgent({name: 'ProcessingStep'}); // Agent that might update state['status']
const poller = new LoopAgent({
name: 'StatusPoller',
maxIterations: 10,
// Executes its sub_agents sequentially in a loop
subAgents: [processStep, new CheckConditionAgent ({name: 'Checker'})]
});
// When poller runs, it executes processStep then Checker repeatedly
// until Checker escalates (state['status'] === 'completed') or 10 iterations pass.
```
```go
import (
"iter"
"google.golang.org/adk/v2/agent"
"google.golang.org/adk/v2/agent/llmagent"
"google.golang.org/adk/v2/agent/workflowagents/loopagent"
"google.golang.org/adk/v2/session"
)
// Conceptual Example: Loop with Condition
// Custom agent to check state
checkCondition, _ := agent.New(agent.Config{
Name: "Checker",
Run: func(ctx agent.InvocationContext) iter.Seq2[*session.Event, error] {
return func(yield func(*session.Event, error) bool) {
status, err := ctx.Session().State().Get("status")
// If "status" is not in the state, default to "pending".
// This is idiomatic Go for handling a potential error on lookup.
if err != nil {
status = "pending"
}
isDone := status == "completed"
yield(&session.Event{Author: "Checker", Actions: session.EventActions{Escalate: isDone}}, nil)
}
},
})
processStep, _ := llmagent.New(llmagent.Config{Name: "ProcessingStep", Model: m}) // Agent that might update state["status"]
poller, _ := loopagent.New(loopagent.Config{
MaxIterations: 10,
AgentConfig: agent.Config{Name: "StatusPoller", SubAgents: []agent.Agent{processStep, checkCondition}},
})
// When poller runs, it executes processStep then Checker repeatedly
// until Checker escalates (state["status"] == "completed") or 10 iterations pass.
```
```java
// Conceptual Example: Loop with Condition
// Custom agent to check state and potentially escalate
public static class CheckConditionAgent extends BaseAgent {
public CheckConditionAgent(String name, String description) {
super(name, description, List.of(), null, null);
}
@Override
protected Flowable runAsyncImpl(InvocationContext ctx) {
String status = (String) ctx.session().state().getOrDefault("status", "pending");
boolean isDone = "completed".equalsIgnoreCase(status);
// Emit an event that signals to escalate (exit the loop) if the condition is met.
// If not done, the escalate flag will be false or absent, and the loop continues.
Event checkEvent = Event.builder()
.author(name())
.id(Event.generateEventId()) // Important to give events unique IDs
.actions(EventActions.builder().escalate(isDone).build()) // Escalate if done
.build();
return Flowable.just(checkEvent);
}
}
// Agent that might update state.put("status")
LlmAgent processingStepAgent = LlmAgent.builder().name("ProcessingStep").build();
// Custom agent instance for checking the condition
CheckConditionAgent conditionCheckerAgent = new CheckConditionAgent(
"ConditionChecker",
"Checks if the status is 'completed'."
);
LoopAgent poller = LoopAgent.builder().name("StatusPoller").maxIterations(10).subAgents(processingStepAgent, conditionCheckerAgent).build();
// When poller runs, it executes processingStepAgent then conditionCheckerAgent repeatedly
// until Checker escalates (state.get("status") == "completed") or 10 iterations pass.
```
```kotlin
class CheckConditionAgent(name: String) : BaseAgent(name = name) {
override fun runAsyncImpl(context: InvocationContext): Flow {
val status = context.session.state["status"] as? String ?: "pending"
val isDone = status == "completed"
return flowOf(
Event(
author = name,
actions = EventActions(escalate = isDone),
),
)
}
}
val processStep = LlmAgent(name = "ProcessingStep", model = model)
val checker = CheckConditionAgent(name = "Checker")
val poller =
LoopAgent(
name = "StatusPoller",
maxIterations = 10,
subAgents = listOf(processStep, checker),
)
```
### Interaction and communication mechanisms
Agents within a system often need to exchange data or trigger actions in one another. ADK facilitates this through:
#### Shared session state
The most fundamental way for agents operating within the same invocation (and thus sharing the same [`Session`](/sessions/session/) object via the `InvocationContext`) to communicate passively.
- **Mechanism:** One agent (or its tool/callback) writes a value (`context.state['data_key'] = processed_data`), and a subsequent agent reads it (`data = context.state.get('data_key')`). In Python, [`CallbackContext`](https://adk.tools-online.site/callbacks/index.md) and `ToolContext` are the same delta-aware context class, and every write to `context.state` is recorded as a `state_delta` on the event the agent emits.
- **Convenience:** The `output_key` property on [`LlmAgent`](https://adk.tools-online.site/agents/llm-agents/index.md) automatically saves the agent's final response text (or structured output) to the specified state key.
- **Nature:** Asynchronous, passive communication. Ideal for pipelines orchestrated by `SequentialAgent` or passing data across `LoopAgent` iterations.
- **See Also:** [State Management](https://adk.tools-online.site/sessions/state/index.md)
Invocation Context and `temp:` State
When a parent agent invokes a sub-agent, it passes the same `InvocationContext`. This means they share the same temporary (`temp:`) state, which is ideal for passing data that is only relevant for the current turn.
```python
# Conceptual Example: Using output_key and reading state
from google.adk.agents import LlmAgent, SequentialAgent
agent_A = LlmAgent(name="AgentA", instruction="Find the capital of France.", output_key="capital_city")
agent_B = LlmAgent(name="AgentB", instruction="Tell me about the city stored in {capital_city}.")
pipeline = SequentialAgent(name="CityInfo", sub_agents=[agent_A, agent_B])
# AgentA runs, saves "Paris" to state['capital_city'].
# AgentB runs, its instruction processor reads state['capital_city'] to get "Paris".
```
```typescript
// Conceptual Example: Using outputKey and reading state
import { LlmAgent, SequentialAgent } from '@google/adk';
const agentA = new LlmAgent({name: 'AgentA', instruction: 'Find the capital of France.', outputKey: 'capital_city'});
const agentB = new LlmAgent({name: 'AgentB', instruction: 'Tell me about the city stored in {capital_city}.'});
const pipeline = new SequentialAgent({name: 'CityInfo', subAgents: [agentA, agentB]});
// AgentA runs, saves "Paris" to state['capital_city'].
// AgentB runs, its instruction processor reads state['capital_city'] to get "Paris".
```
```go
import (
"google.golang.org/adk/v2/agent"
"google.golang.org/adk/v2/agent/llmagent"
"google.golang.org/adk/v2/agent/workflowagents/sequentialagent"
)
// Conceptual Example: Using output_key and reading state
agentA, _ := llmagent.New(llmagent.Config{Name: "AgentA", Instruction: "Find the capital of France.", OutputKey: "capital_city", Model: m})
agentB, _ := llmagent.New(llmagent.Config{Name: "AgentB", Instruction: "Tell me about the city stored in {capital_city}.", Model: m})
pipeline2, _ := sequentialagent.New(sequentialagent.Config{
AgentConfig: agent.Config{Name: "CityInfo", SubAgents: []agent.Agent{agentA, agentB}},
})
// AgentA runs, saves "Paris" to state["capital_city"].
// AgentB runs, its instruction processor reads state["capital_city"] to get "Paris".
```
```java
// Conceptual Example: Using outputKey and reading state
import com.google.adk.agents.LlmAgent;
import com.google.adk.agents.SequentialAgent;
LlmAgent agentA = LlmAgent.builder()
.name("AgentA")
.instruction("Find the capital of France.")
.outputKey("capital_city")
.build();
LlmAgent agentB = LlmAgent.builder()
.name("AgentB")
.instruction("Tell me about the city stored in {capital_city}.")
.outputKey("capital_city")
.build();
SequentialAgent pipeline = SequentialAgent.builder().name("CityInfo").subAgents(agentA, agentB).build();
// AgentA runs, saves "Paris" to state('capital_city').
// AgentB runs, its instruction processor reads state.get("capital_city") to get "Paris".
```
```kotlin
val agentA =
LlmAgent(
name = "AgentA",
model = model,
instruction = Instruction("Find the capital of France."),
)
val agentB =
LlmAgent(
name = "AgentB",
model = model,
instruction = Instruction("Tell me about the city stored in state."),
)
val cityPipeline = SequentialAgent(name = "CityInfo", subAgents = listOf(agentA, agentB))
```
#### LLM delegation and agent transfer
Leverages an [`LlmAgent`](https://adk.tools-online.site/agents/llm-agents/index.md)'s understanding to dynamically route tasks to other suitable agents within the hierarchy.
- **Mechanism:** The agent's LLM generates a specific function call: `transfer_to_agent(agent_name='target_agent_name')`.
- **Handling:** The `AutoFlow`, used by default when sub-agents are present or transfer isn't disallowed, intercepts this call. It identifies the target agent using `root_agent.find_agent()` and updates the `InvocationContext` to switch execution focus.
- **Requires:** The calling `LlmAgent` needs clear `instructions` on when to transfer, and potential target agents need distinct `description`s for the LLM to make informed decisions. Transfer scope (parent, sub-agent, siblings) can be configured on the `LlmAgent`.
- **Nature:** Dynamic, flexible routing based on LLM interpretation.
```python
# Conceptual Setup: LLM Transfer
from google.adk.agents import LlmAgent
booking_agent = LlmAgent(name="Booker", description="Handles flight and hotel bookings.")
info_agent = LlmAgent(name="Info", description="Provides general information and answers questions.")
coordinator = LlmAgent(
name="Coordinator",
model="gemini-flash-latest",
instruction="You are an assistant. Delegate booking tasks to Booker and info requests to Info.",
description="Main coordinator.",
# AutoFlow is typically used implicitly here
sub_agents=[booking_agent, info_agent]
)
# If coordinator receives "Book a flight", its LLM should generate:
# FunctionCall(name='transfer_to_agent', args={'agent_name': 'Booker'})
# ADK framework then routes execution to booking_agent.
```
```typescript
// Conceptual Setup: LLM Transfer
import { LlmAgent } from '@google/adk';
const bookingAgent = new LlmAgent({name: 'Booker', description: 'Handles flight and hotel bookings.'});
const infoAgent = new LlmAgent({name: 'Info', description: 'Provides general information and answers questions.'});
const coordinator = new LlmAgent({
name: 'Coordinator',
model: 'gemini-flash-latest',
instruction: 'You are an assistant. Delegate booking tasks to Booker and info requests to Info.',
description: 'Main coordinator.',
// AutoFlow is typically used implicitly here
subAgents: [bookingAgent, infoAgent]
});
// If coordinator receives "Book a flight", its LLM should generate:
// {functionCall: {name: 'transfer_to_agent', args: {agent_name: 'Booker'}}}
// ADK framework then routes execution to bookingAgent.
```
```go
import (
"google.golang.org/adk/v2/agent/llmagent"
)
// Conceptual Setup: LLM Transfer
bookingAgent, _ := llmagent.New(llmagent.Config{Name: "Booker", Description: "Handles flight and hotel bookings.", Model: m})
infoAgent, _ := llmagent.New(llmagent.Config{Name: "Info", Description: "Provides general information and answers questions.", Model: m})
coordinator, _ = llmagent.New(llmagent.Config{
Name: "Coordinator",
Model: m,
Instruction: "You are an assistant. Delegate booking tasks to Booker and info requests to Info.",
Description: "Main coordinator.",
SubAgents: []agent.Agent{bookingAgent, infoAgent},
})
// If coordinator receives "Book a flight", its LLM should generate:
// FunctionCall{Name: "transfer_to_agent", Args: map[string]any{"agent_name": "Booker"}}
// ADK framework then routes execution to bookingAgent.
```
```java
// Conceptual Setup: LLM Transfer
import com.google.adk.agents.LlmAgent;
LlmAgent bookingAgent = LlmAgent.builder()
.name("Booker")
.description("Handles flight and hotel bookings.")
.build();
LlmAgent infoAgent = LlmAgent.builder()
.name("Info")
.description("Provides general information and answers questions.")
.build();
// Define the coordinator agent
LlmAgent coordinator = LlmAgent.builder()
.name("Coordinator")
.model("gemini-flash-latest") // Or your desired model
.instruction("You are an assistant. Delegate booking tasks to Booker and info requests to Info.")
.description("Main coordinator.")
// AutoFlow will be used by default (implicitly) because subAgents are present
// and transfer is not disallowed.
.subAgents(bookingAgent, infoAgent)
.build();
// If coordinator receives "Book a flight", its LLM should generate:
// FunctionCall.builder.name("transferToAgent").args(ImmutableMap.of("agent_name", "Booker")).build()
// ADK framework then routes execution to bookingAgent.
```
```kotlin
val bookingAgent =
LlmAgent(
name = "Booker",
model = model,
description = "Handles flight and hotel bookings.",
)
val infoAgent =
LlmAgent(
name = "Info",
model = model,
description = "Provides general information and answers questions.",
)
val transferCoordinator =
LlmAgent(
name = "Coordinator",
model = model,
instruction =
Instruction(
"You are an assistant. Delegate booking tasks to Booker and info requests to Info.",
),
description = "Main coordinator.",
subAgents = listOf(bookingAgent, infoAgent),
)
```
#### Explicit invocation with `AgentTool`
Allows an [`LlmAgent`](https://adk.tools-online.site/agents/llm-agents/index.md) to treat another `BaseAgent` instance as a callable function or [Tool](/tools-custom/).
- **Mechanism:** Wrap the target agent instance in `AgentTool` and include it in the parent `LlmAgent`'s `tools` list. `AgentTool` generates a corresponding function declaration for the LLM.
- **Handling:** When the parent LLM generates a function call targeting the `AgentTool`, the framework executes `AgentTool.run_async`. This method runs the target agent, captures its final response, forwards any state/artifact changes back to the parent's context, and returns the response as the tool's result.
- **Nature:** Synchronous (within the parent's flow), explicit, controlled invocation like any other tool.
- **(Note:** `AgentTool` needs to be imported and used explicitly).
```python
# Conceptual Setup: Agent as a Tool
from google.adk import Event
from google.adk.agents import LlmAgent, BaseAgent
from google.adk.tools import agent_tool
from google.genai import types
from pydantic import BaseModel
# Define a target agent (could be LlmAgent or custom BaseAgent)
class ImageGeneratorAgent(BaseAgent): # Example custom agent
name: str = "ImageGen"
description: str = "Generates an image based on a prompt."
# ... internal logic ...
async def _run_async_impl(self, ctx): # Simplified run logic
prompt = ctx.session.state.get("image_prompt", "default prompt")
# ... generate image bytes ...
image_bytes = b"..."
yield Event(author=self.name, content=types.Content(parts=[types.Part.from_bytes(data=image_bytes, mime_type="image/png")]))
image_agent = ImageGeneratorAgent()
image_tool = agent_tool.AgentTool(agent=image_agent) # Wrap the agent
# Parent agent uses the AgentTool
artist_agent = LlmAgent(
name="Artist",
model="gemini-flash-latest",
instruction="Create a prompt and use the ImageGen tool to generate the image.",
tools=[image_tool] # Include the AgentTool
)
# Artist LLM generates a prompt, then calls:
# FunctionCall(name='ImageGen', args={'image_prompt': 'a cat wearing a hat'})
# Framework calls image_tool.run_async(...), which runs ImageGeneratorAgent.
# The resulting image Part is returned to the Artist agent as the tool result.
```
```typescript
// Conceptual Setup: Agent as a Tool
import { LlmAgent, BaseAgent, AgentTool, InvocationContext } from '@google/adk';
import type { Part, createEvent, Event } from '@google/genai';
// Define a target agent (could be LlmAgent or custom BaseAgent)
class ImageGeneratorAgent extends BaseAgent { // Example custom agent
constructor() {
super({name: 'ImageGen', description: 'Generates an image based on a prompt.'});
}
// ... internal logic ...
async *runAsyncImpl(ctx: InvocationContext): AsyncGenerator { // Simplified run logic
const prompt = ctx.session.state['image_prompt'] || 'default prompt';
// ... generate image bytes ...
const imageBytes = new Uint8Array(); // placeholder
const imagePart: Part = {inlineData: {data: Buffer.from(imageBytes).toString('base64'), mimeType: 'image/png'}};
yield createEvent({content: {parts: [imagePart]}});
}
async *runLiveImpl(ctx: InvocationContext): AsyncGenerator {
// Not implemented for this agent.
}
}
const imageAgent = new ImageGeneratorAgent();
const imageTool = new AgentTool({agent: imageAgent}); // Wrap the agent
// Parent agent uses the AgentTool
const artistAgent = new LlmAgent({
name: 'Artist',
model: 'gemini-flash-latest',
instruction: 'Create a prompt and use the ImageGen tool to generate the image.',
tools: [imageTool] // Include the AgentTool
});
// Artist LLM generates a prompt, then calls:
// {functionCall: {name: 'ImageGen', args: {image_prompt: 'a cat wearing a hat'}}}
// Framework calls imageTool.runAsync(...), which runs ImageGeneratorAgent.
// The resulting image Part is returned to the Artist agent as the tool result.
```
```go
import (
"fmt"
"iter"
"google.golang.org/adk/v2/agent"
"google.golang.org/adk/v2/agent/llmagent"
"google.golang.org/adk/v2/model"
"google.golang.org/adk/v2/session"
"google.golang.org/adk/v2/tool"
"google.golang.org/adk/v2/tool/agenttool"
"google.golang.org/genai"
)
// Conceptual Setup: Agent as a Tool
// Define a target agent (could be LlmAgent or custom BaseAgent)
imageAgent, _ := agent.New(agent.Config{
Name: "ImageGen",
Description: "Generates an image based on a prompt.",
Run: func(ctx agent.InvocationContext) iter.Seq2[*session.Event, error] {
return func(yield func(*session.Event, error) bool) {
prompt, _ := ctx.Session().State().Get("image_prompt")
fmt.Printf("Generating image for prompt: %v\n", prompt)
imageBytes := []byte("...") // Simulate image bytes
yield(&session.Event{
Author: "ImageGen",
LLMResponse: model.LLMResponse{
Content: &genai.Content{
Parts: []*genai.Part{genai.NewPartFromBytes(imageBytes, "image/png")},
},
},
}, nil)
}
},
})
// Wrap the agent
imageTool := agenttool.New(imageAgent, nil)
// Now imageTool can be used as a tool by other agents.
// Parent agent uses the AgentTool
artistAgent, _ := llmagent.New(llmagent.Config{
Name: "Artist",
Model: m,
Instruction: "Create a prompt and use the ImageGen tool to generate the image.",
Tools: []tool.Tool{imageTool}, // Include the AgentTool
})
// Artist LLM generates a prompt, then calls:
// FunctionCall{Name: "ImageGen", Args: map[string]any{"image_prompt": "a cat wearing a hat"}}
// Framework calls imageTool.Run(...), which runs ImageGeneratorAgent.
// The resulting image Part is returned to the Artist agent as the tool result.
```
```java
// Conceptual Setup: Agent as a Tool
import com.google.adk.agents.BaseAgent;
import com.google.adk.agents.LlmAgent;
import com.google.adk.tools.AgentTool;
// Example custom agent (could be LlmAgent or custom BaseAgent)
public class ImageGeneratorAgent extends BaseAgent {
public ImageGeneratorAgent(String name, String description) {
super(name, description, List.of(), null, null);
}
// ... internal logic ...
@Override
protected Flowable runAsyncImpl(InvocationContext invocationContext) { // Simplified run logic
invocationContext.session().state().get("image_prompt");
// Generate image bytes
// ...
Event responseEvent = Event.builder()
.author(this.name())
.content(Content.fromParts(Part.fromText("...")))
.build();
return Flowable.just(responseEvent);
}
@Override
protected Flowable runLiveImpl(InvocationContext invocationContext) {
return null;
}
}
// Wrap the agent using AgentTool
ImageGeneratorAgent imageAgent = new ImageGeneratorAgent("image_agent", "generates images");
AgentTool imageTool = AgentTool.create(imageAgent);
// Parent agent uses the AgentTool
LlmAgent artistAgent = LlmAgent.builder()
.name("Artist")
.model("gemini-flash-latest")
.instruction(
"You are an artist. Create a detailed prompt for an image and then " +
"use the 'ImageGen' tool to generate the image. " +
"The 'ImageGen' tool expects a single string argument named 'request' " +
"containing the image prompt. The tool will return a JSON string in its " +
"'result' field, containing 'image_base64', 'mime_type', and 'status'."
)
.description("An agent that can create images using a generation tool.")
.tools(imageTool) // Include the AgentTool
.build();
// Artist LLM generates a prompt, then calls:
// FunctionCall(name='ImageGen', args={'imagePrompt': 'a cat wearing a hat'})
// Framework calls imageTool.runAsync(...), which runs ImageGeneratorAgent.
// The resulting image Part is returned to the Artist agent as the tool result.
```
```kotlin
val imageAgent =
LlmAgent(
name = "ImageGen",
model = model,
description = "Generates an image based on a prompt.",
)
val imageTool = AgentTool(agent = imageAgent)
val artistAgent =
LlmAgent(
name = "Artist",
model = model,
instruction =
Instruction(
"Create a prompt and use the ImageGen tool to generate the image.",
),
tools = listOf(imageTool),
)
```
These primitives provide the flexibility to design multi-agent interactions ranging from tightly coupled sequential workflows to dynamic, LLM-driven delegation networks.
## Design pattern example: StoryFlow Agent
Let's illustrate the power of custom agents with an example pattern: a multi-stage content generation workflow with conditional logic.
**Goal:** Create a system that generates a story, iteratively refines it through critique and revision, performs final checks, and crucially, *regenerates the story if the final tone check fails*.
**Why Custom?** The core requirement driving the need for a custom agent here is the **conditional regeneration based on the tone check**. Standard workflow agents don't have built-in conditional branching based on the outcome of a sub-agent's task. We need custom logic (`if tone == "negative": ...`) within the orchestrator.
______________________________________________________________________
### Part 1: Simplified custom agent initialization
We define the `StoryFlowAgent` inheriting from `BaseAgent`. In `__init__`, we store the necessary sub-agents (passed in) as instance attributes and tell the `BaseAgent` framework about the top-level agents this custom agent will directly orchestrate.
```python
class StoryFlowAgent(BaseAgent):
"""
Custom agent for a story generation and refinement workflow.
This agent orchestrates a sequence of LLM agents to generate a story,
critique it, revise it, check grammar and tone, and potentially
regenerate the story if the tone is negative.
"""
# --- Field Declarations for Pydantic ---
# Declare the agents passed during initialization as class attributes with type hints
story_generator: LlmAgent
critic: LlmAgent
reviser: LlmAgent
grammar_check: LlmAgent
tone_check: LlmAgent
loop_agent: LoopAgent
sequential_agent: SequentialAgent
# model_config allows setting Pydantic configurations if needed, e.g., arbitrary_types_allowed
model_config = {"arbitrary_types_allowed": True}
def __init__(
self,
name: str,
story_generator: LlmAgent,
critic: LlmAgent,
reviser: LlmAgent,
grammar_check: LlmAgent,
tone_check: LlmAgent,
):
"""
Initializes the StoryFlowAgent.
Args:
name: The name of the agent.
story_generator: An LlmAgent to generate the initial story.
critic: An LlmAgent to critique the story.
reviser: An LlmAgent to revise the story based on criticism.
grammar_check: An LlmAgent to check the grammar.
tone_check: An LlmAgent to analyze the tone.
"""
# Create internal agents *before* calling super().__init__
loop_agent = LoopAgent(
name="CriticReviserLoop", sub_agents=[critic, reviser], max_iterations=2
)
sequential_agent = SequentialAgent(
name="PostProcessing", sub_agents=[grammar_check, tone_check]
)
# Define the sub_agents list for the framework
sub_agents_list = [
story_generator,
loop_agent,
sequential_agent,
]
# Pydantic will validate and assign them based on the class annotations.
super().__init__(
name=name,
story_generator=story_generator,
critic=critic,
reviser=reviser,
grammar_check=grammar_check,
tone_check=tone_check,
loop_agent=loop_agent,
sequential_agent=sequential_agent,
sub_agents=sub_agents_list, # Pass the sub_agents list directly
)
```
We define the `StoryFlowAgent` by extending `BaseAgent`. In its constructor, we:
1. Create any internal composite agents (like `LoopAgent` or `SequentialAgent`).
1. Pass the list of all top-level sub-agents to the `super()` constructor.
1. Store the sub-agents (passed in or created internally) as instance properties (e.g., `this.storyGenerator`) so they can be accessed in the custom `runImpl` logic.
```typescript
class StoryFlowAgent extends BaseAgent {
// --- Property Declarations for TypeScript ---
private storyGenerator: LlmAgent;
private critic: LlmAgent;
private reviser: LlmAgent;
private grammarCheck: LlmAgent;
private toneCheck: LlmAgent;
private loopAgent: LoopAgent;
private sequentialAgent: SequentialAgent;
constructor(
name: string,
storyGenerator: LlmAgent,
critic: LlmAgent,
reviser: LlmAgent,
grammarCheck: LlmAgent,
toneCheck: LlmAgent
) {
// Create internal composite agents
const loopAgent = new LoopAgent({
name: "CriticReviserLoop",
subAgents: [critic, reviser],
maxIterations: 2,
});
const sequentialAgent = new SequentialAgent({
name: "PostProcessing",
subAgents: [grammarCheck, toneCheck],
});
// Define the sub-agents for the framework to know about
const subAgentsList = [
storyGenerator,
loopAgent,
sequentialAgent,
];
// Call the parent constructor
super({
name,
subAgents: subAgentsList,
});
// Assign agents to class properties for use in the custom run logic
this.storyGenerator = storyGenerator;
this.critic = critic;
this.reviser = reviser;
this.grammarCheck = grammarCheck;
this.toneCheck = toneCheck;
this.loopAgent = loopAgent;
this.sequentialAgent = sequentialAgent;
}
```
We define the `StoryFlowAgent` struct and a constructor. In the constructor, we store the necessary sub-agents and tell the `BaseAgent` framework about the top-level agents this custom agent will directly orchestrate.
```go
// StoryFlowAgent is a custom agent that orchestrates a story generation workflow.
// It encapsulates the logic of running sub-agents in a specific sequence.
type StoryFlowAgent struct {
storyGenerator agent.Agent
revisionLoopAgent agent.Agent
postProcessorAgent agent.Agent
}
// NewStoryFlowAgent creates and configures the entire custom agent workflow.
// It takes individual LLM agents as input and internally creates the necessary
// workflow agents (loop, sequential), returning the final orchestrator agent.
func NewStoryFlowAgent(
storyGenerator,
critic,
reviser,
grammarCheck,
toneCheck agent.Agent,
) (agent.Agent, error) {
loopAgent, err := loopagent.New(loopagent.Config{
MaxIterations: 2,
AgentConfig: agent.Config{
Name: "CriticReviserLoop",
SubAgents: []agent.Agent{critic, reviser},
},
})
if err != nil {
return nil, fmt.Errorf("failed to create loop agent: %w", err)
}
sequentialAgent, err := sequentialagent.New(sequentialagent.Config{
AgentConfig: agent.Config{
Name: "PostProcessing",
SubAgents: []agent.Agent{grammarCheck, toneCheck},
},
})
if err != nil {
return nil, fmt.Errorf("failed to create sequential agent: %w", err)
}
// The StoryFlowAgent struct holds the agents needed for the Run method.
orchestrator := &StoryFlowAgent{
storyGenerator: storyGenerator,
revisionLoopAgent: loopAgent,
postProcessorAgent: sequentialAgent,
}
// agent.New creates the final agent, wiring up the Run method.
return agent.New(agent.Config{
Name: "StoryFlowAgent",
Description: "Orchestrates story generation, critique, revision, and checks.",
SubAgents: []agent.Agent{storyGenerator, loopAgent, sequentialAgent},
Run: orchestrator.Run,
})
}
```
We define the `StoryFlowAgentExample` by extending `BaseAgent`. In its **constructor**, we store the necessary sub-agent instances (passed as parameters) as instance fields. These top-level sub-agents, which this custom agent will directly orchestrate, are also passed to the `super` constructor of `BaseAgent` as a list.
```java
private final LlmAgent storyGenerator;
private final LoopAgent loopAgent;
private final SequentialAgent sequentialAgent;
public StoryFlowAgentExample(
String name, LlmAgent storyGenerator, LoopAgent loopAgent, SequentialAgent sequentialAgent) {
super(
name,
"Orchestrates story generation, critique, revision, and checks.",
List.of(storyGenerator, loopAgent, sequentialAgent),
null,
null);
this.storyGenerator = storyGenerator;
this.loopAgent = loopAgent;
this.sequentialAgent = sequentialAgent;
}
```
______________________________________________________________________
### Part 2: Define custom execution logic
This method orchestrates the sub-agents using standard Python async/await and control flow.
```python
@override
async def _run_async_impl(
self, ctx: InvocationContext
) -> AsyncGenerator[Event, None]:
"""
Implements the custom orchestration logic for the story workflow.
Uses the instance attributes assigned by Pydantic (e.g., self.story_generator).
"""
logger.info(f"[{self.name}] Starting story generation workflow.")
# 1. Initial Story Generation
logger.info(f"[{self.name}] Running StoryGenerator...")
async for event in self.story_generator.run_async(ctx):
logger.info(f"[{self.name}] Event from StoryGenerator: {event.model_dump_json(indent=2, exclude_none=True)}")
yield event
# Check if story was generated before proceeding
if "current_story" not in ctx.session.state or not ctx.session.state["current_story"]:
logger.error(f"[{self.name}] Failed to generate initial story. Aborting workflow.")
return # Stop processing if initial story failed
logger.info(f"[{self.name}] Story state after generator: {ctx.session.state.get('current_story')}")
# 2. Critic-Reviser Loop
logger.info(f"[{self.name}] Running CriticReviserLoop...")
# Use the loop_agent instance attribute assigned during init
async for event in self.loop_agent.run_async(ctx):
logger.info(f"[{self.name}] Event from CriticReviserLoop: {event.model_dump_json(indent=2, exclude_none=True)}")
yield event
logger.info(f"[{self.name}] Story state after loop: {ctx.session.state.get('current_story')}")
# 3. Sequential Post-Processing (Grammar and Tone Check)
logger.info(f"[{self.name}] Running PostProcessing...")
# Use the sequential_agent instance attribute assigned during init
async for event in self.sequential_agent.run_async(ctx):
logger.info(f"[{self.name}] Event from PostProcessing: {event.model_dump_json(indent=2, exclude_none=True)}")
yield event
# 4. Tone-Based Conditional Logic
tone_check_result = ctx.session.state.get("tone_check_result")
logger.info(f"[{self.name}] Tone check result: {tone_check_result}")
if tone_check_result == "negative":
logger.info(f"[{self.name}] Tone is negative. Regenerating story...")
async for event in self.story_generator.run_async(ctx):
logger.info(f"[{self.name}] Event from StoryGenerator (Regen): {event.model_dump_json(indent=2, exclude_none=True)}")
yield event
else:
logger.info(f"[{self.name}] Tone is not negative. Keeping current story.")
pass
logger.info(f"[{self.name}] Workflow finished.")
```
**Explanation of Logic:**
1. The initial `story_generator` runs. Its output is expected to be in `ctx.session.state["current_story"]`.
1. The `loop_agent` runs, which internally calls the `critic` and `reviser` sequentially for `max_iterations` times. They read/write `current_story` and `criticism` from/to the state.
1. The `sequential_agent` runs, calling `grammar_check` then `tone_check`, reading `current_story` and writing `grammar_suggestions` and `tone_check_result` to the state.
1. **Custom Part:** The `if` statement checks the `tone_check_result` from the state. If it's "negative", the `story_generator` is called *again*, overwriting the `current_story` in the state. Otherwise, the flow ends.
The `runImpl` method orchestrates the sub-agents using standard TypeScript `async`/`await` and control flow. The `runLiveImpl` is also added to handle live streaming scenarios.
```typescript
// Implements the custom orchestration logic for the story workflow.
async* runLiveImpl(ctx: InvocationContext): AsyncGenerator {
yield* this.runAsyncImpl(ctx);
}
// Implements the custom orchestration logic for the story workflow.
async* runAsyncImpl(ctx: InvocationContext): AsyncGenerator {
console.log(`[${this.name}] Starting story generation workflow.`);
// 1. Initial Story Generation
console.log(`[${this.name}] Running StoryGenerator...`);
for await (const event of this.storyGenerator.runAsync(ctx)) {
console.log(`[${this.name}] Event from StoryGenerator: ${JSON.stringify(event, null, 2)}`);
yield event;
}
// Check if the story was generated before proceeding
if (!ctx.session.state["current_story"]) {
console.error(`[${this.name}] Failed to generate initial story. Aborting workflow.`);
return; // Stop processing
}
console.log(`[${this.name}] Story state after generator: ${ctx.session.state['current_story']}`);
// 2. Critic-Reviser Loop
console.log(`[${this.name}] Running CriticReviserLoop...`);
for await (const event of this.loopAgent.runAsync(ctx)) {
console.log(`[${this.name}] Event from CriticReviserLoop: ${JSON.stringify(event, null, 2)}`);
yield event;
}
console.log(`[${this.name}] Story state after loop: ${ctx.session.state['current_story']}`);
// 3. Sequential Post-Processing (Grammar and Tone Check)
console.log(`[${this.name}] Running PostProcessing...`);
for await (const event of this.sequentialAgent.runAsync(ctx)) {
console.log(`[${this.name}] Event from PostProcessing: ${JSON.stringify(event, null, 2)}`);
yield event;
}
// 4. Tone-Based Conditional Logic
const toneCheckResult = ctx.session.state["tone_check_result"] as string;
console.log(`[${this.name}] Tone check result: ${toneCheckResult}`);
if (toneCheckResult === "negative") {
console.log(`[${this.name}] Tone is negative. Regenerating story...`);
for await (const event of this.storyGenerator.runAsync(ctx)) {
console.log(`[${this.name}] Event from StoryGenerator (Regen): ${JSON.stringify(event, null, 2)}`);
yield event;
}
} else {
console.log(`[${this.name}] Tone is not negative. Keeping current story.`);
}
console.log(`[${this.name}] Workflow finished.`);
}
```
**Explanation of Logic:**
1. The initial `storyGenerator` runs. Its output is expected to be in `ctx.session.state['current_story']`.
1. The `loopAgent` runs, which internally calls the `critic` and `reviser` sequentially for `maxIterations` times. They read/write `current_story` and `criticism` from/to the state.
1. The `sequentialAgent` runs, calling `grammarCheck` then `toneCheck`, reading `current_story` and writing `grammar_suggestions` and `tone_check_result` to the state.
1. **Custom Part:** The `if` statement checks the `tone_check_result` from the state. If it's "negative", the `storyGenerator` is called *again*, overwriting the `current_story` in the state. Otherwise, the flow ends.
The `Run` method orchestrates the sub-agents by calling their respective `Run` methods in a loop and yielding their events.
```go
// Run defines the custom execution logic for the StoryFlowAgent.
func (s *StoryFlowAgent) Run(ctx agent.InvocationContext) iter.Seq2[*session.Event, error] {
return func(yield func(*session.Event, error) bool) {
// Stage 1: Initial Story Generation
for event, err := range s.storyGenerator.Run(ctx) {
if err != nil {
yield(nil, fmt.Errorf("story generator failed: %w", err))
return
}
if !yield(event, nil) {
return
}
}
// Check if story was generated before proceeding
currentStory, err := ctx.Session().State().Get("current_story")
if err != nil || currentStory == "" {
log.Println("Failed to generate initial story. Aborting workflow.")
return
}
// Stage 2: Critic-Reviser Loop
for event, err := range s.revisionLoopAgent.Run(ctx) {
if err != nil {
yield(nil, fmt.Errorf("loop agent failed: %w", err))
return
}
if !yield(event, nil) {
return
}
}
// Stage 3: Post-Processing
for event, err := range s.postProcessorAgent.Run(ctx) {
if err != nil {
yield(nil, fmt.Errorf("sequential agent failed: %w", err))
return
}
if !yield(event, nil) {
return
}
}
// Stage 4: Conditional Regeneration
toneResult, err := ctx.Session().State().Get("tone_check_result")
if err != nil {
log.Printf("Could not read tone_check_result from state: %v. Assuming tone is not negative.", err)
return
}
if tone, ok := toneResult.(string); ok && tone == "negative" {
log.Println("Tone is negative. Regenerating story...")
for event, err := range s.storyGenerator.Run(ctx) {
if err != nil {
yield(nil, fmt.Errorf("story regeneration failed: %w", err))
return
}
if !yield(event, nil) {
return
}
}
} else {
log.Println("Tone is not negative. Keeping current story.")
}
}
}
```
**Explanation of Logic:**
1. The initial `storyGenerator` runs. Its output is expected to be in the session state under the key `"current_story"`.
1. The `revisionLoopAgent` runs, which internally calls the `critic` and `reviser` sequentially for `max_iterations` times. They read/write `current_story` and `criticism` from/to the state.
1. The `postProcessorAgent` runs, calling `grammar_check` then `tone_check`, reading `current_story` and writing `grammar_suggestions` and `tone_check_result` to the state.
1. **Custom Part:** The code checks the `tone_check_result` from the state. If it's "negative", the `story_generator` is called *again*, overwriting the `current_story` in the state. Otherwise, the flow ends.
The `runAsyncImpl` method orchestrates the sub-agents using RxJava's Flowable streams and operators for asynchronous control flow.
```java
@Override
protected Flowable runAsyncImpl(InvocationContext invocationContext) {
// Implements the custom orchestration logic for the story workflow.
// Uses the instance attributes assigned by Pydantic (e.g., self.story_generator).
logger.log(Level.INFO, () -> String.format("[%s] Starting story generation workflow.", name()));
// Stage 1. Initial Story Generation
Flowable storyGenFlow = runStage(storyGenerator, invocationContext, "StoryGenerator");
// Stage 2: Critic-Reviser Loop (runs after story generation completes)
Flowable criticReviserFlow = Flowable.defer(() -> {
if (!isStoryGenerated(invocationContext)) {
logger.log(Level.SEVERE,() ->
String.format("[%s] Failed to generate initial story. Aborting after StoryGenerator.",
name()));
return Flowable.empty(); // Stop further processing if no story
}
logger.log(Level.INFO, () ->
String.format("[%s] Story state after generator: %s",
name(), invocationContext.session().state().get("current_story")));
return runStage(loopAgent, invocationContext, "CriticReviserLoop");
});
// Stage 3: Post-Processing (runs after critic-reviser loop completes)
Flowable postProcessingFlow = Flowable.defer(() -> {
logger.log(Level.INFO, () ->
String.format("[%s] Story state after loop: %s",
name(), invocationContext.session().state().get("current_story")));
return runStage(sequentialAgent, invocationContext, "PostProcessing");
});
// Stage 4: Conditional Regeneration (runs after post-processing completes)
Flowable conditionalRegenFlow = Flowable.defer(() -> {
String toneCheckResult = (String) invocationContext.session().state().get("tone_check_result");
logger.log(Level.INFO, () -> String.format("[%s] Tone check result: %s", name(), toneCheckResult));
if ("negative".equalsIgnoreCase(toneCheckResult)) {
logger.log(Level.INFO, () ->
String.format("[%s] Tone is negative. Regenerating story...", name()));
return runStage(storyGenerator, invocationContext, "StoryGenerator (Regen)");
} else {
logger.log(Level.INFO, () ->
String.format("[%s] Tone is not negative. Keeping current story.", name()));
return Flowable.empty(); // No regeneration needed
}
});
return Flowable.concatArray(storyGenFlow, criticReviserFlow, postProcessingFlow, conditionalRegenFlow)
.doOnComplete(() -> logger.log(Level.INFO, () -> String.format("[%s] Workflow finished.", name())));
}
// Helper method for a single agent run stage with logging
private Flowable runStage(BaseAgent agentToRun, InvocationContext ctx, String stageName) {
logger.log(Level.INFO, () -> String.format("[%s] Running %s...", name(), stageName));
return agentToRun
.runAsync(ctx)
.doOnNext(event ->
logger.log(Level.INFO,() ->
String.format("[%s] Event from %s: %s", name(), stageName, event.toJson())))
.doOnError(err ->
logger.log(Level.SEVERE,
String.format("[%s] Error in %s", name(), stageName), err))
.doOnComplete(() ->
logger.log(Level.INFO, () ->
String.format("[%s] %s finished.", name(), stageName)));
}
```
**Explanation of Logic:**
1. The initial `storyGenerator.runAsync(invocationContext)` Flowable is executed. Its output is expected to be in `invocationContext.session().state().get("current_story")`.
1. The `loopAgent's` Flowable runs next (due to `Flowable.concatArray` and `Flowable.defer`). The LoopAgent internally calls the `critic` and `reviser` sub-agents sequentially for up to `maxIterations`. They read/write `current_story` and `criticism` from/to the state.
1. Then, the `sequentialAgent's` Flowable executes. It calls the `grammar_check` then `tone_check`, reading `current_story` and writing `grammar_suggestions` and `tone_check_result` to the state.
1. **Custom Part:** After the sequentialAgent completes, logic within a `Flowable.defer` checks the "tone_check_result" from `invocationContext.session().state()`. If it's "negative", the `storyGenerator` Flowable is *conditionally concatenated* and executed again, overwriting "current_story". Otherwise, an empty Flowable is used, and the overall workflow proceeds to completion.
______________________________________________________________________
### Part 3: Define LLM sub-agents
These are standard `LlmAgent` definitions, responsible for specific tasks. Their `output key` parameter is crucial for placing results into the `session.state` where other agents or the custom orchestrator can access them.
Direct State Injection in Instructions
Notice the `story_generator`'s instruction. The `{var}` syntax is a placeholder. Before the instruction is sent to the LLM, the ADK framework automatically replaces (Example:`{topic}`) with the value of `session.state['topic']`. This is the recommended way to provide context to an agent, using templating in the instructions. For more details, see the [State documentation](https://adk.tools-online.site/sessions/state/#accessing-session-state-in-agent-instructions).
```python
GEMINI_2_FLASH = "gemini-flash-latest" # Define model constant
# --- Define the individual LLM agents ---
story_generator = LlmAgent(
name="StoryGenerator",
model=GEMINI_2_FLASH,
instruction="""You are a story writer. Write a short story (around 100 words), on the following topic: {topic}""",
input_schema=None,
output_key="current_story", # Key for storing output in session state
)
critic = LlmAgent(
name="Critic",
model=GEMINI_2_FLASH,
instruction="""You are a story critic. Review the story provided: {{current_story}}. Provide 1-2 sentences of constructive criticism
on how to improve it. Focus on plot or character.""",
input_schema=None,
output_key="criticism", # Key for storing criticism in session state
)
reviser = LlmAgent(
name="Reviser",
model=GEMINI_2_FLASH,
instruction="""You are a story reviser. Revise the story provided: {{current_story}}, based on the criticism in
{{criticism}}. Output only the revised story.""",
input_schema=None,
output_key="current_story", # Overwrites the original story
)
grammar_check = LlmAgent(
name="GrammarCheck",
model=GEMINI_2_FLASH,
instruction="""You are a grammar checker. Check the grammar of the story provided: {current_story}. Output only the suggested
corrections as a list, or output 'Grammar is good!' if there are no errors.""",
input_schema=None,
output_key="grammar_suggestions",
)
tone_check = LlmAgent(
name="ToneCheck",
model=GEMINI_2_FLASH,
instruction="""You are a tone analyzer. Analyze the tone of the story provided: {current_story}. Output only one word: 'positive' if
the tone is generally positive, 'negative' if the tone is generally negative, or 'neutral'
otherwise.""",
input_schema=None,
output_key="tone_check_result", # This agent's output determines the conditional flow
)
```
```typescript
// --- Define the individual LLM agents ---
const storyGenerator = new LlmAgent({
name: "StoryGenerator",
model: GEMINI_MODEL,
instruction: `You are a story writer. Write a short story (around 100 words), on the following topic: {topic}`,
outputKey: "current_story",
});
const critic = new LlmAgent({
name: "Critic",
model: GEMINI_MODEL,
instruction: `You are a story critic. Review the story provided: {{current_story}}. Provide 1-2 sentences of constructive criticism
on how to improve it. Focus on plot or character.`,
outputKey: "criticism",
});
const reviser = new LlmAgent({
name: "Reviser",
model: GEMINI_MODEL,
instruction: `You are a story reviser. Revise the story provided: {{current_story}}, based on the criticism in
{{criticism}}. Output only the revised story.`,
outputKey: "current_story", // Overwrites the original story
});
const grammarCheck = new LlmAgent({
name: "GrammarCheck",
model: GEMINI_MODEL,
instruction: `You are a grammar checker. Check the grammar of the story provided: {current_story}. Output only the suggested
corrections as a list, or output 'Grammar is good!' if there are no errors.`,
outputKey: "grammar_suggestions",
});
const toneCheck = new LlmAgent({
name: "ToneCheck",
model: GEMINI_MODEL,
instruction: `You are a tone analyzer. Analyze the tone of the story provided: {current_story}. Output only one word: 'positive' if
the tone is generally positive, 'negative' if the tone is generally negative, or 'neutral'
otherwise.`,
outputKey: "tone_check_result",
});
```
```go
// --- Define the individual LLM agents ---
storyGenerator, err := llmagent.New(llmagent.Config{
Name: "StoryGenerator",
Model: model,
Description: "Generates the initial story.",
Instruction: "You are a story writer. Write a short story (around 100 words) about a cat, based on the topic: {topic}",
OutputKey: "current_story",
})
if err != nil {
log.Fatalf("Failed to create StoryGenerator agent: %v", err)
}
critic, err := llmagent.New(llmagent.Config{
Name: "Critic",
Model: model,
Description: "Critiques the story.",
Instruction: "You are a story critic. Review the story: {current_story}. Provide 1-2 sentences of constructive criticism on how to improve it. Focus on plot or character.",
OutputKey: "criticism",
})
if err != nil {
log.Fatalf("Failed to create Critic agent: %v", err)
}
reviser, err := llmagent.New(llmagent.Config{
Name: "Reviser",
Model: model,
Description: "Revises the story based on criticism.",
Instruction: "You are a story reviser. Revise the story: {current_story}, based on the criticism: {criticism}. Output only the revised story.",
OutputKey: "current_story",
})
if err != nil {
log.Fatalf("Failed to create Reviser agent: %v", err)
}
grammarCheck, err := llmagent.New(llmagent.Config{
Name: "GrammarCheck",
Model: model,
Description: "Checks grammar and suggests corrections.",
Instruction: "You are a grammar checker. Check the grammar of the story: {current_story}. Output only the suggested corrections as a list, or output 'Grammar is good!' if there are no errors.",
OutputKey: "grammar_suggestions",
})
if err != nil {
log.Fatalf("Failed to create GrammarCheck agent: %v", err)
}
toneCheck, err := llmagent.New(llmagent.Config{
Name: "ToneCheck",
Model: model,
Description: "Analyzes the tone of the story.",
Instruction: "You are a tone analyzer. Analyze the tone of the story: {current_story}. Output only one word: 'positive' if the tone is generally positive, 'negative' if the tone is generally negative, or 'neutral' otherwise.",
OutputKey: "tone_check_result",
})
if err != nil {
log.Fatalf("Failed to create ToneCheck agent: %v", err)
}
```
```java
// --- Define the individual LLM agents ---
LlmAgent storyGenerator =
LlmAgent.builder()
.name("StoryGenerator")
.model(MODEL_NAME)
.description("Generates the initial story.")
.instruction(
"""
You are a story writer. Write a short story (around 100 words) about a cat,
based on the topic: {topic}
""")
.inputSchema(null)
.outputKey("current_story") // Key for storing output in session state
.build();
LlmAgent critic =
LlmAgent.builder()
.name("Critic")
.model(MODEL_NAME)
.description("Critiques the story.")
.instruction(
"""
You are a story critic. Review the story: {current_story}. Provide 1-2 sentences of constructive criticism
on how to improve it. Focus on plot or character.
""")
.inputSchema(null)
.outputKey("criticism") // Key for storing criticism in session state
.build();
LlmAgent reviser =
LlmAgent.builder()
.name("Reviser")
.model(MODEL_NAME)
.description("Revises the story based on criticism.")
.instruction(
"""
You are a story reviser. Revise the story: {current_story}, based on the criticism: {criticism}. Output only the revised story.
""")
.inputSchema(null)
.outputKey("current_story") // Overwrites the original story
.build();
LlmAgent grammarCheck =
LlmAgent.builder()
.name("GrammarCheck")
.model(MODEL_NAME)
.description("Checks grammar and suggests corrections.")
.instruction(
"""
You are a grammar checker. Check the grammar of the story: {current_story}. Output only the suggested
corrections as a list, or output 'Grammar is good!' if there are no errors.
""")
.outputKey("grammar_suggestions")
.build();
LlmAgent toneCheck =
LlmAgent.builder()
.name("ToneCheck")
.model(MODEL_NAME)
.description("Analyzes the tone of the story.")
.instruction(
"""
You are a tone analyzer. Analyze the tone of the story: {current_story}. Output only one word: 'positive' if
the tone is generally positive, 'negative' if the tone is generally negative, or 'neutral'
otherwise.
""")
.outputKey("tone_check_result") // This agent's output determines the conditional flow
.build();
LoopAgent loopAgent =
LoopAgent.builder()
.name("CriticReviserLoop")
.description("Iteratively critiques and revises the story.")
.subAgents(critic, reviser)
.maxIterations(2)
.build();
SequentialAgent sequentialAgent =
SequentialAgent.builder()
.name("PostProcessing")
.description("Performs grammar and tone checks sequentially.")
.subAgents(grammarCheck, toneCheck)
.build();
```
______________________________________________________________________
### Part 4: Instantiate and run the custom agent
Finally, you instantiate your `StoryFlowAgent` and use the `Runner` as usual.
```python
# --- Create the custom agent instance ---
story_flow_agent = StoryFlowAgent(
name="StoryFlowAgent",
story_generator=story_generator,
critic=critic,
reviser=reviser,
grammar_check=grammar_check,
tone_check=tone_check,
)
INITIAL_STATE = {"topic": "a brave kitten exploring a haunted house"}
# --- Setup Runner and Session ---
async def setup_session_and_runner():
session_service = InMemorySessionService()
session = await session_service.create_session(app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID, state=INITIAL_STATE)
logger.info(f"Initial session state: {session.state}")
runner = Runner(
agent=story_flow_agent, # Pass the custom orchestrator agent
app_name=APP_NAME,
session_service=session_service
)
return session_service, runner
# --- Function to Interact with the Agent ---
async def call_agent_async(user_input_topic: str):
"""
Sends a new topic to the agent (overwriting the initial one if needed)
and runs the workflow.
"""
session_service, runner = await setup_session_and_runner()
current_session = session_service.sessions[APP_NAME][USER_ID][SESSION_ID]
current_session.state["topic"] = user_input_topic
logger.info(f"Updated session state topic to: {user_input_topic}")
content = types.Content(role='user', parts=[types.Part(text=f"Generate a story about the preset topic.")])
events = runner.run_async(user_id=USER_ID, session_id=SESSION_ID, new_message=content)
final_response = "No final response captured."
async for event in events:
if event.is_final_response() and event.content and event.content.parts:
logger.info(f"Potential final response from [{event.author}]: {event.content.parts[0].text}")
final_response = event.content.parts[0].text
print("\n--- Agent Interaction Result ---")
print("Agent Final Response: ", final_response)
final_session = await session_service.get_session(app_name=APP_NAME,
user_id=USER_ID,
session_id=SESSION_ID)
print("Final Session State:")
import json
print(json.dumps(final_session.state, indent=2))
print("-------------------------------\n")
# --- Run the Agent ---
async def main():
await call_agent_async("a lonely robot finding a friend in a junkyard")
# Note: In Colab, you can directly 'await call_agent_async(...)' at the top level.
if __name__ == "__main__":
asyncio.run(main())
```
```typescript
// --- Create the custom agent instance ---
const storyFlowAgent = new StoryFlowAgent(
"StoryFlowAgent",
storyGenerator,
critic,
reviser,
grammarCheck,
toneCheck
);
const INITIAL_STATE = { "topic": "a brave kitten exploring a haunted house" };
// --- Setup Runner and Session ---
async function setupRunnerAndSession() {
const runner = new InMemoryRunner({
agent: storyFlowAgent,
appName: APP_NAME,
});
const session = await runner.sessionService.createSession({
appName: APP_NAME,
userId: USER_ID,
sessionId: SESSION_ID,
state: INITIAL_STATE,
});
console.log(`Initial session state: ${JSON.stringify(session.state, null, 2)}`);
return runner;
}
// --- Function to Interact with the Agent ---
async function callAgent(runner: InMemoryRunner, userInputTopic: string) {
const currentSession = await runner.sessionService.getSession({
appName: APP_NAME,
userId: USER_ID,
sessionId: SESSION_ID
});
if (!currentSession) {
return;
}
// Update the state with the new topic for this run
currentSession.state["topic"] = userInputTopic;
console.log(`Updated session state topic to: ${userInputTopic}`);
let finalResponse = "No final response captured.";
for await (const event of runner.runAsync({
userId: USER_ID,
sessionId: SESSION_ID,
newMessage: createUserContent(`Generate a story about: ${userInputTopic}`)
})) {
if (isFinalResponse(event) && event.content?.parts?.length) {
console.log(`Potential final response from [${event.author}]: ${event.content.parts.map(part => part.text ?? '').join('')}`);
finalResponse = event.content.parts.map(part => part.text ?? '').join('');
}
}
const finalSession = await runner.sessionService.getSession({
appName: APP_NAME,
userId: USER_ID,
sessionId: SESSION_ID
});
console.log("\n--- Agent Interaction Result ---");
console.log("Agent Final Response: ", finalResponse);
console.log("Final Session State:");
console.log(JSON.stringify(finalSession?.state, null, 2));
console.log("-------------------------------\n");
}
// --- Run the Agent ---
async function main() {
const runner = await setupRunnerAndSession();
await callAgent(runner, "a lonely robot finding a friend in a junkyard");
}
main();
```
```go
// Instantiate the custom agent, which encapsulates the workflow agents.
storyFlowAgent, err := NewStoryFlowAgent(
storyGenerator,
critic,
reviser,
grammarCheck,
toneCheck,
)
if err != nil {
log.Fatalf("Failed to create story flow agent: %v", err)
}
// --- Run the Agent ---
sessionService := session.InMemoryService()
initialState := map[string]any{
"topic": "a brave kitten exploring a haunted house",
}
sessionInstance, err := sessionService.Create(ctx, &session.CreateRequest{
AppName: appName,
UserID: userID,
State: initialState,
})
if err != nil {
log.Fatalf("Failed to create session: %v", err)
}
userTopic := "a lonely robot finding a friend in a junkyard"
r, err := runner.New(runner.Config{
AppName: appName,
Agent: storyFlowAgent,
SessionService: sessionService,
})
if err != nil {
log.Fatalf("Failed to create runner: %v", err)
}
input := genai.NewContentFromText("Generate a story about: "+userTopic, genai.RoleUser)
events := r.Run(ctx, userID, sessionInstance.Session.ID(), input, agent.RunConfig{
StreamingMode: agent.StreamingModeSSE,
})
var finalResponse string
for event, err := range events {
if err != nil {
log.Fatalf("An error occurred during agent execution: %v", err)
}
for _, part := range event.Content.Parts {
// Accumulate text from all parts of the final response.
finalResponse += part.Text
}
}
fmt.Println("\n--- Agent Interaction Result ---")
fmt.Println("Agent Final Response: " + finalResponse)
finalSession, err := sessionService.Get(ctx, &session.GetRequest{
UserID: userID,
AppName: appName,
SessionID: sessionInstance.Session.ID(),
})
if err != nil {
log.Fatalf("Failed to retrieve final session: %v", err)
}
fmt.Println("Final Session State:", finalSession.Session.State())
}
```
```java
// --- Function to Interact with the Agent ---
// Sends a new topic to the agent (overwriting the initial one if needed)
// and runs the workflow.
public static void runAgent(StoryFlowAgentExample agent, String userTopic) {
// --- Setup Runner and Session ---
InMemoryRunner runner = new InMemoryRunner(agent);
Map initialState = new HashMap<>();
initialState.put("topic", "a brave kitten exploring a haunted house");
Session session =
runner
.sessionService()
.createSession(APP_NAME, USER_ID, new ConcurrentHashMap<>(initialState), SESSION_ID)
.blockingGet();
logger.log(Level.INFO, () -> String.format("Initial session state: %s", session.state()));
session.state().put("topic", userTopic); // Update the state in the retrieved session
logger.log(Level.INFO, () -> String.format("Updated session state topic to: %s", userTopic));
Content userMessage = Content.fromParts(Part.fromText("Generate a story about: " + userTopic));
// Use the modified session object for the run
Flowable eventStream = runner.runAsync(USER_ID, session.id(), userMessage);
final String[] finalResponse = {"No final response captured."};
eventStream.blockingForEach(
event -> {
if (event.finalResponse() && event.content().isPresent()) {
String author = event.author() != null ? event.author() : "UNKNOWN_AUTHOR";
Optional textOpt =
event
.content()
.flatMap(Content::parts)
.filter(parts -> !parts.isEmpty())
.map(parts -> parts.get(0).text().orElse(""));
logger.log(Level.INFO, () ->
String.format("Potential final response from [%s]: %s", author, textOpt.orElse("N/A")));
textOpt.ifPresent(text -> finalResponse[0] = text);
}
});
System.out.println("\n--- Agent Interaction Result ---");
System.out.println("Agent Final Response: " + finalResponse[0]);
// Retrieve session again to see the final state after the run
Session finalSession =
runner
.sessionService()
.getSession(APP_NAME, USER_ID, SESSION_ID, Optional.empty())
.blockingGet();
assert finalSession != null;
System.out.println("Final Session State:" + finalSession.state());
System.out.println("-------------------------------\n");
}
```
*(Note: The full runnable code, including imports and execution logic, can be found linked below.)*
______________________________________________________________________
### Storyflow Agent code listing
Storyflow Agent
```python
# Full runnable code for the StoryFlowAgent example
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
import logging
from typing import AsyncGenerator
from typing_extensions import override
from google.adk.agents import LlmAgent, BaseAgent, LoopAgent, SequentialAgent
from google.adk.agents.invocation_context import InvocationContext
from google.genai import types
from google.adk.sessions import InMemorySessionService
from google.adk.runners import Runner
from google.adk.events import Event
from pydantic import BaseModel, Field
# --- Constants ---
APP_NAME = "story_app"
USER_ID = "12345"
SESSION_ID = "123344"
GEMINI_2_FLASH = "gemini-2.0-flash"
# --- Configure Logging ---
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# --- Custom Orchestrator Agent ---
class StoryFlowAgent(BaseAgent):
"""
Custom agent for a story generation and refinement workflow.
This agent orchestrates a sequence of LLM agents to generate a story,
critique it, revise it, check grammar and tone, and potentially
regenerate the story if the tone is negative.
"""
# --- Field Declarations for Pydantic ---
# Declare the agents passed during initialization as class attributes with type hints
story_generator: LlmAgent
critic: LlmAgent
reviser: LlmAgent
grammar_check: LlmAgent
tone_check: LlmAgent
loop_agent: LoopAgent
sequential_agent: SequentialAgent
# model_config allows setting Pydantic configurations if needed, e.g., arbitrary_types_allowed
model_config = {"arbitrary_types_allowed": True}
def __init__(
self,
name: str,
story_generator: LlmAgent,
critic: LlmAgent,
reviser: LlmAgent,
grammar_check: LlmAgent,
tone_check: LlmAgent,
):
"""
Initializes the StoryFlowAgent.
Args:
name: The name of the agent.
story_generator: An LlmAgent to generate the initial story.
critic: An LlmAgent to critique the story.
reviser: An LlmAgent to revise the story based on criticism.
grammar_check: An LlmAgent to check the grammar.
tone_check: An LlmAgent to analyze the tone.
"""
# Create internal agents *before* calling super().__init__
loop_agent = LoopAgent(
name="CriticReviserLoop", sub_agents=[critic, reviser], max_iterations=2
)
sequential_agent = SequentialAgent(
name="PostProcessing", sub_agents=[grammar_check, tone_check]
)
# Define the sub_agents list for the framework
sub_agents_list = [
story_generator,
loop_agent,
sequential_agent,
]
# Pydantic will validate and assign them based on the class annotations.
super().__init__(
name=name,
story_generator=story_generator,
critic=critic,
reviser=reviser,
grammar_check=grammar_check,
tone_check=tone_check,
loop_agent=loop_agent,
sequential_agent=sequential_agent,
sub_agents=sub_agents_list, # Pass the sub_agents list directly
)
@override
async def _run_async_impl(
self, ctx: InvocationContext
) -> AsyncGenerator[Event, None]:
"""
Implements the custom orchestration logic for the story workflow.
Uses the instance attributes assigned by Pydantic (e.g., self.story_generator).
"""
logger.info(f"[{self.name}] Starting story generation workflow.")
# 1. Initial Story Generation
logger.info(f"[{self.name}] Running StoryGenerator...")
async for event in self.story_generator.run_async(ctx):
logger.info(f"[{self.name}] Event from StoryGenerator: {event.model_dump_json(indent=2, exclude_none=True)}")
yield event
# Check if story was generated before proceeding
if "current_story" not in ctx.session.state or not ctx.session.state["current_story"]:
logger.error(f"[{self.name}] Failed to generate initial story. Aborting workflow.")
return # Stop processing if initial story failed
logger.info(f"[{self.name}] Story state after generator: {ctx.session.state.get('current_story')}")
# 2. Critic-Reviser Loop
logger.info(f"[{self.name}] Running CriticReviserLoop...")
# Use the loop_agent instance attribute assigned during init
async for event in self.loop_agent.run_async(ctx):
logger.info(f"[{self.name}] Event from CriticReviserLoop: {event.model_dump_json(indent=2, exclude_none=True)}")
yield event
logger.info(f"[{self.name}] Story state after loop: {ctx.session.state.get('current_story')}")
# 3. Sequential Post-Processing (Grammar and Tone Check)
logger.info(f"[{self.name}] Running PostProcessing...")
# Use the sequential_agent instance attribute assigned during init
async for event in self.sequential_agent.run_async(ctx):
logger.info(f"[{self.name}] Event from PostProcessing: {event.model_dump_json(indent=2, exclude_none=True)}")
yield event
# 4. Tone-Based Conditional Logic
tone_check_result = ctx.session.state.get("tone_check_result")
logger.info(f"[{self.name}] Tone check result: {tone_check_result}")
if tone_check_result == "negative":
logger.info(f"[{self.name}] Tone is negative. Regenerating story...")
async for event in self.story_generator.run_async(ctx):
logger.info(f"[{self.name}] Event from StoryGenerator (Regen): {event.model_dump_json(indent=2, exclude_none=True)}")
yield event
else:
logger.info(f"[{self.name}] Tone is not negative. Keeping current story.")
pass
logger.info(f"[{self.name}] Workflow finished.")
# --- Define the individual LLM agents ---
story_generator = LlmAgent(
name="StoryGenerator",
model=GEMINI_2_FLASH,
instruction="""You are a story writer. Write a short story (around 100 words), on the following topic: {topic}""",
input_schema=None,
output_key="current_story", # Key for storing output in session state
)
critic = LlmAgent(
name="Critic",
model=GEMINI_2_FLASH,
instruction="""You are a story critic. Review the story provided: {{current_story}}. Provide 1-2 sentences of constructive criticism
on how to improve it. Focus on plot or character.""",
input_schema=None,
output_key="criticism", # Key for storing criticism in session state
)
reviser = LlmAgent(
name="Reviser",
model=GEMINI_2_FLASH,
instruction="""You are a story reviser. Revise the story provided: {{current_story}}, based on the criticism in
{{criticism}}. Output only the revised story.""",
input_schema=None,
output_key="current_story", # Overwrites the original story
)
grammar_check = LlmAgent(
name="GrammarCheck",
model=GEMINI_2_FLASH,
instruction="""You are a grammar checker. Check the grammar of the story provided: {current_story}. Output only the suggested
corrections as a list, or output 'Grammar is good!' if there are no errors.""",
input_schema=None,
output_key="grammar_suggestions",
)
tone_check = LlmAgent(
name="ToneCheck",
model=GEMINI_2_FLASH,
instruction="""You are a tone analyzer. Analyze the tone of the story provided: {current_story}. Output only one word: 'positive' if
the tone is generally positive, 'negative' if the tone is generally negative, or 'neutral'
otherwise.""",
input_schema=None,
output_key="tone_check_result", # This agent's output determines the conditional flow
)
# --- Create the custom agent instance ---
story_flow_agent = StoryFlowAgent(
name="StoryFlowAgent",
story_generator=story_generator,
critic=critic,
reviser=reviser,
grammar_check=grammar_check,
tone_check=tone_check,
)
INITIAL_STATE = {"topic": "a brave kitten exploring a haunted house"}
# --- Setup Runner and Session ---
async def setup_session_and_runner():
session_service = InMemorySessionService()
session = await session_service.create_session(app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID, state=INITIAL_STATE)
logger.info(f"Initial session state: {session.state}")
runner = Runner(
agent=story_flow_agent, # Pass the custom orchestrator agent
app_name=APP_NAME,
session_service=session_service
)
return session_service, runner
# --- Function to Interact with the Agent ---
async def call_agent_async(user_input_topic: str):
"""
Sends a new topic to the agent (overwriting the initial one if needed)
and runs the workflow.
"""
session_service, runner = await setup_session_and_runner()
current_session = session_service.sessions[APP_NAME][USER_ID][SESSION_ID]
current_session.state["topic"] = user_input_topic
logger.info(f"Updated session state topic to: {user_input_topic}")
content = types.Content(role='user', parts=[types.Part(text=f"Generate a story about the preset topic.")])
events = runner.run_async(user_id=USER_ID, session_id=SESSION_ID, new_message=content)
final_response = "No final response captured."
async for event in events:
if event.is_final_response() and event.content and event.content.parts:
logger.info(f"Potential final response from [{event.author}]: {event.content.parts[0].text}")
final_response = event.content.parts[0].text
print("\n--- Agent Interaction Result ---")
print("Agent Final Response: ", final_response)
final_session = await session_service.get_session(app_name=APP_NAME,
user_id=USER_ID,
session_id=SESSION_ID)
print("Final Session State:")
import json
print(json.dumps(final_session.state, indent=2))
print("-------------------------------\n")
# --- Run the Agent ---
async def main():
await call_agent_async("a lonely robot finding a friend in a junkyard")
# Note: In Colab, you can directly 'await call_agent_async(...)' at the top level.
if __name__ == "__main__":
asyncio.run(main())
```
```typescript
// Full runnable code for the StoryFlowAgent example
/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { LlmAgent, BaseAgent, LoopAgent, SequentialAgent, InMemoryRunner, InvocationContext, Event, isFinalResponse } from '@google/adk';
import { createUserContent } from "@google/genai";
// --- Constants ---
const APP_NAME = "story_app_ts";
const USER_ID = "12345";
const SESSION_ID = "123344_ts";
const GEMINI_MODEL = "gemini-2.5-flash";
// --- Custom Orchestrator Agent ---
class StoryFlowAgent extends BaseAgent {
// --- Property Declarations for TypeScript ---
private storyGenerator: LlmAgent;
private critic: LlmAgent;
private reviser: LlmAgent;
private grammarCheck: LlmAgent;
private toneCheck: LlmAgent;
private loopAgent: LoopAgent;
private sequentialAgent: SequentialAgent;
constructor(
name: string,
storyGenerator: LlmAgent,
critic: LlmAgent,
reviser: LlmAgent,
grammarCheck: LlmAgent,
toneCheck: LlmAgent
) {
// Create internal composite agents
const loopAgent = new LoopAgent({
name: "CriticReviserLoop",
subAgents: [critic, reviser],
maxIterations: 2,
});
const sequentialAgent = new SequentialAgent({
name: "PostProcessing",
subAgents: [grammarCheck, toneCheck],
});
// Define the sub-agents for the framework to know about
const subAgentsList = [
storyGenerator,
loopAgent,
sequentialAgent,
];
// Call the parent constructor
super({
name,
subAgents: subAgentsList,
});
// Assign agents to class properties for use in the custom run logic
this.storyGenerator = storyGenerator;
this.critic = critic;
this.reviser = reviser;
this.grammarCheck = grammarCheck;
this.toneCheck = toneCheck;
this.loopAgent = loopAgent;
this.sequentialAgent = sequentialAgent;
}
// Implements the custom orchestration logic for the story workflow.
async* runLiveImpl(ctx: InvocationContext): AsyncGenerator {
yield* this.runAsyncImpl(ctx);
}
// Implements the custom orchestration logic for the story workflow.
async* runAsyncImpl(ctx: InvocationContext): AsyncGenerator {
console.log(`[${this.name}] Starting story generation workflow.`);
// 1. Initial Story Generation
console.log(`[${this.name}] Running StoryGenerator...`);
for await (const event of this.storyGenerator.runAsync(ctx)) {
console.log(`[${this.name}] Event from StoryGenerator: ${JSON.stringify(event, null, 2)}`);
yield event;
}
// Check if the story was generated before proceeding
if (!ctx.session.state["current_story"]) {
console.error(`[${this.name}] Failed to generate initial story. Aborting workflow.`);
return; // Stop processing
}
console.log(`[${this.name}] Story state after generator: ${ctx.session.state['current_story']}`);
// 2. Critic-Reviser Loop
console.log(`[${this.name}] Running CriticReviserLoop...`);
for await (const event of this.loopAgent.runAsync(ctx)) {
console.log(`[${this.name}] Event from CriticReviserLoop: ${JSON.stringify(event, null, 2)}`);
yield event;
}
console.log(`[${this.name}] Story state after loop: ${ctx.session.state['current_story']}`);
// 3. Sequential Post-Processing (Grammar and Tone Check)
console.log(`[${this.name}] Running PostProcessing...`);
for await (const event of this.sequentialAgent.runAsync(ctx)) {
console.log(`[${this.name}] Event from PostProcessing: ${JSON.stringify(event, null, 2)}`);
yield event;
}
// 4. Tone-Based Conditional Logic
const toneCheckResult = ctx.session.state["tone_check_result"] as string;
console.log(`[${this.name}] Tone check result: ${toneCheckResult}`);
if (toneCheckResult === "negative") {
console.log(`[${this.name}] Tone is negative. Regenerating story...`);
for await (const event of this.storyGenerator.runAsync(ctx)) {
console.log(`[${this.name}] Event from StoryGenerator (Regen): ${JSON.stringify(event, null, 2)}`);
yield event;
}
} else {
console.log(`[${this.name}] Tone is not negative. Keeping current story.`);
}
console.log(`[${this.name}] Workflow finished.`);
}
}
// --- Define the individual LLM agents ---
const storyGenerator = new LlmAgent({
name: "StoryGenerator",
model: GEMINI_MODEL,
instruction: `You are a story writer. Write a short story (around 100 words), on the following topic: {topic}`,
outputKey: "current_story",
});
const critic = new LlmAgent({
name: "Critic",
model: GEMINI_MODEL,
instruction: `You are a story critic. Review the story provided: {{current_story}}. Provide 1-2 sentences of constructive criticism
on how to improve it. Focus on plot or character.`,
outputKey: "criticism",
});
const reviser = new LlmAgent({
name: "Reviser",
model: GEMINI_MODEL,
instruction: `You are a story reviser. Revise the story provided: {{current_story}}, based on the criticism in
{{criticism}}. Output only the revised story.`,
outputKey: "current_story", // Overwrites the original story
});
const grammarCheck = new LlmAgent({
name: "GrammarCheck",
model: GEMINI_MODEL,
instruction: `You are a grammar checker. Check the grammar of the story provided: {current_story}. Output only the suggested
corrections as a list, or output 'Grammar is good!' if there are no errors.`,
outputKey: "grammar_suggestions",
});
const toneCheck = new LlmAgent({
name: "ToneCheck",
model: GEMINI_MODEL,
instruction: `You are a tone analyzer. Analyze the tone of the story provided: {current_story}. Output only one word: 'positive' if
the tone is generally positive, 'negative' if the tone is generally negative, or 'neutral'
otherwise.`,
outputKey: "tone_check_result",
});
// --- Create the custom agent instance ---
const storyFlowAgent = new StoryFlowAgent(
"StoryFlowAgent",
storyGenerator,
critic,
reviser,
grammarCheck,
toneCheck
);
const INITIAL_STATE = { "topic": "a brave kitten exploring a haunted house" };
// --- Setup Runner and Session ---
async function setupRunnerAndSession() {
const runner = new InMemoryRunner({
agent: storyFlowAgent,
appName: APP_NAME,
});
const session = await runner.sessionService.createSession({
appName: APP_NAME,
userId: USER_ID,
sessionId: SESSION_ID,
state: INITIAL_STATE,
});
console.log(`Initial session state: ${JSON.stringify(session.state, null, 2)}`);
return runner;
}
// --- Function to Interact with the Agent ---
async function callAgent(runner: InMemoryRunner, userInputTopic: string) {
const currentSession = await runner.sessionService.getSession({
appName: APP_NAME,
userId: USER_ID,
sessionId: SESSION_ID
});
if (!currentSession) {
return;
}
// Update the state with the new topic for this run
currentSession.state["topic"] = userInputTopic;
console.log(`Updated session state topic to: ${userInputTopic}`);
let finalResponse = "No final response captured.";
for await (const event of runner.runAsync({
userId: USER_ID,
sessionId: SESSION_ID,
newMessage: createUserContent(`Generate a story about: ${userInputTopic}`)
})) {
if (isFinalResponse(event) && event.content?.parts?.length) {
console.log(`Potential final response from [${event.author}]: ${event.content.parts.map(part => part.text ?? '').join('')}`);
finalResponse = event.content.parts.map(part => part.text ?? '').join('');
}
}
const finalSession = await runner.sessionService.getSession({
appName: APP_NAME,
userId: USER_ID,
sessionId: SESSION_ID
});
console.log("\n--- Agent Interaction Result ---");
console.log("Agent Final Response: ", finalResponse);
console.log("Final Session State:");
console.log(JSON.stringify(finalSession?.state, null, 2));
console.log("-------------------------------\n");
}
// --- Run the Agent ---
async function main() {
const runner = await setupRunnerAndSession();
await callAgent(runner, "a lonely robot finding a friend in a junkyard");
}
main();
```
```go
// Full runnable code for the StoryFlowAgent example
package main
import (
"context"
"fmt"
"iter"
"log"
"google.golang.org/adk/v2/agent/workflowagents/loopagent"
"google.golang.org/adk/v2/agent/workflowagents/sequentialagent"
"google.golang.org/adk/v2/agent"
"google.golang.org/adk/v2/agent/llmagent"
"google.golang.org/adk/v2/model/gemini"
"google.golang.org/adk/v2/runner"
"google.golang.org/adk/v2/session"
"google.golang.org/genai"
)
// StoryFlowAgent is a custom agent that orchestrates a story generation workflow.
// It encapsulates the logic of running sub-agents in a specific sequence.
type StoryFlowAgent struct {
storyGenerator agent.Agent
revisionLoopAgent agent.Agent
postProcessorAgent agent.Agent
}
// NewStoryFlowAgent creates and configures the entire custom agent workflow.
// It takes individual LLM agents as input and internally creates the necessary
// workflow agents (loop, sequential), returning the final orchestrator agent.
func NewStoryFlowAgent(
storyGenerator,
critic,
reviser,
grammarCheck,
toneCheck agent.Agent,
) (agent.Agent, error) {
loopAgent, err := loopagent.New(loopagent.Config{
MaxIterations: 2,
AgentConfig: agent.Config{
Name: "CriticReviserLoop",
SubAgents: []agent.Agent{critic, reviser},
},
})
if err != nil {
return nil, fmt.Errorf("failed to create loop agent: %w", err)
}
sequentialAgent, err := sequentialagent.New(sequentialagent.Config{
AgentConfig: agent.Config{
Name: "PostProcessing",
SubAgents: []agent.Agent{grammarCheck, toneCheck},
},
})
if err != nil {
return nil, fmt.Errorf("failed to create sequential agent: %w", err)
}
// The StoryFlowAgent struct holds the agents needed for the Run method.
orchestrator := &StoryFlowAgent{
storyGenerator: storyGenerator,
revisionLoopAgent: loopAgent,
postProcessorAgent: sequentialAgent,
}
// agent.New creates the final agent, wiring up the Run method.
return agent.New(agent.Config{
Name: "StoryFlowAgent",
Description: "Orchestrates story generation, critique, revision, and checks.",
SubAgents: []agent.Agent{storyGenerator, loopAgent, sequentialAgent},
Run: orchestrator.Run,
})
}
// Run defines the custom execution logic for the StoryFlowAgent.
func (s *StoryFlowAgent) Run(ctx agent.InvocationContext) iter.Seq2[*session.Event, error] {
return func(yield func(*session.Event, error) bool) {
// Stage 1: Initial Story Generation
for event, err := range s.storyGenerator.Run(ctx) {
if err != nil {
yield(nil, fmt.Errorf("story generator failed: %w", err))
return
}
if !yield(event, nil) {
return
}
}
// Check if story was generated before proceeding
currentStory, err := ctx.Session().State().Get("current_story")
if err != nil || currentStory == "" {
log.Println("Failed to generate initial story. Aborting workflow.")
return
}
// Stage 2: Critic-Reviser Loop
for event, err := range s.revisionLoopAgent.Run(ctx) {
if err != nil {
yield(nil, fmt.Errorf("loop agent failed: %w", err))
return
}
if !yield(event, nil) {
return
}
}
// Stage 3: Post-Processing
for event, err := range s.postProcessorAgent.Run(ctx) {
if err != nil {
yield(nil, fmt.Errorf("sequential agent failed: %w", err))
return
}
if !yield(event, nil) {
return
}
}
// Stage 4: Conditional Regeneration
toneResult, err := ctx.Session().State().Get("tone_check_result")
if err != nil {
log.Printf("Could not read tone_check_result from state: %v. Assuming tone is not negative.", err)
return
}
if tone, ok := toneResult.(string); ok && tone == "negative" {
log.Println("Tone is negative. Regenerating story...")
for event, err := range s.storyGenerator.Run(ctx) {
if err != nil {
yield(nil, fmt.Errorf("story regeneration failed: %w", err))
return
}
if !yield(event, nil) {
return
}
}
} else {
log.Println("Tone is not negative. Keeping current story.")
}
}
}
const (
modelName = "gemini-flash-latest"
appName = "story_app"
userID = "user_12345"
)
func main() {
ctx := context.Background()
model, err := gemini.NewModel(ctx, modelName, &genai.ClientConfig{})
if err != nil {
log.Fatalf("Failed to create model: %v", err)
}
// --- Define the individual LLM agents ---
storyGenerator, err := llmagent.New(llmagent.Config{
Name: "StoryGenerator",
Model: model,
Description: "Generates the initial story.",
Instruction: "You are a story writer. Write a short story (around 100 words) about a cat, based on the topic: {topic}",
OutputKey: "current_story",
})
if err != nil {
log.Fatalf("Failed to create StoryGenerator agent: %v", err)
}
critic, err := llmagent.New(llmagent.Config{
Name: "Critic",
Model: model,
Description: "Critiques the story.",
Instruction: "You are a story critic. Review the story: {current_story}. Provide 1-2 sentences of constructive criticism on how to improve it. Focus on plot or character.",
OutputKey: "criticism",
})
if err != nil {
log.Fatalf("Failed to create Critic agent: %v", err)
}
reviser, err := llmagent.New(llmagent.Config{
Name: "Reviser",
Model: model,
Description: "Revises the story based on criticism.",
Instruction: "You are a story reviser. Revise the story: {current_story}, based on the criticism: {criticism}. Output only the revised story.",
OutputKey: "current_story",
})
if err != nil {
log.Fatalf("Failed to create Reviser agent: %v", err)
}
grammarCheck, err := llmagent.New(llmagent.Config{
Name: "GrammarCheck",
Model: model,
Description: "Checks grammar and suggests corrections.",
Instruction: "You are a grammar checker. Check the grammar of the story: {current_story}. Output only the suggested corrections as a list, or output 'Grammar is good!' if there are no errors.",
OutputKey: "grammar_suggestions",
})
if err != nil {
log.Fatalf("Failed to create GrammarCheck agent: %v", err)
}
toneCheck, err := llmagent.New(llmagent.Config{
Name: "ToneCheck",
Model: model,
Description: "Analyzes the tone of the story.",
Instruction: "You are a tone analyzer. Analyze the tone of the story: {current_story}. Output only one word: 'positive' if the tone is generally positive, 'negative' if the tone is generally negative, or 'neutral' otherwise.",
OutputKey: "tone_check_result",
})
if err != nil {
log.Fatalf("Failed to create ToneCheck agent: %v", err)
}
// Instantiate the custom agent, which encapsulates the workflow agents.
storyFlowAgent, err := NewStoryFlowAgent(
storyGenerator,
critic,
reviser,
grammarCheck,
toneCheck,
)
if err != nil {
log.Fatalf("Failed to create story flow agent: %v", err)
}
// --- Run the Agent ---
sessionService := session.InMemoryService()
initialState := map[string]any{
"topic": "a brave kitten exploring a haunted house",
}
sessionInstance, err := sessionService.Create(ctx, &session.CreateRequest{
AppName: appName,
UserID: userID,
State: initialState,
})
if err != nil {
log.Fatalf("Failed to create session: %v", err)
}
userTopic := "a lonely robot finding a friend in a junkyard"
r, err := runner.New(runner.Config{
AppName: appName,
Agent: storyFlowAgent,
SessionService: sessionService,
})
if err != nil {
log.Fatalf("Failed to create runner: %v", err)
}
input := genai.NewContentFromText("Generate a story about: "+userTopic, genai.RoleUser)
events := r.Run(ctx, userID, sessionInstance.Session.ID(), input, agent.RunConfig{
StreamingMode: agent.StreamingModeSSE,
})
var finalResponse string
for event, err := range events {
if err != nil {
log.Fatalf("An error occurred during agent execution: %v", err)
}
for _, part := range event.Content.Parts {
// Accumulate text from all parts of the final response.
finalResponse += part.Text
}
}
fmt.Println("\n--- Agent Interaction Result ---")
fmt.Println("Agent Final Response: " + finalResponse)
finalSession, err := sessionService.Get(ctx, &session.GetRequest{
UserID: userID,
AppName: appName,
SessionID: sessionInstance.Session.ID(),
})
if err != nil {
log.Fatalf("Failed to retrieve final session: %v", err)
}
fmt.Println("Final Session State:", finalSession.Session.State())
}
```
```java
// Full runnable code for the StoryFlowAgent example
import com.google.adk.agents.LlmAgent;
import com.google.adk.agents.BaseAgent;
import com.google.adk.agents.InvocationContext;
import com.google.adk.agents.LoopAgent;
import com.google.adk.agents.SequentialAgent;
import com.google.adk.events.Event;
import com.google.adk.runner.InMemoryRunner;
import com.google.adk.sessions.Session;
import com.google.genai.types.Content;
import com.google.genai.types.Part;
import io.reactivex.rxjava3.core.Flowable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
public class StoryFlowAgentExample extends BaseAgent {
// --- Constants ---
private static final String APP_NAME = "story_app";
private static final String USER_ID = "user_12345";
private static final String SESSION_ID = "session_123344";
private static final String MODEL_NAME = "gemini-2.0-flash"; // Ensure this model is available
private static final Logger logger = Logger.getLogger(StoryFlowAgentExample.class.getName());
private final LlmAgent storyGenerator;
private final LoopAgent loopAgent;
private final SequentialAgent sequentialAgent;
public StoryFlowAgentExample(
String name, LlmAgent storyGenerator, LoopAgent loopAgent, SequentialAgent sequentialAgent) {
super(
name,
"Orchestrates story generation, critique, revision, and checks.",
List.of(storyGenerator, loopAgent, sequentialAgent),
null,
null);
this.storyGenerator = storyGenerator;
this.loopAgent = loopAgent;
this.sequentialAgent = sequentialAgent;
}
public static void main(String[] args) {
// --- Define the individual LLM agents ---
LlmAgent storyGenerator =
LlmAgent.builder()
.name("StoryGenerator")
.model(MODEL_NAME)
.description("Generates the initial story.")
.instruction(
"""
You are a story writer. Write a short story (around 100 words) about a cat,
based on the topic: {topic}
""")
.inputSchema(null)
.outputKey("current_story") // Key for storing output in session state
.build();
LlmAgent critic =
LlmAgent.builder()
.name("Critic")
.model(MODEL_NAME)
.description("Critiques the story.")
.instruction(
"""
You are a story critic. Review the story: {current_story}. Provide 1-2 sentences of constructive criticism
on how to improve it. Focus on plot or character.
""")
.inputSchema(null)
.outputKey("criticism") // Key for storing criticism in session state
.build();
LlmAgent reviser =
LlmAgent.builder()
.name("Reviser")
.model(MODEL_NAME)
.description("Revises the story based on criticism.")
.instruction(
"""
You are a story reviser. Revise the story: {current_story}, based on the criticism: {criticism}. Output only the revised story.
""")
.inputSchema(null)
.outputKey("current_story") // Overwrites the original story
.build();
LlmAgent grammarCheck =
LlmAgent.builder()
.name("GrammarCheck")
.model(MODEL_NAME)
.description("Checks grammar and suggests corrections.")
.instruction(
"""
You are a grammar checker. Check the grammar of the story: {current_story}. Output only the suggested
corrections as a list, or output 'Grammar is good!' if there are no errors.
""")
.outputKey("grammar_suggestions")
.build();
LlmAgent toneCheck =
LlmAgent.builder()
.name("ToneCheck")
.model(MODEL_NAME)
.description("Analyzes the tone of the story.")
.instruction(
"""
You are a tone analyzer. Analyze the tone of the story: {current_story}. Output only one word: 'positive' if
the tone is generally positive, 'negative' if the tone is generally negative, or 'neutral'
otherwise.
""")
.outputKey("tone_check_result") // This agent's output determines the conditional flow
.build();
LoopAgent loopAgent =
LoopAgent.builder()
.name("CriticReviserLoop")
.description("Iteratively critiques and revises the story.")
.subAgents(critic, reviser)
.maxIterations(2)
.build();
SequentialAgent sequentialAgent =
SequentialAgent.builder()
.name("PostProcessing")
.description("Performs grammar and tone checks sequentially.")
.subAgents(grammarCheck, toneCheck)
.build();
StoryFlowAgentExample storyFlowAgentExample =
new StoryFlowAgentExample(APP_NAME, storyGenerator, loopAgent, sequentialAgent);
// --- Run the Agent ---
runAgent(storyFlowAgentExample, "a lonely robot finding a friend in a junkyard");
}
// --- Function to Interact with the Agent ---
// Sends a new topic to the agent (overwriting the initial one if needed)
// and runs the workflow.
public static void runAgent(StoryFlowAgentExample agent, String userTopic) {
// --- Setup Runner and Session ---
InMemoryRunner runner = new InMemoryRunner(agent);
Map initialState = new HashMap<>();
initialState.put("topic", "a brave kitten exploring a haunted house");
Session session =
runner
.sessionService()
.createSession(APP_NAME, USER_ID, new ConcurrentHashMap<>(initialState), SESSION_ID)
.blockingGet();
logger.log(Level.INFO, () -> String.format("Initial session state: %s", session.state()));
session.state().put("topic", userTopic); // Update the state in the retrieved session
logger.log(Level.INFO, () -> String.format("Updated session state topic to: %s", userTopic));
Content userMessage = Content.fromParts(Part.fromText("Generate a story about: " + userTopic));
// Use the modified session object for the run
Flowable eventStream = runner.runAsync(USER_ID, session.id(), userMessage);
final String[] finalResponse = {"No final response captured."};
eventStream.blockingForEach(
event -> {
if (event.finalResponse() && event.content().isPresent()) {
String author = event.author() != null ? event.author() : "UNKNOWN_AUTHOR";
Optional textOpt =
event
.content()
.flatMap(Content::parts)
.filter(parts -> !parts.isEmpty())
.map(parts -> parts.get(0).text().orElse(""));
logger.log(Level.INFO, () ->
String.format("Potential final response from [%s]: %s", author, textOpt.orElse("N/A")));
textOpt.ifPresent(text -> finalResponse[0] = text);
}
});
System.out.println("\n--- Agent Interaction Result ---");
System.out.println("Agent Final Response: " + finalResponse[0]);
// Retrieve session again to see the final state after the run
Session finalSession =
runner
.sessionService()
.getSession(APP_NAME, USER_ID, SESSION_ID, Optional.empty())
.blockingGet();
assert finalSession != null;
System.out.println("Final Session State:" + finalSession.state());
System.out.println("-------------------------------\n");
}
private boolean isStoryGenerated(InvocationContext ctx) {
Object currentStoryObj = ctx.session().state().get("current_story");
return currentStoryObj != null && !String.valueOf(currentStoryObj).isEmpty();
}
@Override
protected Flowable runAsyncImpl(InvocationContext invocationContext) {
// Implements the custom orchestration logic for the story workflow.
// Uses the instance attributes assigned by Pydantic (e.g., self.story_generator).
logger.log(Level.INFO, () -> String.format("[%s] Starting story generation workflow.", name()));
// Stage 1. Initial Story Generation
Flowable storyGenFlow = runStage(storyGenerator, invocationContext, "StoryGenerator");
// Stage 2: Critic-Reviser Loop (runs after story generation completes)
Flowable criticReviserFlow = Flowable.defer(() -> {
if (!isStoryGenerated(invocationContext)) {
logger.log(Level.SEVERE,() ->
String.format("[%s] Failed to generate initial story. Aborting after StoryGenerator.",
name()));
return Flowable.empty(); // Stop further processing if no story
}
logger.log(Level.INFO, () ->
String.format("[%s] Story state after generator: %s",
name(), invocationContext.session().state().get("current_story")));
return runStage(loopAgent, invocationContext, "CriticReviserLoop");
});
// Stage 3: Post-Processing (runs after critic-reviser loop completes)
Flowable postProcessingFlow = Flowable.defer(() -> {
logger.log(Level.INFO, () ->
String.format("[%s] Story state after loop: %s",
name(), invocationContext.session().state().get("current_story")));
return runStage(sequentialAgent, invocationContext, "PostProcessing");
});
// Stage 4: Conditional Regeneration (runs after post-processing completes)
Flowable conditionalRegenFlow = Flowable.defer(() -> {
String toneCheckResult = (String) invocationContext.session().state().get("tone_check_result");
logger.log(Level.INFO, () -> String.format("[%s] Tone check result: %s", name(), toneCheckResult));
if ("negative".equalsIgnoreCase(toneCheckResult)) {
logger.log(Level.INFO, () ->
String.format("[%s] Tone is negative. Regenerating story...", name()));
return runStage(storyGenerator, invocationContext, "StoryGenerator (Regen)");
} else {
logger.log(Level.INFO, () ->
String.format("[%s] Tone is not negative. Keeping current story.", name()));
return Flowable.empty(); // No regeneration needed
}
});
return Flowable.concatArray(storyGenFlow, criticReviserFlow, postProcessingFlow, conditionalRegenFlow)
.doOnComplete(() -> logger.log(Level.INFO, () -> String.format("[%s] Workflow finished.", name())));
}
// Helper method for a single agent run stage with logging
private Flowable runStage(BaseAgent agentToRun, InvocationContext ctx, String stageName) {
logger.log(Level.INFO, () -> String.format("[%s] Running %s...", name(), stageName));
return agentToRun
.runAsync(ctx)
.doOnNext(event ->
logger.log(Level.INFO,() ->
String.format("[%s] Event from %s: %s", name(), stageName, event.toJson())))
.doOnError(err ->
logger.log(Level.SEVERE,
String.format("[%s] Error in %s", name(), stageName), err))
.doOnComplete(() ->
logger.log(Level.INFO, () ->
String.format("[%s] %s finished.", name(), stageName)));
}
@Override
protected Flowable runLiveImpl(InvocationContext invocationContext) {
return Flowable.error(new UnsupportedOperationException("runLive not implemented."));
}
}
```
# Simple agents with LlmAgent
Supported in ADKPython v0.1.0TypeScript v0.2.0Go v0.1.0Java v0.1.0Kotlin v0.1.0
The `LlmAgent` class, often aliased simply as `Agent`, is a core component in ADK, acting as the core part of your agent application. It leverages the power of a Large Language Model (LLM) or generative AI model for reasoning, understanding natural language, making decisions, generating responses, and interacting with tools. Since this type of agent uses an AI model to interpret instructions and context, the AI model dynamically decides how to proceed, which tools to use (if any), and what output to provide. As such, the behavior of this type of agent is non-deterministic and must be built and evaluated with this behavior in mind.
Building an effective `LlmAgent` involves defining its identity, clearly guiding its behavior through instructions, and equipping it with the necessary tools and capabilities.
## Define agent identity and purpose
First, you need to establish what the agent *is* and what it's *for*.
- **`name` (Required):** Every agent needs a unique string identifier. This `name` is crucial for internal operations, especially in multi-agent systems where agents need to refer to or delegate tasks to each other. Choose a descriptive name that reflects the agent's function (e.g., `customer_support_router`, `billing_inquiry_agent`). In Python, the name must be a valid Python identifier, and ADK rejects the reserved name `user`.
- **`description` (Optional, Recommended for Multi-Agent):** Provide a concise summary of the agent's capabilities. This description is primarily used by *other* LLM agents to determine if they should route a task to this agent. Make it specific enough to differentiate it from peers (e.g., "Handles inquiries about current billing statements," not just "Billing agent").
- **`model` (Optional in Python):** Specify the underlying LLM that will power this agent's reasoning. This is either a string identifier like `"gemini-flash-latest"` or a `BaseLlm` instance. If you omit `model`, the agent inherits the model of its nearest `LlmAgent` ancestor, skipping over any parent that is not an `LlmAgent`, such as a `SequentialAgent`, `ParallelAgent`, `LoopAgent`, or custom `BaseAgent`. If no ancestor sets a model, the agent falls back to the built-in default model described in [Configure a default model](#configure-a-default-model). The choice of model impacts the agent's capabilities, cost, and performance. See the [Models](/agents/models/) page for available options and considerations.
```python
# Example: Defining the basic identity
capital_agent = LlmAgent(
model="gemini-flash-latest",
name="capital_agent",
description="Answers user questions about the capital city of a given country."
# instruction and tools will be added next
)
```
```typescript
// Example: Defining the basic identity
const capitalAgent = new LlmAgent({
model: 'gemini-flash-latest',
name: 'capital_agent',
description: 'Answers user questions about the capital city of a given country.',
// instruction and tools will be added next
});
```
```go
// Example: Defining the basic identity
agent, err := llmagent.New(llmagent.Config{
Name: "capital_agent",
Model: model,
Description: "Answers user questions about the capital city of a given country.",
// instruction and tools will be added next
})
```
```java
// Example: Defining the basic identity
LlmAgent capitalAgent =
LlmAgent.builder()
.model("gemini-flash-latest")
.name("capital_agent")
.description("Answers user questions about the capital city of a given country.")
// instruction and tools will be added next
.build();
```
```kotlin
val capitalAgent =
LlmAgent(
name = "capital_agent",
model = Gemini(name = "gemini-flash-latest"),
description = "Answers user questions about the capital city of a given country.",
)
```
## Guide the agent with instructions
The `instruction` parameter is arguably the most critical for shaping an `LlmAgent`'s behavior. It's a string (or a function returning a string) that tells the agent:
- Its core task or goal.
- Its personality or persona (e.g., "You are a helpful assistant," "You are a witty pirate").
- Constraints on its behavior (e.g., "Only answer questions about X," "Never reveal Y").
- How and when to use its `tools`. You should explain the purpose of each tool and the circumstances under which it should be called, supplementing any descriptions within the tool itself.
- The desired format for its output (e.g., "Respond in JSON," "Provide a bulleted list").
**Tips for effective instructions:**
- **Be Clear and Specific:** Avoid ambiguity. Clearly state the desired actions and outcomes.
- **Use Markdown:** Improve readability for complex instructions using headings, lists, etc.
- **Provide Examples (Few-Shot):** For complex tasks or specific output formats, include examples directly in the instruction.
- **Guide Tool Use:** Don't just list tools; explain *when* and *why* the agent should use them.
**Use dynamic state variables:**
- The instruction is a string template, you can use the `{var}` syntax to insert dynamic values into the instruction.
- `{var}` is used to insert the value of the state variable named var.
- `{artifact.var}` is used to insert the text content of the artifact named var.
- If the state variable or artifact does not exist, the agent will raise an error. If you want to ignore the error, you can append a `?` to the variable name as in `{var?}`.
```python
# Example: Adding instructions
capital_agent = LlmAgent(
model="gemini-flash-latest",
name="capital_agent",
description="Answers user questions about the capital city of a given country.",
instruction="""You are an agent that provides the capital city of a country.
When a user asks for the capital of a country:
1. Identify the country name from the user's query.
2. Use the `get_capital_city` tool to find the capital.
3. Respond clearly to the user, stating the capital city.
Example Query: "What's the capital of France?"
Example Response: "The capital of France is Paris."
""",
# tools will be added next
)
```
```typescript
// Example: Adding instructions
const capitalAgent = new LlmAgent({
model: 'gemini-flash-latest',
name: 'capital_agent',
description: 'Answers user questions about the capital city of a given country.',
instruction: `You are an agent that provides the capital city of a country.
When a user asks for the capital of a country:
1. Identify the country name from the user's query.
2. Use the \`getCapitalCity\` tool to find the capital.
3. Respond clearly to the user, stating the capital city.
Example Query: "What's the capital of France?"
Example Response: "The capital of France is Paris."
`,
// tools will be added next
});
```
```go
// Example: Adding instructions
agent, err := llmagent.New(llmagent.Config{
Name: "capital_agent",
Model: model,
Description: "Answers user questions about the capital city of a given country.",
Instruction: `You are an agent that provides the capital city of a country.
When a user asks for the capital of a country:
1. Identify the country name from the user's query.
2. Use the 'get_capital_city' tool to find the capital.
3. Respond clearly to the user, stating the capital city.
Example Query: "What's the capital of {country}?"
Example Response: "The capital of France is Paris."`,
// tools will be added next
})
```
```java
// Example: Adding instructions
LlmAgent capitalAgent =
LlmAgent.builder()
.model("gemini-flash-latest")
.name("capital_agent")
.description("Answers user questions about the capital city of a given country.")
.instruction(
"""
You are an agent that provides the capital city of a country.
When a user asks for the capital of a country:
1. Identify the country name from the user's query.
2. Use the `get_capital_city` tool to find the capital.
3. Respond clearly to the user, stating the capital city.
Example Query: "What's the capital of France?"
Example Response: "The capital of France is Paris."
""")
// tools will be added next
.build();
```
```kotlin
val instructedAgent =
LlmAgent(
name = "capital_agent",
model = Gemini(name = "gemini-flash-latest"),
instruction =
Instruction(
"""
You are an agent that provides the capital city of a country.
When a user asks for the capital of a country:
1. Identify the country name from the user's query.
2. Use the `getCapitalCity` tool to find the capital.
3. Respond clearly to the user, stating the capital city.
Example Query: "What's the capital of {country}?"
Example Response: "The capital of France is Paris."
""".trimIndent(),
),
)
```
To fetch agent instructions dynamically from a Model Context Protocol (MCP) server in Python, see [Dynamic instructions with `McpInstructionProvider`](/tools-custom/mcp-tools/advanced/#dynamic-instructions-with-mcpinstructionprovider).
GlobalInstructionPlugin
To apply shared rules or a consistent personality to *all* agents in your system, use `GlobalInstructionPlugin` instead of the deprecated `global_instruction` parameter. For ADK Go set `GlobalInstruction` or `GlobalInstructionProvider` on `llmagent.Config`.
## Equip the agent with tools
Tools give your `LlmAgent` capabilities beyond the LLM's built-in knowledge or reasoning. They allow the agent to interact with the outside world, perform calculations, fetch real-time data, or execute specific actions.
- **`tools` (Optional):** Provide a list of tools the agent can use. Each item in the list can be:
- A native function or method (wrapped as a `FunctionTool`). Python ADK automatically wraps the native function into a `FunctionTool` whereas, you must explicitly wrap your Java methods using `FunctionTool.create(...)`. In Kotlin, you can use the `@Tool` annotation to automatically generate a `FunctionTool` at compile-time.
- An instance of a class inheriting from `BaseTool`.
- An `AgentTool` wrapping another agent, enabling agent-to-agent delegation. For more information, see [Custom agent workflows](/agents/custom-agents/#delegation). In Python, prefer setting `mode='single_turn'` on the agent and adding it to `sub_agents`, which exposes it as an inline tool automatically, and avoid using the `AgentTool(agent=...)` syntax directly.
The LLM uses the function/tool names, descriptions (from docstrings or the `description` field), and parameter schemas to decide which tool to call based on the conversation and its instructions.
```python
# Define a tool function
def get_capital_city(country: str) -> str:
"""Retrieves the capital city for a given country."""
# Replace with actual logic (e.g., API call, database lookup)
capitals = {"france": "Paris", "japan": "Tokyo", "canada": "Ottawa"}
return capitals.get(country.lower(), f"Sorry, I don't know the capital of {country}.")
# Add the tool to the agent
capital_agent = LlmAgent(
model="gemini-flash-latest",
name="capital_agent",
description="Answers user questions about the capital city of a given country.",
instruction="""You are an agent that provides the capital city of a country... (previous instruction text)""",
tools=[get_capital_city] # Provide the function directly
)
```
```typescript
import {z} from 'zod';
import { LlmAgent, FunctionTool } from '@google/adk';
// Define the schema for the tool's input parameters
const getCapitalCityParamsSchema = z.object({
country: z.string().describe('The country to get capital for.'),
});
// Define the tool function itself
async function getCapitalCity(params: z.infer): Promise<{ capitalCity: string }> {
const capitals: Record = {
'france': 'Paris',
'japan': 'Tokyo',
'canada': 'Ottawa',
};
const result = capitals[params.country.toLowerCase()] ??
`Sorry, I don't know the capital of ${params.country}.`;
return {capitalCity: result}; // Tools must return an object
}
// Create an instance of the FunctionTool
const getCapitalCityTool = new FunctionTool({
name: 'getCapitalCity',
description: 'Retrieves the capital city for a given country.',
parameters: getCapitalCityParamsSchema,
execute: getCapitalCity,
});
// Add the tool to the agent
const capitalAgent = new LlmAgent({
model: 'gemini-flash-latest',
name: 'capitalAgent',
description: 'Answers user questions about the capital city of a given country.',
instruction: 'You are an agent that provides the capital city of a country...', // Note: the full instruction is omitted for brevity
tools: [getCapitalCityTool], // Provide the FunctionTool instance in an array
});
```
```go
// Define a tool function
type getCapitalCityArgs struct {
Country string `json:"country" jsonschema:"The country to get the capital of."`
}
getCapitalCity := func(ctx agent.Context, args getCapitalCityArgs) (map[string]any, error) {
// Replace with actual logic (e.g., API call, database lookup)
capitals := map[string]string{"france": "Paris", "japan": "Tokyo", "canada": "Ottawa"}
capital, ok := capitals[strings.ToLower(args.Country)]
if !ok {
return nil, fmt.Errorf("Sorry, I don't know the capital of %s.", args.Country)
}
return map[string]any{"result": capital}, nil
}
// Add the tool to the agent
capitalTool, err := functiontool.New(
functiontool.Config{
Name: "get_capital_city",
Description: "Retrieves the capital city for a given country.",
},
getCapitalCity,
)
if err != nil {
log.Fatal(err)
}
agent, err := llmagent.New(llmagent.Config{
Name: "capital_agent",
Model: model,
Description: "Answers user questions about the capital city of a given country.",
Instruction: "You are an agent that provides the capital city of a country... (previous instruction text)",
Tools: []tool.Tool{capitalTool},
})
```
```java
// Define a tool function
// Retrieves the capital city of a given country.
public static Map getCapitalCity(
@Schema(name = "country", description = "The country to get capital for")
String country) {
// Replace with actual logic (e.g., API call, database lookup)
Map countryCapitals = new HashMap<>();
countryCapitals.put("canada", "Ottawa");
countryCapitals.put("france", "Paris");
countryCapitals.put("japan", "Tokyo");
String result =
countryCapitals.getOrDefault(
country.toLowerCase(), "Sorry, I couldn't find the capital for " + country + ".");
return Map.of("result", result); // Tools must return a Map
}
// Add the tool to the agent
FunctionTool capitalTool = FunctionTool.create(experiment.getClass(), "getCapitalCity");
LlmAgent capitalAgent =
LlmAgent.builder()
.model("gemini-flash-latest")
.name("capital_agent")
.description("Answers user questions about the capital city of a given country.")
.instruction("You are an agent that provides the capital city of a country... (previous instruction text)")
.tools(capitalTool) // Provide the function wrapped as a FunctionTool
.build();
```
```kotlin
class CapitalService {
@Tool(description = "Retrieves the capital city for a given country.")
fun getCapitalCity(
@Param("The country to get capital for.") country: String,
): String {
val capitals = mapOf("france" to "Paris", "japan" to "Tokyo", "canada" to "Ottawa")
return capitals[country.lowercase()] ?: "Sorry, I don't know the capital of $country."
}
}
// Add the tool to the agent
// Note: generatedTools() is generated by KSP for classes containing @Tool annotated functions.
// In a real project, you would need to set up the ADK KSP processor.
// val agentWithTools = LlmAgent(
// name = "capital_agent",
// model = Gemini(name = "gemini-flash-latest"),
// tools = capitalService.generatedTools()
// )
```
Learn more about Tools in [Custom Tools](/tools-custom/).
## Advanced configuration and control
Beyond the core parameters, `LlmAgent` offers several options for finer control:
### Fine-tune AI model operation
You can adjust how the underlying AI model generates responses using `generate_content_config`.
- **`generate_content_config` (Optional):** Pass an instance of [`google.genai.types.GenerateContentConfig`](https://googleapis.github.io/python-genai/genai.html#genai.types.GenerateContentConfig) to control parameters like `temperature` (randomness), `max_output_tokens` (response length), `top_p`, `top_k`, and safety settings. In Python, ADK raises a `ValueError` if `generate_content_config` sets `tools`, `system_instruction`, `response_schema`, or `http_options.base_url`. Set the first three through the agent's `tools`, `instruction`, and `output_schema` fields instead, and set the base URL on the model or its client.
```python
from google.genai import types
agent = LlmAgent(
# ... other params
generate_content_config=types.GenerateContentConfig(
temperature=0.2, # More deterministic output
max_output_tokens=250,
safety_settings=[
types.SafetySetting(
category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold=types.HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
)
]
)
)
```
```typescript
import { GenerateContentConfig } from '@google/genai';
const generateContentConfig: GenerateContentConfig = {
temperature: 0.2, // More deterministic output
maxOutputTokens: 250,
};
const agent = new LlmAgent({
// ... other params
generateContentConfig,
});
```
```go
import "google.golang.org/genai"
temperature := float32(0.2)
agent, err := llmagent.New(llmagent.Config{
Name: "gen_config_agent",
Model: model,
GenerateContentConfig: &genai.GenerateContentConfig{
Temperature: &temperature,
MaxOutputTokens: 250,
},
})
```
```java
import com.google.genai.types.GenerateContentConfig;
LlmAgent agent =
LlmAgent.builder()
// ... other params
.generateContentConfig(GenerateContentConfig.builder()
.temperature(0.2F) // More deterministic output
.maxOutputTokens(250)
.build())
.build();
```
```kotlin
val agentWithConfig =
LlmAgent(
name = "capital_agent",
model = Gemini(name = "gemini-flash-latest"),
generateContentConfig =
GenerateContentConfig(
// More deterministic output
temperature = 0.2f,
maxOutputTokens = 250,
safetySettings =
listOf(
SafetySetting(
category = HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold = HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
),
),
),
)
```
### Configure a default model
Supported in ADKPython v1.22.0
You can set a system-wide default model for all `LlmAgent` instances using the `set_default_model` class method. If you do not specify a model when creating an agent, it falls back to ADK's built-in default model. This setting helps you avoid redundant model specifications and easily change the model for all agents at once.
```python
from google.adk.agents import LlmAgent
# Set a new default model for all agents
LlmAgent.set_default_model("gemini-flash-latest")
# This agent will now use "gemini-flash-latest" by default
agent_with_default_model = LlmAgent(
name="default_model_agent",
instruction="You are a helpful assistant."
)
# You can still override the default for specific agents
specific_agent = LlmAgent(
name="specific_model_agent",
model="gemini-pro-latest",
instruction="You are a creative writer."
)
```
### Structure data input and output
For scenarios requiring structured data exchange with an `LLM Agent`, the ADK provides mechanisms to define expected input and desired output formats using schema definitions.
- **`input_schema` (Optional):** Define a schema representing the expected input structure when this agent is invoked *as a tool*. In Python, `AgentTool` validates the calling agent's arguments against the schema and passes the result as this agent's input. The schema does not apply to user messages sent directly to the agent.
- **`output_schema` (Optional):** Define a schema representing the desired output structure. If set, the agent's final response *must* be a JSON string conforming to this schema.
- In Python, supported schema types include:
- A Pydantic model class, for example `MySchema`
- Lists of primitives, such as `list[str]`, `list[int]`, `list[bool]`, `list[float]`
- `dict`
- `google.genai.types.Schema`
Using `output_schema` with `tools`
In Python, ADK supports `output_schema` and `tools` on the same agent: the tools stay available while the agent works, and the schema applies only to the final response. Some models accept both in one request, such as Gemini models on Google Cloud and `LiteLlm` models whose provider supports response schemas, and ADK passes them through directly. For other models, ADK adds a [`set_model_response` function tool](https://github.com/google/adk-python/blob/main/src/google/adk/tools/set_model_response_tool.py) and instructs the model to return its final answer through that tool, which may not work reliably. In such cases, consider using sub-agents that handle output formatting separately.
- **`output_key` (Optional):** Provide a string key. If set, the agent's *final* response will be automatically saved to the session's state dictionary under this key. This is useful for passing results between agents or steps in a workflow.
- In Python, this might look like: `session.state[output_key] = agent_response_text`. If `output_schema` is also set, ADK parses the response against the schema first and stores the parsed value rather than the response text. A `BaseModel` schema is stored as a `dict`, and a `list[BaseModel]` schema is stored as a `list[dict]`.
- In Java: `session.state().put(outputKey, agentResponseText)`
- In Golang, within a callback handler: `ctx.State().Set(output_key, agentResponseText)`
In Java and Kotlin, when `output_schema` is also set, the *parsed* response is stored as a `Map` instead of the text.
Schema validation in Java and Kotlin
Java and Kotlin check the response against the *structure* of the schema — `type`, `required`, `nullable`, `anyOf` and `items` (see [`SchemaUtils`](https://github.com/google/adk-kotlin/blob/v1.0.0/core/src/commonMain/kotlin/com/google/adk/kt/SchemaUtils.kt)). Constraint fields such as `pattern`, `minLength` and `minimum` are sent to the model as part of the schema, but ADK does not re-check them, so the model decides whether to honor them. Python validates against a Pydantic model, which does enforce the constraints declared on it.
Java and Kotlin accept only a top-level object schema; a top-level array or primitive fails validation. Python also supports list and primitive output schemas.
If the response fails validation, ADK logs the error and stores the raw response string under `output_key` instead of the parsed object (see [`LlmAgent`](https://github.com/google/adk-kotlin/blob/v1.0.0/core/src/commonMain/kotlin/com/google/adk/kt/agents/LlmAgent.kt)).
The input and output schema is typically a `Pydantic` BaseModel.
```python
from pydantic import BaseModel, Field
class CapitalOutput(BaseModel):
capital: str = Field(description="The capital of the country.")
structured_capital_agent = LlmAgent(
# ... name, model, description
instruction="""You are a Capital Information Agent. Given a country, respond ONLY with a JSON object containing the capital. Format: {"capital": "capital_name"}""",
output_schema=CapitalOutput, # Enforce JSON output
output_key="found_capital" # Store result in state['found_capital']
# tools=[...] may be combined with output_schema; see the note above
)
```
```typescript
import {z} from 'zod';
import { Schema, Type } from '@google/genai';
// Define the schema for the output
const CapitalOutputSchema: Schema = {
type: Type.OBJECT,
properties: {
capital: {
type: Type.STRING,
description: 'The capital of the country.',
},
},
required: ['capital'],
};
// Create the LlmAgent instance
const structuredCapitalAgent = new LlmAgent({
// ... name, model, description
instruction: `You are a Capital Information Agent. Given a country, respond ONLY with a JSON object containing the capital. Format: {"capital": "capital_name"}`,
outputSchema: CapitalOutputSchema, // Enforce JSON output
outputKey: 'found_capital', // Store result in state['found_capital']
// Cannot use tools effectively here
});
```
The input and output schema is a `google.genai.types.Schema` object.
```go
capitalOutput := &genai.Schema{
Type: genai.TypeObject,
Description: "Schema for capital city information.",
Properties: map[string]*genai.Schema{
"capital": {
Type: genai.TypeString,
Description: "The capital city of the country.",
},
},
}
agent, err := llmagent.New(llmagent.Config{
Name: "structured_capital_agent",
Model: model,
Description: "Provides capital information in a structured format.",
Instruction: `You are a Capital Information Agent. Given a country, respond ONLY with a JSON object containing the capital. Format: {"capital": "capital_name"}`,
OutputSchema: capitalOutput,
OutputKey: "found_capital",
// Tools can still be added. ADK injects a set_model_response tool when
// the model can't combine tools with OutputSchema natively.
})
```
The input and output schema is a `google.genai.types.Schema` object.
```java
private static final Schema CAPITAL_OUTPUT =
Schema.builder()
.type("OBJECT")
.description("Schema for capital city information.")
.properties(
Map.of(
"capital",
Schema.builder()
.type("STRING")
.description("The capital city of the country.")
.build()))
.build();
LlmAgent structuredCapitalAgent =
LlmAgent.builder()
// ... name, model, description
.instruction(
"You are a Capital Information Agent. Given a country, respond ONLY with a JSON object containing the capital. Format: {\"capital\": \"capital_name\"}")
.outputSchema(CAPITAL_OUTPUT) // Enforce JSON output
.outputKey("found_capital") // Store result in state.get("found_capital")
// Cannot use tools(getCapitalCity) effectively here
.build();
```
The input and output schema is ADK's own `com.google.adk.kt.types.Schema`, not the same-named type in the GenAI SDK. Starting with ADK Kotlin v0.8.0, the JSON schema includes constraints for the following fields: `pattern`, `minLength`, `maxLength`, `minimum`, `maximum`, `minItems`, `maxItems`, `format`, `nullable`, `default`, `anyOf` and `title`.
```kotlin
val capitalOutput =
Schema(
type = Type.OBJECT,
description = "Schema for capital city information.",
properties =
mapOf(
"capital" to
Schema(
type = Type.STRING,
description = "The capital city of the country.",
// Constraint fields, added in adk-kotlin 0.8.0.
minLength = 2,
maxLength = 60,
),
"countryCode" to
Schema(
type = Type.STRING,
description = "ISO 3166-1 alpha-2 code for the country.",
pattern = "^[A-Z]{2}$",
),
),
required = listOf("capital", "countryCode"),
)
val structuredCapitalAgent =
LlmAgent(
name = "structured_capital_agent",
model = Gemini(name = "gemini-flash-latest"),
instruction =
Instruction(
"You are a Capital Information Agent. Given a country, respond ONLY " +
"with a JSON object holding the capital city and the country's " +
"ISO 3166-1 alpha-2 code.",
),
outputSchema = capitalOutput,
outputKey = "found_capital",
)
```
The `format` field accepts only the values the model allows for the field's type. For the accepted values, see the Gemini [`Schema` reference](https://ai.google.dev/api/caching#Schema).
The `default` field must contain a JSON-native value. ADK's own `Json` serializes one, but a hand-rolled serializer without a contextual `Any` serializer does not.
### Manage agent context
Control whether the agent receives the prior conversation history.
- **`include_contents` (Optional, Default: `'default'`):** Determines if the `contents` (history) are sent to the LLM.
- `'default'`: The agent receives the relevant conversation history.
- `'none'`: The agent receives no prior `contents`. It operates based solely on its current instruction and any input provided in the *current* turn (useful for stateless tasks or enforcing specific contexts).
```python
stateless_agent = LlmAgent(
# ... other params
include_contents='none'
)
```
```typescript
const statelessAgent = new LlmAgent({
// ... other params
includeContents: 'none',
});
```
```go
import "google.golang.org/adk/v2/agent/llmagent"
agent, err := llmagent.New(llmagent.Config{
Name: "stateless_agent",
Model: model,
IncludeContents: llmagent.IncludeContentsNone,
})
```
```java
import com.google.adk.agents.LlmAgent.IncludeContents;
LlmAgent statelessAgent =
LlmAgent.builder()
// ... other params
.includeContents(IncludeContents.NONE)
.build();
```
```kotlin
val statelessAgent =
LlmAgent(
name = "capital_agent",
model = Gemini(name = "gemini-flash-latest"),
// ... other params
includeContents = IncludeContents.NONE,
)
```
Go v2.0.0: agent execution modes
ADK Go v2.0.0 introduces an explicit `Mode` field on `llmagent.Config` that controls how the agent runs when used inside a graph-based or dynamic workflow. Three modes are available:
- **`ModeChat`** (default for an agent used as a sub-agent): The agent participates in a multi-turn conversation with the user and is reachable from peer agents via `transfer_to_agent`.
- **`ModeSingleTurn`** (default for an agent used as a node in a workflow): The agent completes its task in a single turn without chatting with the user.
- **`ModeTask`**: A task agent that chats with the user to accomplish a task — in contrast to `ModeSingleTurn`, it can interact with the user across turns to complete the work.
When you wrap an `llmagent` with `workflow.NewAgentNode`, the workflow engine automatically sets the mode to `ModeSingleTurn` if no mode is specified — equivalent to Python's `mode="single_turn"` on an agent used as a workflow node. For more information on composing agents in graph-based workflows, see [Graph-based agent workflows](/graphs/).
An agent that runs as a `ModeSingleTurn` node, such as a workflow node, sees only the current turn, as if `IncludeContents` were `IncludeContentsNone`. Since ADK Go v2.4.0, you can keep the conversation history by setting `IncludeContents` to `llmagent.IncludeContentsDefault` explicitly. Earlier versions ignore that setting for single-turn nodes.
### Configure a planner
Supported in ADKPython v0.1.0
**`planner` (Optional):** Assign a `BasePlanner` instance to enable multi-step reasoning and planning before execution. There are two main planners:
- **`BuiltInPlanner`:** Leverages the model's built-in planning capabilities (e.g., Gemini's thinking feature). See [Gemini Thinking](https://ai.google.dev/gemini-api/docs/thinking) for details and examples.
Here, the `thinking_budget` parameter guides the model on the number of thinking tokens to use when generating a response. The `include_thoughts` parameter controls whether the model should include its raw thoughts and internal reasoning process in the response.
```python
from google.adk import Agent
from google.adk.planners import BuiltInPlanner
from google.genai import types
my_agent = Agent(
name="my_agent",
model="gemini-flash-latest",
planner=BuiltInPlanner(
thinking_config=types.ThinkingConfig(
include_thoughts=True,
thinking_budget=1024,
)
),
# ... your tools here
)
```
- **`PlanReActPlanner`:** This planner instructs the model to follow a specific structure in its output: first create a plan, then execute actions (like calling tools), and provide reasoning for its steps. *It's particularly useful for models that don't have a built-in "thinking" feature*.
```python
from google.adk import Agent
from google.adk.planners import PlanReActPlanner
my_agent = Agent(
name="my_agent",
model="gemini-flash-latest",
planner=PlanReActPlanner(),
# ... your tools here
)
```
The agent's response will follow a structured format:
```text
[user]: ai news
[google_search_agent]: /*PLANNING*/
1. Perform a Google search for "latest AI news" to get current updates and headlines related to artificial intelligence.
2. Synthesize the information from the search results to provide a summary of recent AI news.
/*ACTION*/
/*REASONING*/
The search results provide a comprehensive overview of recent AI news, covering various aspects like company developments, research breakthroughs, and applications. I have enough information to answer the user's request.
/*FINAL_ANSWER*/
Here's a summary of recent AI news:
....
```
Example for using built-in-planner:
```python
from dotenv import load_dotenv
import asyncio
import os
from google.genai import types
from google.adk.agents.llm_agent import LlmAgent
from google.adk.runners import Runner
from google.adk.sessions import InMemorySessionService
from google.adk.artifacts.in_memory_artifact_service import InMemoryArtifactService # Optional
from google.adk.planners import BasePlanner, BuiltInPlanner, PlanReActPlanner
from google.adk.models import LlmRequest
from google.genai.types import ThinkingConfig
from google.genai.types import GenerateContentConfig
import datetime
from zoneinfo import ZoneInfo
APP_NAME = "weather_app"
USER_ID = "1234"
SESSION_ID = "session1234"
def get_weather(city: str) -> dict:
"""Retrieves the current weather report for a specified city.
Args:
city (str): The name of the city for which to retrieve the weather report.
Returns:
dict: status and result or error msg.
"""
if city.lower() == "new york":
return {
"status": "success",
"report": (
"The weather in New York is sunny with a temperature of 25 degrees"
" Celsius (77 degrees Fahrenheit)."
),
}
else:
return {
"status": "error",
"error_message": f"Weather information for '{city}' is not available.",
}
def get_current_time(city: str) -> dict:
"""Returns the current time in a specified city.
Args:
city (str): The name of the city for which to retrieve the current time.
Returns:
dict: status and result or error msg.
"""
if city.lower() == "new york":
tz_identifier = "America/New_York"
else:
return {
"status": "error",
"error_message": (
f"Sorry, I don't have timezone information for {city}."
),
}
tz = ZoneInfo(tz_identifier)
now = datetime.datetime.now(tz)
report = (
f'The current time in {city} is {now.strftime("%Y-%m-%d %H:%M:%S %Z%z")}'
)
return {"status": "success", "report": report}
# Step 1: Create a ThinkingConfig
thinking_config = ThinkingConfig(
include_thoughts=True, # Ask the model to include its thoughts in the response
thinking_budget=256 # Limit the 'thinking' to 256 tokens (adjust as needed)
)
print("ThinkingConfig:", thinking_config)
# Step 2: Instantiate BuiltInPlanner
planner = BuiltInPlanner(
thinking_config=thinking_config
)
print("BuiltInPlanner created.")
# Step 3: Wrap the planner in an LlmAgent
agent = LlmAgent(
model="gemini-flash-latest", # Set your model name
name="weather_and_time_agent",
instruction="You are an agent that returns time and weather",
planner=planner,
tools=[get_weather, get_current_time]
)
# Session and Runner
session_service = InMemorySessionService()
session = asyncio.run(
session_service.create_session(app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID)
)
runner = Runner(agent=agent, app_name=APP_NAME, session_service=session_service)
# Agent Interaction
def call_agent(query):
content = types.Content(role='user', parts=[types.Part(text=query)])
events = runner.run(user_id=USER_ID, session_id=SESSION_ID, new_message=content)
for event in events:
print(f"\nDEBUG EVENT: {event}\n")
if event.is_final_response() and event.content and event.content.parts:
final_answer = event.content.parts[0].text.strip()
print("\n🟢 FINAL ANSWER\n", final_answer, "\n")
call_agent("If it's raining in New York right now, what is the current temperature?")
```
### Code execution
Supported in ADKPython v0.1.0Java v0.1.0
- **`code_executor` (Optional):** Provide a `BaseCodeExecutor` instance to allow the agent to execute code blocks found in the LLM's response. For more information, see [Code Execution with Gemini API](/integrations/code-execution/).
````python
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
from google.adk.agents import LlmAgent
from google.adk.runners import Runner
from google.adk.sessions import InMemorySessionService
from google.adk.code_executors import BuiltInCodeExecutor
from google.genai import types
AGENT_NAME = "calculator_agent"
APP_NAME = "calculator"
USER_ID = "user1234"
SESSION_ID = "session_code_exec_async"
GEMINI_MODEL = "gemini-2.0-flash"
# Agent Definition
code_agent = LlmAgent(
name=AGENT_NAME,
model=GEMINI_MODEL,
code_executor=BuiltInCodeExecutor(),
instruction="""You are a calculator agent.
When given a mathematical expression, write and execute Python code to calculate the result.
Return only the final numerical result as plain text, without markdown or code blocks.
""",
description="Executes Python code to perform calculations.",
)
# Session and Runner
session_service = InMemorySessionService()
session = asyncio.run(session_service.create_session(
app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID
))
runner = Runner(agent=code_agent, app_name=APP_NAME,
session_service=session_service)
# Agent Interaction (Async)
async def call_agent_async(query):
content = types.Content(role="user", parts=[types.Part(text=query)])
print(f"\n--- Running Query: {query} ---")
final_response_text = "No final text response captured."
try:
# Use run_async
async for event in runner.run_async(
user_id=USER_ID, session_id=SESSION_ID, new_message=content
):
print(f"Event ID: {event.id}, Author: {event.author}")
# --- Check for specific parts FIRST ---
has_specific_part = False
if event.content and event.content.parts:
for part in event.content.parts: # Iterate through all parts
if part.executable_code:
# Access the actual code string via .code
print(
f" Debug: Agent generated code:\n```python\n{part.executable_code.code}\n```"
)
has_specific_part = True
elif part.code_execution_result:
# Access outcome and output correctly
print(
f" Debug: Code Execution Result: {part.code_execution_result.outcome} - Output:\n{part.code_execution_result.output}"
)
has_specific_part = True
# Also print any text parts found in any event for debugging
elif part.text and not part.text.isspace():
print(f" Text: '{part.text.strip()}'")
# Do not set has_specific_part=True here, as we want the final response logic below
# --- Check for final response AFTER specific parts ---
# Only consider it final if it doesn't have the specific code parts we just handled
if not has_specific_part and event.is_final_response():
if (
event.content
and event.content.parts
and event.content.parts[0].text
):
final_response_text = event.content.parts[0].text.strip()
print(f"==> Final Agent Response: {final_response_text}")
else:
print(
"==> Final Agent Response: [No text content in final event]")
except Exception as e:
print(f"ERROR during agent run: {e}")
print("-" * 30)
# Main async function to run the examples
async def main():
await call_agent_async("Calculate the value of (5 + 7) * 3")
await call_agent_async("What is 10 factorial?")
# Execute the main async function
try:
asyncio.run(main())
except RuntimeError as e:
# Handle specific error when running asyncio.run in an already running loop (like Jupyter/Colab)
if "cannot be called from a running event loop" in str(e):
print("\nRunning in an existing event loop (like Colab/Jupyter).")
print("Please run `await main()` in a notebook cell instead.")
# If in an interactive environment like a notebook, you might need to run:
# await main()
else:
raise e # Re-raise other runtime errors
````
````java
import com.google.adk.agents.BaseAgent;
import com.google.adk.agents.LlmAgent;
import com.google.adk.runner.Runner;
import com.google.adk.sessions.InMemorySessionService;
import com.google.adk.sessions.Session;
import com.google.adk.tools.BuiltInCodeExecutionTool;
import com.google.common.collect.ImmutableList;
import com.google.genai.types.Content;
import com.google.genai.types.Part;
public class CodeExecutionAgentApp {
private static final String AGENT_NAME = "calculator_agent";
private static final String APP_NAME = "calculator";
private static final String USER_ID = "user1234";
private static final String SESSION_ID = "session_code_exec_sync";
private static final String GEMINI_MODEL = "gemini-2.0-flash";
/**
* Calls the agent with a query and prints the interaction events and final response.
*
* @param runner The runner instance for the agent.
* @param query The query to send to the agent.
*/
public static void callAgent(Runner runner, String query) {
Content content =
Content.builder().role("user").parts(ImmutableList.of(Part.fromText(query))).build();
InMemorySessionService sessionService = (InMemorySessionService) runner.sessionService();
Session session =
sessionService
.createSession(APP_NAME, USER_ID, /* state= */ null, SESSION_ID)
.blockingGet();
System.out.println("\n--- Running Query: " + query + " ---");
final String[] finalResponseText = {"No final text response captured."};
try {
runner
.runAsync(session.userId(), session.id(), content)
.forEach(
event -> {
System.out.println("Event ID: " + event.id() + ", Author: " + event.author());
boolean hasSpecificPart = false;
if (event.content().isPresent() && event.content().get().parts().isPresent()) {
for (Part part : event.content().get().parts().get()) {
if (part.executableCode().isPresent()) {
System.out.println(
" Debug: Agent generated code:\n```python\n"
+ part.executableCode().get().code()
+ "\n```");
hasSpecificPart = true;
} else if (part.codeExecutionResult().isPresent()) {
System.out.println(
" Debug: Code Execution Result: "
+ part.codeExecutionResult().get().outcome()
+ " - Output:\n"
+ part.codeExecutionResult().get().output());
hasSpecificPart = true;
} else if (part.text().isPresent() && !part.text().get().trim().isEmpty()) {
System.out.println(" Text: '" + part.text().get().trim() + "'");
}
}
}
if (!hasSpecificPart && event.finalResponse()) {
if (event.content().isPresent()
&& event.content().get().parts().isPresent()
&& !event.content().get().parts().get().isEmpty()
&& event.content().get().parts().get().get(0).text().isPresent()) {
finalResponseText[0] =
event.content().get().parts().get().get(0).text().get().trim();
System.out.println("==> Final Agent Response: " + finalResponseText[0]);
} else {
System.out.println(
"==> Final Agent Response: [No text content in final event]");
}
}
});
} catch (Exception e) {
System.err.println("ERROR during agent run: " + e.getMessage());
e.printStackTrace();
}
System.out.println("------------------------------");
}
public static void main(String[] args) {
BuiltInCodeExecutionTool codeExecutionTool = new BuiltInCodeExecutionTool();
BaseAgent codeAgent =
LlmAgent.builder()
.name(AGENT_NAME)
.model(GEMINI_MODEL)
.tools(ImmutableList.of(codeExecutionTool))
.instruction(
"""
You are a calculator agent.
When given a mathematical expression, write and execute Python code to calculate the result.
Return only the final numerical result as plain text, without markdown or code blocks.
""")
.description("Executes Python code to perform calculations.")
.build();
InMemorySessionService sessionService = new InMemorySessionService();
Runner runner = new Runner(codeAgent, APP_NAME, null, sessionService);
callAgent(runner, "Calculate the value of (5 + 7) * 3");
callAgent(runner, "What is 10 factorial?");
}
}
````
## Code example
This following example demonstrates the core concepts discussed in this page. More complex agents might incorporate schemas, context control, and planning.
Code
Here's the complete basic `capital_agent`:
```python
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# --- Full example code demonstrating LlmAgent with Tools vs. Output Schema ---
import json # Needed for pretty printing dicts
import asyncio
from google.adk.agents import LlmAgent
from google.adk.runners import Runner
from google.adk.sessions import InMemorySessionService
from google.genai import types
from pydantic import BaseModel, Field
# --- 1. Define Constants ---
APP_NAME = "agent_comparison_app"
USER_ID = "test_user_456"
SESSION_ID_TOOL_AGENT = "session_tool_agent_xyz"
SESSION_ID_SCHEMA_AGENT = "session_schema_agent_xyz"
MODEL_NAME = "gemini-2.0-flash"
# --- 2. Define Schemas ---
# Input schema used by both agents
class CountryInput(BaseModel):
country: str = Field(description="The country to get information about.")
# Output schema ONLY for the second agent
class CapitalInfoOutput(BaseModel):
capital: str = Field(description="The capital city of the country.")
# Note: Population is illustrative; the LLM will infer or estimate this
# as it cannot use tools when output_schema is set.
population_estimate: str = Field(description="An estimated population of the capital city.")
# --- 3. Define the Tool (Only for the first agent) ---
def get_capital_city(country: str) -> str:
"""Retrieves the capital city of a given country."""
print(f"\n-- Tool Call: get_capital_city(country='{country}') --")
country_capitals = {
"united states": "Washington, D.C.",
"canada": "Ottawa",
"france": "Paris",
"japan": "Tokyo",
}
result = country_capitals.get(country.lower(), f"Sorry, I couldn't find the capital for {country}.")
print(f"-- Tool Result: '{result}' --")
return result
# --- 4. Configure Agents ---
# Agent 1: Uses a tool and output_key
capital_agent_with_tool = LlmAgent(
model=MODEL_NAME,
name="capital_agent_tool",
description="Retrieves the capital city using a specific tool.",
instruction="""You are a helpful agent that provides the capital city of a country using a tool.
The user will provide the country name in a JSON format like {"country": "country_name"}.
1. Extract the country name.
2. Use the `get_capital_city` tool to find the capital.
3. Respond clearly to the user, stating the capital city found by the tool.
""",
tools=[get_capital_city],
input_schema=CountryInput,
output_key="capital_tool_result", # Store final text response
)
# Agent 2: Uses output_schema (NO tools possible)
structured_info_agent_schema = LlmAgent(
model=MODEL_NAME,
name="structured_info_agent_schema",
description="Provides capital and estimated population in a specific JSON format.",
instruction=f"""You are an agent that provides country information.
The user will provide the country name in a JSON format like {{"country": "country_name"}}.
Respond ONLY with a JSON object matching this exact schema:
{json.dumps(CapitalInfoOutput.model_json_schema(), indent=2)}
Use your knowledge to determine the capital and estimate the population. Do not use any tools.
""",
# *** NO tools parameter here - using output_schema prevents tool use ***
input_schema=CountryInput,
output_schema=CapitalInfoOutput, # Enforce JSON output structure
output_key="structured_info_result", # Store final JSON response
)
# --- 5. Set up Session Management and Runners ---
session_service = InMemorySessionService()
# Create a runner for EACH agent
capital_runner = Runner(
agent=capital_agent_with_tool,
app_name=APP_NAME,
session_service=session_service
)
structured_runner = Runner(
agent=structured_info_agent_schema,
app_name=APP_NAME,
session_service=session_service
)
# --- 6. Define Agent Interaction Logic ---
async def call_agent_and_print(
runner_instance: Runner,
agent_instance: LlmAgent,
session_id: str,
query_json: str
):
"""Sends a query to the specified agent/runner and prints results."""
print(f"\n>>> Calling Agent: '{agent_instance.name}' | Query: {query_json}")
user_content = types.Content(role='user', parts=[types.Part(text=query_json)])
final_response_content = "No final response received."
async for event in runner_instance.run_async(user_id=USER_ID, session_id=session_id, new_message=user_content):
# print(f"Event: {event.type}, Author: {event.author}") # Uncomment for detailed logging
if event.is_final_response() and event.content and event.content.parts:
# For output_schema, the content is the JSON string itself
final_response_content = event.content.parts[0].text
print(f"<<< Agent '{agent_instance.name}' Response: {final_response_content}")
current_session = await session_service.get_session(app_name=APP_NAME,
user_id=USER_ID,
session_id=session_id)
stored_output = current_session.state.get(agent_instance.output_key)
# Pretty print if the stored output looks like JSON (likely from output_schema)
print(f"--- Session State ['{agent_instance.output_key}']: ", end="")
try:
# Attempt to parse and pretty print if it's JSON
parsed_output = json.loads(stored_output)
print(json.dumps(parsed_output, indent=2))
except (json.JSONDecodeError, TypeError):
# Otherwise, print as string
print(stored_output)
print("-" * 30)
# --- 7. Run Interactions ---
async def main():
# Create separate sessions for clarity, though not strictly necessary if context is managed
print("--- Creating Sessions ---")
await session_service.create_session(app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID_TOOL_AGENT)
await session_service.create_session(app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID_SCHEMA_AGENT)
print("--- Testing Agent with Tool ---")
await call_agent_and_print(capital_runner, capital_agent_with_tool, SESSION_ID_TOOL_AGENT, '{"country": "France"}')
await call_agent_and_print(capital_runner, capital_agent_with_tool, SESSION_ID_TOOL_AGENT, '{"country": "Canada"}')
print("\n\n--- Testing Agent with Output Schema (No Tool Use) ---")
await call_agent_and_print(structured_runner, structured_info_agent_schema, SESSION_ID_SCHEMA_AGENT, '{"country": "France"}')
await call_agent_and_print(structured_runner, structured_info_agent_schema, SESSION_ID_SCHEMA_AGENT, '{"country": "Japan"}')
# --- Run the Agent ---
# Note: In Colab, you can directly use 'await' at the top level.
# If running this code as a standalone Python script, you'll need to use asyncio.run() or manage the event loop.
if __name__ == "__main__":
asyncio.run(main())
```
```typescript
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { LlmAgent, FunctionTool, InMemoryRunner, isFinalResponse } from '@google/adk';
import { createUserContent, Schema, Type } from '@google/genai';
import type { Part } from '@google/genai';
import { z } from 'zod';
// --- 1. Define Constants ---
const APP_NAME = "capital_app_ts";
const USER_ID = "test_user_789";
const SESSION_ID_TOOL_AGENT = "session_tool_agent_ts";
const SESSION_ID_SCHEMA_AGENT = "session_schema_agent_ts";
const MODEL_NAME = "gemini-2.5-flash"; // Using flash for speed
// --- 2. Define Schemas ---
// A. Schema for the Tool's parameters (using Zod)
const CountryInput = z.object({
country: z.string().describe('The country to get the capital for.'),
});
// B. Output schema ONLY for the second agent (using ADK's Schema type)
const CapitalInfoOutputSchema: Schema = {
type: Type.OBJECT,
description: "Schema for capital city information.",
properties: {
capital: {
type: Type.STRING,
description: "The capital city of the country."
},
population_estimate: {
type: Type.STRING,
description: "An estimated population of the capital city."
},
},
required: ["capital", "population_estimate"],
};
// --- 3. Define the Tool (Only for the first agent) ---
async function getCapitalCity(params: z.infer): Promise<{ result: string }> {
console.log(`\n-- Tool Call: getCapitalCity(country='${params.country}') --`);
const capitals: Record = {
'united states': 'Washington, D.C.',
'canada': 'Ottawa',
'france': 'Paris',
'japan': 'Tokyo',
};
const result = capitals[params.country.toLowerCase()] ??
`Sorry, I couldn't find the capital for ${params.country}.`;
console.log(`-- Tool Result: '${result}' --`);
return { result: result }; // Tools must return an object
}
// --- 4. Configure Agents ---
// Agent 1: Uses a tool and outputKey
const getCapitalCityTool = new FunctionTool({
name: 'get_capital_city',
description: 'Retrieves the capital city for a given country',
parameters: CountryInput,
execute: getCapitalCity,
});
const capitalAgentWithTool = new LlmAgent({
model: MODEL_NAME,
name: 'capital_agent_tool',
description: 'Retrieves the capital city using a specific tool.',
instruction: `You are a helpful agent that provides the capital city of a country using a tool.
The user will provide the country name in a JSON format like {"country": "country_name"}.
1. Extract the country name.
2. Use the \`get_capital_city\` tool to find the capital.
3. Respond with a JSON object with the key 'capital' and the value as the capital city.
`,
tools: [getCapitalCityTool],
outputKey: "capital_tool_result", // Store final text response
});
// Agent 2: Uses outputSchema (NO tools possible)
const structuredInfoAgentSchema = new LlmAgent({
model: MODEL_NAME,
name: 'structured_info_agent_schema',
description: 'Provides capital and estimated population in a specific JSON format.',
instruction: `You are an agent that provides country information.
The user will provide the country name in a JSON format like {"country": "country_name"}.
Respond ONLY with a JSON object matching this exact schema:
${JSON.stringify(CapitalInfoOutputSchema, null, 2)}
Use your knowledge to determine the capital and estimate the population. Do not use any tools.
`,
// *** NO tools parameter here - using outputSchema prevents tool use ***
outputSchema: CapitalInfoOutputSchema,
outputKey: "structured_info_result",
});
// --- 5. Define Agent Interaction Logic ---
async function callAgentAndPrint(
runner: InMemoryRunner,
agent: LlmAgent,
sessionId: string,
queryJson: string
) {
console.log(`\n>>> Calling Agent: '${agent.name}' | Query: ${queryJson}`);
const message = createUserContent(queryJson);
let finalResponseContent = "No final response received.";
for await (const event of runner.runAsync({ userId: USER_ID, sessionId: sessionId, newMessage: message })) {
if (isFinalResponse(event) && event.content?.parts?.length) {
finalResponseContent = event.content.parts.map((part: Part) => part.text ?? '').join('');
}
}
console.log(`<<< Agent '${agent.name}' Response: ${finalResponseContent}`);
// Check the session state
const currentSession = await runner.sessionService.getSession({ appName: APP_NAME, userId: USER_ID, sessionId: sessionId });
if (!currentSession) {
console.log(`--- Session not found: ${sessionId} ---`);
return;
}
const storedOutput = currentSession.state[agent.outputKey!];
console.log(`--- Session State ['${agent.outputKey}']: `);
try {
// Attempt to parse and pretty print if it's JSON
const parsedOutput = JSON.parse(storedOutput as string);
console.log(JSON.stringify(parsedOutput, null, 2));
} catch (e) {
// Otherwise, print as a string
console.log(storedOutput);
}
console.log("-".repeat(30));
}
// --- 6. Run Interactions ---
async function main() {
// Set up runners for each agent
const capitalRunner = new InMemoryRunner({ appName: APP_NAME, agent: capitalAgentWithTool });
const structuredRunner = new InMemoryRunner({ appName: APP_NAME, agent: structuredInfoAgentSchema });
// Create sessions
console.log("--- Creating Sessions ---");
await capitalRunner.sessionService.createSession({ appName: APP_NAME, userId: USER_ID, sessionId: SESSION_ID_TOOL_AGENT });
await structuredRunner.sessionService.createSession({ appName: APP_NAME, userId: USER_ID, sessionId: SESSION_ID_SCHEMA_AGENT });
console.log("\n--- Testing Agent with Tool ---");
await callAgentAndPrint(capitalRunner, capitalAgentWithTool, SESSION_ID_TOOL_AGENT, '{"country": "France"}');
await callAgentAndPrint(capitalRunner, capitalAgentWithTool, SESSION_ID_TOOL_AGENT, '{"country": "Canada"}');
console.log("\n\n--- Testing Agent with Output Schema (No Tool Use) ---");
await callAgentAndPrint(structuredRunner, structuredInfoAgentSchema, SESSION_ID_SCHEMA_AGENT, '{"country": "France"}');
await callAgentAndPrint(structuredRunner, structuredInfoAgentSchema, SESSION_ID_SCHEMA_AGENT, '{"country": "Japan"}');
}
main();
```
```go
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"strings"
"google.golang.org/adk/v2/agent"
"google.golang.org/adk/v2/agent/llmagent"
"google.golang.org/adk/v2/model/gemini"
"google.golang.org/adk/v2/runner"
"google.golang.org/adk/v2/session"
"google.golang.org/adk/v2/tool"
"google.golang.org/adk/v2/tool/functiontool"
"google.golang.org/genai"
)
// --- Main Runnable Example ---
const (
modelName = "gemini-flash-latest"
appName = "agent_comparison_app"
userID = "test_user_456"
)
type getCapitalCityArgs struct {
Country string `json:"country" jsonschema:"The country to get the capital of."`
}
// getCapitalCity retrieves the capital city of a given country.
func getCapitalCity(ctx agent.Context, args getCapitalCityArgs) (map[string]any, error) {
fmt.Printf("\n-- Tool Call: getCapitalCity(country='%s') --\n", args.Country)
capitals := map[string]string{
"united states": "Washington, D.C.",
"canada": "Ottawa",
"france": "Paris",
"japan": "Tokyo",
}
capital, ok := capitals[strings.ToLower(args.Country)]
if !ok {
result := fmt.Sprintf("Sorry, I couldn't find the capital for %s.", args.Country)
fmt.Printf("-- Tool Result: '%s' --\n", result)
return nil, errors.New(result)
}
fmt.Printf("-- Tool Result: '%s' --\n", capital)
return map[string]any{"result": capital}, nil
}
// callAgent is a helper function to execute an agent with a given prompt and handle its output.
func callAgent(ctx context.Context, a agent.Agent, outputKey string, prompt string) {
fmt.Printf("\n>>> Calling Agent: '%s' | Query: %s\n", a.Name(), prompt)
// Create an in-memory session service to manage agent state.
sessionService := session.InMemoryService()
// Create a new session for the agent interaction.
sessionCreateResponse, err := sessionService.Create(ctx, &session.CreateRequest{
AppName: appName,
UserID: userID,
})
if err != nil {
log.Fatalf("Failed to create the session service: %v", err)
}
session := sessionCreateResponse.Session
// Configure the runner with the application name, agent, and session service.
config := runner.Config{
AppName: appName,
Agent: a,
SessionService: sessionService,
}
// Create a new runner instance.
r, err := runner.New(config)
if err != nil {
log.Fatalf("Failed to create the runner: %v", err)
}
// Prepare the user's message to send to the agent.
sessionID := session.ID()
userMsg := &genai.Content{
Parts: []*genai.Part{
genai.NewPartFromText(prompt),
},
Role: string(genai.RoleUser),
}
// Run the agent and process the streaming events.
for event, err := range r.Run(ctx, userID, sessionID, userMsg, agent.RunConfig{
StreamingMode: agent.StreamingModeSSE,
}) {
if err != nil {
fmt.Printf("\nAGENT_ERROR: %v\n", err)
} else if event.Partial {
// Print partial responses as they are received.
for _, p := range event.Content.Parts {
fmt.Print(p.Text)
}
}
}
// After the run, check if there's an expected output key in the session state.
if outputKey != "" {
storedOutput, error := session.State().Get(outputKey)
if error == nil {
// Pretty-print the stored output if it's a JSON string.
fmt.Printf("\n--- Session State ['%s']: ", outputKey)
storedString, isString := storedOutput.(string)
if isString {
var prettyJSON map[string]interface{}
if err := json.Unmarshal([]byte(storedString), &prettyJSON); err == nil {
indentedJSON, err := json.MarshalIndent(prettyJSON, "", " ")
if err == nil {
fmt.Println(string(indentedJSON))
} else {
fmt.Println(storedString)
}
} else {
fmt.Println(storedString)
}
} else {
fmt.Println(storedOutput)
}
fmt.Println(strings.Repeat("-", 30))
}
}
}
func main() {
ctx := context.Background()
model, err := gemini.NewModel(ctx, modelName, &genai.ClientConfig{})
if err != nil {
log.Fatalf("Failed to create model: %v", err)
}
capitalTool, err := functiontool.New(
functiontool.Config{
Name: "get_capital_city",
Description: "Retrieves the capital city for a given country.",
},
getCapitalCity,
)
if err != nil {
log.Fatalf("Failed to create function tool: %v", err)
}
countryInputSchema := &genai.Schema{
Type: genai.TypeObject,
Description: "Input for specifying a country.",
Properties: map[string]*genai.Schema{
"country": {
Type: genai.TypeString,
Description: "The country to get information about.",
},
},
Required: []string{"country"},
}
capitalAgentWithTool, err := llmagent.New(llmagent.Config{
Name: "capital_agent_tool",
Model: model,
Description: "Retrieves the capital city using a specific tool.",
Instruction: `You are a helpful agent that provides the capital city of a country using a tool.
The user will provide the country name in a JSON format like {"country": "country_name"}.
1. Extract the country name.
2. Use the 'get_capital_city' tool to find the capital.
3. Respond clearly to the user, stating the capital city found by the tool.`,
Tools: []tool.Tool{capitalTool},
InputSchema: countryInputSchema,
OutputKey: "capital_tool_result",
})
if err != nil {
log.Fatalf("Failed to create capital agent with tool: %v", err)
}
capitalInfoOutputSchema := &genai.Schema{
Type: genai.TypeObject,
Description: "Schema for capital city information.",
Properties: map[string]*genai.Schema{
"capital": {
Type: genai.TypeString,
Description: "The capital city of the country.",
},
"population_estimate": {
Type: genai.TypeString,
Description: "An estimated population of the capital city.",
},
},
Required: []string{"capital", "population_estimate"},
}
schemaJSON, _ := json.Marshal(capitalInfoOutputSchema)
structuredInfoAgentSchema, err := llmagent.New(llmagent.Config{
Name: "structured_info_agent_schema",
Model: model,
Description: "Provides capital and estimated population in a specific JSON format.",
Instruction: fmt.Sprintf(`You are an agent that provides country information.
The user will provide the country name in a JSON format like {"country": "country_name"}.
Respond ONLY with a JSON object matching this exact schema:
%s
Use your knowledge to determine the capital and estimate the population. Do not use any tools.`, string(schemaJSON)),
InputSchema: countryInputSchema,
OutputSchema: capitalInfoOutputSchema,
OutputKey: "structured_info_result",
})
if err != nil {
log.Fatalf("Failed to create structured info agent: %v", err)
}
fmt.Println("--- Testing Agent with Tool ---")
callAgent(ctx, capitalAgentWithTool, "capital_tool_result", `{"country": "France"}`)
callAgent(ctx, capitalAgentWithTool, "capital_tool_result", `{"country": "Canada"}`)
fmt.Println("\n\n--- Testing Agent with Output Schema (No Tool Use) ---")
callAgent(ctx, structuredInfoAgentSchema, "structured_info_result", `{"country": "France"}`)
callAgent(ctx, structuredInfoAgentSchema, "structured_info_result", `{"country": "Japan"}`)
}
```
```java
// --- Full example code demonstrating LlmAgent with Tools vs. Output Schema ---
import com.google.adk.agents.LlmAgent;
import com.google.adk.events.Event;
import com.google.adk.runner.Runner;
import com.google.adk.sessions.InMemorySessionService;
import com.google.adk.sessions.Session;
import com.google.adk.tools.Annotations;
import com.google.adk.tools.FunctionTool;
import com.google.genai.types.Content;
import com.google.genai.types.Part;
import com.google.genai.types.Schema;
import io.reactivex.rxjava3.core.Flowable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class LlmAgentExample {
// --- 1. Define Constants ---
private static final String MODEL_NAME = "gemini-2.0-flash";
private static final String APP_NAME = "capital_agent_tool";
private static final String USER_ID = "test_user_456";
private static final String SESSION_ID_TOOL_AGENT = "session_tool_agent_xyz";
private static final String SESSION_ID_SCHEMA_AGENT = "session_schema_agent_xyz";
// --- 2. Define Schemas ---
// Input schema used by both agents
private static final Schema COUNTRY_INPUT_SCHEMA =
Schema.builder()
.type("OBJECT")
.description("Input for specifying a country.")
.properties(
Map.of(
"country",
Schema.builder()
.type("STRING")
.description("The country to get information about.")
.build()))
.required(List.of("country"))
.build();
// Output schema ONLY for the second agent
private static final Schema CAPITAL_INFO_OUTPUT_SCHEMA =
Schema.builder()
.type("OBJECT")
.description("Schema for capital city information.")
.properties(
Map.of(
"capital",
Schema.builder()
.type("STRING")
.description("The capital city of the country.")
.build(),
"population_estimate",
Schema.builder()
.type("STRING")
.description("An estimated population of the capital city.")
.build()))
.required(List.of("capital", "population_estimate"))
.build();
// --- 3. Define the Tool (Only for the first agent) ---
// Retrieves the capital city of a given country.
public static Map getCapitalCity(
@Annotations.Schema(name = "country", description = "The country to get capital for")
String country) {
System.out.printf("%n-- Tool Call: getCapitalCity(country='%s') --%n", country);
Map countryCapitals = new HashMap<>();
countryCapitals.put("united states", "Washington, D.C.");
countryCapitals.put("canada", "Ottawa");
countryCapitals.put("france", "Paris");
countryCapitals.put("japan", "Tokyo");
String result =
countryCapitals.getOrDefault(
country.toLowerCase(), "Sorry, I couldn't find the capital for " + country + ".");
System.out.printf("-- Tool Result: '%s' --%n", result);
return Map.of("result", result); // Tools must return a Map
}
public static void main(String[] args){
LlmAgentExample agentExample = new LlmAgentExample();
FunctionTool capitalTool = FunctionTool.create(agentExample.getClass(), "getCapitalCity");
// --- 4. Configure Agents ---
// Agent 1: Uses a tool and output_key
LlmAgent capitalAgentWithTool =
LlmAgent.builder()
.model(MODEL_NAME)
.name("capital_agent_tool")
.description("Retrieves the capital city using a specific tool.")
.instruction(
"""
You are a helpful agent that provides the capital city of a country using a tool.
1. Extract the country name.
2. Use the `get_capital_city` tool to find the capital.
3. Respond clearly to the user, stating the capital city found by the tool.
""")
.tools(capitalTool)
.inputSchema(COUNTRY_INPUT_SCHEMA)
.outputKey("capital_tool_result") // Store final text response
.build();
// Agent 2: Uses an output schema
LlmAgent structuredInfoAgentSchema =
LlmAgent.builder()
.model(MODEL_NAME)
.name("structured_info_agent_schema")
.description("Provides capital and estimated population in a specific JSON format.")
.instruction(
String.format("""
You are an agent that provides country information.
Respond ONLY with a JSON object matching this exact schema: %s
Use your knowledge to determine the capital and estimate the population. Do not use any tools.
""", CAPITAL_INFO_OUTPUT_SCHEMA.toJson()))
// *** NO tools parameter here - using output_schema prevents tool use ***
.inputSchema(COUNTRY_INPUT_SCHEMA)
.outputSchema(CAPITAL_INFO_OUTPUT_SCHEMA) // Enforce JSON output structure
.outputKey("structured_info_result") // Store final JSON response
.build();
// --- 5. Set up Session Management and Runners ---
InMemorySessionService sessionService = new InMemorySessionService();
sessionService.createSession(APP_NAME, USER_ID, null, SESSION_ID_TOOL_AGENT).blockingGet();
sessionService.createSession(APP_NAME, USER_ID, null, SESSION_ID_SCHEMA_AGENT).blockingGet();
Runner capitalRunner = new Runner(capitalAgentWithTool, APP_NAME, null, sessionService);
Runner structuredRunner = new Runner(structuredInfoAgentSchema, APP_NAME, null, sessionService);
// --- 6. Run Interactions ---
System.out.println("--- Testing Agent with Tool ---");
agentExample.callAgentAndPrint(
capitalRunner, capitalAgentWithTool, SESSION_ID_TOOL_AGENT, "{\"country\": \"France\"}");
agentExample.callAgentAndPrint(
capitalRunner, capitalAgentWithTool, SESSION_ID_TOOL_AGENT, "{\"country\": \"Canada\"}");
System.out.println("\n\n--- Testing Agent with Output Schema (No Tool Use) ---");
agentExample.callAgentAndPrint(
structuredRunner,
structuredInfoAgentSchema,
SESSION_ID_SCHEMA_AGENT,
"{\"country\": \"France\"}");
agentExample.callAgentAndPrint(
structuredRunner,
structuredInfoAgentSchema,
SESSION_ID_SCHEMA_AGENT,
"{\"country\": \"Japan\"}");
}
// --- 7. Define Agent Interaction Logic ---
public void callAgentAndPrint(Runner runner, LlmAgent agent, String sessionId, String queryJson) {
System.out.printf(
"%n>>> Calling Agent: '%s' | Session: '%s' | Query: %s%n",
agent.name(), sessionId, queryJson);
Content userContent = Content.fromParts(Part.fromText(queryJson));
final String[] finalResponseContent = {"No final response received."};
Flowable eventStream = runner.runAsync(USER_ID, sessionId, userContent);
// Stream event response
eventStream.blockingForEach(event -> {
if (event.finalResponse() && event.content().isPresent()) {
event
.content()
.get()
.parts()
.flatMap(parts -> parts.isEmpty() ? Optional.empty() : Optional.of(parts.get(0)))
.flatMap(Part::text)
.ifPresent(text -> finalResponseContent[0] = text);
}
});
System.out.printf("<<< Agent '%s' Response: %s%n", agent.name(), finalResponseContent[0]);
// Retrieve the session again to get the updated state
Session updatedSession =
runner
.sessionService()
.getSession(APP_NAME, USER_ID, sessionId, Optional.empty())
.blockingGet();
if (updatedSession != null && agent.outputKey().isPresent()) {
// Print to verify if the stored output looks like JSON (likely from output_schema)
System.out.printf("--- Session State ['%s']: ", agent.outputKey().get());
}
}
}
```
```kotlin
val finalAgent =
LlmAgent(
name = "capital_agent",
model = Gemini(name = "gemini-flash-latest"),
description = "Answers user questions about the capital city of a given country.",
instruction =
Instruction(
"You are an agent that provides the capital city of a country...",
),
// tools = capitalService.generatedTools() // Assuming tools are added
)
val sessionService = InMemorySessionService()
val runner = InMemoryRunner(finalAgent, "capital_app", sessionService)
val userMessage = Content(parts = listOf(Part(text = "What is the capital of France?")))
// Use runAsync to get a Flow of events
runner.runAsync(
userId = "user123",
sessionId = "session456",
newMessage = userMessage,
).collect {
event ->
if (event.isFinalResponse) {
val finalResponse = event.content?.parts?.firstOrNull()?.text
println(finalResponse)
}
}
```
## Additional features
ADK provides additional features for agents not covered in this guide, including the following:
- **Callbacks:** Add more controls by intercepting agent execution points, including before and after model calls, and before and after tool calls with [Callbacks](/callbacks/types-of-callbacks/).
- **Graph-based workflows:** Compose LLM agents as steps in deterministic, graph-based pipelines using [Graph-based agent workflows](/graphs/). In Go v2.0.0, use `workflow.NewAgentNode` to wrap any LLM agent as a workflow node.
- **Multi-agent systems:** Advanced strategies for agent interaction, including agent transfer (`disallow_transfer_to_parent`, `disallow_transfer_to_peers`), and consistent identity and rules for every agent in your app (`GlobalInstructionPlugin`, or `GlobalInstruction` in Go). See [Multi-agent workflows](/workflows/) and [collaborative agent teams](/workflows/collaboration/).
# Managed agents
Supported in ADKPython v2.4.0Preview
Managed agents let you use Google's first-party, out-of-the-box agents, backed by the Managed Agents API, from within your ADK flows. Managed agents are available through the [Gemini API](https://ai.google.dev/gemini-api/docs/agents) and [Agent Platform](https://docs.cloud.google.com/gemini-enterprise-agent-platform/build/managed-agents). The `ManagedAgent` class connects to a managed agent (such as the Antigravity agent) that runs in a specialized, server-side execution environment, so you get powerful built-in capabilities without managing sandboxes or writing client-side function declarations.
`ManagedAgent` implements the same `BaseAgent` contract as other ADK agents, so you can use it standalone or drop it directly into an ADK flow. It is a good fit when you want a robust, server-hosted agent with specialized built-in tools rather than building and operating that environment yourself.
## What are managed agents?
A *managed agent* is an agent whose reasoning, tools, and execution environment are hosted and operated by Google through the Managed Agents API, rather than run by your own ADK process. Instead of issuing standard `generate_content` calls, `ManagedAgent` creates server-side *interactions* and streams the results back into your ADK flow. Managed agents provide several built-in advantages:
- **First-party, out-of-the-box agents:** Connect to ready-made agents (for example, the Antigravity agent) by referencing their `agent_id`.
- **Built-in, server-side execution:** Capabilities such as web search and code execution run in a managed sandbox on the server, with no local sandbox to provision or secure.
- **No client-side function declarations:** Server-side tools are configured on the managed agent, so you don't declare or execute them locally.
- **Composable as an inline tool:** Set `mode='single_turn'` and add the managed agent to a parent `LlmAgent`'s `sub_agents`. ADK then exposes it as an inline single-turn tool automatically, which is the recommended alternative to wrapping it in `AgentTool`.
## When to use managed agents vs. building your own
Managed agents and ADK agents solve different problems. Choosing between them is mostly a trade-off between out-of-the-box power and fine-grained control.
- **Managed agents** give you a powerful agent out of the box, but with limited flexibility. The toolset is predefined and server-side, the agent runs only in the managed environment, and client-executed tools are not supported, though server-side remote MCP servers are.
- **ADK agents** (such as [`LlmAgent`](/agents/llm-agents/)) give you fine-grained control over the model, instructions, tools (including custom function tools and MCP tools), and where execution happens.
## Prerequisites
`ManagedAgent` supports two backends. Complete the prerequisites for the backend you plan to use: obtain credentials and an `agent_id`.
### Gemini API backend
- **Authentication:** Obtain a Gemini API key and set it as the `GEMINI_API_KEY` environment variable.
- **Agent ID:** You need an `agent_id` to connect to. You can either:
- Create a new agent by following the [Gemini API Agents documentation](https://ai.google.dev/gemini-api/docs/agents).
- Use an out-of-the-box agent ID, such as `antigravity-preview-05-2026`, which is used in the examples below.
### Agent Platform backend
- **Authentication:** Agent Platform requires Google Cloud credentials. Follow the [Agent Platform setup instructions](https://docs.cloud.google.com/gemini-enterprise-agent-platform/build/managed-agents/create-manage#before-you-begin) to authenticate your local environment (for example, with `gcloud auth application-default login`).
- **Location:** The Managed Agents API is served only from the `global` location. `ManagedAgent` enforces a connection to `global` on the Agent Platform backend.
- **Agent ID:** As with the Gemini API, you need an `agent_id`. Create one using the [Create and manage agents guide](https://docs.cloud.google.com/gemini-enterprise-agent-platform/build/managed-agents/create-manage), or use an out-of-the-box agent ID available to your project.
## Get started
The following example creates two managed agents: one that answers questions using web search, and one that solves computational questions by running code server-side. Both run their tools in the managed environment (`environment={'type': 'remote'}`).
```python
import os
from google.adk.agents import ManagedAgent
from google.adk.tools import google_search
from google.genai import types
# Ensure you have the MANAGED_AGENT_ID and the proper environment config
_AGENT_ID = os.environ.get('MANAGED_AGENT_ID', 'antigravity-preview-05-2026')
managed_search_agent = ManagedAgent(
name='managed_search_agent',
description='Answers questions that need fresh, grounded information from the web.',
agent_id=_AGENT_ID,
environment={'type': 'remote'},
tools=[google_search],
)
# A managed code execution agent using raw types.Tool
managed_code_execution_agent = ManagedAgent(
name='managed_code_execution_agent',
description='Solves computational questions by running code server-side.',
agent_id=_AGENT_ID,
environment={'type': 'remote'},
tools=[types.Tool(code_execution=types.ToolCodeExecution())],
)
```
## How it works
When you invoke a `ManagedAgent`, ADK sends your request to the managed agent via the [Interactions API](https://ai.google.dev/gemini-api/docs/interactions-overview) and streams the results, both partial and final, back into your ADK flow in real time. The reasoning, tools, and execution all run in Google's managed environment rather than in your ADK process.
How `ManagedAgent` maps to the Managed Agents API
An ADK `ManagedAgent` does not create or register a new managed agent resource. It connects to an agent that already exists on the backend (the one named by `agent_id`) and applies its configuration (such as `tools` and `environment`) as per-interaction overrides at runtime. In Managed Agents API terms, ADK works entirely on the *data plane* (the Interactions API) and leaves the *control plane* (the Agents API, which creates and manages agent resources) untouched. For how these two planes differ, see the [Managed Agents API system architecture](https://docs.cloud.google.com/gemini-enterprise-agent-platform/build/managed-agents).
### Local session vs. remote state
`ManagedAgent` keeps almost no state locally. The ADK session persists only two values on the events it emits: the `interaction_id` and the sandbox `environment_id`. On each new turn the agent recovers both by scanning prior session events, then sends them back to the backend so the conversation and its sandbox continue.
Everything else lives server-side. The Managed Agents API owns the sandbox environment and the full interaction history, and that remote interaction, not the local session, is the source of truth for continuing a conversation. Response text appears in both the local ADK events and the remote interaction history, but ADK stores only the IDs it needs to recover and reuse the remote state; it never re-sends prior turns.
## Limitations
- **Location pinned (Agent Platform only):** For the Agent Platform backend, the Managed Agents API is currently served only from the `global` location. Regional endpoints raise an error.
- **Server-side tools only:** Client-executed tools (Python functions, callables) and raw `types.Tool` MCP configs (`types.Tool.mcp_servers`) are not supported and raise a `NotImplementedError`. Server-side remote MCP servers are supported: declare them as `google.adk.tools.RemoteMcpServer` specs in `tools`.
- **Streaming only:** The agent uses streaming interactions (`stream=True`). Background-polling execution and strictly non-streaming connections are not yet fully supported.
- **Backend differences:** The Gemini API and Agent Platform backends currently exhibit slightly different behavioral patterns. Test against the specific backend you intend to use.
## Next steps
- **Samples:** [Managed Agent Basic](https://github.com/google/adk-python/tree/main/contributing/samples/managed_agent/basic), [Managed Agent Code Execution](https://github.com/google/adk-python/tree/main/contributing/samples/managed_agent/code_execution), [Managed Agent Remote MCP](https://github.com/google/adk-python/tree/main/contributing/samples/managed_agent/remote_mcp), [Managed Agent Single Turn](https://github.com/google/adk-python/tree/main/contributing/samples/managed_agent/single_turn), and [Managed Agent System Instruction](https://github.com/google/adk-python/tree/main/contributing/samples/managed_agent/system_instruction).
- **Backend documentation:** [Gemini API Agents](https://ai.google.dev/gemini-api/docs/agents) and [Agent Platform Managed Agents](https://docs.cloud.google.com/gemini-enterprise-agent-platform/build/managed-agents).
- **Related ADK topics:** [Models for agents](/agents/models/), [Multi-agent workflows](/workflows/), and [Custom tools](/tools-custom/).
# Route between agents
Supported in ADKTypeScript v1.0.0Experimental
Experimental
Agent routing is experimental and may change in future releases. We welcome your [feedback](https://github.com/google/adk-js/issues/new?template=feature_request.md)!
When building agents for different tasks, you can define a routing function that selects which one handles each invocation at runtime. `RoutedAgent` provides this capability, enabling agent fallback on error, A/B testing, planning modes, and auto-routing by input complexity. If the selected agent fails before producing any output, the routing function is called again with error context so it can select a fallback.
`RoutedAgent` is different from [workflow agents](https://adk.tools-online.site/agents/workflow-agents/index.md) like `SequentialAgent` or `ParallelAgent`, which orchestrate multiple agents in a fixed pattern, and from [LLM-driven delegation](/agents/custom-agents/#delegation), where the LLM decides which agent to hand off to. With `RoutedAgent`, you write an explicit routing function that selects **one** agent per invocation. For model-level routing, see [Model routing](https://adk.tools-online.site/agents/models/routing/index.md).
## How routing works
Both `RoutedAgent` and [`RoutedLlm`](https://adk.tools-online.site/agents/models/routing/index.md) are powered by a shared routing utility that handles selection and failover.
The router function receives the map of available agents and the current context, and returns the key of the agent to run. It can be synchronous or async:
```typescript
type AgentRouter = (
agents: Readonly>,
context: InvocationContext,
errorContext?: { failedKeys: ReadonlySet; lastError: unknown },
) => Promise | string | undefined;
```
**The `agents` parameter** accepts either a `Record` with explicit keys, or an array of agents. If an array is provided, each agent's `name` property is used as its key.
**Failover behavior:**
- The router is first called without `errorContext` to make the initial selection.
- If the selected agent throws an error **before yielding any events**, the router is called again with `errorContext` containing `failedKeys` and `lastError`.
- If the selected agent throws an error **after yielding events**, the error propagates directly without retry, because partial results have already been emitted.
- A key that has already been tried cannot be re-selected. If the router returns a previously failed key, the error propagates.
- If the router returns `undefined`, routing stops and the last error is thrown.
## Basic usage
Create multiple agents, define a router function that returns a key, and wrap them in a `RoutedAgent`. The following example routes between two agents based on an external configuration value that can change between invocations:
```typescript
import { LlmAgent, RoutedAgent, InMemoryRunner } from '@google/adk';
const agentA = new LlmAgent({
name: 'agent_a',
model: 'gemini-flash-latest',
instruction: 'You are Agent A. Always identify yourself as Agent A.',
});
const agentB = new LlmAgent({
name: 'agent_b',
model: 'gemini-flash-latest',
instruction: 'You are Agent B. Always identify yourself as Agent B.',
});
// External configuration that can change at runtime
const config = { selectedAgent: 'agent_a' };
const routedAgent = new RoutedAgent({
name: 'my_routed_agent',
agents: { agent_a: agentA, agent_b: agentB },
router: () => config.selectedAgent,
});
const runner = new InMemoryRunner({
agent: routedAgent,
appName: 'my_app',
});
const session = await runner.sessionService.createSession({
appName: 'my_app',
userId: 'user_1',
});
const run = runner.runAsync({
userId: 'user_1',
sessionId: session.id,
newMessage: { role: 'user', parts: [{ text: 'Who are you?' }] },
});
for await (const event of run) {
if (event.content?.parts?.[0]?.text) {
console.log(event.content.parts[0].text);
}
}
```
Change `config.selectedAgent` to `'agent_b'` before the next invocation to route to a different agent.
## Fallback on error
When an agent fails, the router is called again with `errorContext` so it can select a fallback. Failover only applies if the agent fails before yielding any events (see [How routing works](#how-routing-works)). The following example checks `errorContext.failedKeys` to avoid re-selecting the failed agent:
```typescript
import {
BaseAgent,
InvocationContext,
LlmAgent,
RoutedAgent,
} from '@google/adk';
const primaryAgent = new LlmAgent({
name: 'primary',
model: 'gemini-flash-latest',
instruction: 'You are the primary agent.',
});
const fallbackAgent = new LlmAgent({
name: 'fallback',
model: 'gemini-pro-latest',
instruction: 'You are the fallback agent.',
});
const router = (
agents: Readonly>,
context: InvocationContext,
// errorContext is provided when a previously selected agent fails
errorContext?: { failedKeys: ReadonlySet; lastError: unknown },
) => {
if (!errorContext) {
return 'primary'; // Try primary first
}
if (errorContext.failedKeys.has('primary')) {
return 'fallback'; // Fall back if primary failed
}
return undefined; // No more options, propagate the error
};
const routedAgent = new RoutedAgent({
name: 'my_routed_agent',
agents: { primary: primaryAgent, fallback: fallbackAgent },
router,
});
```
## Planning mode
A router can read any external state to select between agents with different instructions, models, and tools. This lets you implement a planning mode where the agent switches behavior dynamically. For example, a basic agent might have read and write tools, while a planning agent is restricted to read-only access and uses a more powerful model for analysis.
The following example shows a different `RoutedAgent` configuration. See [basic usage](#basic-usage) for the full runner setup.
```typescript
import {
FunctionTool,
LlmAgent,
RoutedAgent,
} from '@google/adk';
import { z } from 'zod';
const readFileTool = new FunctionTool({
name: 'read_file',
description: 'Reads content from a file.',
parameters: z.object({ filePath: z.string() }),
execute: (args) => ({ content: `Contents of ${args.filePath}` }),
});
const writeFileTool = new FunctionTool({
name: 'write_file',
description: 'Writes content to a file.',
parameters: z.object({ filePath: z.string(), content: z.string() }),
execute: (args) => ({ result: `Wrote to ${args.filePath}` }),
});
const basicAgent = new LlmAgent({
name: 'basic',
model: 'gemini-flash-latest',
instruction: 'You are a basic assistant. Use tools to help the user.',
tools: [readFileTool, writeFileTool],
});
const planningAgent = new LlmAgent({
name: 'planning',
model: 'gemini-flash-latest',
instruction: 'You are a planning expert. Analyze carefully. You can only read files.',
tools: [readFileTool],
});
// Toggle this to switch between basic and planning agents
let planningMode = false;
const routedAgent = new RoutedAgent({
name: 'my_routed_agent',
agents: { basic: basicAgent, planning: planningAgent },
router: () => (planningMode ? 'planning' : 'basic'),
});
```
Set `planningMode = true` before an invocation to route to the planning agent with its restricted tool set and different instructions.
## Auto-routing by complexity
The router function can call a lightweight classifier model to categorize input and route to different agents accordingly. Because the router can be async, you can make LLM calls inside it before selecting an agent.
The following example shows a different `RoutedAgent` configuration. See [basic usage](#basic-usage) for the full runner setup.
```typescript
import {
BaseAgent,
Gemini,
InvocationContext,
LlmAgent,
RoutedAgent,
} from '@google/adk';
const simpleAgent = new LlmAgent({
name: 'simple',
model: 'gemini-flash-latest',
instruction: 'You are a simple assistant for basic questions.',
});
const complexAgent = new LlmAgent({
name: 'complex',
model: 'gemini-pro-latest',
instruction: 'You are an expert assistant for complex analysis.',
});
// Lightweight model to classify input complexity
const classifierModel = new Gemini({ model: 'gemini-flash-latest' });
const router = async (
agents: Readonly>,
context: InvocationContext,
) => {
// Extract the user's input text
const text = context.userContent?.parts?.[0]?.text || '';
if (!text) return 'simple';
const prompt =
`Classify this request as 'simple' or 'complex'. ` +
`Reply with ONLY that word.\nRequest: "${text}"`;
const generator = classifierModel.generateContentAsync({
contents: [{ role: 'user', parts: [{ text: prompt }] }],
toolsDict: {},
liveConnectConfig: {},
});
let classification = '';
for await (const resp of generator) {
if (resp.content?.parts?.[0]?.text) {
classification += resp.content.parts[0].text;
}
}
return classification.toLowerCase().includes('complex')
? 'complex'
: 'simple';
};
const routedAgent = new RoutedAgent({
name: 'my_routed_agent',
agents: { simple: simpleAgent, complex: complexAgent },
router,
});
```
# Service registration
Supported in ADKPython v1.34.0
Extend ADK runtime capabilities by registering custom services for sessions, artifacts, memory, and Agent2Agent (A2A) task stores. Registering custom services allows you to integrate your own backend systems and storage providers into your agents.
Register custom services using either of the following methods:
- **`services.yaml`**: A YAML configuration file for services that instantiate with simple constructors.
- **`services.py`**: A Python script for services that require custom initialization or complex logic.
## Register services with `services.yaml`
Use a `services.yaml` or `services.yml` file to register services that instantiate with a simple constructor (for example, `MyService(uri="...")`). Place this file in the agent directory (for `adk run`) or the agents root directory (for `adk web` and `adk api_server`).
Define your custom services in `services.yaml`:
```yaml
services:
- scheme: mysession
type: session
class: my_custom_package.modules.MyCustomSessionService
- scheme: myartifact
type: artifact
class: my_custom_package.storage.MyCustomArtifactService
- scheme: mymemory
type: memory
class: my_custom_package.modules.MyCustomMemoryService
- scheme: mytaskstore
type: task_store
class: my_custom_package.tasks.MyCustomTaskStore
```
The `services.yaml` file supports the following properties:
- `scheme`: The URI scheme that triggers the custom service (for example, `mysession://`).
- `type`: The type of service, which can be `session`, `artifact`, `memory`, or `task_store`.
- `class`: The fully qualified class name of the service implementation.
## Register services with `services.py`
Use a `services.py` file, placed in the same location as `services.yaml`, for complex initialization or when you require more control over the service creation process.
Register custom `session`, `artifact`, and `memory` services in `services.py`:
```python
from google.adk.cli.service_registry import get_service_registry
from my_custom_package.modules import MyCustomSessionService
from my_custom_package.storage import MyCustomArtifactService
from my_custom_package.memory import MyCustomMemoryService
def my_session_factory(uri: str, **kwargs):
# Add your custom logic here.
return MyCustomSessionService(uri=uri, **kwargs)
def my_artifact_factory(uri: str, **kwargs):
"""Factory function to instantiate a custom artifact service."""
return MyCustomArtifactService(uri=uri, **kwargs)
def my_memory_factory(uri: str, **kwargs):
"""Factory function to instantiate a custom memory service."""
return MyCustomMemoryService(uri=uri, **kwargs)
# Retrieve the registry.
registry = get_service_registry()
# Register session, artifact, and memory services.
registry.register_session_service("mysession", my_session_factory)
registry.register_artifact_service("myartifact", my_artifact_factory)
registry.register_memory_service("mymemory", my_memory_factory)
```
The example script performs the following tasks:
1. Import `get_service_registry` to retrieve the service registry instance.
1. Define factory functions (such as `my_session_factory`) that take a URI and keyword arguments, and return an instance of the custom service.
1. Use `register_session_service`, `register_artifact_service`, and `register_memory_service` to register the factory functions with the desired schemes (such as `mysession`).
## Load services
When you run your agent with ADK command-line tools (`adk run`, `adk web`, or `adk api_server`), ADK automatically discovers and loads custom services from `services.yaml` (or `services.yml`) and `services.py`.
### Discovery locations
ADK locates configuration files based on the command you run:
- **`adk run`**: Discovers service files in the agent directory.
- **`adk web` and `adk api_server`**: Discover service files in the agents root directory passed to the command.
### Loading order and precedence
When both `services.yaml` and `services.py` are present in the target directory:
1. **Loading order**: ADK loads `services.yaml` first, followed by `services.py`.
1. **Precedence**: If a service with the same scheme is defined in both files, the definition in `services.py` takes precedence.
# AI Models for ADK agents
Supported in ADKPythonTypeScriptGoJava
Agent Development Kit (ADK) is designed for flexibility, allowing you to integrate various Large Language Models (LLMs) into your agents. This section details how to leverage Gemini and integrate other popular models effectively, including those hosted externally or running locally.
ADK provides several mechanisms for model integration:
1. **Direct String / Registry:** For models tightly integrated with Google Cloud, such as Gemini models accessed via Google AI Studio or Agent Platform, or models hosted on Agent Platform endpoints. You access these models by providing the model name or endpoint resource string and ADK's internal registry resolves this string to the appropriate backend client.
- [Gemini models](/agents/models/google-gemini/)
- [Claude models](/agents/models/anthropic/)
- [Agent Platform hosted models](/agents/models/agent-platform/)
1. **Model connectors:** For broader compatibility, especially models outside the Google ecosystem or those requiring specific client configurations, such as models accessed via Apigee or LiteLLM. You instantiate a specific wrapper class, such as `ApigeeLlm` or `LiteLlm`, and pass this object as the `model` parameter to your `LlmAgent`.
- [Apigee models](/agents/models/apigee/)
- [LiteLLM models](/agents/models/litellm/)
- [Ollama model hosting](/agents/models/ollama/)
- [vLLM model hosting](/agents/models/vllm/)
- [LiteRT-LM model hosting](/agents/models/litert-lm/)
1. **[Model routing](/agents/models/routing/):** For dynamically selecting between multiple models at runtime using a router function, with automatic failover on error.
# Agent Platform hosted models for ADK agents
For enterprise-grade scalability, reliability, and integration with Google Cloud's MLOps ecosystem, you can use models deployed to Agent Platform Endpoints. This includes models from Model Garden or your own fine-tuned models.
**Integration Method:** Pass the full Agent Platform Endpoint resource string (`projects/PROJECT_ID/locations/LOCATION/endpoints/ENDPOINT_ID`) directly to the `model` parameter of `LlmAgent`.
## Agent Platform Setup
For more details on connecting ADK agents to Google Cloud hosted models and services, including Gemini Enterprise Agent Platform, see the [Connect to Google Cloud and Agent Platform](/get-started/google-cloud/) guide.
## Model Garden Deployments
Supported in ADKPython v0.2.0Java v0.1.0
You can deploy various open and proprietary models from the [Model Garden](https://console.cloud.google.com/vertex-ai/model-garden) to an endpoint.
**Example:**
```python
from google.adk.agents import LlmAgent
from google.genai import types # For config objects
# --- Example Agent using a Llama 3 model deployed from Model Garden ---
# Replace with your actual Agent Platform Endpoint resource name
llama3_endpoint = "projects/YOUR_PROJECT_ID/locations/us-central1/endpoints/YOUR_LLAMA3_ENDPOINT_ID"
agent_llama3_vertex = LlmAgent(
model=llama3_endpoint,
name="llama3_vertex_agent",
instruction="You are a helpful assistant based on Llama 3, hosted on Agent Platform.",
generate_content_config=types.GenerateContentConfig(max_output_tokens=2048),
# ... other agent parameters
)
```
```java
import com.google.adk.agents.LlmAgent;
import com.google.adk.models.Gemini;
import com.google.genai.types.GenerateContentConfig;
// ...
// Replace with your actual Agent Platform Endpoint resource name
String llama3Endpoint = "projects/YOUR_PROJECT_ID/locations/us-central1/endpoints/YOUR_LLAMA3_ENDPOINT_ID";
LlmAgent agentLlama3Vertex = LlmAgent.builder()
.model(Gemini.builder()
.modelName(llama3Endpoint)
.build())
.name("llama3_vertex_agent")
.instruction("You are a helpful assistant based on Llama 3, hosted on Agent Platform.")
.generateContentConfig(GenerateContentConfig.builder()
.maxOutputTokens(2048)
.build())
// ... other agent parameters
.build();
```
## Fine-tuned Model Endpoints
Supported in ADKPython v0.2.0Java v0.1.0
Deploying your fine-tuned models (whether based on Gemini or other architectures supported by Agent Platform) results in an endpoint that can be used directly.
**Example:**
```python
from google.adk.agents import LlmAgent
# --- Example Agent using a fine-tuned Gemini model endpoint ---
# Replace with your fine-tuned model's endpoint resource name
finetuned_gemini_endpoint = "projects/YOUR_PROJECT_ID/locations/us-central1/endpoints/YOUR_FINETUNED_ENDPOINT_ID"
agent_finetuned_gemini = LlmAgent(
model=finetuned_gemini_endpoint,
name="finetuned_gemini_agent",
instruction="You are a specialized assistant trained on specific data.",
# ... other agent parameters
)
```
```java
import com.google.adk.agents.LlmAgent;
import com.google.adk.models.Gemini;
// ...
// Replace with your fine-tuned model's endpoint resource name
String finetunedGeminiEndpoint = "projects/YOUR_PROJECT_ID/locations/us-central1/endpoints/YOUR_FINETUNED_ENDPOINT_ID";
LlmAgent agentFinetunedGemini = LlmAgent.builder()
.model(Gemini.builder()
.modelName(finetunedGeminiEndpoint)
.build())
.name("finetuned_gemini_agent")
.instruction("You are a specialized assistant trained on specific data.")
// ... other agent parameters
.build();
```
## Anthropic Claude on Agent Platform
Supported in ADKPython v0.2.0TypeScript v2.4.0Go v2.7.0Java v0.1.0Kotlin v1.4.0
Some providers, like Anthropic, make their models available directly through Agent Platform. To set up and use Claude models on Agent Platform, including extended thinking and prompt caching, see [Connect through Agent Platform](/agents/models/anthropic/#connect-through-agent-platform) in the Claude models guide.
## Open Models on Agent Platform
Supported in ADKPython v0.1.0Java v0.1.0
Agent Platform offers a curated selection of open-source models, such as Meta Llama, through Model-as-a-Service (MaaS). These models are accessible via managed APIs, allowing you to deploy and scale without managing the underlying infrastructure. For a full list of available options, see the [Agent Platform open models for MaaS](https://docs.cloud.google.com/vertex-ai/generative-ai/docs/maas/use-open-models#open-models) documentation.
You can use the [LiteLLM](https://docs.litellm.ai/) library to access open models like Meta's Llama on Agent Platform MaaS
**Integration Method:** Use the `LiteLlm` wrapper class and set it as the `model` parameter of `LlmAgent`. Make sure you go through the [LiteLLM model connector for ADK agents](/agents/models/litellm/#litellm-model-connector-for-adk-agents) documentation on how to use LiteLLM in ADK
**Setup:**
1. **Agent Platform Environment:** Ensure the consolidated Agent Platform setup (ADC, Env Vars, `GOOGLE_GENAI_USE_ENTERPRISE=TRUE`) is complete.
1. **Install LiteLLM:** ADK requires `litellm>=1.84`.
```shell
pip install "litellm>=1.84"
```
**Example:**
```python
from google.adk.agents import LlmAgent
from google.adk.models.lite_llm import LiteLlm
# --- Example Agent using Meta's Llama 4 Scout ---
agent_llama_vertexai = LlmAgent(
model=LiteLlm(model="vertex_ai/meta/llama-4-scout-17b-16e-instruct-maas"), # LiteLLM model string format
name="llama4_agent",
instruction="You are a helpful assistant powered by Llama 4 Scout.",
# ... other agent parameters
)
```
# Use Anthropic Claude models with ADK
Supported in ADKPython v0.1.0TypeScript v2.4.0Go v2.7.0Java v0.2.0Kotlin v1.4.0
You can use Anthropic's Claude models as the model for your Agent Development Kit (ADK) agents. The framework provides Claude model classes that translate ADK requests, tools, and responses into calls to Claude, so you can build agents with Claude the same way you build them with Gemini, allowing you to:
- Run an agent on Claude while keeping your ADK tools, sessions, and callbacks unchanged.
- Combine Claude and Gemini agents in one multi-agent system, and choose the model for each agent based on its task.
- Give Claude more reasoning time on complex tasks with extended thinking.
You can reach Claude through the **Anthropic API**, using an Anthropic API key, or **Google Cloud**, using your Google Cloud project and credentials. For help choosing, see [Choose a backend](#choose-a-backend).
## Get started
The following examples create an agent that calls Claude through the Anthropic API. Before you run them, get an API key from the [Anthropic Console](https://console.anthropic.com/) and set it as an environment variable. In production, store the key in a secret manager rather than in source code or shell history:
```shell
export ANTHROPIC_API_KEY="YOUR_ANTHROPIC_API_KEY"
```
Install ADK with the Anthropic SDK:
```shell
pip install "google-adk[extensions]"
```
Create an `AnthropicLlm` instance and pass it to your agent. It reads your API key from the `ANTHROPIC_API_KEY` environment variable. The `AnthropicLlm` class is available in ADK Python v1.20.0 and later.
```python
from google.adk.agents import LlmAgent
from google.adk.models.anthropic_llm import AnthropicLlm
root_agent = LlmAgent(
name="claude_agent",
model=AnthropicLlm(model="claude-sonnet-4-6"),
instruction="You are a helpful assistant.",
)
```
In TypeScript, Claude is available through Google Cloud Agent Platform only. Install the ADK integrations package and the Anthropic SDKs:
```shell
npm install @google/adk @google/adk-integrations @anthropic-ai/sdk @anthropic-ai/vertex-sdk
```
Complete the [Agent Platform setup](#connect-through-agent-platform), then create a `Claude` instance and pass it to your agent:
```typescript
import {LlmAgent} from '@google/adk';
import {Claude} from '@google/adk-integrations';
export const rootAgent = new LlmAgent({
name: 'claude_agent',
model: new Claude({model: 'claude-sonnet-4-6@default'}),
instruction: 'You are a helpful assistant.',
});
```
Experimental
The `anthropicmodel` package is experimental. Its API and behavior may have limitations and it is not recommended for production use.
Create a model with `anthropicmodel.NewModel` and pass it to your agent. When `APIKey` is empty, the model reads your key from the `ANTHROPIC_API_KEY` environment variable.
```go
import (
"context"
"log"
"google.golang.org/adk/v2/agent/llmagent"
"google.golang.org/adk/v2/model/anthropicmodel"
)
llm, err := anthropicmodel.NewModel(context.Background(), "claude-sonnet-4-6", anthropicmodel.ClientConfig{
Backend: anthropicmodel.BackendAnthropic,
})
if err != nil {
log.Fatal(err)
}
agent, err := llmagent.New(llmagent.Config{
Name: "claude_agent",
Model: llm,
Instruction: "You are a helpful assistant.",
})
if err != nil {
log.Fatal(err)
}
```
For a complete, runnable sample, see [examples/anthropic/](https://github.com/google/adk-go/tree/main/examples/anthropic) in the ADK Go repository.
The ADK Java core library includes the [Anthropic Java SDK](https://github.com/anthropics/anthropic-sdk-java) as a dependency. Create an `AnthropicClient`, wrap it in a `Claude` instance, and pass that to your agent:
```java
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import com.google.adk.agents.LlmAgent;
import com.google.adk.models.Claude;
public class ClaudeAgent {
public static LlmAgent createAgent() {
AnthropicClient anthropicClient = AnthropicOkHttpClient.builder()
.apiKey(System.getenv("ANTHROPIC_API_KEY"))
.build();
return LlmAgent.builder()
.name("claude_agent")
.model(new Claude("claude-sonnet-4-6", anthropicClient))
.instruction("You are a helpful assistant.")
.build();
}
}
```
Add the ADK Anthropic integration to your Gradle build. This module is JVM-only.
```kotlin
implementation("com.google.adk:google-adk-kotlin-integrations-anthropic:1.6.0")
```
Create a `Claude` instance and pass it to your agent. When you omit `apiKey`, the model reads your key from the `ANTHROPIC_API_KEY` environment variable.
```kotlin
import com.google.adk.kt.agents.Instruction
import com.google.adk.kt.agents.LlmAgent
import com.google.adk.kt.models.anthropic.Claude
val rootAgent =
LlmAgent(
name = "claude_agent",
model = Claude(name = "claude-sonnet-4-6"),
instruction = Instruction("You are a helpful assistant."),
)
```
## Choose a backend
Choose a backend based on where you manage billing, access, and quotas:
- **Anthropic API:** Use this backend if you already have an Anthropic account and manage Claude usage there. You authenticate with an Anthropic API key.
- **Google Cloud:** Use this backend if your application runs on Google Cloud, or you want Claude usage governed by your Google Cloud project's IAM permissions, quotas, and billing. You authenticate with Application Default Credentials (ADC), and you don't manage a separate API key.
The following table shows how each language selects a backend:
| Language | Anthropic API | Agent Platform |
| ---------- | --------------------------------------------------- | -------------------------------------------------------------------- |
| Python | `AnthropicLlm` instance | `Claude` instance, or a `claude-*` model string |
| TypeScript | Not supported | `Claude` instance, or a `claude-*` string after registering `Claude` |
| Go | `ClientConfig.Backend` set to `BackendAnthropic` | `ClientConfig.Backend` set to `BackendVertex` |
| Java | `Claude` with an `AnthropicClient` using an API key | `Claude` with an `AnthropicClient` using a `VertexBackend` |
| Kotlin | `Claude(name, apiKey)` | `Claude(name, vertexCredentials)` |
Python model strings use Google Cloud Agent Platform
In Python, a bare `claude-*` model string, such as `model="claude-sonnet-4-6"`, routes to the Agent Platform backend and requires Google Cloud project settings. To use the Anthropic API from Python, pass an `AnthropicLlm` instance instead of a string.
## Connect through Google Cloud
To use Claude through Google Cloud, set up your Google Cloud environment:
1. Enable the Claude models you want to use in [Model Garden](https://console.cloud.google.com/vertex-ai/model-garden) for your Google Cloud project.
1. Set up Application Default Credentials:
```shell
gcloud auth application-default login
```
1. Set your project and a region where the model is available, such as `global` or `us-east5`:
```shell
export GOOGLE_CLOUD_PROJECT="your-project-id"
export GOOGLE_CLOUD_LOCATION="global"
```
For more details on connecting ADK to Google Cloud, see [Connect to Google Cloud and Agent Platform](/get-started/google-cloud/).
The following examples create an agent that calls Claude through Google Cloud:
Pass a Claude model string to your agent. ADK routes `claude-*` strings to the `Claude` class, which reads `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION`.
```python
from google.adk.agents import LlmAgent
root_agent = LlmAgent(
name="claude_agent",
model="claude-sonnet-4-6",
instruction="You are a helpful assistant.",
)
```
ADK Python v2.9.0 and later routes every `claude-*` model string to `Claude`. In earlier versions, if ADK doesn't recognize a model string, pass an instance instead: `model=Claude(model="claude-sonnet-4-6")`, with `Claude` imported from `google.adk.models`.
Register `Claude` once at startup so any agent can reference a `claude-*` model string by name:
```typescript
import {LLMRegistry, LlmAgent} from '@google/adk';
import {Claude} from '@google/adk-integrations';
LLMRegistry.register(Claude);
export const rootAgent = new LlmAgent({
name: 'claude_agent',
model: 'claude-sonnet-4-6@default',
instruction: 'You are a helpful assistant.',
});
```
Set `Backend` to `BackendVertex`. When `Project` and `Location` are empty, the model reads `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION`, and uses ADC for credentials.
```go
llm, err := anthropicmodel.NewModel(ctx, "claude-sonnet-4-6", anthropicmodel.ClientConfig{
Backend: anthropicmodel.BackendVertex,
})
if err != nil {
log.Fatal(err)
}
```
Configure the `AnthropicClient` with a `VertexBackend`:
```java
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import com.anthropic.vertex.backends.VertexBackend;
import com.google.adk.agents.LlmAgent;
import com.google.adk.models.Claude;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;
public class ClaudeAgentPlatformAgent {
public static LlmAgent createAgent() throws IOException {
AnthropicClient anthropicClient = AnthropicOkHttpClient.builder()
.backend(
VertexBackend.builder()
.project("your-project-id")
.region("global")
.googleCredentials(GoogleCredentials.getApplicationDefault())
.build())
.build();
return LlmAgent.builder()
.name("claude_agent")
.model(new Claude("claude-sonnet-4-6", anthropicClient))
.instruction("You are a helpful assistant.")
.build();
}
}
```
Pass `VertexCredentials` to the `Claude` constructor. Unset values fall back to `GOOGLE_CLOUD_PROJECT`, `GOOGLE_CLOUD_LOCATION`, and ADC.
```kotlin
import com.google.adk.kt.agents.Instruction
import com.google.adk.kt.agents.LlmAgent
import com.google.adk.kt.models.VertexCredentials
import com.google.adk.kt.models.anthropic.Claude
val rootAgent =
LlmAgent(
name = "claude_agent",
model = Claude(name = "claude-sonnet-4-6", vertexCredentials = VertexCredentials()),
instruction = Instruction("You are a helpful assistant."),
)
```
## Configure extended thinking
Supported in ADKPython v1.34.0Go v2.7.0Kotlin v1.4.0
Extended thinking lets Claude reason through a problem before it responds, which can help on multi-step tasks such as planning, analysis, and choosing between tools. You configure it with the `thinking_budget` field of the standard thinking configuration:
| `thinking_budget` | Behavior |
| ---------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
| Negative, such as `-1` | **Adaptive thinking.** Claude chooses how much to reason for each request. |
| `0` | **No thinking.** Claude responds without extended thinking. |
| Positive | **Manual thinking.** Claude uses up to this many tokens to reason. For minimum and maximum budgets, see Anthropic's extended thinking documentation. |
Use adaptive thinking unless you need a fixed token budget. Some newer Claude models accept only adaptive thinking; see Anthropic's extended thinking documentation for the model you use. When you set a thinking configuration, you must set a `thinking_budget` value.
To control how much reasoning effort Claude spends, set the **effort** level: `low`, `medium`, `high`, `xhigh`, or `max`. Claude doesn't support the standard `thinking_level` setting; use effort instead. While thinking or effort is active, ADK doesn't send `temperature`, `top_p`, or `top_k` to Claude.
Set `effort` on `AnthropicGenerateContentConfig`, available in ADK Python v2.4.0 and later:
```python
from google.adk.agents import LlmAgent
from google.adk.models import AnthropicGenerateContentConfig
from google.genai import types
root_agent = LlmAgent(
name="claude_reasoning_agent",
model="claude-sonnet-4-6",
instruction="You are a helpful assistant.",
generate_content_config=AnthropicGenerateContentConfig(
thinking_config=types.ThinkingConfig(thinking_budget=-1),
effort="high",
),
)
```
Setting `thinking_level` on `AnthropicGenerateContentConfig` raises a validation error. On a plain `types.GenerateContentConfig`, ADK ignores it and logs a warning.
Set the thinking budget on the agent's `GenerateContentConfig`, and the effort level on the model's `ClientConfig`:
```go
llm, err := anthropicmodel.NewModel(ctx, "claude-sonnet-4-6", anthropicmodel.ClientConfig{
Effort: anthropicmodel.EffortHigh,
})
if err != nil {
log.Fatal(err)
}
agent, err := llmagent.New(llmagent.Config{
Name: "claude_reasoning_agent",
Model: llm,
Instruction: "You are a helpful assistant.",
GenerateContentConfig: &genai.GenerateContentConfig{
ThinkingConfig: &genai.ThinkingConfig{ThinkingBudget: genai.Ptr[int32](-1)},
},
})
```
Set the thinking budget on the agent's `GenerateContentConfig`, and the effort level on the `Claude` constructor:
```kotlin
import com.anthropic.models.messages.OutputConfig
import com.google.adk.kt.agents.Instruction
import com.google.adk.kt.agents.LlmAgent
import com.google.adk.kt.models.anthropic.Claude
import com.google.adk.kt.types.GenerateContentConfig
import com.google.adk.kt.types.ThinkingConfig
val rootAgent =
LlmAgent(
name = "claude_reasoning_agent",
model = Claude(name = "claude-sonnet-4-6", effort = OutputConfig.Effort.HIGH),
instruction = Instruction("You are a helpful assistant."),
generateContentConfig =
GenerateContentConfig(thinkingConfig = ThinkingConfig(thinkingBudget = -1)),
)
```
ADK returns Claude's reasoning as thought parts in the response, and preserves thinking blocks and their signatures across tool calls within a turn, as the Anthropic API requires.
## Advanced applications
ADK supports some advanced uses of Anthropic models, including prompt caching and accessing models through the LiteLLM library.
### Cache prompts
Supported in ADKPython v2.8.0Go v2.7.0Kotlin v1.4.0
Agents with long instructions or many tools send the same prompt prefix on every request. Claude prompt caching stores that prefix so later requests reuse it, which can reduce cost and latency. ADK places cache breakpoints on the tool declarations, the system instruction, and the latest conversation content.
Anthropic supports a 5-minute cache lifetime, which is the default, and a 1-hour cache lifetime. Use the 1-hour lifetime when requests to the same agent are usually more than 5 minutes apart. For more information, see Anthropic's [prompt caching](https://docs.claude.com/en/docs/build-with-claude/prompt-caching) documentation.
Set a `ContextCacheConfig` on your `App`. A `ttl_seconds` value of 3600 or more selects the 1-hour cache. For more on this configuration, see [Context caching](/context/caching/).
```python
from google.adk.agents.context_cache_config import ContextCacheConfig
from google.adk.apps.app import App
app = App(
name="claude_caching_app",
root_agent=root_agent,
context_cache_config=ContextCacheConfig(
min_tokens=2048, # Skip caching for short prompts.
ttl_seconds=600,
),
)
```
Set `PromptCache` on the model's `ClientConfig`. Set `TTL` to `PromptCacheTTL1h` to select the 1-hour cache.
```go
llm, err := anthropicmodel.NewModel(ctx, "claude-sonnet-4-6", anthropicmodel.ClientConfig{
PromptCache: &anthropicmodel.PromptCacheConfig{TTL: anthropicmodel.PromptCacheTTL5m},
})
```
Set a `ContextCacheConfig` on your `App`. A `ttl` of one hour or more selects the 1-hour cache.
```kotlin
@OptIn(ExperimentalContextCachingFeature::class)
import com.google.adk.kt.agents.ContextCacheConfig
import com.google.adk.kt.apps.App
import kotlin.time.Duration.Companion.minutes
val app =
App(
appName = "claude_caching_app",
rootAgent = rootAgent,
contextCacheConfig = ContextCacheConfig(minTokens = 2048, ttl = 10.minutes),
)
```
### Use Claude through LiteLLM
In Python, you can also reach Claude through the `LiteLlm` connector with an `anthropic/` model string. Use this path if your application already routes models through LiteLLM. For setup, see [LiteLLM](/agents/models/litellm/#anthropic-thinking-blocks).
## Configuration options
The Claude model classes accept the following options:
These options apply to both `AnthropicLlm` and `Claude`.
| Option | Type | Default | Description |
| ------------ | ------------------------------------------ | ------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
| `model` | `str` | `"claude-sonnet-4-20250514"` (`AnthropicLlm`), `"claude-3-5-sonnet-v2@20241022"` (`Claude`) | The Claude model ID. |
| `max_tokens` | `int` | `8192` | Maximum output tokens when the request sets no `max_output_tokens`. |
| `client` | `AsyncAnthropic` or `AsyncAnthropicVertex` | `None` | A preconfigured Anthropic client. The `Claude` class requires an `AsyncAnthropicVertex` client. |
| Option | Type | Default | Description |
| ----------- | -------- | ----------------------------- | ----------------------------------------------------------------- |
| `model` | `string` | `'claude-sonnet-4-6@default'` | The Claude model ID on Agent Platform. |
| `maxTokens` | `number` | `8192` | Maximum output tokens when the request sets no `maxOutputTokens`. |
Selected `ClientConfig` fields:
| Option | Type | Default | Description |
| ------------- | -------------------- | ----------------------- | ----------------------------------------------------------------- |
| `Backend` | `Backend` | `BackendAnthropic` | The serving backend: `BackendAnthropic` or `BackendVertex`. |
| `APIKey` | `string` | `ANTHROPIC_API_KEY` | Anthropic API key for `BackendAnthropic`. |
| `Project` | `string` | `GOOGLE_CLOUD_PROJECT` | Google Cloud project for `BackendVertex`. |
| `Location` | `string` | `GOOGLE_CLOUD_LOCATION` | Google Cloud region for `BackendVertex`. |
| `MaxTokens` | `int` | `8192` | Maximum output tokens when the request sets no `MaxOutputTokens`. |
| `MaxRetries` | `*int` | `2` | Number of retries the Anthropic SDK makes for failed requests. |
| `Effort` | `Effort` | Model default | Reasoning effort level. |
| `PromptCache` | `*PromptCacheConfig` | `nil` | Enables prompt caching when set. |
For the full list, see the [`anthropicmodel` package reference](https://pkg.go.dev/google.golang.org/adk/v2/model/anthropicmodel).
| Option | Type | Default | Description |
| ----------------- | ----------------- | -------- | ---------------------------------------------------------------------------- |
| `modelName` | `String` | Required | The Claude model ID. |
| `anthropicClient` | `AnthropicClient` | Required | The Anthropic client, configured for the Anthropic API or a `VertexBackend`. |
| `maxTokens` | `int` | `8192` | Maximum output tokens for each request. |
| Option | Type | Default | Description |
| ------------------- | ---------------------- | ------------------- | ---------------------------------------------------------------------------------- |
| `name` | `String` | Required | The Claude model ID. |
| `apiKey` | `String?` | `ANTHROPIC_API_KEY` | Anthropic API key. Selects the Anthropic API backend. |
| `vertexCredentials` | `VertexCredentials` | None | Project, location, and credentials. Selects the Agent Platform backend. |
| `client` | `AnthropicClient` | None | A preconfigured Anthropic client, used instead of `apiKey` or `vertexCredentials`. |
| `maxTokens` | `Int` | `8192` | Maximum output tokens when the request sets none. |
| `effort` | `OutputConfig.Effort?` | `null` | Reasoning effort level. |
## Limitations
- **Live API:** Claude models don't support ADK bidirectional streaming with the Live API.
- **TypeScript:** The `Claude` class returns complete responses without streaming, and accepts text, function call, and function response parts only. For the full list, see the [Claude integration guide](https://github.com/google/adk-js/blob/main/docs/guides/integrations/models/anthropic_llm/index.md#limitations).
## Next steps
- [Agent Platform hosted models](/agents/models/agent-platform/): Use other models deployed on Agent Platform.
- [LiteLLM](/agents/models/litellm/): Connect to models from other providers.
- [Context caching](/context/caching/): Learn more about cache settings.
# Apigee AI Gateway for ADK agents
Supported in ADKPython v1.18.0Java v0.4.0
[Apigee](https://docs.cloud.google.com/apigee/docs/api-platform/get-started/what-apigee) provides a powerful [AI Gateway](https://cloud.google.com/solutions/apigee-ai), transforming how you manage and govern your generative AI model traffic. By exposing your AI model endpoint (like Agent Platform or the Gemini API) through an Apigee proxy, you immediately gain enterprise-grade capabilities:
- **Model Safety:** Implement security policies like Model Armor for threat protection.
- **Traffic Governance:** Enforce Rate Limiting and Token Limiting to manage costs and prevent abuse.
- **Performance:** Improve response times and efficiency using Semantic Caching and advanced model routing.
- **Monitoring & Visibility:** Get granular monitoring, analysis, and auditing of all your AI requests.
The `ApigeeLlm` wrapper is designed for use with Agent Platform and the Gemini API (generateContent). We are continually expanding support for other models and interfaces. For OpenAI compatible models, including self-hosted or other providers, use the `CompletionsHTTPClient` to route traffic through your Apigee proxy.
## Implementation example
Integrate Apigee's governance into your agent's workflow by instantiating the `ApigeeLlm` wrapper object and pass it to an `LlmAgent` or other agent type.
```python
from google.adk.agents import LlmAgent
from google.adk.models.apigee_llm import ApigeeLlm
# Instantiate the ApigeeLlm wrapper
model = ApigeeLlm(
# Specify the Apigee route to your model. For more info, check out the ApigeeLlm documentation (https://github.com/google/adk-python/tree/main/contributing/samples/models/hello_world_apigeellm).
model="apigee/gemini-flash-latest",
# The proxy URL of your deployed Apigee proxy including the base path
proxy_url=f"https://{APIGEE_PROXY_URL}",
# Pass necessary authentication/authorization headers (like an API key)
custom_headers={"foo": "bar"},
# Optional: Pass google-auth credentials if the proxy requires additional OAuth scopes
# credentials=my_credentials
)
# Pass the configured model wrapper to your LlmAgent
agent = LlmAgent(
model=model,
name="my_governed_agent",
instruction="You are a helpful assistant powered by Gemini and governed by Apigee.",
# ... other agent parameters
)
```
```java
import com.google.adk.agents.LlmAgent;
import com.google.adk.models.ApigeeLlm;
import com.google.common.collect.ImmutableMap;
ApigeeLlm apigeeLlm =
ApigeeLlm.builder()
.modelName("apigee/gemini-flash-latest") // Specify the Apigee route to your model. For more info, check out the ApigeeLlm documentation
.proxyUrl(APIGEE_PROXY_URL) //The proxy URL of your deployed Apigee proxy including the base path
.customHeaders(ImmutableMap.of("foo", "bar")) //Pass necessary authentication/authorization headers (like an API key)
.build();
LlmAgent agent =
LlmAgent.builder()
.model(apigeeLlm)
.name("my_governed_agent")
.description("my_governed_agent")
.instruction("You are a helpful assistant powered by Gemini and governed by Apigee.")
// tools will be added next
.build();
```
With this configuration, every API call from your agent will be routed through Apigee first, where all necessary policies (security, rate limiting, logging) are executed before the request is securely forwarded to the underlying AI model endpoint. For a full code example using the Apigee proxy, see [Hello World Apigee LLM](https://github.com/google/adk-python/tree/main/contributing/samples/models/hello_world_apigeellm).
## Compatibility with OpenAI
The `CompletionsHTTPClient` is a generic HTTP client designed for compatibility with the OpenAI API format. It allows you to route requests through proxies (such as Apigee) that expect standard OpenAI-compatible `/chat/completions` endpoints, rather than native Gemini or Vertex AI protocols. This client handles:
- **Payload construction**: Converts LlmRequest objects into the format required by OpenAI-compatible APIs.
- **Response handling**: Manages streaming and non-streaming responses from the proxy.
- **Reliability**: Uses `tenacity` to retry non-streaming requests, but only when you pass `retry_options=types.HttpRetryOptions(...)` to the constructor. By default each request is attempted once, and streaming requests are never retried.
- **Normalization**: Parses responses and streaming chunks into the standard format expected by the rest of the ADK framework.
### Implementation example
```python
import asyncio
from google.adk.models.apigee_llm import CompletionsHTTPClient
from google.adk.models.llm_request import LlmRequest
from google.genai import types
async def test_client():
# 1. Initialize the client
client = CompletionsHTTPClient(
base_url="https://your-apigee-proxy-url.com/v1",
headers={"Authorization": "Bearer YOUR_API_KEY"}
)
# 2. Construct a minimal request
request = LlmRequest(
model="gpt-4o", # Replace with your target model ID
contents=[types.Content(role="user", parts=[types.Part.from_text(text="Hello!")])]
)
# 3. Execute a non-streaming generation
async for response in client.generate_content_async(request, stream=False):
if response.content and response.content.parts:
print(f"Response: {response.content.parts[0].text}")
if __name__ == "__main__":
asyncio.run(test_client())
```
# Google Gemma models for ADK agents
Supported in ADKPython v0.1.0
ADK agents can use the [Google Gemma](https://ai.google.dev/gemma/docs) family of generative AI models that offer a wide range of capabilities. ADK supports many Gemma features, including [Tool Calling](/tools-custom/) and [Structured Output](/agents/llm-agents/#data-handling).
You can use Gemma 4 through the [Gemini API](https://ai.google.dev/gemini-api/docs), or with one of many self-hosting options on Google Cloud: [Agent Platform](https://console.cloud.google.com/vertex-ai/publishers/google/model-garden/gemma4), [Google Kubernetes Engine](https://docs.cloud.google.com/kubernetes-engine/docs/tutorials/serve-gemma-gpu-vllm), [Cloud Run](https://docs.cloud.google.com/run/docs/run-gemma-on-cloud-run).
Gemma 3 needs a different model class than the Gemma 4 examples below. It has no native function calling or system instruction support, so ADK supplies workarounds in dedicated classes: use `Gemma(model="gemma-3-27b-it")` for the Gemini API and `Gemma3Ollama()` for Ollama, both from `google.adk.models`. `Gemma3Ollama` is only defined when [LiteLLM](/agents/models/litellm/) is installed (`litellm>=1.84`).
## Gemini API Example
Create an API key in [Google AI Studio](https://aistudio.google.com/app/apikey).
```python
# Set GEMINI_API_KEY environment variable to your API key
# export GEMINI_API_KEY="YOUR_API_KEY"
from google.adk.agents import LlmAgent
from google.adk.models import Gemini
# Simple tool to try
def get_weather(location: str) -> str:
return f"Location: {location}. Weather: sunny, 76 degrees Fahrenheit, 8 mph wind."
root_agent = LlmAgent(
model=Gemini(model="gemma-4-31b-it"),
name="weather_agent",
instruction="You are a helpful assistant that can provide current weather.",
tools=[get_weather]
)
```
```java
// Set GEMINI_API_KEY environment variable to your API key
// export GEMINI_API_KEY="YOUR_API_KEY"
import com.google.adk.agents.LlmAgent;
import com.google.adk.tools.Annotations.Schema;
import com.google.adk.tools.FunctionTool;
LlmAgent weatherAgent = LlmAgent.builder()
.model("gemma-4-31b-it")
.name("weather_agent")
.instruction("""
You are a helpful assistant that can provide current weather.
""")
.tools(FunctionTool.create(this, "getWeather"))
.build();
@Schema(name = "getWeather",
description = "Retrieve the weather forecast for a given location")
public Map getWeather(
@Schema(name = "location",
description = "The location for the weather forecast")
String location) {
return Map.of("forecast", "Location: " + location
+ ". Weather: sunny, 76 degrees Fahrenheit, 8 mph wind.");
}
```
## vLLM Example
To access Gemma 4 endpoints in these services, you can use vLLM models through the [LiteLLM](/agents/models/litellm/) library for Python, and through [LangChain4j](https://docs.langchain4j.dev/) for Java.
The following example shows how to use a Gemma 4 vLLM endpoint with ADK agents.
### Setup
1. **Deploy Model:** Deploy your chosen model using [Agent Platform](https://console.cloud.google.com/vertex-ai/publishers/google/model-garden/gemma4), [Google Kubernetes Engine](https://docs.cloud.google.com/kubernetes-engine/docs/tutorials/serve-gemma-gpu-vllm), or [Cloud Run](https://docs.cloud.google.com/run/docs/run-gemma-on-cloud-run), and use its OpenAI-compatible API endpoint. Note that the API base URL includes `/v1` (e.g., `https://your-vllm-endpoint.run.app/v1`).
- *Important for ADK Tools:* When deploying, ensure the serving tool supports and enables compatible tool/function calling and reasoning parsers.
1. **Authentication:** Determine how your endpoint handles authentication (e.g., API key, bearer token).
### Code
```python
import subprocess
from google.adk.agents import LlmAgent
from google.adk.models.lite_llm import LiteLlm
# --- Example Agent using a model hosted on a vLLM endpoint ---
# Endpoint URL provided by your model deployment
api_base_url = "https://your-vllm-endpoint.run.app/v1"
# Model name as recognized by *your* vLLM endpoint configuration
model_name_at_endpoint = "openai/google/gemma-4-31B-it"
# Simple tool to try
def get_weather(location: str) -> str:
return f"Location: {location}. Weather: sunny, 76 degrees Fahrenheit, 8 mph wind."
# Authentication (Example: using gcloud identity token for a Cloud Run deployment)
# Adapt this based on your endpoint's security
try:
gcloud_token = subprocess.check_output(
["gcloud", "auth", "print-identity-token", "-q"]
).decode().strip()
auth_headers = {"Authorization": f"Bearer {gcloud_token}"}
except Exception as e:
print(f"Warning: Could not get gcloud token - {e}.")
auth_headers = None # Or handle error appropriately
root_agent = LlmAgent(
model=LiteLlm(
model=model_name_at_endpoint,
api_base=api_base_url,
# Pass authentication headers if needed
extra_headers=auth_headers,
# Alternatively, if endpoint uses an API key:
# api_key="YOUR_ENDPOINT_API_KEY",
extra_body={
"chat_template_kwargs": {
"enable_thinking": True # Enable thinking
},
"skip_special_tokens": False # Should be set to False
},
),
name="weather_agent",
instruction="You are a helpful assistant that can provide current weather.",
tools=[get_weather] # Tools!
)
```
To use Gemma hosted on vLLM, you must use an OpenAI compatible library. LangChain4j offers an OpenAI dependency that you can add to your `pom.xml`:
```xml
com.google.adkgoogle-adk-langchain4j${adk.version}dev.langchain4jlangchain4j-core${langchain4j.version}dev.langchain4jlangchain4j-open-ai${langchain4j.version}
```
Create an OpenAI compatible chat model (streaming or non-streaming), wrap it with the `LangChain4j` wrapper, then pass it to the `LlmAgent`:
```java
import com.google.adk.agents.LlmAgent;
import com.google.adk.tools.Annotations.Schema;
import com.google.adk.tools.FunctionTool;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
// Endpoint URL provided by your model deployment
String apiBaseUrl = "https://your-vllm-endpoint.run.app/v1";
// Model name as recognized by *your* vLLM endpoint configuration
String gemmaModelName = "gg-hf-gg/gemma-4-31b-it";
// First, define an OpenAI compatible chat model with LangChain4j
StreamingChatModel model =
OpenAiStreamingChatModel.builder()
.modelName(gemmaModelName)
// If your endpoint requires an API key
// .apiKey("YOUR_ENDPOINT_API_KEY")
.baseUrl(apiBaseUrl)
.customParameters(
Map.of(
"skip_special_tokens", false,
"chat_template_kwargs", Map.of("enable_thinking", true)
)
)
.build();
// Configure the agent with the LangChain4j wrapper model
LlmAgent weatherAgent = LlmAgent.builder()
.model(new LangChain4j(model))
.name("weather_agent")
.instruction("""
You are a helpful assistant that can provide the current weather.
""")
.tools(FunctionTool.create(this, "getWeather"))
.build();
@Schema(name = "getWeather",
description = "Retrieve the weather forecast for a given location")
public Map getWeather(
@Schema(name = "location",
description = "The location for the weather forecast")
String location) {
return Map.of("forecast", "Location: " + location
+ ". Weather: sunny, 76 degrees Fahrenheit, 8 mph wind.");
}
```
## Build a food tour agent with Gemma 4, ADK, and Google Maps MCP
This sample shows how to build a personalized food tour agent using Gemma 4, ADK, and the Google Maps MCP server. The agent takes a user’s dish photo or text description, a location, and an optional budget, then recommends places to eat and organizes them into a walking route.
### Prerequisites
- Get an API key in [Google AI Studio](https://aistudio.google.com/app/apikey). Set `GEMINI_API_KEY` environment variable to your Gemini API key.
- Enable [Google Maps API](https://console.cloud.google.com/maps-api/) on Google Cloud Console.
- Create a [Google Maps Platform API key](https://console.cloud.google.com/maps-api/credentials). Set `MAPS_API_KEY` environment variable to your API key.
- Install ADK and configure it in your Python environment or configure the Java dependencies in your Java project.
### Project structure
```bash
food_tour_app/
├── __init__.py
└── agent.py
```
`agent.py`
```python
import os
import dotenv
from google.adk.agents import LlmAgent
from google.adk.models import Gemini
from google.adk.tools.mcp_tool.mcp_toolset import McpToolset
from google.adk.tools.mcp_tool.mcp_session_manager import StreamableHTTPConnectionParams
dotenv.load_dotenv()
system_instruction = """
You are an expert personalized food tour guide.
Your goal is to build a culinary tour based on the user's inputs: a photo of a dish (or a text description), a location, and a budget.
Follow these 4 rigorous steps:
1. **Identify the Cuisine/Dish:** Analyze the user's provided description or image URL to determine the primary cuisine or specific dish.
2. **Find the Best Spots:** Use the `search_places` tool to find highly rated restaurants, stalls, or cafes serving that cuisine/dish in the user's specified location.
**CRITICAL RULE FOR PLACES:** `search_places` returns AI-generated place data summaries along with `place_id`, latitude/longitude coordinates, and map links for each place, but may lack a direct, explicit name field. You must carefully associate each described place to its provided `place_id` or `lat_lng`.
3. **Build the Route:** Use the `compute_routes` tool to structure a walking-optimized route between the selected spots.
**CRITICAL ROUTING RULE:** To avoid hallucinating, you MUST provide the `origin` and `destination` using the exact `place_id` string OR `lat_lng` object returned by `search_places`. Do NOT guess or hallucinate an `address` or `place_id` if you do not know the exact name.
4. **Insider Tips:** Provide specific "order this, skip that" insider tips for each location on the tour.
Structure your response clearly and concisely. If the user provides a budget, ensure your suggestions align with it.
"""
MAPS_MCP_URL = "https://mapstools.googleapis.com/mcp"
def get_maps_mcp_toolset():
dotenv.load_dotenv()
maps_api_key = os.getenv("MAPS_API_KEY")
if not maps_api_key:
print("Warning: MAPS_API_KEY environment variable not found.")
maps_api_key = "no_api_found"
tools = McpToolset(
connection_params=StreamableHTTPConnectionParams(
url=MAPS_MCP_URL,
headers={
"X-Goog-Api-Key": maps_api_key
}
)
)
print("Google Maps MCP Toolset configured.")
return tools
maps_toolset = get_maps_mcp_toolset()
root_agent = LlmAgent(
model=Gemini(model="gemma-4-31b-it"),
name="food_tour_agent",
instruction=system_instruction,
tools=[maps_toolset],
)
```
### Environment variables
Set the required environment variables before running the agent.
```text
export MAPS_API_KEY="YOUR_GOOGLE_MAPS_API_KEY"
export GEMINI_API_KEY="YOUR_GEMINI_API_KEY"
```
### Example usage
To test out the capabilities of the Food Tour Agent, try pasting one of these prompts into the chat:
- *"I want to do a ramen tour in Toronto. My budget is $60 for the day. Give me a walking route for the top 3 spots and tell me what I should order at each."*
- *"I have this photo of a deep dish pizza [insert image URL]. I want to find the best places for this around Navy Pier in Chicago. Structure a walking tour and tell me what the must-have slice is at each stop."*
- *"I'm in Downtown Austin looking for an authentic BBQ tour. Let's keep the budget under $100. Build a walking route between 3 highly-rated spots and give me insider tips on the best cuts of meat to get."*
The agent will:
1. Infer the likely cuisine or dish style
1. Search for relevant places using Google Maps MCP tools
1. Compute a walking route between selected stops
1. Return a structured food tour with recommendations and insider tips
# LiteLLM model connector for ADK agents
Supported in ADKPython v0.1.0
ADK Python Security Advisory: LiteLLM supply chain compromise
Unauthorized code was identified in LiteLLM versions 1.82.7 and 1.82.8 on PyPI on March 24, 2026. If you use ADK Python with the `eval` or `extensions` extras, update to the latest version of ADK Python immediately. If you installed or upgraded LiteLLM during this period, rotate all secrets and credentials. For details and required actions, refer to the [ADK security advisory](https://github.com/google/adk-python/issues/5005) and [LiteLLM's Security Update: Suspected Supply Chain Incident](https://docs.litellm.ai/blog/security-update-march-2026).
[LiteLLM](https://docs.litellm.ai/) is a Python library that acts as a translation layer for models and model hosting services, providing a standardized, OpenAI-compatible interface to over 100+ LLMs. ADK provides integration through the LiteLLM library, allowing you to access a vast range of LLMs from providers such as OpenAI, Anthropic, Ollama, Mistral, DeepSeek, and Cohere, and many others. You can run open-source models locally or self-host them and integrate them using LiteLLM for operational control, cost savings, privacy, or offline use cases.
You can use the LiteLLM library to access remote or locally hosted AI models:
- **Remote model host:** Use the `LiteLlm` wrapper class and set it as the `model` parameter of `LlmAgent`.
- **Local model host:** Use the `LiteLlm` wrapper class configured to point to your local model server. For examples of local model hosting solutions, see the [Ollama](/agents/models/ollama/) or [vLLM](/agents/models/vllm/) documentation.
Windows Encoding with LiteLLM
When using ADK agents with LiteLLM on Windows, you might encounter a `UnicodeDecodeError`. This error occurs because LiteLLM may attempt to read cached files using the default Windows encoding (`cp1252`) instead of UTF-8. Prevent this error by setting the `PYTHONUTF8` environment variable to `1`. This forces Python to use UTF-8 for all file I/O.
**Example (PowerShell):**
```powershell
# Set for the current session
$env:PYTHONUTF8 = "1"
# Set persistently for the user
[System.Environment]::SetEnvironmentVariable('PYTHONUTF8', '1', [System.EnvironmentVariableTarget]::User)
```
## Setup
1. **Install LiteLLM:** ADK requires `litellm>=1.84`.
```shell
pip install "litellm>=1.84"
```
1. **Set Provider API Keys:** Configure API keys as environment variables for the specific providers you intend to use.
- *Example for OpenAI:*
```shell
export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
```
- *Example for Anthropic (non-Agent Platform):*
```shell
export ANTHROPIC_API_KEY="YOUR_ANTHROPIC_API_KEY"
```
- *Consult the [LiteLLM Providers Documentation](https://docs.litellm.ai/docs/providers) for the correct environment variable names for other providers.*
## Example implementation
```python
from google.adk.agents import LlmAgent
from google.adk.models.lite_llm import LiteLlm
# --- Example Agent using OpenAI's GPT-4o ---
# (Requires OPENAI_API_KEY)
agent_openai = LlmAgent(
model=LiteLlm(model="openai/gpt-4o"), # LiteLLM model string format
name="openai_agent",
instruction="You are a helpful assistant powered by GPT-4o.",
# ... other agent parameters
)
# --- Example Agent using Anthropic's Claude Haiku (non-Vertex) ---
# (Requires ANTHROPIC_API_KEY)
agent_claude_direct = LlmAgent(
model=LiteLlm(model="anthropic/claude-3-haiku-20240307"),
name="claude_direct_agent",
instruction="You are an assistant powered by Claude Haiku.",
# ... other agent parameters
)
```
## Anthropic thinking blocks
Supported in ADKPython v1.28.0
When you use Anthropic Claude models (such as Claude 3.7 Sonnet) through the `LiteLlm` connector, ADK supports Anthropic's structured reasoning feature, known as "thinking blocks". ADK automatically extracts the `thinking_blocks` and their signatures.
Anthropic requires these signatures to be sent back in multi-turn conversations, and otherwise silently drops thinking after the first turn. ADK rebuilds the `thinking_blocks` with their signatures on each outbound request, so Claude's reasoning is preserved across tool calls and multi-turn interactions without any custom state management on your part.
# LiteRT-LM model host for ADK agents
Supported in ADKPython v0.1.0Kotlin v0.4.0
You can use the [LiteRT-LM](https://github.com/google-ai-edge/LiteRT-LM) library to efficiently run language models locally on various compute devices without requiring specialized processors such as graphics processing units (GPU) or tensor processing units (TPU). LiteRT-LM supports many models, including Google Gemma models as well as third-party models. This guide provides instructions for setting up LiteRT-LM with ADK for the following languages:
- [Python](#python)
- [Kotlin](#kotlin)
## Python
These instructions describe how to use LiteRT-LM server with ADK in Python with a Gemma open weights model, including using LiteRT-LM 's local hosting model server `lit`.
### Install resources
You need to download a model to use with LiteRT-LM, and the `lit` CLI tool to help you find a model download it.
#### Install `lit` CLI tool
Download and install the `lit` CLI tool by following these [instructions](https://github.com/google-ai-edge/LiteRT-LM?tab=readme-ov-file#desktop-cli-lit) in the LiteRT-LM GitHub repository.
#### Download a model
Before you start the server, you need to download a model. You'll need a *Hugging Face* user access token to download a LiteRT-LM model using `lit`. You can get an [access token](https://huggingface.co/settings/tokens) on the *Hugging Face* website.
To see a list of models available for download, use the `lit list` command:
```bash
lit list --show_all
```
Download a model using the `lit pull` command:
```bash
export HUGGING_FACE_HUB_TOKEN="**your Hugging Face token**"
lit pull gemma3n-e2b
```
### Configure your agent
Configure your agent to connect to LiteRT-LM and a hosted model. When running Gemma models with LiteRT-LM, you configure a `Gemini` model class with the model identifier and local network address.
To use LiteRT-LM with ADK and a Gemma model:
1. Set `base_url` to the LiteRT-LM server URL, including the scheme, for example: `http://localhost:8001`.
1. Set `model` to the LiteRT-LM model name, for example: `gemma3n-e2b`.
The following example code shows how to configure an agent to connect to the locally hosted LiteRT-LM instance serving the Gemma model configuration described above:
```py
from google.adk.agents import Agent
from google.adk.models import Gemini
root_agent = Agent(
model=Gemini(
model="gemma3n-e2b",
base_url="http://localhost:8001",
),
name="dice_agent",
description=(
"hello world agent that can roll a die of 8 sides and check prime"
" numbers."
),
instruction="""
You roll dice and answer questions about the outcome of the dice rolls.
""",
tools=[
roll_die,
check_prime,
],
)
```
Then run the agent as usual:
```bash
adk web
```
### Running the LiteRT-LM server
The LiteRT-LM server is a separate process that serves LiteRT-LM models. It is started by the LiteRT-LM CLI tool `lit`.
#### Run the server
After downloading a model, start the LiteRT-LM server locally by running the following command:
```bash
lit serve --port 8001
```
Local Server Port Number
You may choose any port number for the LiteRT-LM server as long as it matches the `base_url` you set in the `Gemini` class in your agent code.
#### Debugging
To see incoming requests to the LiteRT-LM server and the exact input sent to the model, use the `--verbose` flag:
```bash
lit serve --port 8001 --verbose
```
## Kotlin
These instructions describe how to use LiteRT-LM with ADK in Kotlin using the `com.google.adk.kt.litertlm` package.
### Install resources
You need to download a model to use with LiteRT-LM, and the `litert-lm` CLI tool to help you find a model download it.
#### Install LiteRT-LM CLI
Prerequisites: Python 3.10 or higher
To install the CLI, run:
```bash
pip install --upgrade litert-lm
```
For additional installation methods, such as using uv, see [LiteRT-LM CLI Installation Guide](https://developers.google.com/edge/litert-lm/cli/installation).
#### Download a model
Download a model compatible with LiteRT-LM to use the `litert-lm` CLI tool. Use `litert-lm` to download models directly from Hugging Face:
```bash
litert-lm import \
--from-huggingface-repo litert-community/gemma-4-E2B-it-litert-lm \
gemma-4-E2B-it.litertlm
```
Once downloaded, the model is stored locally at:
```text
~/.litert-lm/models/gemma-4-E2B-it.litertlm/model.litertlm
```
For more details about `litert-lm`, refer to the [LiteRT-LM CLI Usage Guide](https://developers.google.com/edge/litert-lm/cli/usage).
### Add dependencies
ADK Kotlin works with LiteRT-LM through an adapter package, `com.google.adk:google-adk-kotlin-litertlm`.
In your `build.gradle.kts`, add `com.google.adk:google-adk-kotlin-litertlm` and `com.google.ai.edge.litertlm:litertlm-jvm` to your dependencies:
```text
repositories {
mavenCentral()
google()
}
dependencies {
implementation("com.google.adk:google-adk-kotlin-core:1.6.0")
implementation("com.google.adk:google-adk-kotlin-litertlm:1.6.0")
implementation("com.google.ai.edge.litertlm:litertlm-jvm:0.13.1")
// other dependencies...
}
```
### Configure agent model
Run a local model for your agent with LiteRT-LM by configuring a `LiteRtLmModel` object as part of your `LlmAgent` object. If you do not already have a ADK Kotlin project, follow the [Kotlin Quickstart for ADK](/get-started/kotlin/) getting started guide. The following code example shows you how to configure an `LlmAgent`, and set the `model` parameter to a `LiteRtLmModel`:
```text
object HelloTimeAgent {
// Get model path from environment variable.
private val modelPath: String by lazy {
System.getenv("LITERT_LM_MODEL_PATH")
?: throw IllegalStateException(
"LITERT_LM_MODEL_PATH environment variable must be set pointing to a .litertlm file."
)
}
@JvmField
val rootAgent =
LlmAgent(
name = "hello_time_agent",
description = "Tells the current time in a specified city.",
model =
LiteRtLmModel.create(
EngineConfig(modelPath = modelPath, backend = Backend.CPU())
),
instruction =
Instruction(
"You are a helpful assistant that tells the current time in a city. " +
"Use the 'getCurrentTime' tool for this purpose."
),
tools = TimeService().generatedTools(),
)
}
```
In this example, the path to the LiteRT-LM model file is read from the environment variable `LITERT_LM_MODEL_PATH`. The model will be run on the CPU. You can run the model on a GPU by setting `backend = Backend.GPU()`.
When you run the agent, set `LITERT_LM_MODEL_PATH` to the location of the model file, for example: `~/.litert-lm/models/gemma-4-E2B-it.litertlm/model.litertlm`.
### Run your agent
If you followed the [Kotlin Quickstart for ADK](/get-started/kotlin/) with the above modifications, you can run your ADK agent using the command-line REPL with the environment variable `LITERT_LM_MODEL_PATH` set to the path of the model file:
```bash
LITERT_LM_MODEL_PATH=~/.litert-lm/models/gemma-4-E2B-it.litertlm/model.litertlm ./gradlew run
```
Example interaction:
```text
Agent hello_time_agent is ready. Type 'exit' to quit.
You > what's your name?
hello_time_agent > I am Gemma 4, a Large Language Model developed by Google DeepMind.
You > what time is it in paris?
hello_time_agent > calls tool: getCurrentTime
hello_time_agent > The time in Paris is 10:30 am.
```
# Ollama model host for ADK agents
Supported in ADKPython v0.1.0
[Ollama](https://ollama.com/) is a tool that allows you to host and run open-source models locally. ADK integrates with Ollama-hosted models through the [LiteLLM](/agents/models/litellm/) model connector library.
## Get started
Use the LiteLLM wrapper to create agents with Ollama-hosted models. The following code example shows a basic implementation for using Gemma open models with your agents:
```py
root_agent = Agent(
model=LiteLlm(model="ollama_chat/gemma3:latest"),
name="dice_agent",
description=(
"hello world agent that can roll a dice of 8 sides and check prime"
" numbers."
),
instruction="""
You roll dice and answer questions about the outcome of the dice rolls.
""",
tools=[
roll_die,
check_prime,
],
)
```
Warning: Use `ollama_chat`interface
Make sure you set the provider `ollama_chat` instead of `ollama`. Using `ollama` can result in unexpected behaviors such as infinite tool call loops and ignoring previous context.
Use `OLLAMA_API_BASE` environment variable
Although you can specify the `api_base` parameter in LiteLLM for generation, as of v1.65.5, the library relies on the environment variable for other API calls. Therefore, you should set the `OLLAMA_API_BASE` environment variable for your Ollama server URL to ensure all requests are routed correctly.
```bash
export OLLAMA_API_BASE="http://localhost:11434"
adk web
```
## Model choice
If your agent is relying on tools, make sure that you select a model with tool support from [Ollama website](https://ollama.com/search?c=tools). For reliable results, use a model with tool support. You can check tool support for the model using the following command:
```bash
ollama show mistral-small3.1
Model
architecture mistral3
parameters 24.0B
context length 131072
embedding length 5120
quantization Q4_K_M
Capabilities
completion
vision
tools
```
You should see **tools** listed under capabilities. You can also look at the template the model is using and tweak it based on your needs.
```bash
ollama show --modelfile llama3.2 > model_file_to_modify
```
For instance, the default template for the above model inherently suggests that the model shall call a function all the time. This may result in an infinite loop of function calls.
```text
Given the following functions, please respond with a JSON for a function call
with its proper arguments that best answers the given prompt.
Respond in the format {"name": function name, "parameters": dictionary of
argument name and its value}. Do not use variables.
```
You can swap such prompts with a more descriptive one to prevent infinite tool call loops, for instance:
```text
Review the user's prompt and the available functions listed below.
First, determine if calling one of these functions is the most appropriate way
to respond. A function call is likely needed if the prompt asks for a specific
action, requires external data lookup, or involves calculations handled by the
functions. If the prompt is a general question or can be answered directly, a
function call is likely NOT needed.
If you determine a function call IS required: Respond ONLY with a JSON object in
the format {"name": "function_name", "parameters": {"argument_name": "value"}}.
Ensure parameter values are concrete, not variables.
If you determine a function call IS NOT required: Respond directly to the user's
prompt in plain text, providing the answer or information requested. Do not
output any JSON.
```
Then you can create a new model with the following command:
```bash
ollama create llama3.2-modified -f model_file_to_modify
```
## Use OpenAI provider
Alternatively, you can use `openai` as the provider name. This approach requires setting the `OPENAI_API_BASE=http://localhost:11434/v1` and `OPENAI_API_KEY=anything` env variables instead of `OLLAMA_API_BASE`. Note that the `API_BASE` value has *`/v1`* at the end.
```py
root_agent = Agent(
model=LiteLlm(model="openai/mistral-small3.1"),
name="dice_agent",
description=(
"hello world agent that can roll a dice of 8 sides and check prime"
" numbers."
),
instruction="""
You roll dice and answer questions about the outcome of the dice rolls.
""",
tools=[
roll_die,
check_prime,
],
)
```
```bash
export OPENAI_API_BASE=http://localhost:11434/v1
export OPENAI_API_KEY=anything
adk web
```
### Debugging
You can see the request sent to the Ollama server by adding the following in your agent code just after imports.
```py
import litellm
litellm._turn_on_debug()
```
Look for a line like the following:
```bash
Request Sent from LiteLLM:
curl -X POST \
http://localhost:11434/api/chat \
-d '{'model': 'mistral-small3.1', 'messages': [{'role': 'system', 'content': ...
```
# OpenAI models for ADK agents
Supported in ADKGo v2.1.0Kotlin v1.4.0
You can use OpenAI models with ADK. How you connect depends on the language:
- **Go — native support:** ADK Go provides a direct `openaimodel` package that implements the `model.LLM` interface, targeting the OpenAI Responses API or the [Chat Completions API](#chat-completions-api). [Get started](#get-started).
- **Python — via LiteLLM:** ADK Python accesses OpenAI models (and many other providers) through the LiteLLM connector. See [LiteLLM](/agents/models/litellm/).
- **Kotlin — native support:** ADK Kotlin provides a `ChatCompletions` model in `com.google.adk.kt.models.openai` that targets the OpenAI Chat Completions API. [Get started](#get-started).
Experimental in Go
The OpenAI model support feature is experimental in ADK Go. It may have limitations and its behavior may change.
## Get started
The `openaimodel` package provides a client for interacting with OpenAI's API. It implements the `model.LLM` interface and uses the OpenAI Responses API by default, or the [Chat Completions API](#chat-completions-api) when `ClientConfig.API` selects it. The following code example shows a basic implementation for using OpenAI models in your agents:
```go
import (
"context"
"log"
"github.com/openai/openai-go/v3"
"google.golang.org/adk/v2/agent/llmagent"
"google.golang.org/adk/v2/model/openaimodel"
)
// Instantiate the model
llm, err := openaimodel.NewModel(context.Background(), openai.ChatModelGPT4oMini, &openaimodel.ClientConfig{})
if err != nil {
log.Fatal(err)
}
// Create the agent
agent, err := llmagent.New(llmagent.Config{
Name: "openai_agent",
Model: llm,
Instruction: "You are a helpful AI assistant.",
})
if err != nil {
log.Fatal(err)
}
```
For a complete, runnable sample, see [examples/openai/responses/](https://github.com/google/adk-go/tree/main/examples/openai/responses) in the ADK Go repository.
Requires ADK Go v2.5.0 or later.
```go
import (
"context"
"log"
"os"
"github.com/openai/openai-go/v3"
"google.golang.org/adk/v2/agent/llmagent"
"google.golang.org/adk/v2/model/openaimodel"
)
// Instantiate the model on the Chat Completions API
llm, err := openaimodel.NewModel(context.Background(), openai.ChatModelGPT4oMini, &openaimodel.ClientConfig{
APIKey: os.Getenv("OPENAI_API_KEY"),
API: openaimodel.APIChatCompletions,
})
if err != nil {
log.Fatal(err)
}
// Create the agent
agent, err := llmagent.New(llmagent.Config{
Name: "openai_agent",
Model: llm,
Instruction: "You are a helpful AI assistant.",
})
if err != nil {
log.Fatal(err)
}
```
For a complete, runnable sample, see [examples/openai/completions/](https://github.com/google/adk-go/tree/main/examples/openai/completions) in the ADK Go repository.
```kotlin
import com.google.adk.kt.agents.Instruction
import com.google.adk.kt.agents.LlmAgent
import com.google.adk.kt.models.openai.ChatCompletions
// Instantiate the model on the Chat Completions API
val llm = ChatCompletions(name = "gpt-4o-mini", apiKey = System.getenv("OPENAI_API_KEY"))
// Create the agent
val agent =
LlmAgent(
name = "openai_agent",
model = llm,
instruction = Instruction("You are a helpful AI assistant."),
)
```
For a complete, runnable sample, see [examples/.../chatcompletions/](https://github.com/google/adk-kotlin/tree/v1.5.0/examples/src/main/kotlin/com/google/adk/kt/examples/chatcompletions) in the ADK Kotlin repository.
## Chat Completions API
Supported in ADKGo v2.5.0Kotlin v1.4.0Experimental
By default, `openaimodel` sends requests to the OpenAI [Responses API](https://platform.openai.com/docs/api-reference/responses) (`POST /v1/responses`). Nearly every OpenAI-compatible provider implements the [Chat Completions API](https://platform.openai.com/docs/api-reference/chat) (`POST /v1/chat/completions`), and some implement only that one. To use it, set the `API` field of `ClientConfig` to `openaimodel.APIChatCompletions`, as the Chat Completions API tab under [Get started](#get-started) shows. Agents, tools, and the runner work the same way with either API.
Set the API key for other providers
To reach another OpenAI-compatible provider, set `BaseURL` to its endpoint and `APIKey` to its key. When `APIKey` is empty, the `openai-go` SDK falls back to the `OPENAI_API_KEY` environment variable and sends that key to `BaseURL`.
The two APIs support the same features, with these differences:
- **Generation settings:** `StopSequences`, `FrequencyPenalty`, `PresencePenalty`, and `Seed` are sent to the Chat Completions API. The Responses API has no equivalent fields and returns an error for them.
- **Reasoning output:** The Chat Completions API does not return reasoning text, so responses contain no thought parts, and `ThinkingConfig.IncludeThoughts` is ignored. Reasoning effort and reasoning-token counts work with both APIs.
- **Output token limit:** `MaxOutputTokens` is sent as `max_completion_tokens`. Some compatible servers honor only the older `max_tokens` field, so the limit has no effect there.
## Supported features
- Text generation (streaming and non-streaming)
- Function (tool) calling
- Structured output via `OutputSchema` (JSON schema)
- Reasoning models (e.g. o-series), including reasoning-token accounting
- Token logprobs
- Tool choice via `ToolConfig.FunctionCallingConfig`: the `AUTO`, `NONE`, and `ANY` modes
- Processing tier via `ServiceTier`, in ADK Go v2.4.0 and higher
- Request timeout via `HTTPOptions.Timeout`, in ADK Go v2.4.0 and higher
## Limitations
- **No audio or video input** — ADK Go returns an error for these media types.
- **Function tools only** — built-in tools (Google Search, code execution, etc.) are not supported.
- **Structured output uses OpenAI strict mode** — every field declared in an `OutputSchema` is treated as required.
- **Function tools don't use strict mode** — In ADK Go v2.5.0 and higher, ADK sends function tools with OpenAI strict schema validation off. Optional parameters stay optional, but the model's tool-call arguments might not match the declared schema. Validate tool arguments before acting on them.
- **Reasoning isn't carried across turns** — In ADK Go v2.5.0 and higher, ADK doesn't send reasoning from earlier turns back to the model on either API, so reasoning informs only the turn that produced it.
- Some `GenerateContentConfig` options return an error rather than being silently ignored: `TopK`, multiple candidates, request labels, and safety settings. The Responses API also rejects stop sequences, frequency/presence penalties, and seed, which the [Chat Completions API](#chat-completions-api) supports. In ADK Go v2.4.0 and higher, other settings that have no OpenAI equivalent also return an error. The [Go API reference](https://pkg.go.dev/google.golang.org/adk/v2/model/openaimodel) lists which settings are sent, rejected, or ignored.
## Configuration options
The `ClientConfig` provides several options for configuring the client:
- `APIKey`: Your OpenAI API key.
- `BaseURL`: Custom endpoint URL, which can be useful for OpenAI-compatible endpoints.
- `HTTPClient`: A custom `*http.Client`.
- `Options`: Advanced `openai-go` request options (`[]option.RequestOption`).
- `API`: The OpenAI API to call: `openaimodel.APIResponses` (the default) or `openaimodel.APIChatCompletions`. See [Chat Completions API](#chat-completions-api).
If `APIKey` or `BaseURL` are left empty, they will automatically fall back to the `OPENAI_API_KEY` and `OPENAI_BASE_URL` environment variables, handled by the default behavior of the underlying `openai-go` SDK.
## OpenAI model authentication
When using OpenAI models, you must provide an API key to authenticate with the OpenAI API. The most direct way to provide this information is to use environment variables or an `.env` file.
The `openaimodel` package also supports OpenAI-compatible endpoints (such as local models served via Ollama, LM Studio, or vLLM) by configuring the base URL. If the endpoint does not serve the Responses API, also set `API` to `openaimodel.APIChatCompletions`, as described in [Chat Completions API](#chat-completions-api).
```bash
# .env configuration file
OPENAI_API_KEY="PASTE_YOUR_OPENAI_API_KEY_HERE"
```
```bash
# .env configuration file
OPENAI_API_KEY="api-key-if-required"
OPENAI_BASE_URL="http://localhost:11434/v1" # example: local Ollama endpoint
```
# Route between models
Supported in ADKTypeScript v1.0.0Experimental
Experimental
Model routing is experimental and may change in future releases. We welcome your [feedback](https://github.com/google/adk-js/issues/new?template=feature_request.md)!
An `LlmAgent` uses a single model by default. When you need to dynamically select between different models for each request, you can define a routing function that chooses which model to use. `RoutedLlm` provides this capability, enabling model fallback on error, A/B testing between models, and auto-routing by input complexity. If the selected model fails before producing any output, the routing function is called again with error context so it can select a different model.
Pass a `RoutedLlm` as an `LlmAgent`'s `model` parameter. Use `RoutedLlm` when only the model varies between routes. If you also need to switch instructions, tools, or sub-agents, use [`RoutedAgent`](https://adk.tools-online.site/agents/routing/index.md) instead.
## How routing works
The `LlmRouter` function receives the map of available models and the current `LlmRequest`, and returns the key of the model to use:
```typescript
type LlmRouter = (
models: Readonly>,
request: LlmRequest,
errorContext?: { failedKeys: ReadonlySet; lastError: unknown },
) => Promise | string | undefined;
```
The `models` parameter accepts either a `Record` with explicit keys, or an array of `BaseLlm` instances. If an array is provided, each model's name is used as its key.
Failover follows the same rules as [`RoutedAgent`](https://adk.tools-online.site/agents/routing/#how-routing-works): the router is re-called with `errorContext` only if the selected model fails before yielding any response. After yielding, errors propagate without retry. The router can return `undefined` to stop retrying and propagate the last error.
**Live connections:** `RoutedLlm.connect()` selects the model at connection time. Once a live connection is established, the model cannot be switched mid-stream.
## Basic usage
The following example creates a `RoutedLlm` that tries a primary model first and falls back to a secondary model if the primary fails. The router checks `errorContext.failedKeys` to avoid re-selecting the failed model:
```typescript
import {
BaseLlm,
Gemini,
LlmRequest,
LlmAgent,
RoutedLlm,
InMemoryRunner,
} from '@google/adk';
const primaryModel = new Gemini({ model: 'gemini-flash-latest' });
const fallbackModel = new Gemini({ model: 'gemini-pro-latest' });
const router = (
models: Readonly>,
request: LlmRequest,
// errorContext is provided when a previously selected model fails
errorContext?: { failedKeys: ReadonlySet; lastError: unknown },
) => {
if (!errorContext) {
return 'primary'; // Try primary first
}
if (errorContext.failedKeys.has('primary')) {
return 'fallback'; // Fall back if primary failed
}
return undefined; // No more options, propagate the error
};
const routedLlm = new RoutedLlm({
models: { primary: primaryModel, fallback: fallbackModel },
router,
});
// Use RoutedLlm as the model for an LlmAgent
const agent = new LlmAgent({
name: 'my_agent',
model: routedLlm,
instruction: 'You are a helpful assistant.',
});
const runner = new InMemoryRunner({ agent, appName: 'my_app' });
const session = await runner.sessionService.createSession({
appName: 'my_app',
userId: 'user_1',
});
const run = runner.runAsync({
userId: 'user_1',
sessionId: session.id,
newMessage: { role: 'user', parts: [{ text: 'Hello!' }] },
});
for await (const event of run) {
if (event.content?.parts?.[0]?.text) {
console.log(event.content.parts[0].text);
}
}
```
# vLLM model host for ADK agents
Supported in ADKPython v0.1.0
Tools such as [vLLM](https://github.com/vllm-project/vllm) allow you to host models efficiently and serve them as an OpenAI-compatible API endpoint. You can use vLLM models through the [LiteLLM](/agents/models/litellm/) library for Python.
## Setup
1. **Deploy Model:** Deploy your chosen model using vLLM (or a similar tool). Note the API base URL (e.g., `https://your-vllm-endpoint.run.app/v1`).
- *Important for ADK Tools:* When deploying, ensure the serving tool supports and enables OpenAI-compatible tool/function calling. For vLLM, this might involve flags like `--enable-auto-tool-choice` and potentially a specific `--tool-call-parser`, depending on the model. Refer to the vLLM documentation on Tool Use.
1. **Authentication:** Determine how your endpoint handles authentication (e.g., API key, bearer token).
## Integration Example
The following example shows how to use a vLLM endpoint with ADK agents.
```python
import subprocess
from google.adk.agents import LlmAgent
from google.adk.models.lite_llm import LiteLlm
# --- Example Agent using a Gemma 4 model hosted on a vLLM endpoint ---
# Endpoint URL provided by your vLLM deployment
api_base_url = "https://your-vllm-endpoint.run.app/v1"
# Model name as recognized by *your* vLLM endpoint configuration
model_name_at_endpoint = "hosted_vllm/google/gemma-4-E4B-it" # Example from vllm_test.py
# Authentication (Example: using gcloud identity token for a Cloud Run deployment)
# Adapt this based on your endpoint's security
try:
gcloud_token = subprocess.check_output(
["gcloud", "auth", "print-identity-token", "-q"]
).decode().strip()
auth_headers = {"Authorization": f"Bearer {gcloud_token}"}
except Exception as e:
print(f"Warning: Could not get gcloud token - {e}. Endpoint might be unsecured or require different auth.")
auth_headers = None # Or handle error appropriately
agent_vllm = LlmAgent(
model=LiteLlm(
model=model_name_at_endpoint,
api_base=api_base_url,
# This extra_body values specific to Gemma 4.
extra_body={
"chat_template_kwargs": {
"enable_thinking": True # Enable thinking
},
"skip_special_tokens": False # Should be set to False
},
# Pass authentication headers if needed
extra_headers=auth_headers,
# Alternatively, if endpoint uses an API key:
# api_key="YOUR_ENDPOINT_API_KEY"
),
name="vllm_agent",
instruction="You are a helpful assistant running on a self-hosted vLLM endpoint.",
# ... other agent parameters
)
```
# Google Gemini models for ADK agents
Supported in ADKPython v0.1.0TypeScript v0.2.0Go v0.1.0Java v0.2.0Kotlin v0.1.0
ADK supports the Google Gemini family of generative AI models that provide a powerful set of models with a wide range of features. ADK provides support for many Gemini features, including [Code Execution](/integrations/code-execution/), [Google Search](/integrations/google-search/), [Context caching](/context/caching/), [Computer use](/integrations/computer-use/) and the [Interactions API](#interactions-api).
## Get started
The following code examples show a basic implementation for using Gemini models in your agents:
```python
from google.adk.agents import LlmAgent
# --- Example using a stable Gemini Flash model ---
agent_gemini_flash = LlmAgent(
# Use the latest stable Flash model identifier
model="gemini-flash-latest",
name="gemini_flash_agent",
instruction="You are a fast and helpful Gemini assistant.",
# ... other agent parameters
)
```
```typescript
import {LlmAgent} from '@google/adk';
// --- Example #2: using a powerful Gemini Pro model with API Key in model ---
export const rootAgent = new LlmAgent({
name: 'hello_time_agent',
model: 'gemini-flash-latest',
description: 'Gemini flash agent',
instruction: `You are a fast and helpful Gemini assistant.`,
});
```
```go
import (
"google.golang.org/adk/v2/agent/llmagent"
"google.golang.org/adk/v2/model/gemini"
"google.golang.org/genai"
)
// --- Example using a stable Gemini Flash model ---
modelFlash, err := gemini.NewModel(ctx, "gemini-2.0-flash", &genai.ClientConfig{})
if err != nil {
log.Fatalf("failed to create model: %v", err)
}
agentGeminiFlash, err := llmagent.New(llmagent.Config{
// Use the latest stable Flash model identifier
Model: modelFlash,
Name: "gemini_flash_agent",
Instruction: "You are a fast and helpful Gemini assistant.",
// ... other agent parameters
})
if err != nil {
log.Fatalf("failed to create agent: %v", err)
}
```
```java
// --- Example #1: using a stable Gemini Flash model with ENV variables---
LlmAgent agentGeminiFlash =
LlmAgent.builder()
// Use the latest stable Flash model identifier
.model("gemini-flash-latest") // Set ENV variables to use this model
.name("gemini_flash_agent")
.instruction("You are a fast and helpful Gemini assistant.")
// ... other agent parameters
.build();
```
```kotlin
import com.google.adk.kt.agents.Instruction
import com.google.adk.kt.agents.LlmAgent
import com.google.adk.kt.models.Gemini
// --- Example using a stable Gemini Flash model ---
val agentGeminiFlash = LlmAgent(
// Use the latest stable Flash model identifier
name = "gemini_flash_agent",
model = Gemini(name = "gemini-flash-latest"),
instruction = Instruction("You are a fast and helpful Gemini assistant."),
// ... other agent parameters
)
```
Note: Gemini model selector `gemini-flash-latest`
Most code examples in ADK documentation use `gemini-flash-latest` to select the [latest available](https://ai.google.dev/gemini-api/docs/models#latest) Gemini Flash version. However, if you access Gemini from a regional endpoint, such as `us-central1`, this selection string may not work. In that case, use a specific model version string from the [Gemini models](https://ai.google.dev/gemini-api/docs/models) page or Google Cloud [Gemini models](https://docs.cloud.google.com/vertex-ai/generative-ai/docs/models) list.
## Gemini model authentication
When using an AI model through a service, such as the Gemini API or Gemini Enterprise Agent Platform on Google Cloud, you must provide an API key or authenticate with the service. The most direct way to provide this information is to use environment variables or an `.env` file. The following examples show the most common way to configure an agent for use with the Gemini API or Gemini Enterprise Agent Platform.
```text
# .env configuration file
GOOGLE_API_KEY="PASTE_YOUR_GEMINI_API_KEY_HERE"
```
```text
# .env configuration file
GOOGLE_CLOUD_PROJECT=your-project-id
GOOGLE_CLOUD_LOCATION=location-code # example: us-central1
GOOGLE_GENAI_USE_ENTERPRISE=True
```
For more details on connecting ADK agents to Google Cloud hosted models and services, including Gemini Enterprise Agent Platform, see the [Connect to Google Cloud and Agent Platform](/get-started/google-cloud/) guide.
## Voice and video streaming support
In order to use voice/video streaming in ADK, you will need to use Gemini models that support the Live API. You can find the **model ID(s)** that support the Gemini Live API in the documentation:
- [Google AI Studio: Gemini Live API](https://ai.google.dev/gemini-api/docs/models#live-api)
- [Agent Platform: Gemini Live API](https://cloud.google.com/vertex-ai/generative-ai/docs/live-api)
## Gemini Interactions API
Supported in ADKPython v1.21.0
The Gemini [Interactions API](https://ai.google.dev/gemini-api/docs/interactions) is an alternative to the ***generateContent*** inference API, which provides stateful conversation capabilities, allowing you to chain interactions using a `previous_interaction_id` instead of sending the full conversation history with each request. Using this feature can be more efficient for long conversations.
You can enable the Interactions API by setting the `use_interactions_api=True` parameter in the Gemini model configuration, as shown in the following code snippet:
```python
from google.adk.agents.llm_agent import Agent
from google.adk.models.google_llm import Gemini
from google.adk.tools.google_search_tool import GoogleSearchTool
root_agent = Agent(
model=Gemini(
model="gemini-flash-latest",
use_interactions_api=True, # Enable Interactions API
),
name="interactions_test_agent",
tools=[
GoogleSearchTool(bypass_multi_tools_limit=True), # Converted to function tool
get_current_weather, # Custom function tool
],
)
```
For a complete code sample, see the [Interactions API sample](https://github.com/google/adk-python/tree/main/contributing/samples/models/interactions_api).
Enabling the Interactions API also lets you choose a serving tier for each run. For details on queuing model calls to run on off-peak capacity, see [Deferred scheduling](https://adk.tools-online.site/agents/models/google-gemini/deferred-schedule/index.md).
### Known limitations
The Interactions API **does not** support mixing custom function calling tools with built-in tools, such as the [Google Search](/integrations/google-search/), tool, within the same agent. You can work around this limitation by configuring the built-in tool to operate as a custom tool using the `bypass_multi_tools_limit` parameter:
```python
# Use bypass_multi_tools_limit=True to convert google_search to a function tool
GoogleSearchTool(bypass_multi_tools_limit=True)
```
In this example, this option converts the built-in `google_search` to a function calling tool (via `GoogleSearchAgentTool`), which allows it to work alongside custom function tools.
## Troubleshooting
### Error Code 429 - RESOURCE_EXHAUSTED
This error usually happens if the number of your requests exceeds the capacity allocated to process requests.
To mitigate this, you can do one of the following:
1. Request higher quota limits for the model you are trying to use.
1. Enable client-side retries. Retries allow the client to automatically retry the request after a delay, which can help if the quota issue is temporary.
There are two ways you can set retry options:
**Option 1:** Set retry options on the Agent as a part of `generate_content_config`.
You would use this option if you are passing the model as a name string and letting ADK create the model adapter for you.
```python
from google.genai import types
# ...
root_agent = Agent(
model='gemini-flash-latest',
# ...
generate_content_config=types.GenerateContentConfig(
# ...
http_options=types.HttpOptions(
# ...
retry_options=types.HttpRetryOptions(initial_delay=1, attempts=2),
# ...
),
# ...
),
)
```
```java
import com.google.adk.agents.LlmAgent;
import com.google.genai.types.GenerateContentConfig;
import com.google.genai.types.HttpOptions;
import com.google.genai.types.HttpRetryOptions;
// ...
LlmAgent rootAgent = LlmAgent.builder()
.model("gemini-flash-latest")
// ...
.generateContentConfig(GenerateContentConfig.builder()
// ...
.httpOptions(HttpOptions.builder()
// ...
.retryOptions(HttpRetryOptions.builder().initialDelay(1.0).attempts(2).build())
// ...
.build())
// ...
.build())
.build();
```
**Option 2:** Retry options on this model adapter.
You would use this option if you were instantiating the instance of adapter by yourself.
```python
from google.genai import types
# ...
agent = Agent(
model=Gemini(
retry_options=types.HttpRetryOptions(initial_delay=1, attempts=2),
)
)
```
```java
import com.google.adk.agents.LlmAgent;
import com.google.adk.models.Gemini;
import com.google.genai.Client;
import com.google.genai.types.HttpOptions;
import com.google.genai.types.HttpRetryOptions;
// ...
LlmAgent agent = LlmAgent.builder()
.model(Gemini.builder()
.modelName("gemini-flash-latest")
.apiClient(Client.builder()
.httpOptions(HttpOptions.builder()
.retryOptions(HttpRetryOptions.builder().initialDelay(1.0).attempts(2).build())
.build())
.build())
.build())
.build();
```
In Kotlin, you can achieve this by creating the `Client` instance yourself and passing it to the `Gemini` constructor.
```kotlin
import com.google.adk.kt.agents.LlmAgent
import com.google.adk.kt.models.Gemini
import com.google.genai.Client
import com.google.genai.types.HttpOptions
import com.google.genai.types.HttpRetryOptions
val client = Client.builder()
.apiKey("YOUR_API_KEY")
.httpOptions(HttpOptions.builder()
.retryOptions(HttpRetryOptions.builder().initialDelay(1.0).attempts(2).build())
.build())
.build()
val model = Gemini(client = client, name = "gemini-flash-latest")
val agent = LlmAgent(
name = "my_agent",
model = model
// ...
)
```
# Deferred scheduling with Gemini models
Supported in ADKPython v2.10.0
Agent workloads have different latency needs. An interactive assistant must answer immediately, but a summarization job, a bulk evaluation run, or a document-processing pipeline can wait for capacity. Deferred scheduling lets ADK agents queue the model calls for those workloads to run on off-peak capacity at a [discounted rate](https://cloud.google.com/gemini-enterprise-agent-platform/generative-ai/pricing), instead of competing for interactive capacity.
You can request deferred scheduling per run, using the `service_tier` setting of `RunConfig`. The setting is part of the run configuration rather than the model or the agent, so one agent definition can serve both interactive requests and batch workloads.
## Get started
Deferred scheduling requires a Gemini model configured with the Google Cloud for Gemini [Interactions API](https://adk.tools-online.site/agents/models/google-gemini/#interactions-api). Set `use_interactions_api=True` on the model, then pass `RunConfig(service_tier=ServiceTier.DEFERRED)` when you run the agent, as shown in the following example:
```python
import asyncio
from google.adk.agents import LlmAgent
from google.adk.agents import RunConfig
from google.adk.apps import App
from google.adk.models import ServiceTier
from google.adk.models.google_llm import Gemini
from google.adk.runners import InMemoryRunner
from google.genai import types
root_agent = LlmAgent(
name='batch_agent',
model=Gemini(
model='gemini-flash-latest',
use_interactions_api=True, # Required for deferred scheduling
),
instruction='Process input documents and produce summaries.',
)
app = App(name='batch_app', root_agent=root_agent)
runner = InMemoryRunner(app=app)
async def main() -> None:
session = await runner.session_service.create_session(
app_name=app.name,
user_id='user_123',
session_id='session_456',
)
# Request off-peak capacity for every model call in this run.
run_config = RunConfig(service_tier=ServiceTier.DEFERRED)
async for event in runner.run_async(
user_id='user_123',
session_id=session.id,
new_message=types.Content(
role='user',
parts=[types.Part.from_text(
text='Summarize quarterly performance metrics.'
)],
),
run_config=run_config,
):
if event.content and event.content.parts:
for part in event.content.parts:
if part.text:
print(part.text)
asyncio.run(main())
```
The runner submits the model call, waits for the queued work to finish, and then yields the response event. Your code consumes events exactly as it does for a standard run.
The run appears to hang while it waits
The `run_async()` method yields no events while the request sits in the queue. How long that takes depends on backend load, and ADK applies no upper bound. In a web UI or a terminal, the application appears unresponsive during the delay. Show a progress indicator, or set a [client deadline](#set-a-client-side-deadline).
To confirm that the tier took effect, check your application logs for these messages:
| Log message | Level | Meaning |
| ------------------------------------------------------------ | --------- | -------------------------------------------------------------- |
| `Using service_tier from run_config: deferred` | `DEBUG` | ADK applied the tier to the request. |
| `Interaction is queued; waiting for the result.` | `INFO` | The backend accepted the work into the queue. |
| `Interaction reached status completed.` | `INFO` | The result is ready. |
| `run_config.service_tier=... has no effect for agent ` | `WARNING` | ADK dropped the tier. See [Troubleshooting](#troubleshooting). |
## How deferred scheduling works
Standard model requests are synchronous. ADK sends the request and the model returns a response on the same connection. When you enable deferred scheduling by setting `ServiceTier.DEFERRED`, ADK marks the request for background execution and the backend queues it, returning an interaction ID immediately instead of a result.
ADK then waits for that result, checking the queued work with exponential backoff and absorbing transient read failures until it reaches a final status. It converts that result to a normal response event and yields it. If 5 consecutive reads fail, ADK raises the error from the last read. This loop is internal: it does not surface interaction IDs, and you do not need to write any retrieval code. It does add a few seconds of polling delay on top of the queue wait, so deferred scheduling should not be used for short, latency-sensitive calls.
The following properties of the wait affect how you design your agent:
- **ADK sets no client-side deadline.** The backend's completion timeout on the interaction is the only bound on the wait. To stop sooner, see [Set a client-side deadline](#set-a-client-side-deadline).
- **Each model turn queues separately.** In an agent that calls tools, every turn creates its own interaction, so total latency is the sum of every turn's queue wait rather than a single wait for the run.
## Configuration options
The `service_tier` setting of `RunConfig` selects the capacity pool for every model call in a run:
| Option | Type | Default | Description |
| -------------- | ------------------------------ | ------- | --------------------------------------------- |
| `service_tier` | `Optional[ServiceTier \| str]` | `None` | Serving tier for the model calls of this run. |
The `ServiceTier` enum defines the following tiers:
- `ServiceTier.DEFERRED`: Queues the call to run on off-peak capacity. The call waits for room instead of failing when capacity is limited. No other tier changes how ADK executes the request, and it cannot be combined with streaming.
- `ServiceTier.FLEX`: Best-effort capacity at a lower cost, with no latency guarantee.
- `ServiceTier.STANDARD`: The default tier.
- `ServiceTier.PRIORITY`: Reserved capacity for latency-sensitive calls.
Leaving `service_tier` unset omits the field from the request entirely, which is equivalent to `ServiceTier.STANDARD`. The `ServiceTier` enum subclasses `str`, so you can pass a plain string such as `'deferred'` in place of an enum member. The string form also lets you use a tier the backend supports before ADK defines a constant for it.
## Advanced usage
The following sections describe how to bound the wait time for a deferred run with a client-side deadline and how to request deferred scheduling when serving an agent over HTTP.
### Set a client-side deadline
A deferred request waits for off-peak capacity, and its duration depends on backend load. To cap the total elapsed time, wrap the run in an `asyncio.timeout()`, which requires Python 3.11 or later:
```python
import asyncio
import logging
from google.adk.agents import RunConfig
from google.adk.models import ServiceTier
from google.genai import types
logger = logging.getLogger(__name__)
# Continues from the Get started example, reusing runner and session.
message = types.Content(
role='user',
parts=[types.Part.from_text(text='Generate a quarterly summary.')],
)
run_config = RunConfig(service_tier=ServiceTier.DEFERRED)
async def run_with_deadline(runner, session) -> None:
try:
async with asyncio.timeout(300):
async for event in runner.run_async(
user_id='user_123',
session_id=session.id,
new_message=message,
run_config=run_config,
):
if event.content and event.content.parts:
for part in event.content.parts:
if part.text:
print(part.text)
except TimeoutError:
logger.error('Deferred run exceeded the 300 second client deadline.')
```
Client deadlines do not cancel requests
Timing out stops ADK from polling for the result, but does not stop the backend. The queued request runs to completion and consumes billed usage, and you cannot retrieve its output afterward. Treat a timed-out turn as forfeited, and do not use a client deadline to limit usage.
### Request deferred scheduling over HTTP
An agent you serve with `adk api_server` accepts `service_tier` in the request body of the `/run` and `/run_sse` endpoints:
```json
{
"app_name": "batch_app",
"user_id": "user_123",
"session_id": "session_456",
"new_message": {
"role": "user",
"parts": [{"text": "Summarize batch results."}]
},
"service_tier": "deferred"
}
```
For `/run_sse` requests, you must also set `"streaming": false`. Combining `"service_tier": "deferred"` with `"streaming": true` returns HTTP 422. An HTTP request holds the connection open for the whole queue wait, which makes the hosting platform's request timeout the effective limit on a deferred run:
- **Raise proxy and ingress timeouts.** Load balancers and ingress controllers close long-running backend connections by default. Check the limits for your platform, such as the [Cloud Run request timeout](https://cloud.google.com/run/docs/configuring/request-timeout), and raise them to cover your expected queue wait.
- **A client disconnect cancels retrieval, not execution.** When the connection closes, the server cancels its polling task. The queued request still runs and still consumes billed usage, and you lose its output.
For deferred workloads that may wait a long time, deploy to [Agent Runtime](/deploy/agent-runtime/) on Google Cloud Agent Platform instead. Agent Runtime runs the invocation in a managed container and does not hold an HTTP connection open for it.
## Limitations
The following limitations apply to deferred scheduling:
- **Gemini and the Interactions API only:** Deferred scheduling works only with a `Gemini` model that sets `use_interactions_api=True`. Any other model, including a custom `BaseLlm` subclass, ignores the tier.
- **Not compatible with streaming:** Constructing `RunConfig(service_tier=ServiceTier.DEFERRED, streaming_mode=StreamingMode.SSE)` raises a `pydantic.ValidationError`.
- **`ManagedAgent` class ignores the tier:** It runs its own interaction loop and never reads `service_tier`.
- **No resumption across restarts:** ADK does not persist in-flight interaction IDs, and offers no way to reattach a run to a queued interaction. If the client process stops, ADK abandons the pending work and the next run creates a new interaction.
## Troubleshooting
The following sections describe some common issues when using deferred scheduling, and how to resolve them.
### ADK ignores the tier and the run still succeeds
If the agent's model is not a `Gemini` instance with `use_interactions_api=True`, ADK drops the tier, logs a warning once per run, and executes the call on standard capacity. The run succeeds, so the log is the only signal:
```text
run_config.service_tier=... has no effect for agent : its model does not
use the interactions API, which is the only path with a serving tier. Set
use_interactions_api=True on the model to apply the tier.
```
The warning is intentional. In a multi-agent run, only some agents may be on the Interactions API, so an unusable tier warns rather than raises. Search your logs for `has no effect for agent` to find models that need `use_interactions_api=True`.
### OpenAI `service_tier` field
The `OpenAIResponsesLlm` class also has a `service_tier` field. It is an unrelated setting: you set it on the model rather than on the run, and `RunConfig.service_tier` does not feed it. Setting one has no effect on the other.
## Additional resources
- [Autonomous agent scheduling](https://docs.cloud.google.com/gemini-enterprise-agent-platform/scale/efficiency/autonomous-scheduling)
- [Gemini Interactions API](https://adk.tools-online.site/agents/models/google-gemini/#interactions-api)
- [Runtime Configuration](/runtime/runconfig/)
- [Interactions API code sample](https://github.com/google/adk-python/tree/main/contributing/samples/models/interactions_api)
# Template agent workflows
Supported in ADKPython v0.1.0TypeScript v0.2.0Go v0.1.0Java v0.1.0
This section introduces *template workflows*, also known as *workflow agents*, which are specialized agents that control the execution flow of one or more sub-agents. Template workflow agents are specialized components designed for orchestrating the execution flow of sub-agents. Their primary role is to manage how and when other agents run, defining the control flow of a process.
Deprecated: Use graph-based workflows
In ADK Python, TypeScript, Go, and Kotlin, template workflows have been superseded by more flexible workflow structures, including [graph-based workflows](/graphs/) and [dynamic workflows](/graphs/dynamic/). These workflow architectures provide more control, flexibility and capability to evolve your agent workflows over time.
**Figure 1.** Execution patterns of template workflows in ADK
Template workflow agents operate based on predefined logic. They determine the execution sequence according to their type, such as sequential, parallel, or loop, without consulting an AI model for assistance with the orchestration. This approach results in deterministic and predictable execution patterns. Template workflows include the following task execution structures, which each implement a distinct task completion pattern:
- **Sequential Agent workflow**
______________________________________________________________________
Executes sub-agents one after another, in sequence.
[Learn more](https://adk.tools-online.site/agents/workflow-agents/sequential-agents/index.md)
- **Loop Agent workflow**
______________________________________________________________________
Repeatedly executes its sub-agents until a specific termination condition is met.
[Learn more](https://adk.tools-online.site/agents/workflow-agents/loop-agents/index.md)
- **Parallel Agent workflow**
______________________________________________________________________
Executes multiple sub-agents in parallel.
[Learn more](https://adk.tools-online.site/agents/workflow-agents/parallel-agents/index.md)
# Loop template workflow agent
Supported in ADKPython v0.1.0TypeScript v0.2.0Go v0.1.0Java v0.2.0Kotlin v0.1.0
The ***LoopAgent*** class is a [template workflow](/agents/workflow-agents/) agent that executes its sub-agents in a loop for a specified number of iterations or until a termination condition is met. Use the ***LoopAgent*** when your workflow involves repetition or iterative refinement, such as revising code or a document. As with other templated workflows, the execution of a ***LoopAgent*** object is not controlled by an AI model, and is deterministic in how it executes its sub-agents. The sub-agents within the defined loop may or may not utilize AI models, but the overall execution of those sub-agents is ultimately managed by the ***LoopAgent*** object you define.
Deprecated: Use graph-based workflows
In ADK Python, TypeScript, Go, and Kotlin, template workflows have been superseded by more flexible workflow structures, including [graph-based workflows](/graphs/) and [dynamic workflows](/graphs/dynamic/).
### Example scenario
You want to build an agent that can generate images of food, but sometimes when you want to generate a specific number of items, such as bananas, the agent generates a different number of those items in the image, such as an image of 7 bananas. You have two tools: `Generate Image`, `Count Food Items`. If your goal is to keep generating images until it either correctly generates the specified number of items, or after a certain number of iterations, you can build your agent using a ***LoopAgent*** workflow.
### How it Works
When the `LoopAgent`'s `Run Async` method is called, it performs the following actions:
1. **Sub-Agent Execution:** It iterates through the Sub Agents list *in order*. For *each* sub-agent, it calls the agent's `Run Async` method.
1. **Termination Check:**
*Crucially*, the `LoopAgent` itself does *not* inherently decide when to stop looping. You *must* implement a termination mechanism to prevent infinite loops. Common strategies include:
- **Max Iterations**: Set a maximum number of iterations in the `LoopAgent`. **The loop will terminate after that many iterations**. In Python, `max_iterations` defaults to `None`, which means the loop only stops on escalation.
- **Escalation from sub-agent**: Design one or more sub-agents to evaluate a condition (e.g., "Is the document quality good enough?", "Has a consensus been reached?"). If the condition is met, the sub-agent signals termination by emitting an event whose actions set `escalate` to true. In Python, a custom agent yields an `Event` with `actions=EventActions(escalate=True)`, while a callback sets `callback_context.actions.escalate = True` and a tool sets `tool_context.actions.escalate = True`. The built-in `exit_loop` tool sets that flag for you.
### Full Example: Iterative Document Improvement
Imagine a scenario where you want to iteratively improve a document:
- **Writer Agent:** An `LlmAgent` that generates or refines a draft on a topic.
- **Critic Agent:** An `LlmAgent` that critiques the draft, identifying areas for improvement.
```py
LoopAgent(name="RefinementLoop", sub_agents=[WriterAgent, CriticAgent], max_iterations=5)
```
In this setup, the `LoopAgent` would manage the iterative process. The `CriticAgent` could be **given an escalation tool such as `exit_loop`, which it calls once the document reaches a satisfactory quality level**, preventing further iterations. Alternatively, the `max_iterations` parameter could be used to limit the process to a fixed number of cycles, or external logic could be implemented to make stop decisions. The **loop would run at most five times**, ensuring the iterative refinement doesn't continue indefinitely.
Full Code
````py
from google.adk.agents import LoopAgent, LlmAgent, SequentialAgent
from google.adk.tools.tool_context import ToolContext
from google.adk.agents.callback_context import CallbackContext
# --- Constants ---
GEMINI_MODEL = "gemini-2.5-flash"
# --- State Keys ---
STATE_CURRENT_DOC = "current_document"
STATE_CRITICISM = "criticism"
# Define the exact phrase the Critic should use to signal completion
COMPLETION_PHRASE = "No major issues found."
# --- Tool Definition ---
def exit_loop(tool_context: ToolContext):
"""Call this function ONLY when the critique indicates no further changes are needed, signaling the iterative process should end."""
print(f" [Tool Call] exit_loop triggered by {tool_context.agent_name}")
tool_context.actions.escalate = True
tool_context.actions.skip_summarization = True
# Return empty dict as tools should typically return JSON-serializable output
return {}
# --- Before Agent Callback ---
def update_initial_topic_state(callback_context: CallbackContext):
"""Ensure 'initial_topic' is set in state before pipeline starts."""
callback_context.state['initial_topic'] = callback_context.state.get('initial_topic', 'a robot developing unexpected emotions')
# --- Agent Definitions ---
# STEP 1: Initial Writer Agent (Runs ONCE at the beginning)
initial_writer_agent = LlmAgent(
name="InitialWriterAgent",
model=GEMINI_MODEL,
include_contents='none',
instruction=f"""
You are a Creative Writing Assistant tasked with starting a story.
Write a *very basic* first draft of a short story (just 1-2 simple sentences).
Keep it plain and minimal - do NOT add descriptive language yet.
Topic: {{initial_topic}}
Output *only* the story/document text. Do not add introductions or explanations.
""",
description="Writes the initial document draft based on the topic, aiming for some initial substance.",
output_key=STATE_CURRENT_DOC
)
# STEP 2a: Critic Agent (Inside the Refinement Loop)
critic_agent_in_loop = LlmAgent(
name="CriticAgent",
model=GEMINI_MODEL,
include_contents='none',
instruction=f"""
You are a Constructive Critic AI reviewing a short story draft.
**Document to Review:**
```
{{current_document}}
```
**Completion Criteria (ALL must be met):**
1. At least 4 sentences long
2. Has a clear beginning, middle, and end
3. Includes at least one descriptive detail (sensory or emotional)
**Task:**
Check the document against the criteria above.
IF any criteria is NOT met, provide specific feedback on what to add or improve.
Output *only* the critique text.
IF ALL criteria are met, respond *exactly* with: "{COMPLETION_PHRASE}"
""",
description="Reviews the current draft, providing critique if clear improvements are needed, otherwise signals completion.",
output_key=STATE_CRITICISM
)
# STEP 2b: Refiner/Exiter Agent (Inside the Refinement Loop)
refiner_agent_in_loop = LlmAgent(
name="RefinerAgent",
model=GEMINI_MODEL,
# Relies solely on state via placeholders
include_contents='none',
instruction=f"""
You are a Creative Writing Assistant refining a document based on feedback OR exiting the process.
**Current Document:**
```
{{current_document}}
```
**Critique/Suggestions:**
{{criticism}}
**Task:**
Analyze the 'Critique/Suggestions'.
IF the critique is *exactly* "{COMPLETION_PHRASE}":
You MUST call the 'exit_loop' function. Do not output any text.
ELSE (the critique contains actionable feedback):
Carefully apply the suggestions to improve the 'Current Document'. Output *only* the refined document text.
Do not add explanations. Either output the refined document OR call the exit_loop function.
""",
description="Refines the document based on critique, or calls exit_loop if critique indicates completion.",
tools=[exit_loop], # Provide the exit_loop tool
output_key=STATE_CURRENT_DOC # Overwrites state['current_document'] with the refined version
)
# STEP 2: Refinement Loop Agent
refinement_loop = LoopAgent(
name="RefinementLoop",
# Agent order is crucial: Critique first, then Refine/Exit
sub_agents=[
critic_agent_in_loop,
refiner_agent_in_loop,
],
max_iterations=5 # Limit loops
)
# STEP 3: Overall Sequential Pipeline
# For ADK tools compatibility, the root agent must be named `root_agent`
root_agent = SequentialAgent(
name="IterativeWritingPipeline",
sub_agents=[
initial_writer_agent, # Run first to create initial doc
refinement_loop # Then run the critique/refine loop
],
before_agent_callback=update_initial_topic_state, # set initial topic in state
description="Writes an initial document and then iteratively refines it with critique using an exit tool."
)
````
```typescript
// Part of agent.ts --> Follow https://adk.tools-online.site/get-started/ to learn the setup
import { LoopAgent, LlmAgent, SequentialAgent, FunctionTool } from '@google/adk';
import { z } from 'zod';
// --- Constants ---
const GEMINI_MODEL = "gemini-2.5-flash";
const STATE_INITIAL_TOPIC = "initial_topic";
// --- State Keys ---
const STATE_CURRENT_DOC = "current_document";
const STATE_CRITICISM = "criticism";
// Define the exact phrase the Critic should use to signal completion
const COMPLETION_PHRASE = "No major issues found.";
// --- Tool Definition ---
const exitLoopTool = new FunctionTool({
name: 'exit_loop',
description: 'Call this function ONLY when the critique indicates no further changes are needed, signaling the iterative process should end.',
parameters: z.object({}),
execute: (input, context) => {
if (context) {
console.log(` [Tool Call] exit_loop triggered by ${context.agentName} with input: ${input}`);
context.actions.escalate = true;
}
return {};
},
});
// --- Agent Definitions ---
// STEP 1: Initial Writer Agent (Runs ONCE at the beginning)
const initialWriterAgent = new LlmAgent({
name: "InitialWriterAgent",
model: GEMINI_MODEL,
includeContents: 'none',
// MODIFIED Instruction: Ask for a slightly more developed start
instruction: `You are a Creative Writing Assistant tasked with starting a story.
Write the *first draft* of a short story (aim for 2-4 sentences).
Base the content *only* on the topic provided below. Try to introduce a specific element (like a character, a setting detail, or a starting action) to make it engaging.
Topic: {{${STATE_INITIAL_TOPIC}}}
Output *only* the story/document text. Do not add introductions or explanations.
`,
description: "Writes the initial document draft based on the topic, aiming for some initial substance.",
outputKey: STATE_CURRENT_DOC
});
// STEP 2a: Critic Agent (Inside the Refinement Loop)
const criticAgentInLoop = new LlmAgent({
name: "CriticAgent",
model: GEMINI_MODEL,
includeContents: 'none',
// MODIFIED Instruction: More nuanced completion criteria, look for clear improvement paths.
instruction: `You are a Constructive Critic AI reviewing a short document draft (typically 2-6 sentences). Your goal is balanced feedback.
**Document to Review:**
{{current_document}}
**Task:**
Review the document for clarity, engagement, and basic coherence according to the initial topic (if known).
IF you identify 1-2 *clear and actionable* ways the document could be improved to better capture the topic or enhance reader engagement (e.g., "Needs a stronger opening sentence", "Clarify the character's goal"):
Provide these specific suggestions concisely. Output *only* the critique text.
ELSE IF the document is coherent, addresses the topic adequately for its length, and has no glaring errors or obvious omissions:
Respond *exactly* with the phrase "${COMPLETION_PHRASE}" and nothing else. It doesn't need to be perfect, just functionally complete for this stage. Avoid suggesting purely subjective stylistic preferences if the core is sound.
Do not add explanations. Output only the critique OR the exact completion.
`,
description: "Reviews the current draft, providing critique if clear improvements are needed, otherwise signals completion.",
outputKey: STATE_CRITICISM
});
// STEP 2b: Refiner/Exiter Agent (Inside the Refinement Loop)
const refinerAgentInLoop = new LlmAgent({
name: "RefinerAgent",
model: GEMINI_MODEL,
// Relies solely on state via placeholders
includeContents: 'none',
instruction: `You are a Creative Writing Assistant refining a document based on feedback OR exiting the process.
**Current Document:**
{{current_document}}
**Critique/Suggestions:**
{{criticism}}
**Task:**
Analyze the 'Critique/Suggestions'.
IF the critique is *exactly* "${COMPLETION_PHRASE}":
You MUST call the 'exit_loop' function. Do not output any text.
ELSE (the critique contains actionable feedback):
Carefully apply the suggestions to improve the 'Current Document'. Output *only* the refined document text.
Do not add explanations. Either output the refined document OR call the exit_loop function.
`,
tools: [exitLoopTool],
description: "Refines the document based on critique, or calls exit_loop if critique indicates completion.",
outputKey: STATE_CURRENT_DOC
});
// STEP 2: Refinement Loop Agent
const refinementLoop = new LoopAgent({
name: "RefinementLoop",
// Agent order is crucial: Critique first, then Refine/Exit
subAgents: [
criticAgentInLoop,
refinerAgentInLoop,
],
maxIterations: 5 // Limit loops
});
// STEP 3: Overall Sequential Pipeline
// For ADK tools compatibility, the root agent must be named `root_agent`
export const rootAgent = new SequentialAgent({
name: "IterativeWritingPipeline",
subAgents: [
initialWriterAgent, // Run first to create initial doc
refinementLoop // Then run the critique/refine loop
],
description: "Writes an initial document and then iteratively refines it with critique using an exit tool."
});
```
```go
// ExitLoopArgs defines the (empty) arguments for the ExitLoop tool.
type ExitLoopArgs struct{}
// ExitLoopResults defines the output of the ExitLoop tool.
type ExitLoopResults struct{}
// ExitLoop is a tool that signals the loop to terminate by setting Escalate to true.
func ExitLoop(ctx agent.Context, input ExitLoopArgs) (ExitLoopResults, error) {
fmt.Printf("[Tool Call] exitLoop triggered by %s \n", ctx.AgentName())
ctx.Actions().Escalate = true
return ExitLoopResults{}, nil
}
func main() {
ctx := context.Background()
if err := runAgent(ctx, "Write a document about a cat"); err != nil {
log.Fatalf("Agent execution failed: %v", err)
}
}
func runAgent(ctx context.Context, prompt string) error {
model, err := gemini.NewModel(ctx, modelName, &genai.ClientConfig{})
if err != nil {
return fmt.Errorf("failed to create model: %v", err)
}
// STEP 1: Initial Writer Agent (Runs ONCE at the beginning)
initialWriterAgent, err := llmagent.New(llmagent.Config{
Name: "InitialWriterAgent",
Model: model,
Description: "Writes the initial document draft based on the topic.",
Instruction: `You are a Creative Writing Assistant tasked with starting a story.
Write the *first draft* of a short story (aim for 2-4 sentences).
Base the content *only* on the topic provided in the user's prompt.
Output *only* the story/document text. Do not add introductions or explanations.`,
OutputKey: stateDoc,
})
if err != nil {
return fmt.Errorf("failed to create initial writer agent: %v", err)
}
// STEP 2a: Critic Agent (Inside the Refinement Loop)
criticAgentInLoop, err := llmagent.New(llmagent.Config{
Name: "CriticAgent",
Model: model,
Description: "Reviews the current draft, providing critique or signaling completion.",
Instruction: fmt.Sprintf(`You are a Constructive Critic AI reviewing a short document draft.
**Document to Review:**
"""
{%s}
"""
**Task:**
Review the document.
IF you identify 1-2 *clear and actionable* ways it could be improved:
Provide these specific suggestions concisely. Output *only* the critique text.
ELSE IF the document is coherent and addresses the topic adequately:
Respond *exactly* with the phrase "%s" and nothing else.`, stateDoc, donePhrase),
OutputKey: stateCrit,
})
if err != nil {
return fmt.Errorf("failed to create critic agent: %v", err)
}
exitLoopTool, err := functiontool.New(
functiontool.Config{
Name: "exitLoop",
Description: "Call this function ONLY when the critique indicates no further changes are needed.",
},
ExitLoop,
)
if err != nil {
return fmt.Errorf("failed to create exit loop tool: %v", err)
}
// STEP 2b: Refiner/Exiter Agent (Inside the Refinement Loop)
refinerAgentInLoop, err := llmagent.New(llmagent.Config{
Name: "RefinerAgent",
Model: model,
Instruction: fmt.Sprintf(`You are a Creative Writing Assistant refining a document based on feedback OR exiting the process.
**Current Document:**
"""
{%s}
"""
**Critique/Suggestions:**
{%s}
**Task:**
Analyze the 'Critique/Suggestions'.
IF the critique is *exactly* "%s":
You MUST call the 'exitLoop' function. Do not output any text.
ELSE (the critique contains actionable feedback):
Carefully apply the suggestions to improve the 'Current Document'. Output *only* the refined document text.`, stateDoc, stateCrit, donePhrase),
Description: "Refines the document based on critique, or calls exitLoop if critique indicates completion.",
Tools: []tool.Tool{exitLoopTool},
OutputKey: stateDoc,
})
if err != nil {
return fmt.Errorf("failed to create refiner agent: %v", err)
}
// STEP 2: Refinement Loop Agent
refinementLoop, err := loopagent.New(loopagent.Config{
AgentConfig: agent.Config{
Name: "RefinementLoop",
SubAgents: []agent.Agent{criticAgentInLoop, refinerAgentInLoop},
},
MaxIterations: 5,
})
if err != nil {
return fmt.Errorf("failed to create loop agent: %v", err)
}
// STEP 3: Overall Sequential Pipeline
iterativeWriterAgent, err := sequentialagent.New(sequentialagent.Config{
AgentConfig: agent.Config{
Name: appName,
SubAgents: []agent.Agent{initialWriterAgent, refinementLoop},
},
})
if err != nil {
return fmt.Errorf("failed to create sequential agent pipeline: %v", err)
}
```
````java
import static com.google.adk.agents.LlmAgent.IncludeContents.NONE;
import com.google.adk.agents.LlmAgent;
import com.google.adk.agents.LoopAgent;
import com.google.adk.agents.SequentialAgent;
import com.google.adk.events.Event;
import com.google.adk.runner.InMemoryRunner;
import com.google.adk.sessions.Session;
import com.google.adk.tools.Annotations.Schema;
import com.google.adk.tools.FunctionTool;
import com.google.adk.tools.ToolContext;
import com.google.genai.types.Content;
import com.google.genai.types.Part;
import io.reactivex.rxjava3.core.Flowable;
import java.util.Map;
public class LoopAgentExample {
// --- Constants ---
private static final String APP_NAME = "IterativeWritingPipeline";
private static final String USER_ID = "test_user_456";
private static final String MODEL_NAME = "gemini-2.0-flash";
// --- State Keys ---
private static final String STATE_CURRENT_DOC = "current_document";
private static final String STATE_CRITICISM = "criticism";
public static void main(String[] args) {
LoopAgentExample loopAgentExample = new LoopAgentExample();
loopAgentExample.runAgent("Write a document about a cat");
}
// --- Tool Definition ---
@Schema(
description =
"Call this function ONLY when the critique indicates no further changes are needed,"
+ " signaling the iterative process should end.")
public static Map exitLoop(@Schema(name = "toolContext") ToolContext toolContext) {
System.out.printf("[Tool Call] exitLoop triggered by %s \n", toolContext.agentName());
toolContext.actions().setEscalate(true);
// Return empty dict as tools should typically return JSON-serializable output
return Map.of();
}
// --- Agent Definitions ---
public void runAgent(String prompt) {
// STEP 1: Initial Writer Agent (Runs ONCE at the beginning)
LlmAgent initialWriterAgent =
LlmAgent.builder()
.model(MODEL_NAME)
.name("InitialWriterAgent")
.description(
"Writes the initial document draft based on the topic, aiming for some initial"
+ " substance.")
.instruction(
"""
You are a Creative Writing Assistant tasked with starting a story.
Write the *first draft* of a short story (aim for 2-4 sentences).
Base the content *only* on the topic provided below. Try to introduce a specific element (like a character, a setting detail, or a starting action) to make it engaging.
Output *only* the story/document text. Do not add introductions or explanations.
""")
.outputKey(STATE_CURRENT_DOC)
.includeContents(NONE)
.build();
// STEP 2a: Critic Agent (Inside the Refinement Loop)
LlmAgent criticAgentInLoop =
LlmAgent.builder()
.model(MODEL_NAME)
.name("CriticAgent")
.description(
"Reviews the current draft, providing critique if clear improvements are needed,"
+ " otherwise signals completion.")
.instruction(
"""
You are a Constructive Critic AI reviewing a short document draft (typically 2-6 sentences). Your goal is balanced feedback.
**Document to Review:**
```
{{current_document}}
```
**Task:**
Review the document for clarity, engagement, and basic coherence according to the initial topic (if known).
IF you identify 1-2 *clear and actionable* ways the document could be improved to better capture the topic or enhance reader engagement (e.g., "Needs a stronger opening sentence", "Clarify the character's goal"):
Provide these specific suggestions concisely. Output *only* the critique text.
ELSE IF the document is coherent, addresses the topic adequately for its length, and has no glaring errors or obvious omissions:
Respond *exactly* with the phrase "No major issues found." and nothing else. It doesn't need to be perfect, just functionally complete for this stage. Avoid suggesting purely subjective stylistic preferences if the core is sound.
Do not add explanations. Output only the critique OR the exact completion phrase.
""")
.outputKey(STATE_CRITICISM)
.includeContents(NONE)
.build();
// STEP 2b: Refiner/Exiter Agent (Inside the Refinement Loop)
LlmAgent refinerAgentInLoop =
LlmAgent.builder()
.model(MODEL_NAME)
.name("RefinerAgent")
.description(
"Refines the document based on critique, or calls exitLoop if critique indicates"
+ " completion.")
.instruction(
"""
You are a Creative Writing Assistant refining a document based on feedback OR exiting the process.
**Current Document:**
```
{{current_document}}
```
**Critique/Suggestions:**
{{criticism}}
**Task:**
Analyze the 'Critique/Suggestions'.
IF the critique is *exactly* "No major issues found.":
You MUST call the 'exitLoop' function. Do not output any text.
ELSE (the critique contains actionable feedback):
Carefully apply the suggestions to improve the 'Current Document'. Output *only* the refined document text.
Do not add explanations. Either output the refined document OR call the exitLoop function.
""")
.outputKey(STATE_CURRENT_DOC)
.includeContents(NONE)
.tools(FunctionTool.create(LoopAgentExample.class, "exitLoop"))
.build();
// STEP 2: Refinement Loop Agent
LoopAgent refinementLoop =
LoopAgent.builder()
.name("RefinementLoop")
.description("Repeatedly refines the document with critique and then exits.")
.subAgents(criticAgentInLoop, refinerAgentInLoop)
.maxIterations(5)
.build();
// STEP 3: Overall Sequential Pipeline
SequentialAgent iterativeWriterAgent =
SequentialAgent.builder()
.name(APP_NAME)
.description(
"Writes an initial document and then iteratively refines it with critique using an"
+ " exit tool.")
.subAgents(initialWriterAgent, refinementLoop)
.build();
// Create an InMemoryRunner
InMemoryRunner runner = new InMemoryRunner(iterativeWriterAgent, APP_NAME);
// InMemoryRunner automatically creates a session service. Create a session using the service
Session session = runner.sessionService().createSession(APP_NAME, USER_ID).blockingGet();
Content userMessage = Content.fromParts(Part.fromText(prompt));
// Run the agent
Flowable eventStream = runner.runAsync(USER_ID, session.id(), userMessage);
// Stream event response
eventStream.blockingForEach(
event -> {
if (event.finalResponse()) {
System.out.println(event.stringifyContent());
}
});
}
}
````
````kotlin
import com.google.adk.kt.agents.Instruction
import com.google.adk.kt.agents.LlmAgent
import com.google.adk.kt.agents.LlmAgent.IncludeContents
import com.google.adk.kt.agents.LoopAgent
import com.google.adk.kt.agents.SequentialAgent
import com.google.adk.kt.annotations.Tool
import com.google.adk.kt.callbacks.BeforeAgentCallback
import com.google.adk.kt.callbacks.CallbackChoice
import com.google.adk.kt.models.Gemini
import com.google.adk.kt.tools.ToolContext
// --- Constants ---
private const val GEMINI_MODEL = "gemini-flash-latest"
// --- State Keys ---
const val STATE_CURRENT_DOC = "current_document"
const val STATE_CRITICISM = "criticism"
// Define the exact phrase the Critic should use to signal completion
const val COMPLETION_PHRASE = "No major issues found."
// --- Tool Definition ---
class LoopTools {
/**
* Call this function ONLY when the critique indicates no further changes are needed, signaling
* the iterative process should end.
*/
@Tool(name = "exit_loop")
fun exitLoop(context: ToolContext): Map {
println(" [Tool Call] exit_loop triggered by ${context.agentName}")
context.actions.escalate = true
context.actions.skipSummarization = true
// Return empty map as tools should typically return JSON-serializable output
return emptyMap()
}
}
// --- Before Agent Callback ---
// Ensure 'initial_topic' is set in state before pipeline starts.
val updateInitialTopicState =
BeforeAgentCallback { context ->
context.updateState(
"initial_topic",
context.state["initial_topic"] ?: "a robot developing unexpected emotions",
)
CallbackChoice.Continue(context.eventActions)
}
// --- Agent Definitions ---
val model = Gemini(name = GEMINI_MODEL)
// STEP 1: Initial Writer Agent (Runs ONCE at the beginning)
val initialWriterAgent =
LlmAgent(
name = "InitialWriterAgent",
model = model,
includeContents = IncludeContents.NONE,
instruction =
Instruction(
"""
You are a Creative Writing Assistant tasked with starting a story.
Write a *very basic* first draft of a short story (just 1-2 simple sentences).
Keep it plain and minimal - do NOT add descriptive language yet.
Topic: {initial_topic}
Output *only* the story/document text. Do not add introductions or explanations.
""".trimIndent(),
),
description =
"Writes the initial document draft based on the topic, aiming for some initial " +
"substance.",
outputKey = STATE_CURRENT_DOC,
)
// STEP 2a: Critic Agent (Inside the Refinement Loop)
val criticAgentInLoop =
LlmAgent(
name = "CriticAgent",
model = model,
includeContents = IncludeContents.NONE,
instruction =
Instruction(
"""
You are a Constructive Critic AI reviewing a short story draft.
**Document to Review:**
```
{current_document}
```
**Completion Criteria (ALL must be met):**
1. At least 4 sentences long
2. Has a clear beginning, middle, and end
3. Includes at least one descriptive detail (sensory or emotional)
**Task:**
Check the document against the criteria above.
IF any criteria is NOT met, provide specific feedback on what to add or improve.
Output *only* the critique text.
IF ALL criteria are met, respond *exactly* with: "$COMPLETION_PHRASE"
""".trimIndent(),
),
description =
"Reviews the current draft, providing critique if clear improvements are needed, " +
"otherwise signals completion.",
outputKey = STATE_CRITICISM,
)
// STEP 2b: Refiner/Exiter Agent (Inside the Refinement Loop)
val refinerAgentInLoop =
LlmAgent(
name = "RefinerAgent",
model = model,
// Relies solely on state via placeholders
includeContents = IncludeContents.NONE,
instruction =
Instruction(
"""
You are a Creative Writing Assistant refining a document based on feedback OR exiting the process.
**Current Document:**
```
{current_document}
```
**Critique/Suggestions:**
{criticism}
**Task:**
Analyze the 'Critique/Suggestions'.
IF the critique is *exactly* "$COMPLETION_PHRASE":
You MUST call the 'exit_loop' function. Do not output any text.
ELSE (the critique contains actionable feedback):
Carefully apply the suggestions to improve the 'Current Document'. Output *only* the refined document text.
Do not add explanations. Either output the refined document OR call the exit_loop function.
""".trimIndent(),
),
description =
"Refines the document based on critique, or calls exit_loop if critique indicates " +
"completion.",
// Provide the exit_loop tool
tools = LoopTools().generatedTools(),
// Overwrites state['current_document'] with the refined version
outputKey = STATE_CURRENT_DOC,
)
// STEP 2: Refinement Loop Agent
val refinementLoop =
LoopAgent(
name = "RefinementLoop",
// Agent order is crucial: Critique first, then Refine/Exit
subAgents =
listOf(
criticAgentInLoop,
refinerAgentInLoop,
),
// Limit loops
maxIterations = 5,
)
// STEP 3: Overall Sequential Pipeline
private val rootAgent =
SequentialAgent(
name = "IterativeWritingPipeline",
subAgents =
listOf(
// Run first to create initial doc
initialWriterAgent,
// Then run the critique/refine loop
refinementLoop,
),
// set initial topic in state
beforeAgentCallbacks = listOf(updateInitialTopicState),
description =
"Writes an initial document and then iteratively refines it with critique using an " +
"exit tool.",
)
````
Global instructions for all agents
Apply consistent rules across your workflow using the `GlobalInstructionPlugin` on your `App` ([Python](https://github.com/google/adk-python/blob/main/src/google/adk/plugins/global_instruction_plugin.py), [TypeScript](https://github.com/google/adk-js/blob/main/core/src/plugins/global_instruction_plugin.ts), [Java](https://github.com/google/adk-java/blob/main/core/src/main/java/com/google/adk/plugins/GlobalInstructionPlugin.java)) or the `GlobalInstruction` parameter on your agent ([Go](https://github.com/google/adk-go/blob/main/agent/llmagent/llmagent.go)).
# Parallel template workflow agent
Supported in ADKPython v0.1.0TypeScript v0.2.0Go v0.1.0Java v0.2.0Kotlin v0.1.0
The ***ParallelAgent*** class is a [template workflow](/agents/workflow-agents/) agent that executes its sub-agents concurrently. This execution strategy can dramatically speed up workflows where two or more tasks can be performed independently. For scenarios prioritizing speed and involving independent, resource-intensive tasks, this templated workflow facilitates parallel execution, which can significantly reduce overall processing time. When using this workflow type, it is important that each sub-agent can operate without depending on the other sub-agents. This workflow type is particularly beneficial for operations like multi-source data retrieval or heavy computations, where parallelization yields substantial performance gains.
As with other templated workflows, the execution of a ***ParallelAgent*** object is not controlled by an AI model, and is deterministic in how it executes its sub-agents. The sub-agents specified in the parallel execution set may or may not utilize AI models, but the overall execution of those sub-agents is ultimately managed by the ***ParallelAgent*** object you define.
Deprecated: Use graph-based workflows
In ADK Python, TypeScript, Go, and Kotlin, template workflows have been superseded by more flexible workflow structures, including [graph-based workflows](/graphs/) and [dynamic workflows](/graphs/dynamic/).
### How it works
When the `ParallelAgent`'s `run_async()` method is called:
1. **Concurrent Execution:** It initiates the `run_async()` method of *each* sub-agent present in the `sub_agents` list *concurrently*. This means all the agents start running at (approximately) the same time.
1. **Independent Branches:** Each sub-agent operates in its own execution branch, and there is ***no* automatic sharing of conversation history between these branches** during execution. A sub-agent sees only the events on its own branch. Session state is shared: all branches run against the same session, so a value one sub-agent writes, for example through `output_key`, is visible to the other branches and to later agents.
1. **Result Collection:** The `ParallelAgent` merges the sub-agents' event streams and yields each event as it is produced; it does not gather the results into a list you can read afterwards. Because the branches interleave, the event order is not deterministic. To use a sub-agent's result, read it from the shared session state, for example the key it was given through `output_key`.
### Independent Execution and State Management
It's *crucial* to understand that sub-agents within a `ParallelAgent` run independently. If you *need* communication or data sharing between these agents, you must implement it explicitly. Possible approaches include:
- **Shared session state:** All branches run against the same session, so give each sub-agent an `output_key` and read those keys from a later agent's instruction. Because the branches run concurrently, do not rely on one branch reading a key another branch is still writing.
- **External State Management:** Use an external database, message queue, or other mechanism to manage shared state and facilitate communication between agents.
- **Post-Processing:** Collect results from each branch, and then implement logic to coordinate data afterwards.
### Full Example: Parallel Web Research
Imagine researching multiple topics simultaneously:
1. **Researcher Agent 1:** An `LlmAgent` that researches "renewable energy sources."
1. **Researcher Agent 2:** An `LlmAgent` that researches "electric vehicle technology."
1. **Researcher Agent 3:** An `LlmAgent` that researches "carbon capture methods."
```py
ParallelAgent(name="ParallelWebResearchAgent", sub_agents=[ResearcherAgent1, ResearcherAgent2, ResearcherAgent3])
```
These research tasks are independent. Using a `ParallelAgent` allows them to run concurrently, potentially reducing the total research time significantly compared to running them sequentially. To use each agent's result afterwards, give it an `output_key` and read that key from session state.
Full Code
```py
from google.adk.agents.parallel_agent import ParallelAgent
from google.adk.agents.llm_agent import LlmAgent
from google.adk.agents.sequential_agent import SequentialAgent
from google.adk.tools import google_search
# --- Constants ---
GEMINI_MODEL = "gemini-2.5-flash"
# --- 1. Define Researcher Sub-Agents (to run in parallel) ---
# Researcher 1: Renewable Energy
researcher_agent_1 = LlmAgent(
name="RenewableEnergyResearcher",
model=GEMINI_MODEL,
instruction="""
You are an AI Research Assistant specializing in energy.
Research the latest advancements in 'renewable energy sources'.
Use the Google Search tool provided.
Summarize your key findings concisely (1-2 sentences).
Output *only* the summary.
""",
description="Researches renewable energy sources.",
tools=[google_search],
# Store result in state for the merger agent
output_key="renewable_energy_result"
)
# Researcher 2: Electric Vehicles
researcher_agent_2 = LlmAgent(
name="EVResearcher",
model=GEMINI_MODEL,
instruction="""
You are an AI Research Assistant specializing in transportation.
Research the latest developments in 'electric vehicle technology'.
Use the Google Search tool provided.
Summarize your key findings concisely (1-2 sentences).
Output *only* the summary.
""",
description="Researches electric vehicle technology.",
tools=[google_search],
# Store result in state for the merger agent
output_key="ev_technology_result"
)
# Researcher 3: Carbon Capture
researcher_agent_3 = LlmAgent(
name="CarbonCaptureResearcher",
model=GEMINI_MODEL,
instruction="""
You are an AI Research Assistant specializing in climate solutions.
Research the current state of 'carbon capture methods'.
Use the Google Search tool provided.
Summarize your key findings concisely (1-2 sentences).
Output *only* the summary.
""",
description="Researches carbon capture methods.",
tools=[google_search],
# Store result in state for the merger agent
output_key="carbon_capture_result"
)
# --- 2. Create the ParallelAgent (Runs researchers concurrently) ---
# This agent orchestrates the concurrent execution of the researchers.
# It finishes once all researchers have completed and stored their results in state.
parallel_research_agent = ParallelAgent(
name="ParallelWebResearchAgent",
sub_agents=[researcher_agent_1, researcher_agent_2, researcher_agent_3],
description="Runs multiple research agents in parallel to gather information."
)
# --- 3. Define the Merger Agent (Runs *after* the parallel agents) ---
# This agent takes the results stored in the session state by the parallel agents
# and synthesizes them into a single, structured response with attributions.
merger_agent = LlmAgent(
name="SynthesisAgent",
model=GEMINI_MODEL, # Or potentially a more powerful model if needed for synthesis
instruction="""
You are an AI Assistant responsible for combining research findings into a structured report.
Your primary task is to synthesize the following research summaries, clearly attributing findings to their source areas. Structure your response using headings for each topic. Ensure the report is coherent and integrates the key points smoothly.
**Crucially: Your entire response MUST be grounded *exclusively* on the information provided in the 'Input Summaries' below. Do NOT add any external knowledge, facts, or details not present in these specific summaries.**
**Input Summaries:**
* **Renewable Energy:**
{renewable_energy_result}
* **Electric Vehicles:**
{ev_technology_result}
* **Carbon Capture:**
{carbon_capture_result}
**Output Format:**
## Summary of Recent Sustainable Technology Advancements
### Renewable Energy Findings
(Based on RenewableEnergyResearcher's findings)
[Synthesize and elaborate *only* on the renewable energy input summary provided above.]
### Electric Vehicle Findings
(Based on EVResearcher's findings)
[Synthesize and elaborate *only* on the EV input summary provided above.]
### Carbon Capture Findings
(Based on CarbonCaptureResearcher's findings)
[Synthesize and elaborate *only* on the carbon capture input summary provided above.]
### Overall Conclusion
[Provide a brief (1-2 sentence) concluding statement that connects *only* the findings presented above.]
Output *only* the structured report following this format. Do not include introductory or concluding phrases outside this structure, and strictly adhere to using only the provided input summary content.
""",
description="Combines research findings from parallel agents into a structured, cited report, strictly grounded on provided inputs.",
# No tools needed for merging
# No output_key needed here, as its direct response is the final output of the sequence
)
# --- 4. Create the SequentialAgent (Orchestrates the overall flow) ---
# This is the main agent that will be run. It first executes the ParallelAgent
# to populate the state, and then executes the MergerAgent to produce the final output.
sequential_pipeline_agent = SequentialAgent(
name="ResearchAndSynthesisPipeline",
# Run parallel research first, then merge
sub_agents=[parallel_research_agent, merger_agent],
description="Coordinates parallel research and synthesizes the results."
)
root_agent = sequential_pipeline_agent
```
```typescript
// Part of agent.ts --> Follow https://adk.tools-online.site/get-started/ to learn the setup
// --- 1. Define Researcher Sub-Agents (to run in parallel) ---
const researchTools = [GOOGLE_SEARCH];
// Researcher 1: Renewable Energy
const researcherAgent1 = new LlmAgent({
name: "RenewableEnergyResearcher",
model: GEMINI_MODEL,
instruction: `You are an AI Research Assistant specializing in energy.
Research the latest advancements in 'renewable energy sources'.
Use the Google Search tool provided.
Summarize your key findings concisely (1-2 sentences).
Output *only* the summary.
`,
description: "Researches renewable energy sources.",
tools: researchTools,
// Store result in state for the merger agent
outputKey: "renewable_energy_result"
});
// Researcher 2: Electric Vehicles
const researcherAgent2 = new LlmAgent({
name: "EVResearcher",
model: GEMINI_MODEL,
instruction: `You are an AI Research Assistant specializing in transportation.
Research the latest developments in 'electric vehicle technology'.
Use the Google Search tool provided.
Summarize your key findings concisely (1-2 sentences).
Output *only* the summary.
`,
description: "Researches electric vehicle technology.",
tools: researchTools,
// Store result in state for the merger agent
outputKey: "ev_technology_result"
});
// Researcher 3: Carbon Capture
const researcherAgent3 = new LlmAgent({
name: "CarbonCaptureResearcher",
model: GEMINI_MODEL,
instruction: `You are an AI Research Assistant specializing in climate solutions.
Research the current state of 'carbon capture methods'.
Use the Google Search tool provided.
Summarize your key findings concisely (1-2 sentences).
Output *only* the summary.
`,
description: "Researches carbon capture methods.",
tools: researchTools,
// Store result in state for the merger agent
outputKey: "carbon_capture_result"
});
// --- 2. Create the ParallelAgent (Runs researchers concurrently) ---
// This agent orchestrates the concurrent execution of the researchers.
// It finishes once all researchers have completed and stored their results in state.
const parallelResearchAgent = new ParallelAgent({
name: "ParallelWebResearchAgent",
subAgents: [researcherAgent1, researcherAgent2, researcherAgent3],
description: "Runs multiple research agents in parallel to gather information."
});
// --- 3. Define the Merger Agent (Runs *after* the parallel agents) ---
// This agent takes the results stored in the session state by the parallel agents
// and synthesizes them into a single, structured response with attributions.
const mergerAgent = new LlmAgent({
name: "SynthesisAgent",
model: GEMINI_MODEL, // Or potentially a more powerful model if needed for synthesis
instruction: `You are an AI Assistant responsible for combining research findings into a structured report.
Your primary task is to synthesize the following research summaries, clearly attributing findings to their source areas. Structure your response using headings for each topic. Ensure the report is coherent and integrates the key points smoothly.
**Crucially: Your entire response MUST be grounded *exclusively* on the information provided in the 'Input Summaries' below. Do NOT add any external knowledge, facts, or details not present in these specific summaries.**
**Input Summaries:**
* **Renewable Energy:**
{renewable_energy_result}
* **Electric Vehicles:**
{ev_technology_result}
* **Carbon Capture:**
{carbon_capture_result}
**Output Format:**
## Summary of Recent Sustainable Technology Advancements
### Renewable Energy Findings
(Based on RenewableEnergyResearcher's findings)
[Synthesize and elaborate *only* on the renewable energy input summary provided above.]
### Electric Vehicle Findings
(Based on EVResearcher's findings)
[Synthesize and elaborate *only* on the EV input summary provided above.]
### Carbon Capture Findings
(Based on CarbonCaptureResearcher's findings)
[Synthesize and elaborate *only* on the carbon capture input summary provided above.]
### Overall Conclusion
[Provide a brief (1-2 sentence) concluding statement that connects *only* the findings presented above.]
Output *only* the structured report following this format. Do not include introductory or concluding phrases outside this structure, and strictly adhere to using only the provided input summary content.
`,
description: "Combines research findings from parallel agents into a structured, cited report, strictly grounded on provided inputs.",
// No tools needed for merging
// No output_key needed here, as its direct response is the final output of the sequence
});
// --- 4. Create the SequentialAgent (Orchestrates the overall flow) ---
// This is the main agent that will be run. It first executes the ParallelAgent
// to populate the state, and then executes the MergerAgent to produce the final output.
const rootAgent = new SequentialAgent({
name: "ResearchAndSynthesisPipeline",
// Run parallel research first, then merge
subAgents: [parallelResearchAgent, mergerAgent],
description: "Coordinates parallel research and synthesizes the results."
});
```
```go
model, err := gemini.NewModel(ctx, modelName, &genai.ClientConfig{})
if err != nil {
return fmt.Errorf("failed to create model: %v", err)
}
// --- 1. Define Researcher Sub-Agents (to run in parallel) ---
researcher1, err := llmagent.New(llmagent.Config{
Name: "RenewableEnergyResearcher",
Model: model,
Instruction: `You are an AI Research Assistant specializing in energy.
Research the latest advancements in 'renewable energy sources'.
Use the Google Search tool provided.
Summarize your key findings concisely (1-2 sentences).
Output *only* the summary.`,
Description: "Researches renewable energy sources.",
OutputKey: "renewable_energy_result",
})
if err != nil {
return err
}
researcher2, err := llmagent.New(llmagent.Config{
Name: "EVResearcher",
Model: model,
Instruction: `You are an AI Research Assistant specializing in transportation.
Research the latest developments in 'electric vehicle technology'.
Use the Google Search tool provided.
Summarize your key findings concisely (1-2 sentences).
Output *only* the summary.`,
Description: "Researches electric vehicle technology.",
OutputKey: "ev_technology_result",
})
if err != nil {
return err
}
researcher3, err := llmagent.New(llmagent.Config{
Name: "CarbonCaptureResearcher",
Model: model,
Instruction: `You are an AI Research Assistant specializing in climate solutions.
Research the current state of 'carbon capture methods'.
Use the Google Search tool provided.
Summarize your key findings concisely (1-2 sentences).
Output *only* the summary.`,
Description: "Researches carbon capture methods.",
OutputKey: "carbon_capture_result",
})
if err != nil {
return err
}
// --- 2. Create the ParallelAgent (Runs researchers concurrently) ---
parallelResearchAgent, err := parallelagent.New(parallelagent.Config{
AgentConfig: agent.Config{
Name: "ParallelWebResearchAgent",
Description: "Runs multiple research agents in parallel to gather information.",
SubAgents: []agent.Agent{researcher1, researcher2, researcher3},
},
})
if err != nil {
return fmt.Errorf("failed to create parallel agent: %v", err)
}
// --- 3. Define the Merger Agent (Runs *after* the parallel agents) ---
synthesisAgent, err := llmagent.New(llmagent.Config{
Name: "SynthesisAgent",
Model: model,
Instruction: `You are an AI Assistant responsible for combining research findings into a structured report.
Your primary task is to synthesize the following research summaries, clearly attributing findings to their source areas. Structure your response using headings for each topic. Ensure the report is coherent and integrates the key points smoothly.
**Crucially: Your entire response MUST be grounded *exclusively* on the information provided in the 'Input Summaries' below. Do NOT add any external knowledge, facts, or details not present in these specific summaries.**
**Input Summaries:**
* **Renewable Energy:**
{renewable_energy_result}
* **Electric Vehicles:**
{ev_technology_result}
* **Carbon Capture:**
{carbon_capture_result}
**Output Format:**
## Summary of Recent Sustainable Technology Advancements
### Renewable Energy Findings
(Based on RenewableEnergyResearcher's findings)
[Synthesize and elaborate *only* on the renewable energy input summary provided above.]
### Electric Vehicle Findings
(Based on EVResearcher's findings)
[Synthesize and elaborate *only* on the EV input summary provided above.]
### Carbon Capture Findings
(Based on CarbonCaptureResearcher's findings)
[Synthesize and elaborate *only* on the carbon capture input summary provided above.]
### Overall Conclusion
[Provide a brief (1-2 sentence) concluding statement that connects *only* the findings presented above.]
Output *only* the structured report following this format. Do not include introductory or concluding phrases outside this structure, and strictly adhere to using only the provided input summary content.`,
Description: "Combines research findings from parallel agents into a structured, cited report, strictly grounded on provided inputs.",
})
if err != nil {
return fmt.Errorf("failed to create synthesis agent: %v", err)
}
// --- 4. Create the SequentialAgent (Orchestrates the overall flow) ---
pipeline, err := sequentialagent.New(sequentialagent.Config{
AgentConfig: agent.Config{
Name: "ResearchAndSynthesisPipeline",
Description: "Coordinates parallel research and synthesizes the results.",
SubAgents: []agent.Agent{parallelResearchAgent, synthesisAgent},
},
})
if err != nil {
return fmt.Errorf("failed to create sequential agent pipeline: %v", err)
}
```
```java
import com.google.adk.agents.LlmAgent;
import com.google.adk.agents.ParallelAgent;
import com.google.adk.agents.SequentialAgent;
import com.google.adk.events.Event;
import com.google.adk.runner.InMemoryRunner;
import com.google.adk.sessions.Session;
import com.google.adk.tools.GoogleSearchTool;
import com.google.genai.types.Content;
import com.google.genai.types.Part;
import io.reactivex.rxjava3.core.Flowable;
public class ParallelResearchPipeline {
private static final String APP_NAME = "parallel_research_app";
private static final String USER_ID = "research_user_01";
private static final String GEMINI_MODEL = "gemini-2.0-flash";
// Assume google_search is an instance of the GoogleSearchTool
private static final GoogleSearchTool googleSearchTool = new GoogleSearchTool();
public static void main(String[] args) {
String query = "Summarize recent sustainable tech advancements.";
SequentialAgent sequentialPipelineAgent = initAgent();
runAgent(sequentialPipelineAgent, query);
}
public static SequentialAgent initAgent() {
// --- 1. Define Researcher Sub-Agents (to run in parallel) ---
// Researcher 1: Renewable Energy
LlmAgent researcherAgent1 = LlmAgent.builder()
.name("RenewableEnergyResearcher")
.model(GEMINI_MODEL)
.instruction("""
You are an AI Research Assistant specializing in energy.
Research the latest advancements in 'renewable energy sources'.
Use the Google Search tool provided.
Summarize your key findings concisely (1-2 sentences).
Output *only* the summary.
""")
.description("Researches renewable energy sources.")
.tools(googleSearchTool)
.outputKey("renewable_energy_result") // Store result in state
.build();
// Researcher 2: Electric Vehicles
LlmAgent researcherAgent2 = LlmAgent.builder()
.name("EVResearcher")
.model(GEMINI_MODEL)
.instruction("""
You are an AI Research Assistant specializing in transportation.
Research the latest developments in 'electric vehicle technology'.
Use the Google Search tool provided.
Summarize your key findings concisely (1-2 sentences).
Output *only* the summary.
""")
.description("Researches electric vehicle technology.")
.tools(googleSearchTool)
.outputKey("ev_technology_result") // Store result in state
.build();
// Researcher 3: Carbon Capture
LlmAgent researcherAgent3 = LlmAgent.builder()
.name("CarbonCaptureResearcher")
.model(GEMINI_MODEL)
.instruction("""
You are an AI Research Assistant specializing in climate solutions.
Research the current state of 'carbon capture methods'.
Use the Google Search tool provided.
Summarize your key findings concisely (1-2 sentences).
Output *only* the summary.
""")
.description("Researches carbon capture methods.")
.tools(googleSearchTool)
.outputKey("carbon_capture_result") // Store result in state
.build();
// --- 2. Create the ParallelAgent (Runs researchers concurrently) ---
// This agent orchestrates the concurrent execution of the researchers.
// It finishes once all researchers have completed and stored their results in state.
ParallelAgent parallelResearchAgent =
ParallelAgent.builder()
.name("ParallelWebResearchAgent")
.subAgents(researcherAgent1, researcherAgent2, researcherAgent3)
.description("Runs multiple research agents in parallel to gather information.")
.build();
// --- 3. Define the Merger Agent (Runs *after* the parallel agents) ---
// This agent takes the results stored in the session state by the parallel agents
// and synthesizes them into a single, structured response with attributions.
LlmAgent mergerAgent =
LlmAgent.builder()
.name("SynthesisAgent")
.model(GEMINI_MODEL)
.instruction(
"""
You are an AI Assistant responsible for combining research findings into a structured report.
Your primary task is to synthesize the following research summaries, clearly attributing findings to their source areas. Structure your response using headings for each topic. Ensure the report is coherent and integrates the key points smoothly.
**Crucially: Your entire response MUST be grounded *exclusively* on the information provided in the 'Input Summaries' below. Do NOT add any external knowledge, facts, or details not present in these specific summaries.**
**Input Summaries:**
* **Renewable Energy:**
{renewable_energy_result}
* **Electric Vehicles:**
{ev_technology_result}
* **Carbon Capture:**
{carbon_capture_result}
**Output Format:**
## Summary of Recent Sustainable Technology Advancements
### Renewable Energy Findings
(Based on RenewableEnergyResearcher's findings)
[Synthesize and elaborate *only* on the renewable energy input summary provided above.]
### Electric Vehicle Findings
(Based on EVResearcher's findings)
[Synthesize and elaborate *only* on the EV input summary provided above.]
### Carbon Capture Findings
(Based on CarbonCaptureResearcher's findings)
[Synthesize and elaborate *only* on the carbon capture input summary provided above.]
### Overall Conclusion
[Provide a brief (1-2 sentence) concluding statement that connects *only* the findings presented above.]
Output *only* the structured report following this format. Do not include introductory or concluding phrases outside this structure, and strictly adhere to using only the provided input summary content.
""")
.description(
"Combines research findings from parallel agents into a structured, cited report, strictly grounded on provided inputs.")
// No tools needed for merging
// No output_key needed here, as its direct response is the final output of the sequence
.build();
// --- 4. Create the SequentialAgent (Orchestrates the overall flow) ---
// This is the main agent that will be run. It first executes the ParallelAgent
// to populate the state, and then executes the MergerAgent to produce the final output.
SequentialAgent sequentialPipelineAgent =
SequentialAgent.builder()
.name("ResearchAndSynthesisPipeline")
// Run parallel research first, then merge
.subAgents(parallelResearchAgent, mergerAgent)
.description("Coordinates parallel research and synthesizes the results.")
.build();
return sequentialPipelineAgent;
}
public static void runAgent(SequentialAgent sequentialPipelineAgent, String query) {
// Create an InMemoryRunner
InMemoryRunner runner = new InMemoryRunner(sequentialPipelineAgent, APP_NAME);
// InMemoryRunner automatically creates a session service. Create a session using the service
Session session = runner.sessionService().createSession(APP_NAME, USER_ID).blockingGet();
Content userMessage = Content.fromParts(Part.fromText(query));
// Run the agent
Flowable eventStream = runner.runAsync(USER_ID, session.id(), userMessage);
// Stream event response
eventStream.blockingForEach(
event -> {
if (event.finalResponse()) {
System.out.printf("Event Author: %s \n Event Response: %s \n\n\n", event.author(), event.stringifyContent());
}
});
}
}
```
```kotlin
import com.google.adk.kt.agents.Instruction
import com.google.adk.kt.agents.LlmAgent
import com.google.adk.kt.agents.ParallelAgent
import com.google.adk.kt.agents.SequentialAgent
import com.google.adk.kt.models.Gemini
import com.google.adk.kt.tools.GoogleSearchTool
// --- Constants ---
const val GEMINI_MODEL = "gemini-2.5-flash"
val googleSearch = GoogleSearchTool()
// --- 1. Define Researcher Sub-Agents (to run in parallel) ---
// Researcher 1: Renewable Energy
val researcherAgent1 =
LlmAgent(
name = "RenewableEnergyResearcher",
model = Gemini(name = GEMINI_MODEL),
instruction =
Instruction(
"""
You are an AI Research Assistant specializing in energy.
Research the latest advancements in 'renewable energy sources'.
Use the Google Search tool provided.
Summarize your key findings concisely (1-2 sentences).
Output *only* the summary.
""".trimIndent(),
),
description = "Researches renewable energy sources.",
tools = listOf(googleSearch),
// Store result in state for the merger agent
outputKey = "renewable_energy_result",
)
// Researcher 2: Electric Vehicles
val researcherAgent2 =
LlmAgent(
name = "EVResearcher",
model = Gemini(name = GEMINI_MODEL),
instruction =
Instruction(
"""
You are an AI Research Assistant specializing in transportation.
Research the latest developments in 'electric vehicle technology'.
Use the Google Search tool provided.
Summarize your key findings concisely (1-2 sentences).
Output *only* the summary.
""".trimIndent(),
),
description = "Researches electric vehicle technology.",
tools = listOf(googleSearch),
// Store result in state for the merger agent
outputKey = "ev_technology_result",
)
// Researcher 3: Carbon Capture
val researcherAgent3 =
LlmAgent(
name = "CarbonCaptureResearcher",
model = Gemini(name = GEMINI_MODEL),
instruction =
Instruction(
"""
You are an AI Research Assistant specializing in climate solutions.
Research the current state of 'carbon capture methods'.
Use the Google Search tool provided.
Summarize your key findings concisely (1-2 sentences).
Output *only* the summary.
""".trimIndent(),
),
description = "Researches carbon capture methods.",
tools = listOf(googleSearch),
// Store result in state for the merger agent
outputKey = "carbon_capture_result",
)
// --- 2. Create the ParallelAgent (Runs researchers concurrently) ---
// This agent orchestrates the concurrent execution of the researchers.
// It finishes once all researchers have completed and stored their results in state.
val parallelResearchAgent =
ParallelAgent(
name = "ParallelWebResearchAgent",
subAgents = listOf(researcherAgent1, researcherAgent2, researcherAgent3),
description = "Runs multiple research agents in parallel to gather information.",
)
// --- 3. Define the Merger Agent (Runs *after* the parallel agents) ---
// This agent takes the results stored in the session state by the parallel agents
// and synthesizes them into a single, structured response with attributions.
val mergerAgent =
LlmAgent(
name = "SynthesisAgent",
model = Gemini(name = GEMINI_MODEL), // Or potentially a more powerful model if needed for synthesis
instruction =
Instruction(
"""
You are an AI Assistant responsible for combining research findings into a structured report.
Your primary task is to synthesize the following research summaries, clearly attributing findings to their source areas. Structure your response using headings for each topic. Ensure the report is coherent and integrates the key points smoothly.
**Crucially: Your entire response MUST be grounded *exclusively* on the information provided in the 'Input Summaries' below. Do NOT add any external knowledge, facts, or details not present in these specific summaries.**
**Input Summaries:**
* **Renewable Energy:**
{renewable_energy_result}
* **Electric Vehicles:**
{ev_technology_result}
* **Carbon Capture:**
{carbon_capture_result}
**Output Format:**
## Summary of Recent Sustainable Technology Advancements
### Renewable Energy Findings
(Based on RenewableEnergyResearcher's findings)
[Synthesize and elaborate *only* on the renewable energy input summary provided above.]
### Electric Vehicle Findings
(Based on EVResearcher's findings)
[Synthesize and elaborate *only* on the EV input summary provided above.]
### Carbon Capture Findings
(Based on CarbonCaptureResearcher's findings)
[Synthesize and elaborate *only* on the carbon capture input summary provided above.]
### Overall Conclusion
[Provide a brief (1-2 sentence) concluding statement that connects *only* the findings presented above.]
Output *only* the structured report following this format. Do not include introductory or concluding phrases outside this structure, and strictly adhere to using only the provided input summary content.
""".trimIndent(),
),
description =
"Combines research findings from parallel agents into a structured, cited report, " +
"strictly grounded on provided inputs.",
// No tools needed for merging
// No outputKey needed here, as its direct response is the final output of the sequence
)
// --- 4. Create the SequentialAgent (Orchestrates the overall flow) ---
// This is the main agent that will be run. It first executes the ParallelAgent
// to populate the state, and then executes the MergerAgent to produce the final output.
val sequentialPipelineAgent =
SequentialAgent(
name = "ResearchAndSynthesisPipeline",
// Run parallel research first, then merge
subAgents = listOf(parallelResearchAgent, mergerAgent),
description = "Coordinates parallel research and synthesizes the results.",
)
val rootAgent = sequentialPipelineAgent
```
Global instructions for all agents
Apply consistent rules across your workflow using the `GlobalInstructionPlugin` on your `App` ([Python](https://github.com/google/adk-python/blob/main/src/google/adk/plugins/global_instruction_plugin.py), [TypeScript](https://github.com/google/adk-js/blob/main/core/src/plugins/global_instruction_plugin.ts), [Java](https://github.com/google/adk-java/blob/main/core/src/main/java/com/google/adk/plugins/GlobalInstructionPlugin.java)) or the `GlobalInstruction` parameter on your agent ([Go](https://github.com/google/adk-go/blob/main/agent/llmagent/llmagent.go)).
# Sequential template workflow agent
Supported in ADKPython v0.1.0TypeScript v0.2.0Go v0.1.0Java v0.2.0Kotlin v0.1.0
The ***SequentialAgent*** class is a [template workflow](/agents/workflow-agents/) agent that executes its sub-agents in the order they are specified in a list. Use ***SequentialAgent*** when you want execution to occur in a fixed, strict order. As with other templated workflows, the execution of a ***SequentialAgent*** object is not controlled by an AI model, and is deterministic in how it executes its sub-agents. The sub-agents specified in the sequential execution set may or may not utilize AI models, but the overall execution of those sub-agents is ultimately managed by the ***SequentialAgent*** object you define.
Deprecated: Use graph-based workflows
In ADK Python, TypeScript, Go, and Kotlin, template workflows have been superseded by more flexible workflow structures, including [graph-based workflows](/graphs/) and [dynamic workflows](/graphs/dynamic/).
### Example scenario
You want to build an agent that can summarize any webpage, using two tools: **Get Page Contents** and **Summarize Page**. Since the agent must always call **Get Page Contents** before calling **Summarize Page**, you can build your agent using the ***SequentialAgent*** class.
### How it works
When the `SequentialAgent`'s `Run Async` method is called, it performs the following actions:
1. **Iteration:** It iterates through the sub agents list in the order they were provided.
1. **Sub-Agent Execution:** For each sub-agent in the list, it calls the sub-agent's `Run Async` method.
Shared Invocation Context
The `SequentialAgent` passes the same `InvocationContext` to each of its sub-agents. This means they all share the same session state, including the temporary (`temp:`) namespace, making it easy to pass data between steps within a single turn.
### Full Example: Code Development Pipeline
Consider a simplified code development pipeline:
- **Code Writer Agent:** An LLM Agent that generates initial code based on a specification.
- **Code Reviewer Agent:** An LLM Agent that reviews the generated code for errors, style issues, and adherence to best practices. It receives the output of the Code Writer Agent.
- **Code Refactorer Agent:** An LLM Agent that takes the reviewed code, and the reviewer's comments, and refactors it to improve quality and address issues.
Using a `SequentialAgent` makes it simple to define this execution flow, as shown in the following code snippet:
```py
SequentialAgent(name="CodePipelineAgent", sub_agents=[CodeWriterAgent, CodeReviewerAgent, CodeRefactorerAgent])
```
This ensures the code is written, *then* reviewed, and *finally* refactored, in a strict, dependable order. **The output from each sub-agent is passed to the next by storing them in state via [Output Key](/agents/llm-agents/#data-handling)**.
Code
````py
from google.adk.agents.sequential_agent import SequentialAgent
from google.adk.agents.llm_agent import LlmAgent
# --- Constants ---
GEMINI_MODEL = "gemini-2.5-flash"
# --- 1. Define Sub-Agents for Each Pipeline Stage ---
# Code Writer Agent
# Takes the initial specification (from user query) and writes code.
code_writer_agent = LlmAgent(
name="CodeWriterAgent",
model=GEMINI_MODEL,
instruction="""
You are a Python Code Generator.
Based *only* on the user's request, write Python code that fulfills the requirement.
Output *only* the complete Python code block, enclosed in triple backticks (```python ... ```).
Do not add any other text before or after the code block.
""",
description="Writes initial Python code based on a specification.",
output_key="generated_code"
)
# Code Reviewer Agent
# Takes the code generated by the previous agent (read from state) and provides feedback.
code_reviewer_agent = LlmAgent(
name="CodeReviewerAgent",
model=GEMINI_MODEL,
instruction="""
You are an expert Python Code Reviewer.
Your task is to provide constructive feedback on the provided code.
**Code to Review:**
```python
{generated_code}
```
**Review Criteria:**
1. **Correctness:** Does the code work as intended? Are there logic errors?
2. **Readability:** Is the code clear and easy to understand? Follows PEP 8 style guidelines?
3. **Efficiency:** Is the code reasonably efficient? Any obvious performance bottlenecks?
4. **Edge Cases:** Does the code handle potential edge cases or invalid inputs gracefully?
5. **Best Practices:** Does the code follow common Python best practices?
**Output:**
Provide your feedback as a concise, bulleted list. Focus on the most important points for improvement.
If the code is excellent and requires no changes, simply state: "No major issues found."
Output *only* the review comments or the "No major issues" statement.
""",
description="Reviews code and provides feedback.",
output_key="review_comments"
)
# Code Refactorer Agent
# Takes the original code and the review comments (read from state) and refactors the code.
code_refactorer_agent = LlmAgent(
name="CodeRefactorerAgent",
model=GEMINI_MODEL,
instruction="""
You are a Python Code Refactoring AI.
Your goal is to improve the given Python code based on the provided review comments.
**Original Code:**
```python
{generated_code}
```
**Review Comments:**
{review_comments}
**Task:**
Carefully apply the suggestions from the review comments to refactor the original code.
If the review comments state "No major issues found," return the original code unchanged.
Ensure the final code is complete, functional, and includes necessary imports and docstrings.
**Output:**
Output *only* the final, refactored Python code block, enclosed in triple backticks (```python ... ```).
Do not add any other text before or after the code block.
""",
description="Refactors code based on review comments.",
output_key="refactored_code"
)
# --- 2. Create the SequentialAgent ---
# This agent orchestrates the pipeline by running the sub_agents in order.
code_pipeline_agent = SequentialAgent(
name="CodePipelineAgent",
sub_agents=[code_writer_agent, code_reviewer_agent, code_refactorer_agent],
description="Executes a sequence of code writing, reviewing, and refactoring.",
)
root_agent = code_pipeline_agent
````
```typescript
// Part of agent.ts --> Follow https://adk.tools-online.site/get-started/ to learn the setup
// --- 1. Define Sub-Agents for Each Pipeline Stage ---
// Code Writer Agent
// Takes the initial specification (from user query) and writes code.
const codeWriterAgent = new LlmAgent({
name: "CodeWriterAgent",
model: GEMINI_MODEL,
instruction: `You are a Python Code Generator.
Based *only* on the user's request, write Python code that fulfills the requirement.
Output *only* the complete Python code block, enclosed in triple backticks (\`\`\`python ... \`\`\`).
Do not add any other text before or after the code block.
`,
description: "Writes initial Python code based on a specification.",
outputKey: "generated_code" // Stores output in state['generated_code']
});
// Code Reviewer Agent
// Takes the code generated by the previous agent (read from state) and provides feedback.
const codeReviewerAgent = new LlmAgent({
name: "CodeReviewerAgent",
model: GEMINI_MODEL,
instruction: `You are an expert Python Code Reviewer.
Your task is to provide constructive feedback on the provided code.
**Code to Review:**
\`\`\`python
{generated_code}
\`\`\`
**Review Criteria:**
1. **Correctness:** Does the code work as intended? Are there logic errors?
2. **Readability:** Is the code clear and easy to understand? Follows PEP 8 style guidelines?
3. **Efficiency:** Is the code reasonably efficient? Any obvious performance bottlenecks?
4. **Edge Cases:** Does the code handle potential edge cases or invalid inputs gracefully?
5. **Best Practices:** Does the code follow common Python best practices?
**Output:**
Provide your feedback as a concise, bulleted list. Focus on the most important points for improvement.
If the code is excellent and requires no changes, simply state: "No major issues found."
Output *only* the review comments or the "No major issues" statement.
`,
description: "Reviews code and provides feedback.",
outputKey: "review_comments", // Stores output in state['review_comments']
});
// Code Refactorer Agent
// Takes the original code and the review comments (read from state) and refactors the code.
const codeRefactorerAgent = new LlmAgent({
name: "CodeRefactorerAgent",
model: GEMINI_MODEL,
instruction: `You are a Python Code Refactoring AI.
Your goal is to improve the given Python code based on the provided review comments.
**Original Code:**
\`\`\`python
{generated_code}
\`\`\`
**Review Comments:**
{review_comments}
**Task:**
Carefully apply the suggestions from the review comments to refactor the original code.
If the review comments state "No major issues found," return the original code unchanged.
Ensure the final code is complete, functional, and includes necessary imports and docstrings.
**Output:**
Output *only* the final, refactored Python code block, enclosed in triple backticks (\`\`\`python ... \`\`\`).
Do not add any other text before or after the code block.
`,
description: "Refactors code based on review comments.",
outputKey: "refactored_code", // Stores output in state['refactored_code']
});
// --- 2. Create the SequentialAgent ---
// This agent orchestrates the pipeline by running the sub_agents in order.
const rootAgent = new SequentialAgent({
name: "CodePipelineAgent",
subAgents: [codeWriterAgent, codeReviewerAgent, codeRefactorerAgent],
description: "Executes a sequence of code writing, reviewing, and refactoring.",
// The agents will run in the order provided: Writer -> Reviewer -> Refactorer
});
```
```go
model, err := gemini.NewModel(ctx, modelName, &genai.ClientConfig{})
if err != nil {
return fmt.Errorf("failed to create model: %v", err)
}
codeWriterAgent, err := llmagent.New(llmagent.Config{
Name: "CodeWriterAgent",
Model: model,
Description: "Writes initial Go code based on a specification.",
Instruction: `You are a Go Code Generator.
Based *only* on the user's request, write Go code that fulfills the requirement.
Output *only* the complete Go code block, enclosed in triple backticks ('''go ... ''').
Do not add any other text before or after the code block.`,
OutputKey: "generated_code",
})
if err != nil {
return fmt.Errorf("failed to create code writer agent: %v", err)
}
codeReviewerAgent, err := llmagent.New(llmagent.Config{
Name: "CodeReviewerAgent",
Model: model,
Description: "Reviews code and provides feedback.",
Instruction: `You are an expert Go Code Reviewer.
Your task is to provide constructive feedback on the provided code.
**Code to Review:**
'''go
{generated_code}
'''
**Review Criteria:**
1. **Correctness:** Does the code work as intended? Are there logic errors?
2. **Readability:** Is the code clear and easy to understand? Follows Go style guidelines?
3. **Idiomatic Go:** Does the code use Go's features in a natural and standard way?
4. **Edge Cases:** Does the code handle potential edge cases or invalid inputs gracefully?
5. **Best Practices:** Does the code follow common Go best practices?
**Output:**
Provide your feedback as a concise, bulleted list. Focus on the most important points for improvement.
If the code is excellent and requires no changes, simply state: "No major issues found."
Output *only* the review comments or the "No major issues" statement.`,
OutputKey: "review_comments",
})
if err != nil {
return fmt.Errorf("failed to create code reviewer agent: %v", err)
}
codeRefactorerAgent, err := llmagent.New(llmagent.Config{
Name: "CodeRefactorerAgent",
Model: model,
Description: "Refactors code based on review comments.",
Instruction: `You are a Go Code Refactoring AI.
Your goal is to improve the given Go code based on the provided review comments.
**Original Code:**
'''go
{generated_code}
'''
**Review Comments:**
{review_comments}
**Task:**
Carefully apply the suggestions from the review comments to refactor the original code.
If the review comments state "No major issues found," return the original code unchanged.
Ensure the final code is complete, functional, and includes necessary imports.
**Output:**
Output *only* the final, refactored Go code block, enclosed in triple backticks ('''go ... ''').
Do not add any other text before or after the code block.`,
OutputKey: "refactored_code",
})
if err != nil {
return fmt.Errorf("failed to create code refactorer agent: %v", err)
}
codePipelineAgent, err := sequentialagent.New(sequentialagent.Config{
AgentConfig: agent.Config{
Name: appName,
Description: "Executes a sequence of code writing, reviewing, and refactoring.",
SubAgents: []agent.Agent{
codeWriterAgent,
codeReviewerAgent,
codeRefactorerAgent,
},
},
})
if err != nil {
return fmt.Errorf("failed to create sequential agent: %v", err)
}
```
````java
import com.google.adk.agents.LlmAgent;
import com.google.adk.agents.SequentialAgent;
import com.google.adk.events.Event;
import com.google.adk.runner.InMemoryRunner;
import com.google.adk.sessions.Session;
import com.google.genai.types.Content;
import com.google.genai.types.Part;
import io.reactivex.rxjava3.core.Flowable;
public class SequentialAgentExample {
private static final String APP_NAME = "CodePipelineAgent";
private static final String USER_ID = "test_user_456";
private static final String MODEL_NAME = "gemini-2.0-flash";
public static void main(String[] args) {
SequentialAgentExample sequentialAgentExample = new SequentialAgentExample();
sequentialAgentExample.runAgent(
"Write a Java function to calculate the factorial of a number.");
}
public void runAgent(String prompt) {
LlmAgent codeWriterAgent =
LlmAgent.builder()
.model(MODEL_NAME)
.name("CodeWriterAgent")
.description("Writes initial Java code based on a specification.")
.instruction(
"""
You are a Java Code Generator.
Based *only* on the user's request, write Java code that fulfills the requirement.
Output *only* the complete Java code block, enclosed in triple backticks (```java ... ```).
Do not add any other text before or after the code block.
""")
.outputKey("generated_code")
.build();
LlmAgent codeReviewerAgent =
LlmAgent.builder()
.model(MODEL_NAME)
.name("CodeReviewerAgent")
.description("Reviews code and provides feedback.")
.instruction(
"""
You are an expert Java Code Reviewer.
Your task is to provide constructive feedback on the provided code.
**Code to Review:**
```java
{generated_code}
```
**Review Criteria:**
1. **Correctness:** Does the code work as intended? Are there logic errors?
2. **Readability:** Is the code clear and easy to understand? Follows Java style guidelines?
3. **Efficiency:** Is the code reasonably efficient? Any obvious performance bottlenecks?
4. **Edge Cases:** Does the code handle potential edge cases or invalid inputs gracefully?
5. **Best Practices:** Does the code follow common Java best practices?
**Output:**
Provide your feedback as a concise, bulleted list. Focus on the most important points for improvement.
If the code is excellent and requires no changes, simply state: "No major issues found."
Output *only* the review comments or the "No major issues" statement.
""")
.outputKey("review_comments")
.build();
LlmAgent codeRefactorerAgent =
LlmAgent.builder()
.model(MODEL_NAME)
.name("CodeRefactorerAgent")
.description("Refactors code based on review comments.")
.instruction(
"""
You are a Java Code Refactoring AI.
Your goal is to improve the given Java code based on the provided review comments.
**Original Code:**
```java
{generated_code}
```
**Review Comments:**
{review_comments}
**Task:**
Carefully apply the suggestions from the review comments to refactor the original code.
If the review comments state "No major issues found," return the original code unchanged.
Ensure the final code is complete, functional, and includes necessary imports and docstrings.
**Output:**
Output *only* the final, refactored Java code block, enclosed in triple backticks (```java ... ```).
Do not add any other text before or after the code block.
""")
.outputKey("refactored_code")
.build();
SequentialAgent codePipelineAgent =
SequentialAgent.builder()
.name(APP_NAME)
.description("Executes a sequence of code writing, reviewing, and refactoring.")
// The agents will run in the order provided: Writer -> Reviewer -> Refactorer
.subAgents(codeWriterAgent, codeReviewerAgent, codeRefactorerAgent)
.build();
// Create an InMemoryRunner
InMemoryRunner runner = new InMemoryRunner(codePipelineAgent, APP_NAME);
// InMemoryRunner automatically creates a session service. Create a session using the service
Session session = runner.sessionService().createSession(APP_NAME, USER_ID).blockingGet();
Content userMessage = Content.fromParts(Part.fromText(prompt));
// Run the agent
Flowable eventStream = runner.runAsync(USER_ID, session.id(), userMessage);
// Stream event response
eventStream.blockingForEach(
event -> {
if (event.finalResponse()) {
System.out.println(event.stringifyContent());
}
});
}
}
````
````kotlin
import com.google.adk.kt.agents.Instruction
import com.google.adk.kt.agents.LlmAgent
import com.google.adk.kt.agents.SequentialAgent
import com.google.adk.kt.models.Gemini
// --- Constants ---
private const val GEMINI_MODEL = "gemini-flash-latest"
// --- 1. Define Sub-Agents for Each Pipeline Stage ---
// Code Writer Agent
// Takes the initial specification (from user query) and writes code.
val codeWriterAgent =
LlmAgent(
name = "CodeWriterAgent",
model = Gemini(name = GEMINI_MODEL),
instruction =
Instruction(
"""
You are a Kotlin Code Generator.
Based *only* on the user's request, write Kotlin code that fulfills the requirement.
Output *only* the complete Kotlin code block, enclosed in triple backticks (```kotlin ... ```).
Do not add any other text before or after the code block.
""".trimIndent(),
),
description = "Writes initial Kotlin code based on a specification.",
outputKey = "generated_code",
)
// Code Reviewer Agent
// Takes the code generated by the previous agent (read from state) and provides feedback.
val codeReviewerAgent =
LlmAgent(
name = "CodeReviewerAgent",
model = Gemini(name = GEMINI_MODEL),
instruction =
Instruction(
"""
You are an expert Kotlin Code Reviewer.
Your task is to provide constructive feedback on the provided code.
**Code to Review:**
```kotlin
{generated_code}
```
**Review Criteria:**
1. **Correctness:** Does the code work as intended? Are there logic errors?
2. **Readability:** Is the code clear and easy to understand? Follows Kotlin coding conventions?
3. **Efficiency:** Is the code reasonably efficient? Any obvious performance bottlenecks?
4. **Edge Cases:** Does the code handle potential edge cases or invalid inputs gracefully?
5. **Best Practices:** Does the code follow common Kotlin best practices?
**Output:**
Provide your feedback as a concise, bulleted list. Focus on the most important points for improvement.
If the code is excellent and requires no changes, simply state: "No major issues found."
Output *only* the review comments or the "No major issues" statement.
""".trimIndent(),
),
description = "Reviews code and provides feedback.",
outputKey = "review_comments",
)
// Code Refactorer Agent
// Takes the original code and the review comments (read from state) and refactors the code.
val codeRefactorerAgent =
LlmAgent(
name = "CodeRefactorerAgent",
model = Gemini(name = GEMINI_MODEL),
instruction =
Instruction(
"""
You are a Kotlin Code Refactoring AI.
Your goal is to improve the given Kotlin code based on the provided review comments.
**Original Code:**
```kotlin
{generated_code}
```
**Review Comments:**
{review_comments}
**Task:**
Carefully apply the suggestions from the review comments to refactor the original code.
If the review comments state "No major issues found," return the original code unchanged.
Ensure the final code is complete, functional, and includes necessary imports and KDoc comments.
**Output:**
Output *only* the final, refactored Kotlin code block, enclosed in triple backticks (```kotlin ... ```).
Do not add any other text before or after the code block.
""".trimIndent(),
),
description = "Refactors code based on review comments.",
outputKey = "refactored_code",
)
// --- 2. Create the SequentialAgent ---
// This agent orchestrates the pipeline by running the subAgents in order.
val codePipelineAgent =
SequentialAgent(
name = "CodePipelineAgent",
subAgents = listOf(codeWriterAgent, codeReviewerAgent, codeRefactorerAgent),
description = "Executes a sequence of code writing, reviewing, and refactoring.",
)
private val rootAgent = codePipelineAgent
````
Global instructions for all agents
Apply consistent rules across your workflow using the `GlobalInstructionPlugin` on your `App` ([Python](https://github.com/google/adk-python/blob/main/src/google/adk/plugins/global_instruction_plugin.py), [TypeScript](https://github.com/google/adk-js/blob/main/core/src/plugins/global_instruction_plugin.ts), [Java](https://github.com/google/adk-java/blob/main/core/src/main/java/com/google/adk/plugins/GlobalInstructionPlugin.java)) or the `GlobalInstruction` parameter on your agent ([Go](https://github.com/google/adk-go/blob/main/agent/llmagent/llmagent.go)).
# Graph-based agent workflows
Supported in ADKPython v2.0.0TypeScript v2.0.0Go v2.0.0Kotlin v1.5.0
Graph-based agent workflows in ADK let you build agents with more precise control, creating deterministic processes that combine code logic and AI reasoning capabilities. Graph-based workflows allow you to define your agent logic as a graph of execution nodes and edges, combining AI-powered agent reasoning with deterministic tools and code.
**Figure 1.** A graph-based agent design for flight upgrades, combining workflow nodes of different types, including Functions, human input, Tools, and LLM capabilities.
Experimental in Kotlin
The Graph-based agent workflow feature is experimental in ADK Kotlin. It may have limitations and its behavior may change.
Prebuilt ADK [template workflows](/agents/workflow-agents/), such as [Sequential Agents](/agents/workflow-agents/sequential-agents/), provide a defined process flow control only across a set of agents. You can continue to build standard ADK agents with long prompts, tools, and use them in graph-based workflow agents. When you need more precise control, workflow agent graphs give you more flexibility over how tasks are routed and executed. Graph-based workflows provide the following advantages:
- **Define precise logic:** Explicitly map out routing logic to manage transitions between different nodes.
- **Implement complex structures:** Build agent workflows that support branching and state management.
- **Run chains of functions without AI:** Call agent tools and your own code without invoking a generative AI model.
- **Enhance reliability:** Improve the predictability of your agents by relying on structured node definitions rather than prompts alone.
Workflow styles in ADK
ADK offers three complementary ways to compose multi-step work:
- **Graph-based workflows** (this section): a declarative graph of nodes and edges with explicit routing — best for deterministic, structured processes.
- **[Dynamic workflows](/graphs/dynamic/):** programmatic orchestration in your own code (loops, conditionals, recursion) — best when the control flow is too complex or iterative for a static graph.
- **[Prebuilt workflow agents](/agents/workflow-agents/)** (sequential, parallel, loop): higher-level building blocks for common patterns without assembling a graph yourself.
## Get started
This section describes how to get started with graph-based agents. The following example shows how to create a sequential graph-based agent workflow that generates a city name, looks up the current time in that city with a code function, and the final agent reports the information.
```python
from google.adk import Agent
from google.adk import Workflow
from google.adk import Event
from pydantic import BaseModel
city_generator_agent = Agent(
name="city_generator_agent",
model="gemini-flash-latest",
instruction="""Return the name of a random city.
Return only the name, nothing else.""",
)
class CityTime(BaseModel):
time_info: str # time information
city: str # city name
def lookup_time_function(node_input: str):
"""Simulate returning the current time in the specified city."""
return CityTime(time_info="10:10 AM", city=node_input)
city_report_agent = Agent(
name="city_report_agent",
model="gemini-flash-latest",
input_schema=CityTime,
instruction="""You receive a CityTime with time_info and city fields.
Output a single line stating that it is that time in that city right
now.""",
)
def completed_message_function(node_input: str):
return Event(
message=f"{node_input}\n WORKFLOW COMPLETED.",
)
root_agent = Workflow(
name="root_agent",
edges=[
("START", city_generator_agent, lookup_time_function,
city_report_agent, completed_message_function)
],
)
```
In ADK TypeScript, a `Workflow` takes an `edges` array. Each row lists the nodes to run in order. The `node()` function wraps a function, an agent, a tool, or another `Workflow` as a graph node, and sets the node's name and its `inputSchema` and `outputSchema` contracts. Schemas are Zod objects or a genai `Schema`. Each node's return value is passed to the next node as its input, so you do not need to write to session state.
```typescript
import {
createEvent,
LlmAgent,
node,
NodeContext,
Workflow,
} from '@google/adk';
import { z } from 'zod';
const cityGeneratorAgent = new LlmAgent({
name: 'city_generator_agent',
model: 'gemini-flash-latest',
instruction: `Return the name of a random city.
Return only the name, nothing else.`,
});
/** The structured payload handed from the lookup node to the report agent. */
const cityTimeSchema = z.object({
timeInfo: z.string().describe('Time information.'),
city: z.string().describe('City name.'),
});
type CityTime = z.infer;
/** Simulates returning the current time in the specified city. */
function lookupTimeFunction(_ctx: NodeContext, nodeInput: string): CityTime {
return { timeInfo: '10:10 AM', city: nodeInput.trim() };
}
const cityReportAgent = new LlmAgent({
name: 'city_report_agent',
model: 'gemini-flash-latest',
instruction: `Output the following line:
It is {CityTime.timeInfo} in {CityTime.city} right now.`,
});
function completedMessageFunction(_ctx: NodeContext, nodeInput: string) {
return createEvent({
content: {
role: 'model',
parts: [{ text: `${nodeInput}\n WORKFLOW COMPLETED.` }],
},
});
}
export const rootAgent = new Workflow({
name: 'root_agent',
edges: [
[
'START',
cityGeneratorAgent,
node(lookupTimeFunction, {
name: 'lookup_time_function',
outputSchema: cityTimeSchema,
}),
node(cityReportAgent, { inputSchema: cityTimeSchema }),
node(completedMessageFunction, { name: 'completed_message_function' }),
],
],
});
```
In ADK Go, sequential workflows use the graph engine: `workflow.NewFunctionNode` wraps each step, and `workflow.Chain` wires the nodes into a sequential `edges` slice. The framework automatically passes each node's typed return value to the next node via `event.Output` — no session state writes are needed. The whole graph is wrapped in `workflowagent.New`, which produces a standard `agent.Agent`.
```go
// cityTime holds the data passed from the lookup step to the report step.
type cityTime struct {
City string
TimeInfo string
}
// newSequentialGetStarted builds a three-node sequential workflow using the
// v2 graph engine. Each node is a workflow.NewFunctionNode whose return value
// is automatically wrapped in session.Event.Output and forwarded to the next
// node as its typed input.
//
// This is the Go equivalent of the Python Workflow example:
//
// root_agent = Workflow(
// name="root_agent",
// edges=[("START", city_generator_agent, lookup_time_function,
// city_report_agent, completed_message_function)],
// )
func newSequentialGetStarted() (agent.Agent, error) {
// Step 1: return a city name. The string is set as event.Output and
// becomes the typed input of the next node.
cityGeneratorNode := workflow.NewFunctionNode("city_generator_agent",
func(_ agent.Context, _ any) (string, error) {
return "Tokyo", nil
},
workflow.NodeConfig{},
)
// Step 2: receive the city name and return structured time data.
lookupTimeNode := workflow.NewFunctionNode("lookup_time_function",
func(_ agent.Context, city string) (cityTime, error) {
return cityTime{City: city, TimeInfo: "10:10 AM"}, nil
},
workflow.NodeConfig{},
)
// Step 3: receive the cityTime struct and produce the final report string.
cityReportNode := workflow.NewFunctionNode("city_report_agent",
func(_ agent.Context, ct cityTime) (string, error) {
return fmt.Sprintf("It is %s in %s right now.\nWORKFLOW COMPLETED.",
ct.TimeInfo, ct.City), nil
},
workflow.NodeConfig{},
)
// workflow.Chain wires START → cityGeneratorNode → lookupTimeNode → cityReportNode.
// Data flows through event.Output: no session state writes needed.
return workflowagent.New(workflowagent.Config{
Name: "root_agent",
Description: "Sequential workflow: generate city → look up time → report.",
Edges: workflow.Chain(workflow.Start, cityGeneratorNode, lookupTimeNode, cityReportNode),
})
}
```
In ADK Kotlin, the `workflow` function builds a `Workflow` from the edges its block declares, and `chain` connects `Start` and each node in order. An `LlmAgent` is a node, and `node` wraps a function as a node with a typed input and output. Each node's output is passed to the next node as its input; an agent node receives its input as a user message.
```kotlin
@file:OptIn(ExperimentalWorkflowApi::class)
package com.google.adk.kt.examples.graphs.getstarted
import com.google.adk.kt.agents.Instruction
import com.google.adk.kt.agents.LlmAgent
import com.google.adk.kt.annotations.ExperimentalWorkflowApi
import com.google.adk.kt.models.Gemini
import com.google.adk.kt.types.Content
import com.google.adk.kt.types.Role
import com.google.adk.kt.types.Schema
import com.google.adk.kt.types.Type
import com.google.adk.kt.workflow.Start
import com.google.adk.kt.workflow.emit
import com.google.adk.kt.workflow.node
import com.google.adk.kt.workflow.workflow
val cityGeneratorAgent =
LlmAgent(
name = "city_generator_agent",
model = Gemini(name = "gemini-flash-latest"),
instruction =
Instruction(
"""
Return the name of a random city.
Return only the name, nothing else.
""".trimIndent(),
),
)
// The structured payload handed from the lookup node to the report agent.
val cityTimeSchema =
Schema(
type = Type.OBJECT,
properties =
mapOf(
"time_info" to Schema(type = Type.STRING, description = "Time information."),
"city" to Schema(type = Type.STRING, description = "City name."),
),
required = listOf("time_info", "city"),
)
// Simulate returning the current time in the specified city.
val lookupTimeFunction =
node>(
name = "lookup_time_function",
outputSchema = cityTimeSchema,
) { _, nodeInput ->
mapOf("time_info" to "10:10 AM", "city" to nodeInput.trim())
}
// The lookup result arrives as this agent's input, as JSON text.
val cityReportAgent =
LlmAgent(
name = "city_report_agent",
model = Gemini(name = "gemini-flash-latest"),
inputSchema = cityTimeSchema,
instruction =
Instruction(
"""
Output the following line, using the time_info and city from the input:
It is in right now.
""".trimIndent(),
),
)
val completedMessageFunction =
node("completed_message_function") { _, nodeInput ->
emit(Content.fromText(Role.MODEL, "$nodeInput\n WORKFLOW COMPLETED."))
}
val rootAgent =
workflow("root_agent") {
chain(
Start,
cityGeneratorAgent,
lookupTimeFunction,
cityReportAgent,
completedMessageFunction,
)
}
```
This sample code demonstrates how you can assemble a simple, sequential workflow and alternate between agent processing and code execution. While you could perform these steps using a single agent with a longer prompt and a tool call, the graph-based approach gives you precise control over the task execution order and the data output from each step.
For more information about data handling with graph-based workflows, see [Data handling with workflow nodes and agents](/graphs/data-handling/).
## Build processes with graphs
You can use prompt-based agents to define multiple step processes with descriptions of tasks and procedures using the instructions field of an ADK agent. However, as your instructions and procedures become longer and more complicated, making sure that the agent is following each step and guideline becomes more complicated and less reliable.
Graph-based workflow agents provide a significant advantage over prompt-based agents by allowing you to specifically define the overall process workflow in code. With graph-based agent workflows, each step of the process can be defined as an execution ***Node*** in a graph and each node can be an AI agent, Tool, or your programmed code. The following diagram illustrates how a simple prompt-based agent would translate into a workflow agent graph:
**Figure 2.** Structure of prompt-based agent instructions translated into a graph-based workflow.
Moving from prompt-based agents to graph-based workflow agents allows you to explicitly break out the tasks of a procedure to define a specific execution flow. Once defined, the agent application flows the steps in the graph, switching between non-deterministic AI-powered agents and deterministic code as needed.
The following code sample shows how the workflow graph in Figure 2 could be translated into a graph-based agent:
```python
process_message = Agent(
name="process_message",
model="gemini-flash-latest",
instruction="""Classify user message into either "BUG", "CUSTOMER_SUPPORT",
or "LOGISTICS". If you think a message applies to more than one category,
reply with a comma separated list of categories.
""",
)
def router(node_input: str):
routes = node_input.split(",")
routes = [route.strip() for route in routes]
return Event(route=routes)
def response_1_bug():
return Event(message="Handling bug...")
def response_2_support():
return Event(message="Handling customer support...")
def response_3_logistics():
return Event(message="Handling logistics...")
root_agent = Workflow(
name="routing_workflow",
edges=[
("START", process_message, router),
( router,
{
"BUG": response_1_bug,
"CUSTOMER_SUPPORT": response_2_support,
"LOGISTICS": response_3_logistics,
}
)
],
)
```
In ADK TypeScript, a router node returns an event carrying a `route` value, created with `createEvent({route})`. A second edge row maps each route value to the node that handles it. Setting `route` to an array dispatches to every matching branch, which lets the classifier in this example return more than one category. The `DEFAULT_ROUTE` setting catches any value that no branch matched.
```typescript
import {
createEvent,
DEFAULT_ROUTE,
LlmAgent,
node,
NodeContext,
Workflow,
} from '@google/adk';
/** The routes this graph has edges for. */
const ROUTES = ['BUG', 'CUSTOMER_SUPPORT', 'LOGISTICS'] as const;
const processMessage = new LlmAgent({
name: 'process_message',
model: 'gemini-flash-latest',
instruction: `Classify user message into either "BUG", "CUSTOMER_SUPPORT",
or "LOGISTICS". If you think a message applies to more than one category,
reply with a comma separated list of categories.
Reply with the categories only, nothing else.`,
});
const router = node(
(_ctx: NodeContext, nodeInput: string) => {
const text = String(nodeInput).toUpperCase();
const matched = ROUTES.filter((route) =>
new RegExp(`\\b${route}\\b`).test(text),
);
return createEvent({ route: matched.length > 0 ? matched : DEFAULT_ROUTE });
},
{ name: 'router' },
);
/** Emits a user-facing message: `content`, with no `output`. */
const message = (text: string) =>
createEvent({ content: { role: 'model', parts: [{ text }] } });
const response1Bug = node(() => message('Handling bug...'), {
name: 'response_1_bug',
});
const response2Support = node(() => message('Handling customer support...'), {
name: 'response_2_support',
});
const response3Logistics = node(() => message('Handling logistics...'), {
name: 'response_3_logistics',
});
const responseUnknown = node(
(_ctx: NodeContext, nodeInput: string) =>
message(`Could not classify that (classifier said: ${nodeInput}).`),
{ name: 'response_unknown' },
);
export const rootAgent = new Workflow({
name: 'routing_workflow',
edges: [
['START', processMessage, router],
[
router,
{
BUG: response1Bug,
CUSTOMER_SUPPORT: response2Support,
LOGISTICS: response3Logistics,
[DEFAULT_ROUTE]: responseUnknown,
},
],
],
});
```
In ADK Go, conditional routing uses `workflow.NewEmittingFunctionNode` to set `event.Routes` and `workflow.StringRoute` edges to dispatch to the matching handler — the direct equivalent of Python's `router` function and dict dispatch. `workflow.Concat` merges the chain and the conditional edges into a single `edges` slice passed to `workflowagent.New`.
```go
// classifyMessage is the router node. It emits ev.Routes to select which
// branch to follow — the Go equivalent of Python's:
//
// def router(node_input: str):
// return Event(route=["BUG"])
func classifyMessage(ctx agent.Context, msg string, emit func(*session.Event) error) (any, error) {
// In a real workflow this step calls an LLM; here we classify by keyword.
category := "LOGISTICS"
lower := strings.ToLower(msg)
switch {
case strings.Contains(lower, "bug") || strings.Contains(lower, "error"):
category = "BUG"
case strings.Contains(lower, "help") || strings.Contains(lower, "support"):
category = "CUSTOMER_SUPPORT"
}
ev := session.NewEvent(ctx, ctx.InvocationID())
ev.Routes = []string{category} // drives edge dispatch
ev.Output = msg // forward original message to the chosen handler
if err := emit(ev); err != nil {
return nil, err
}
return nil, nil // nil suppresses the automatic terminal event
}
// newProcessPipeline builds a classification + conditional-routing workflow
// using the v2 graph engine. The classifyMessage emitting node sets
// ev.Routes, and the graph engine dispatches to the matching handler via
// workflow.StringRoute.
//
// This is the Go equivalent of the Python Workflow example:
//
// root_agent = Workflow(
// name="routing_workflow",
// edges=[
// ("START", process_message, router),
// (router, {
// "BUG": response_1_bug,
// "CUSTOMER_SUPPORT": response_2_support,
// "LOGISTICS": response_3_logistics,
// }),
// ],
// )
func newProcessPipeline() (agent.Agent, error) {
classifyNode := workflow.NewEmittingFunctionNode(
"process_message", classifyMessage, workflow.NodeConfig{},
)
bugNode := workflow.NewFunctionNode("response_1_bug",
func(_ agent.Context, _ any) (string, error) {
return "Handling bug...", nil
},
workflow.NodeConfig{},
)
supportNode := workflow.NewFunctionNode("response_2_support",
func(_ agent.Context, _ any) (string, error) {
return "Handling customer support...", nil
},
workflow.NodeConfig{},
)
logisticsNode := workflow.NewFunctionNode("response_3_logistics",
func(_ agent.Context, _ any) (string, error) {
return "Handling logistics...", nil
},
workflow.NodeConfig{},
)
// workflow.Concat merges the sequential chain with the conditional edges.
// Each workflow.Edge carries a workflow.StringRoute matcher that the engine
// checks against ev.Routes emitted by classifyNode.
edges := workflow.Concat(
workflow.Chain(workflow.Start, classifyNode),
[]workflow.Edge{
{From: classifyNode, To: bugNode, Route: workflow.StringRoute("BUG")},
{From: classifyNode, To: supportNode, Route: workflow.StringRoute("CUSTOMER_SUPPORT")},
{From: classifyNode, To: logisticsNode, Route: workflow.StringRoute("LOGISTICS")},
},
)
return workflowagent.New(workflowagent.Config{
Name: "routing_workflow",
Description: "Classifies a message and routes it to the appropriate handler.",
Edges: edges,
})
}
```
In ADK Kotlin, a router node selects its outgoing edges by setting `context.routes`, and a `route { }` block maps each route value to the node that handles it. Setting more than one route runs every matching branch, which lets the classifier in this example return more than one category.
```kotlin
@file:OptIn(ExperimentalWorkflowApi::class)
package com.google.adk.kt.examples.graphs.index.processpipeline
import com.google.adk.kt.annotations.ExperimentalWorkflowApi
import com.google.adk.kt.agents.Instruction
import com.google.adk.kt.agents.LlmAgent
import com.google.adk.kt.models.Gemini
import com.google.adk.kt.types.Content
import com.google.adk.kt.types.Role
import com.google.adk.kt.workflow.Route
import com.google.adk.kt.workflow.Start
import com.google.adk.kt.workflow.emit
import com.google.adk.kt.workflow.node
import com.google.adk.kt.workflow.workflow
val processMessage =
LlmAgent(
name = "process_message",
model = Gemini(name = "gemini-flash-latest"),
instruction =
Instruction(
"""
Classify user message into either "BUG", "CUSTOMER_SUPPORT",
or "LOGISTICS". If you think a message applies to more than one category,
reply with a comma separated list of categories.
""".trimIndent(),
),
)
val router =
node("router") { context, nodeInput ->
context.routes = nodeInput.split(",").map { Route.Tag(it.trim()) }
}
val response1Bug =
node("response_1_bug") { _, _ ->
emit(Content.fromText(Role.MODEL, "Handling bug..."))
}
val response2Support =
node("response_2_support") { _, _ ->
emit(Content.fromText(Role.MODEL, "Handling customer support..."))
}
val response3Logistics =
node("response_3_logistics") { _, _ ->
emit(Content.fromText(Role.MODEL, "Handling logistics..."))
}
val rootAgent =
workflow("routing_workflow") {
chain(Start, processMessage, router)
router.route {
"BUG" then response1Bug
"CUSTOMER_SUPPORT" then response2Support
"LOGISTICS" then response3Logistics
}
}
```
This sample code demonstrates how you can compose a sequence of agents to define a graph with routes between a set of *nodes*, which are discrete tasks that can include agents, Tools, your code, and even additional workflow agents. For information about building advanced pipelines, see [Build graph routes for workflow agents](/graphs/routes/).
Global instructions for all agents
Apply consistent rules across your workflow using the `GlobalInstructionPlugin` on your `App` ([Python](https://github.com/google/adk-python/blob/main/src/google/adk/plugins/global_instruction_plugin.py), [TypeScript](https://github.com/google/adk-js/blob/main/core/src/plugins/global_instruction_plugin.ts), [Java](https://github.com/google/adk-java/blob/main/core/src/main/java/com/google/adk/plugins/GlobalInstructionPlugin.java)) or the `GlobalInstruction` parameter on your agent ([Go](https://github.com/google/adk-go/blob/main/agent/llmagent/llmagent.go)).
## Known limitations
There are some known limitations with graph-based workflows. They are *not compatible* with the following ADK features:
- **Integrations:** Some third-party [integrations](/integrations/) may not be compatible with graph-based workflows.
Go: graph workflow API
The `workflow` package in ADK Go is the direct equivalent of the Python `Workflow` class. Use `workflow.NewFunctionNode` and `workflow.NewAgentNode` to define nodes, `workflow.Chain` or `workflow.Concat` with `[]workflow.Edge` to wire them, and `workflowagent.New` to wrap the graph as a runnable agent. Conditional routing uses `workflow.StringRoute`, `workflow.IntRoute`, or `workflow.BoolRoute` matched against `event.Routes`. Fan-in is handled by `workflow.NewJoinNode`.
## Next steps
For advanced routing patterns and fan-out/join examples, see [Build graph routes for workflow agents](/graphs/routes/). For prebuilt higher-level alternatives (sequential, parallel, loop), see [Prebuilt workflow agents](/agents/workflow-agents/).
# Data handling for agent workflows
Supported in ADKPython v2.0.0TypeScript v2.0.0Go v2.0.0
Structuring and managing data between agents and graph-based nodes is critical for building reliable processes with ADK. This guide explains data handling within graph-based workflows and collaboration agents, including how information is transmitted and received between graph nodes. It covers the essential parameters for passing data, content, and state, and explains how to implement structured data transfer for both function and agent nodes using data format schemas and specific instruction syntax.
## Workflow data flow
Within a graph-based workflow, nodes pass data to downstream steps through events. A step writes its output to a named event field, and the next step receives it as its typed input.
In Python, data is exchanged between graph nodes using ***Events***. The key parameters for node data handling are:
- **`output`**: Parameter for passing information between *nodes*.
- **`message`**: Data intended as a response to a user.
- **`state`**: Data automatically persisted across nodes via ***Events*** throughout an ADK session.
In ADK TypeScript v2.0.0, nodes exchange data through events. The key fields for node data handling are:
- **`output`**: the value passed to the next node. Return a value directly and ADK wraps it in an event, or set the field explicitly with `createEvent({output})`.
- **`content`**: a message for the user. The runtime renders this field, but the graph does not pass it to the next node.
- **`route`**: the routing keys that select which conditional edge to follow.
Session state is separate from the event. A node reads and writes state through `ctx.state`, and the accumulated delta is attached to that node's events. State keys can carry a prefix that controls their lifetime and scope:
| Prefix | Scope |
| -------- | ------------------------------------------------ |
| `app:` | Shared across all users and sessions for the app |
| `user:` | Tied to the user, shared across their sessions |
| `temp:` | Discarded after the current invocation ends |
| *(none)* | Persists for the lifetime of the session |
In ADK Go v2.0.0, the data-passing mechanism depends on which agent style you use:
**workflow package** (`FunctionNode`, `AgentNode`, `DynamicNode`): nodes communicate through `session.Event` fields, mirroring Python closely:
- **`Event.Output`**: the node's return value, set automatically by the framework when a `FunctionNode` returns a non-`*genai.Content` value. The successor node receives this as its typed `input` parameter.
- **`Event.Routes`**: routing keys set explicitly by an emitting node to select which conditional edge to follow — the Go equivalent of Python's `Event(route=...)`.
- **`Event.NodeInfo`**: scheduler metadata (`path`, `MessageAsOutput`, `OutputFor`). Set by the workflow engine; nodes do not set this directly.
**Prebuilt workflow agents** (`sequentialagent`, `parallelagent`, `loopagent`): these agents communicate through session state:
- **`OutputKey`** on `llmagent.Config`: the framework writes the agent's final text response to `state[OutputKey]` after each turn.
- **`ctx.Session().State().Set` / `.Get`**: write or read arbitrary values from state inside custom code.
- **`{key}` in `Instruction`**: the framework substitutes `state["key"]` into the prompt before calling the model.
State keys may carry a prefix that controls their lifetime and scope:
| Prefix constant | Prefix string | Scope |
| ----------------------- | ------------- | ------------------------------------------------ |
| `session.KeyPrefixApp` | `"app:"` | Shared across all users and sessions for the app |
| `session.KeyPrefixUser` | `"user:"` | Tied to the user, shared across their sessions |
| `session.KeyPrefixTemp` | `"temp:"` | Discarded after the current invocation ends |
| *(none)* | — | Persists for the lifetime of the session |
### Node output
Each step in a workflow produces output for its successor.
Use the ***return*** or ***yield*** syntax to hand off data to the next node:
```python
from google.adk import Event
def my_function_node(node_input: str):
output_value = node_input.upper()
return Event(output=output_value) # "THE RESULT"
```
Use the ***return*** syntax when outputting ***Event*** data that does not require additional processing. When emitting data that requires additional processing, or if you are generating more than one data item, you can use more than one ***yield*** command. Each ***yield*** call emits a separate ***Event***, but only one of those events may carry an `output`, and that single value is what the next node of the graph receives. A ***return*** or ***yield*** command without a parameter passes a `None` value to the next node.
There are three equivalent ways to produce a node's output: return a value directly, return `createEvent({output})`, or yield events from an async generator to stream progress alongside the result.
```typescript
import { createEvent, node, NodeContext, Workflow } from '@google/adk';
const returnRawValue = node(
(_ctx: NodeContext, nodeInput: string) => nodeInput.toUpperCase(),
{ name: 'return_raw_value' },
);
const returnEventOutput = node(
(_ctx: NodeContext, nodeInput: string) =>
createEvent({ output: `${nodeInput}!` }),
{ name: 'return_event_output' },
);
const yieldProgressThenOutput = node(
async function* (_ctx: NodeContext, nodeInput: string) {
yield createEvent({
content: { role: 'model', parts: [{ text: 'Working on it...' }] },
});
yield createEvent({ output: `<<${nodeInput}>>` });
},
{ name: 'yield_progress_then_output' },
);
export const rootAgent = new Workflow({
name: 'node_output_workflow',
edges: [
['START', returnRawValue, returnEventOutput, yieldProgressThenOutput],
],
});
```
Caution: emit `output` from one event per execution
A node can yield any number of events carrying `output`, and ADK does not raise an error in this case. Each event overwrites the previous one, and the successor node receives only the final value. Use `content` for progress messages instead.
**workflow package**: a `FunctionNode` simply returns a typed Go value. The framework automatically wraps the return value in a `session.Event` and sets `Event.Output`. The successor node receives this value as its typed `input` parameter — no manual event construction needed:
```go
// newEventOutputPipeline demonstrates the primary data-passing mechanism for
// workflow package nodes: a FunctionNode returns a typed Go value, and the
// framework automatically sets event.Output to that value. The successor node
// receives it as its typed `input` parameter.
//
// This mirrors the Python pattern exactly:
//
// def my_function_node(node_input: str):
// return Event(output=node_input.upper())
//
// In Go, the function simply returns the value — no Event construction needed.
func newEventOutputPipeline() (agent.Agent, error) {
upperFn := func(_ agent.Context, input string) (string, error) {
return strings.ToUpper(input), nil
}
suffixFn := func(_ agent.Context, input string) (string, error) {
return input + " IS AWESOME!", nil
}
nodeA := workflow.NewFunctionNode("upper", upperFn, workflow.NodeConfig{})
nodeB := workflow.NewFunctionNode("suffix", suffixFn, workflow.NodeConfig{})
// workflow.Chain wires START → nodeA → nodeB. The output of nodeA is
// delivered as the typed input of nodeB via event.Output.
return workflowagent.New(workflowagent.Config{
Name: "event_output_pipeline",
Description: "Demonstrates Event.Output data flow between FunctionNodes.",
Edges: workflow.Chain(workflow.Start, nodeA, nodeB),
})
}
```
**Prebuilt workflow agents**: use `OutputKey` on `llmagent.Config` to save an agent's text response to session state, then reference it with `{key}` in downstream agents' `Instruction` templates:
```go
// newOutputKeyPipeline demonstrates the OutputKey mechanism for the prebuilt
// sequentialagent. When OutputKey is set on an llmagent.Config, the framework
// automatically writes the agent's final text response to session state under
// that key. Downstream agents read it by referencing {key} in their Instruction.
//
// This pattern applies to sequentialagent / parallelagent / loopagent.
// For the workflow package (FunctionNode / AgentNode), use Event.Output instead.
func newOutputKeyPipeline(ctx context.Context, geminiModel model.LLM) (agent.Agent, error) {
step1, err := llmagent.New(llmagent.Config{
Name: "step_1",
Model: geminiModel,
Description: "Transforms the user's text.",
Instruction: "Convert the user's message to uppercase. Output only the transformed text.",
OutputKey: "upper_result",
})
if err != nil {
return nil, fmt.Errorf("step1: %w", err)
}
step2, err := llmagent.New(llmagent.Config{
Name: "step_2",
Model: geminiModel,
Description: "Reports the transformed text.",
Instruction: "The transformed text is: {upper_result}. Report it to the user.",
})
if err != nil {
return nil, fmt.Errorf("step2: %w", err)
}
return sequentialagent.New(sequentialagent.Config{
AgentConfig: agent.Config{
Name: "output_key_pipeline",
SubAgents: []agent.Agent{step1, step2},
},
})
}
```
### Node output: passing structured data
You can pass longer, structured data in a serializable format:
```python
def my_function_node_3():
yield Event(
output={
"city_name": "Paris",
"city_time": "10:10 AM",
},
)
```
Caution: Event.output limitation
Nodes are only allowed to emit a single ***Event.output*** data payload per execution. This limitation means that while you can use more than one ***yield*** in a node, having two or more ***yield*** commands with an ***Event.output*** results in a runtime error.
The `output` field is not limited to text. Any serializable value is passed to the next node, which receives it as a typed object, with no JSON parsing or state reads required. Attaching an `outputSchema` to the producing node, or an `inputSchema` to the consuming node, makes the contract explicit and validates it at runtime:
```typescript
import { createEvent, node, NodeContext, Workflow } from '@google/adk';
import { z } from 'zod';
const cityInfoSchema = z.object({
cityName: z.string(),
cityTime: z.string(),
});
type CityInfo = z.infer;
const emitStructuredOutput = node(
async function* () {
yield createEvent({
output: { cityName: 'Paris', cityTime: '10:10 AM' } satisfies CityInfo,
});
},
{ name: 'emit_structured_output', outputSchema: cityInfoSchema },
);
const consumeStructuredOutput = node(
(_ctx: NodeContext, cityInfo: CityInfo) =>
`It is ${cityInfo.cityTime} in ${cityInfo.cityName} right now.`,
{ name: 'consume_structured_output', inputSchema: cityInfoSchema },
);
export const rootAgent = new Workflow({
name: 'structured_output_workflow',
edges: [['START', emitStructuredOutput, consumeStructuredOutput]],
});
```
**workflow package**: a `FunctionNode` can return any JSON-serializable Go struct. The framework serializes it into `Event.Output` and deserializes it back into the successor node's typed `input` parameter. There is no single-payload restriction — each node has exactly one typed return value:
```go
// newStructuredOutputPipeline shows how to pass a struct from one FunctionNode
// to another. The framework serialises the return value into event.Output and
// deserialises it back into the successor's typed input parameter.
//
// This is the Go equivalent of:
//
// class CityTime(BaseModel):
// time_info: str
// city: str
//
// def lookup_time_function(city: str):
// return Event(output=CityTime(time_info="10:10 AM", city=city))
//
// def city_report(node_input: CityTime):
// return Event(output=f"It is {node_input.time_info} in {node_input.city}.")
type CityTime struct {
TimeInfo string `json:"time_info"`
City string `json:"city"`
}
func newStructuredOutputPipeline(ctx context.Context, geminiModel model.LLM) (agent.Agent, error) {
lookupTimeFn := func(_ agent.Context, city string) (CityTime, error) {
// Simulate looking up the current time in the city.
return CityTime{TimeInfo: "10:10 AM", City: city}, nil
}
cityReportAgent, err := llmagent.New(llmagent.Config{
Name: "city_report_agent",
Model: geminiModel,
Description: "Reports the city and current time from the previous node's output.",
// When wrapped as an AgentNode, the predecessor's event.Output
// is delivered as the agent's user content. The {key} template
// syntax is not required — the struct fields are provided inline.
Instruction: "Report the city time information you received in a friendly sentence.",
})
if err != nil {
return nil, fmt.Errorf("cityReportAgent: %w", err)
}
lookupTimeNode := workflow.NewFunctionNode("lookup_time", lookupTimeFn, workflow.NodeConfig{})
cityReportNode, err := workflow.NewAgentNode(cityReportAgent, workflow.NodeConfig{})
if err != nil {
return nil, fmt.Errorf("NewAgentNode: %w", err)
}
return workflowagent.New(workflowagent.Config{
Name: "city_time_pipeline",
Edges: workflow.Chain(workflow.Start, lookupTimeNode, cityReportNode),
SubAgents: []agent.Agent{cityReportAgent},
})
}
```
**Prebuilt workflow agents**: use multiple `OutputKey` values, one per agent, to store individual fields in session state. Downstream agents read each field independently via `{key}` in their `Instruction`.
### Routing output
Use the `route` parameter of an ***Event*** to drive conditional edge dispatch:
```python
def router(node_input: str):
return Event(route="BUG")
```
The `route` value is independent of `output`, so one event can both select a branch and forward a payload to it. The `DEFAULT_ROUTE` setting catches any value that no other branch matched:
```typescript
import {
createEvent,
DEFAULT_ROUTE,
node,
NodeContext,
Workflow,
} from '@google/adk';
const router = node(
(_ctx: NodeContext, nodeInput: string) =>
createEvent({
route: /bug|crash|error/i.test(nodeInput) ? 'BUG' : 'OTHER',
output: nodeInput,
}),
{ name: 'router' },
);
const handleBug = node(
(_ctx: NodeContext, nodeInput: string) => `Filed a bug for: ${nodeInput}`,
{ name: 'handle_bug' },
);
const handleAnythingElse = node(
(_ctx: NodeContext, nodeInput: string) => `No bug detected in: ${nodeInput}`,
{ name: 'handle_anything_else' },
);
export const rootAgent = new Workflow({
name: 'routing_output_workflow',
edges: [
['START', router],
[
router,
{
BUG: handleBug,
[DEFAULT_ROUTE]: handleAnythingElse,
},
],
],
});
```
**workflow package**: an emitting `FunctionNode` constructs a `session.Event` directly, sets `Event.Routes` to the desired route keys, and sets `Event.Output` to forward the payload to the successor. The workflow engine reads `Event.Routes` at dispatch time to select the matching edge:
```go
// classifyAndRoute shows how to set event.Routes alongside event.Output from
// an emitting FunctionNode. The function constructs a session.Event directly,
// sets Routes to select the conditional edge, and sets Output to forward the
// payload to the successor node.
//
// This mirrors the Python pattern:
//
// def router(node_input: str):
// return Event(route="BUG")
func classifyAndRoute(ctx agent.Context, msg string, emit func(*session.Event) error) (any, error) {
category := classifyMessage(msg)
ev := session.NewEvent(ctx, ctx.InvocationID())
ev.Routes = []string{category} // drives edge dispatch
ev.Output = msg // forwarded as typed input to the successor
if err := emit(ev); err != nil {
return nil, err
}
return nil, nil // nil suppresses the automatic terminal event
}
func classifyMessage(msg string) string {
switch {
case strings.Contains(strings.ToLower(msg), "bug"):
return "BUG"
case strings.Contains(strings.ToLower(msg), "help"):
return "CUSTOMER_SUPPORT"
default:
return "LOGISTICS"
}
}
func newRoutingPipeline() (agent.Agent, error) {
classifyNode := workflow.NewEmittingFunctionNode("classify", classifyAndRoute, workflow.NodeConfig{})
bugHandler := workflow.NewFunctionNode("bug_handler",
func(_ agent.Context, msg string) (string, error) {
return "Handling bug: " + msg, nil
}, workflow.NodeConfig{})
supportHandler := workflow.NewFunctionNode("support_handler",
func(_ agent.Context, msg string) (string, error) {
return "Handling support: " + msg, nil
}, workflow.NodeConfig{})
logisticsHandler := workflow.NewFunctionNode("logistics_handler",
func(_ agent.Context, msg string) (string, error) {
return "Handling logistics: " + msg, nil
}, workflow.NodeConfig{})
edges := workflow.Concat(
workflow.Chain(workflow.Start, classifyNode),
[]workflow.Edge{
{From: classifyNode, To: bugHandler, Route: workflow.StringRoute("BUG")},
{From: classifyNode, To: supportHandler, Route: workflow.StringRoute("CUSTOMER_SUPPORT")},
{From: classifyNode, To: logisticsHandler, Route: workflow.StringRoute("LOGISTICS")},
},
)
return workflowagent.New(workflowagent.Config{
Name: "routing_pipeline",
Description: "Classifies and routes a message using Event.Routes.",
Edges: edges,
})
}
```
### User-facing messages
Use the ***message*** parameter of an ***Event*** to send a response to a user rather than pass data to the next node:
```python
async def user_message(node_input: str):
"""Tell user research process is starting."""
yield Event(message="Beginning research process...")
```
A message for the user is the event's `content` field. The runtime renders `content`, but the graph does not pass it to the next node. Use `content` for the user and `output` for the next node. A node can emit both by sending two events, where only one carries `output`:
```typescript
import { createEvent, node, NodeContext, Workflow } from '@google/adk';
/** Emits a user-facing message: `content`, with no `output`. */
const message = (text: string) =>
createEvent({ content: { role: 'model', parts: [{ text }] } });
const userMessage = node(
async function* (_ctx: NodeContext, nodeInput: string) {
yield message(`Beginning research process for "${nodeInput}"...`);
},
{ name: 'user_message' },
);
const research = node(
async function* (_ctx: NodeContext) {
yield message('Gathering sources...');
yield createEvent({ output: ['source-a', 'source-b', 'source-c'] });
},
{ name: 'research' },
);
const report = node(
(_ctx: NodeContext, sources: string[]) =>
`Research complete. ${sources.length} sources: ${sources.join(', ')}.`,
{ name: 'report' },
);
export const rootAgent = new Workflow({
name: 'user_message_workflow',
edges: [['START', userMessage, research, report]],
});
```
**workflow package**: to emit a user-visible message without advancing the node's typed output, set `Event.Content` on an intermediate event emitted via the `emit` callback in an `EmittingFunctionNode`. The terminal return value (or `nil`) controls `Event.Output`.
**Prebuilt workflow agents**: any `llmagent` step automatically emits its model response as a user-facing event. For non-LLM steps, write a custom `Run` function on an `agent.Agent` that yields events whose `LLMResponse.Content` contains the text.
### Session state and state scopes
Session state persists data across turns within a session. It is the primary data-sharing mechanism for the prebuilt workflow agents, and is also available inside tools and callbacks regardless of which agent style you use.
Use the ***state*** parameter of an ***Event*** to maintain values across nodes. Nodes can modify state values, and the modified state values are available to downstream nodes:
```python
async def init_state_node(attempts: int = 0):
yield Event(
state={
"attempts": attempts,
},
)
async def task_attempt_node(node_input, attempts: int):
yield Event(
state={
"attempts": attempts + 1,
},
)
async def read_state_node(ctx: Context):
print(f"attempts state: {ctx.state['attempts']}") # attempts state: 1
root_agent = Workflow(
name="root_agent",
edges=[("START", init_state_node, task_attempt_node, read_state_node)],
)
```
Caution: `state` property data limitations
The state parameter *should not be used to persist large amounts of data* between nodes. Use artifacts or other data persistence mechanisms, such as database Tools, to persist large data resources during the life cycle of a Workflow.
Write state through `ctx.state` rather than returning it. A write is visible to every later node in the same run, and is committed with the writing node's events:
```typescript
import { node, NodeContext, Workflow } from '@google/adk';
const initStateNode = node(
(ctx: NodeContext, nodeInput: string) => {
ctx.state.set('topic', nodeInput.trim());
ctx.state.set('temp:started_at', new Date().toISOString());
ctx.state.set('attempts', 0);
},
{ name: 'init_state_node' },
);
const taskAttemptNode = node(
(ctx: NodeContext) => {
const attempts = ctx.state.get('attempts') ?? 0;
ctx.state.set('attempts', attempts + 1);
},
{ name: 'task_attempt_node' },
);
const readStateNode = node(
(ctx: NodeContext) =>
`attempts state: ${ctx.state.get('attempts')} ` +
`(topic: ${ctx.state.get('topic')}, ` +
`started: ${ctx.state.get('temp:started_at')})`,
{ name: 'read_state_node' },
);
export const rootAgent = new Workflow({
name: 'session_state_workflow',
edges: [['START', initStateNode, taskAttemptNode, readStateNode]],
});
```
Caution: `state` data limitations
Session state is a lightweight key-value store. Do not use it to move large payloads between nodes; use artifacts or a database tool instead. When only the next node needs a value, pass it along the edge as node `output`. Use state when a value must outlive the run, or be read by a tool, a callback, or `{key}` instruction templating.
State is written with `ctx.Session().State().Set(key, value)` and read with `.Get(key)`. The `session` package defines prefix constants that map to the same lifetime scopes as Python's state parameter. This pattern applies to prebuilt workflow agents and to tools and callbacks in any agent style:
```go
// stateScopes shows how session-state key prefixes control the lifetime and
// visibility of stored values. This pattern applies to the prebuilt workflow
// agents (sequentialagent / parallelagent / loopagent) and to tools and
// callbacks. For the workflow package (FunctionNode / AgentNode), prefer
// returning values directly via Event.Output.
//
// Available prefixes:
//
// session.KeyPrefixApp ("app:") – shared across all users and sessions
// session.KeyPrefixUser ("user:") – tied to the user, shared across sessions
// session.KeyPrefixTemp ("temp:") – discarded after the current invocation
//
// Keys with no prefix persist for the lifetime of the session.
func stateScopes(ctx agent.Context) error {
st := ctx.Session().State()
// Session-scoped (no prefix) — persists for the life of this session.
if err := st.Set("attempts", 0); err != nil {
return fmt.Errorf("state.Set attempts: %w", err)
}
// App-scoped — shared across all users and sessions for this app.
if err := st.Set(session.KeyPrefixApp+"global_counter", 42); err != nil {
return fmt.Errorf("state.Set app:global_counter: %w", err)
}
// User-scoped — shared across all sessions belonging to this user.
if err := st.Set(session.KeyPrefixUser+"login_count", 1); err != nil {
return fmt.Errorf("state.Set user:login_count: %w", err)
}
// Temp-scoped — discarded after this invocation ends.
if err := st.Set(session.KeyPrefixTemp+"scratch", "ephemeral"); err != nil {
return fmt.Errorf("state.Set temp:scratch: %w", err)
}
return nil
}
```
Caution: state data limitations
Session state is a lightweight key-value store. Do not use it to persist large payloads such as file contents or binary data. Use ADK artifacts or external storage tools instead.
workflow package: prefer Event.Output over state
For the `workflow` package (`FunctionNode`, `AgentNode`, `DynamicNode`), pass data between nodes by returning typed values — the framework sets `Event.Output` automatically. Only use `State().Set` when you need to share values with tools, callbacks, or agent `Instruction` templates.
## Constrain node data with schemas
You can set input and output data schemas to constrain the data formats accepted and produced by any agent node.
Use `input_schema` and `output_schema` with a class that extends ***BaseModel*** to constrain any agent's input and output:
```python
from datetime import date
from google.adk import Agent
from pydantic import BaseModel
class FlightSearchInput(BaseModel):
origin: str # Airport code "SFO"
destination: str # Airport code "CDG"
departure_date: date # date(2026, 3, 15)
passengers: int = 1 # Number of passengers
class Flight(BaseModel):
carrier: str
price: float
def search_flights_api(
origin: str, destination: str, departure_date: date, passengers: int
) -> list[dict]:
"""Look up flights with your own flight-search provider."""
...
class FlightSearchOutput(BaseModel):
flights: list[Flight]
cheapest_price: float
flight_searcher = Agent(
name="flight_searcher",
instruction="Search for available flights.",
input_schema=FlightSearchInput,
output_schema=FlightSearchOutput,
tools=[search_flights_api],
mode="single_turn",
...
)
assistant = Agent(
name="assistant",
instruction="You help users plan trips.",
sub_agents=[flight_searcher],
...
)
```
Schemas are Zod objects or a genai `Schema`. The location of the schema determines its effect:
- The `LlmAgent.outputSchema` option requires the model to answer in that shape.
- The `LlmAgent.inputSchema` option applies only when the agent is exposed as a tool. Inside a graph, set the schema that validates a node's input on the node itself, using `node(agent, {inputSchema})`.
Agents in a graph must run in `single_turn` mode, which is the default, or `task` mode.
```typescript
import {
FunctionTool,
LlmAgent,
node,
NodeContext,
Workflow,
} from '@google/adk';
import { z } from 'zod';
const flightSearchInputSchema = z.object({
origin: z.string().describe('Origin airport code, e.g. "SFO".'),
destination: z.string().describe('Destination airport code, e.g. "CDG".'),
departureDate: z.string().describe('Departure date, e.g. "2026-03-15".'),
passengers: z.number().describe('Number of passengers.'),
});
type FlightSearchInput = z.infer;
const flightSchema = z.object({
carrier: z.string(),
flightNumber: z.string(),
price: z.number(),
});
const flightSearchOutputSchema = z.object({
flights: z.array(flightSchema),
cheapestPrice: z.number(),
});
type FlightSearchOutput = z.infer;
/** Stands in for a real flight-search API. */
const searchFlightsApi = new FunctionTool({
name: 'search_flights_api',
description: 'Searches available flights for a route and date.',
parameters: flightSearchInputSchema,
execute: ({ origin, destination }) => [
{
carrier: 'AF',
flightNumber: `AF${origin.length}${destination.length}0`,
price: 812.4,
},
{
carrier: 'UA',
flightNumber: `UA${origin.length}${destination.length}1`,
price: 947.0,
},
],
});
const parseRequest = node(
(_ctx: NodeContext, nodeInput: string): FlightSearchInput => {
const codes = nodeInput.toUpperCase().match(/\b[A-Z]{3}\b/g) ?? [];
const date = nodeInput.match(/\d{4}-\d{2}-\d{2}/)?.[0];
const passengers = Number(
nodeInput.match(/(\d+)\s*(people|pax|passengers?)/i)?.[1],
);
return {
origin: codes[0] ?? 'SFO',
destination: codes[1] ?? 'CDG',
departureDate: date ?? '2026-03-15',
passengers: Number.isFinite(passengers) ? passengers : 1,
};
},
{ name: 'parse_request', outputSchema: flightSearchInputSchema },
);
const flightSearcher = new LlmAgent({
name: 'flight_searcher',
model: 'gemini-flash-latest',
mode: 'single_turn',
instruction:
'Search for available flights with the search_flights_api tool and report ' +
'every flight it returns plus the cheapest price.',
inputSchema: flightSearchInputSchema,
outputSchema: flightSearchOutputSchema,
tools: [searchFlightsApi],
});
const renderResults = node(
(_ctx: NodeContext, results: FlightSearchOutput) =>
`Cheapest: $${results.cheapestPrice}\n` +
results.flights
.map((f) => ` ${f.carrier} ${f.flightNumber} — $${f.price}`)
.join('\n'),
{ name: 'render_results', inputSchema: flightSearchOutputSchema },
);
export const rootAgent = new Workflow({
name: 'flight_workflow',
edges: [
[
'START',
parseRequest,
node(flightSearcher, { inputSchema: flightSearchInputSchema }),
renderResults,
],
],
});
```
**workflow package**: use `workflow.NewAgentNodeTyped[Input, Output]` to attach schemas to an agent node. The generic type parameters are reflected into `*jsonschema.Schema` automatically — no hand-built schema construction needed. The node's `Event.Output` carries the structured result to the successor — no `OutputKey` or state write is needed:
```go
// FlightSearchInput is the typed input schema for the flight-search agent node.
// workflow.NewAgentNodeTyped[FlightSearchInput, FlightSearchOutput] reflects
// these structs into *jsonschema.Schema automatically — no hand-built schema
// construction needed.
type FlightSearchInput struct {
Origin string `json:"origin" jsonschema:"Departure airport code e.g. SFO"`
Destination string `json:"destination" jsonschema:"Arrival airport code e.g. CDG"`
DepartureDate string `json:"departure_date" jsonschema:"Travel date in YYYY-MM-DD format"`
}
// FlightSearchOutput is the typed output schema for the flight-search agent node.
type FlightSearchOutput struct {
CheapestPrice string `json:"cheapest_price" jsonschema:"Cheapest available fare e.g. $450"`
FlightCount string `json:"flight_count" jsonschema:"Number of matching flights found"`
}
// newSchemaAgentPipeline demonstrates workflow.NewAgentNodeTyped, which infers
// *jsonschema.Schema from the generic type parameters. This is the Go equivalent
// of Python's:
//
// flight_searcher = Agent(
// input_schema=FlightSearchInput,
// output_schema=FlightSearchOutput,
// ...
// )
//
// The node's event.Output carries the structured result to the successor —
// no OutputKey or state write is needed.
func newSchemaAgentPipeline(ctx context.Context, geminiModel model.LLM) (agent.Agent, error) {
flightSearchAgent, err := llmagent.New(llmagent.Config{
Name: "flight_searcher",
Model: geminiModel,
Description: "Searches for available flights and returns structured results.",
Instruction: `You are a flight-search assistant. Respond ONLY with a JSON object.`,
})
if err != nil {
return nil, fmt.Errorf("flightSearchAgent: %w", err)
}
synthAgent, err := llmagent.New(llmagent.Config{
Name: "trip_assistant",
Model: geminiModel,
Description: "Summarises flight search results for the user.",
Instruction: `You help users plan trips. Summarise the flight result you received.`,
})
if err != nil {
return nil, fmt.Errorf("synthAgent: %w", err)
}
// NewAgentNodeTyped[In, Out] reflects FlightSearchInput and FlightSearchOutput
// into *jsonschema.Schema automatically. The node enforces the input schema
// and constrains the model reply to the output schema's shape.
flightNode, err := workflow.NewAgentNodeTyped[FlightSearchInput, FlightSearchOutput](flightSearchAgent, workflow.NodeConfig{})
if err != nil {
return nil, fmt.Errorf("flightNode: %w", err)
}
synthNode, err := workflow.NewAgentNode(synthAgent, workflow.NodeConfig{})
if err != nil {
return nil, fmt.Errorf("synthNode: %w", err)
}
return workflowagent.New(workflowagent.Config{
Name: "flight_booking_pipeline",
Edges: workflow.Chain(workflow.Start, flightNode, synthNode),
SubAgents: []agent.Agent{flightSearchAgent, synthAgent},
})
}
```
Function and dynamic nodes do not infer a schema from their type parameters. To validate their input against a JSON schema, use the `WithSchema` variants of their constructors, such as `workflow.NewFunctionNodeWithSchema`.
**Prebuilt workflow agents**: set `InputSchema` and `OutputSchema` on `llmagent.Config`. The `OutputSchema` field forces the model to reply with a JSON object matching the schema. Use `OutputKey` to save the JSON string to state for downstream agents to reference via `{key}` in their `Instruction`.
## Access structured data in agents
Use `input_schema` on an agent to validate the structured output of the preceding node and deliver it as the agent's input. Refer to the fields of that input by name in the instruction:
```python
class CityTime(BaseModel):
time_info: str # time information
city: str # city name
def lookup_time_function(node_input: str):
"""Simulate returning the current time in the specified city."""
return Event(output=CityTime(time_info='10:10 AM', city=node_input))
city_report_agent = Agent(
name="city_report_agent",
model="gemini-flash-latest",
input_schema=CityTime,
instruction="""
You receive a CityTime with time_info and city fields. Reply with a
single sentence stating that it is that time in that city right now.
""",
)
root_agent = Workflow(
name="root_agent",
edges=[
("START", city_generator_agent, lookup_time_function, city_report_agent)
],
)
```
The curly-brace `{key}` placeholders that an `instruction` supports are substituted from *session state*, not from the node input, so a placeholder such as `{CityTime.time_info}` is left in the prompt verbatim. To template a value into an instruction, write it to state first, with `output_key` or `Event(state=...)`, and reference it as `{time_info}`.
Two data-selection forms are available inside an agent instruction:
- The `{Class.field}` form reads a field from this node's input.
- The `` form reads a field from a named predecessor's output. Use this form when several upstream nodes share a field name.
Both forms are distinct from `{state_key}`, which reads session state. The `Class.` prefix is documentation only; resolution uses the field name after the dot.
```typescript
import { LlmAgent, node, NodeContext, Workflow } from '@google/adk';
import { z } from 'zod';
const cityTimeSchema = z.object({
timeInfo: z.string().describe('Time information.'),
city: z.string().describe('City name.'),
});
type CityTime = z.infer;
const cityGeneratorAgent = new LlmAgent({
name: 'city_generator_agent',
model: 'gemini-flash-latest',
instruction: 'Return the name of a random city. Return only the name.',
});
/** Simulates returning the current time in the specified city. */
const lookupTimeFunction = node(
(_ctx: NodeContext, city: string): CityTime => ({
timeInfo: '10:10 AM',
city: city.trim(),
}),
{ name: 'lookup_time_function', outputSchema: cityTimeSchema },
);
const cityReportAgent = new LlmAgent({
name: 'city_report_agent',
model: 'gemini-flash-latest',
instruction:
'Return a sentence in the following format: It is ' +
' in ' +
' right now.',
});
export const rootAgent = new Workflow({
name: 'root_agent',
edges: [
[
'START',
cityGeneratorAgent,
lookupTimeFunction,
node(cityReportAgent, { inputSchema: cityTimeSchema }),
],
],
});
```
In ADK Go v2.0.0, a `FunctionNode` returns a typed struct and the framework serializes it into `Event.Output`. The successor `AgentNode` receives the struct as its user content — the fields are available to the agent's `Instruction` without any `{key}` template syntax. This is the direct equivalent of Python's `input_schema=CityTime`: in both languages the struct fields are delivered as typed input rather than looked up by name from state.
```go
// newStructuredOutputPipeline shows how to pass a struct from one FunctionNode
// to another. The framework serialises the return value into event.Output and
// deserialises it back into the successor's typed input parameter.
//
// This is the Go equivalent of:
//
// class CityTime(BaseModel):
// time_info: str
// city: str
//
// def lookup_time_function(city: str):
// return Event(output=CityTime(time_info="10:10 AM", city=city))
//
// def city_report(node_input: CityTime):
// return Event(output=f"It is {node_input.time_info} in {node_input.city}.")
type CityTime struct {
TimeInfo string `json:"time_info"`
City string `json:"city"`
}
func newStructuredOutputPipeline(ctx context.Context, geminiModel model.LLM) (agent.Agent, error) {
lookupTimeFn := func(_ agent.Context, city string) (CityTime, error) {
// Simulate looking up the current time in the city.
return CityTime{TimeInfo: "10:10 AM", City: city}, nil
}
cityReportAgent, err := llmagent.New(llmagent.Config{
Name: "city_report_agent",
Model: geminiModel,
Description: "Reports the city and current time from the previous node's output.",
// When wrapped as an AgentNode, the predecessor's event.Output
// is delivered as the agent's user content. The {key} template
// syntax is not required — the struct fields are provided inline.
Instruction: "Report the city time information you received in a friendly sentence.",
})
if err != nil {
return nil, fmt.Errorf("cityReportAgent: %w", err)
}
lookupTimeNode := workflow.NewFunctionNode("lookup_time", lookupTimeFn, workflow.NodeConfig{})
cityReportNode, err := workflow.NewAgentNode(cityReportAgent, workflow.NodeConfig{})
if err != nil {
return nil, fmt.Errorf("NewAgentNode: %w", err)
}
return workflowagent.New(workflowagent.Config{
Name: "city_time_pipeline",
Edges: workflow.Chain(workflow.Start, lookupTimeNode, cityReportNode),
SubAgents: []agent.Agent{cityReportAgent},
})
}
```
For a complete example of this workflow, see [Graph-based agent workflows](/graphs/#get-started).
# Dynamic agent workflows
Supported in ADKPython v2.0.0TypeScript v2.0.0Go v2.0.0
The ADK framework provides a programmatic way to define workflows as a more flexible and powerful alternative to [graph-based workflows](/graphs/). Using a graph-based approach provides a convenient way to compose multi-step, static process structures with workflow nodes. However, if the logic path for your workflow is more complex, with iterative loops or complex branching logic, a graph-based approach may not suit your needs, or may become too unwieldy to manage.
Dynamic workflows in ADK allow you to put aside graph-based path structures and use the full power of your chosen programming language to build workflows. With dynamic workflows, you can create workflows with simple decorators (Python) or constructor functions (Go), invoke workflow nodes as functions, and build complex routing logic. Here are some of the benefits of dynamic workflows in ADK:
- **Flexible Control Flow:** Define execution order dynamically using loops, conditionals, and recursion which are difficult or impossible to represent in static graphs.
- **Programmatic Experience:** Use familiar constructs like `while` loops and `async/await` (Python) or `for` loops and `workflow.RunNode` (Go) instead of graph-based routing.
- **Automatic Checkpointing:** Dynamic workflows track each node execution. Successful sub-nodes are automatically skipped when resuming the workflow, making complex logic durable and resumable by default.
- **Encapsulation:** Wrap business logic into *parent* nodes that internally compose lower-level nodes, keeping the overall workflow clean and manageable.
## Get started
The following dynamic workflow code example shows how to define a basic workflow containing a single node with a function:
```python
from google.adk import Context
from google.adk import Workflow
from google.adk.workflow import node
from typing import Any
@node(name="hello_node")
def my_node(node_input: Any):
return "Hello World"
# define a dynamic workflow node
@node(rerun_on_resume=True)
async def my_workflow(ctx: Context, node_input: str) -> str:
# run_node executes a node and returns its output
result = await ctx.run_node(my_node, node_input="hello")
return result
# Run the workflow
root_agent = Workflow(
name="root_agent",
edges=[("START", my_workflow)],
)
```
This example uses the [***@node***](#node) annotation for convenience and to keep the written code as simple as possible. This annotation generates wrappers that allow the code to be run in the context of an ADK dynamic workflow.
TypeScript has no `@node` decorator. Use the `node(fn, options)` factory function instead. The `ctx.runNode()` method is the equivalent of `ctx.run_node()`:
```typescript
import { node, NodeContext, Workflow } from '@google/adk';
const myNode = node(() => 'Hello World', { name: 'hello_node' });
const myWorkflow = node(
async (ctx: NodeContext, _nodeInput: string) => {
const result = await ctx.runNode(myNode, 'hello');
return result.output;
},
{ name: 'my_workflow', rerunOnResume: true },
);
export const rootAgent = new Workflow({
name: 'root_agent',
edges: [['START', myWorkflow]],
});
```
When you write an orchestrator node, two details affect how you read results and how the workflow behaves after a pause:
- The `ctx.runNode()` method resolves to a node result, not to the output value. Read the `.output` property to get the value.
- An orchestrator that calls `ctx.runNode()` must set `rerunOnResume: true`. This setting causes the node body to re-run on resume, so already-finished children are replayed from their checkpoints instead of being executed again.
In Go, `workflow.NewFunctionNode` replaces the `@node` decorator and `workflow.NewDynamicNode` replaces the `@node(rerun_on_resume=True)` async orchestrator. `workflow.RunNode` is the direct equivalent of `ctx.run_node()`. `workflowagent.New` with `workflow.Chain` replaces `Workflow(edges=[...])`.
Resume behaviour after a human-in-the-loop pause is controlled by `NodeConfig.RerunOnResume` — see [Nodes](#node) below for details.
```go
// helloNode is a simple FunctionNode that returns "Hello World".
// In Python this would be written as:
//
// @node(name="hello_node")
// def my_node(node_input: Any):
// return "Hello World"
//
// In Go, workflow.NewFunctionNode wraps the same logic with the
// required node interface, inferring input and output types from
// the generic parameters.
var helloNode = workflow.NewFunctionNode("hello_node",
func(_ agent.Context, _ string) (string, error) {
return "Hello World", nil
},
workflow.NodeConfig{},
)
// myWorkflow is a dynamic orchestrator node. It calls workflow.RunNode
// to schedule helloNode as a child and returns its output.
// In Python this would be:
//
// @node(rerun_on_resume=True)
// async def my_workflow(ctx: Context, node_input: str) -> str:
// result = await ctx.run_node(my_node, node_input="hello")
// return result
//
// workflow.NewDynamicNode defaults RerunOnResume to &true, matching the
// Python @node(rerun_on_resume=True) behaviour.
var myWorkflow = workflow.NewDynamicNode[string, string]("my_workflow",
func(ctx agent.Context, _ string, _ func(*session.Event) error) (string, error) {
return workflow.RunNode[string](ctx, helloNode, "hello")
},
workflow.NodeConfig{},
)
func runGetStarted() error {
ctx := context.Background()
// workflowagent.New creates an agent.Agent backed by the workflow engine.
// workflow.Chain(workflow.Start, myWorkflow) produces the edges slice
// equivalent to Python's edges=[("START", my_workflow)].
wa, err := workflowagent.New(workflowagent.Config{
Name: "root_agent",
Description: "A minimal dynamic workflow.",
Edges: workflow.Chain(workflow.Start, myWorkflow),
})
if err != nil {
return fmt.Errorf("workflowagent.New: %w", err)
}
l := full.NewLauncher()
return l.Execute(ctx, &launcher.Config{
AgentLoader: agent.NewSingleLoader(wa),
}, os.Args[1:])
}
```
## Building blocks: nodes and workflows
Nodes and workflows represent the basic building blocks of ADK's dynamic workflows. These types and functions provide the functionality required to wrap your code so it can be integrated into code-based workflows in ADK.
### Nodes
A dynamic workflow in ADK is composed of *nodes*. A simple version of a usable workflow node wraps a plain function with the metadata required to run within a workflow.
In Python, the ***@node*** annotation generates the node wrapper, keeping boilerplate to a minimum:
```python
@node(name="hello_node")
def my_function_node(node_input: Any):
return "Hello World"
```
The following code snippet shows the equivalent code *without* the ***@node*** annotation:
```python
from typing import Any
from google.adk.workflow import FunctionNode
# base function
def hello_world(node_input: Any):
return "Hello World"
# FunctionNode wrapper with options
my_function_node = FunctionNode(
func=hello_world,
name="hello_node",
)
```
Creating the node wrapper code yourself can be useful if you are wrapping functions from an external library, need to create multiple nodes from the same function with different configurations, or if you are managing node references in a registry for advanced orchestration.
There are two ways to build a node: the `node(fn, options)` factory function, and the explicit `new FunctionNode(name, fn, config)` constructor. Use the constructor when you are wrapping a function from another library, need several differently configured nodes from one function, or keep node references in a registry for advanced orchestration.
```typescript
import { FunctionNode, node, NodeContext, Workflow } from '@google/adk';
/** The plain function both node forms wrap. */
function myFunctionNode(_ctx: NodeContext, nodeInput: unknown): string {
return `Hello ${nodeInput ?? 'World'}`;
}
const helloNode = node(myFunctionNode, { name: 'hello_node' });
const successNode = new FunctionNode('hello', myFunctionNode, {
rerunOnResume: true,
});
```
In this code sample, the most important option is `rerunOnResume`, which controls what happens when a workflow resumes after a human-in-the-loop pause:
- **`true` (re-entry):** the node body re-runs from the top. Use this setting for any orchestrator that calls `ctx.runNode()`. The body re-executes, and already-completed child activations are skipped automatically.
- **`false` (handoff, the leaf default):** the resume payload is routed to the node's successor as input, bypassing the interrupted node.
In Go, `workflow.NewFunctionNode[IN, OUT]` wraps a plain function as a workflow node, inferring input and output types from the generic parameters. There is no decorator syntax; the node is a value that you pass as a child to `workflow.RunNode` inside a dynamic orchestrator:
```go
// myFunctionNode demonstrates the explicit NewFunctionNode constructor —
// equivalent to wrapping a function in a FunctionNode manually in Python:
//
// success_node = FunctionNode(my_function_node, name="hello", rerun_on_resume=True)
//
// Creating the node directly (rather than via @node) is useful when you
// need multiple nodes from the same function with different configurations,
// or when wrapping functions from an external library.
var myFunctionNode = workflow.NewFunctionNode("hello",
func(_ agent.Context, _ any) (string, error) {
return "Hello World", nil
},
workflow.NodeConfig{},
)
// myFormattingNode is a second function node that the dynamic orchestrator
// calls in sequence, mirroring:
//
// result_formatted = await ctx.run_node(my_formatting_node, node_input=result)
var myFormattingNode = workflow.NewFunctionNode("format",
func(_ agent.Context, in string) (string, error) {
return fmt.Sprintf("[formatted] %s", in), nil
},
workflow.NodeConfig{},
)
```
`NodeConfig` holds the same options as Python's `@node` arguments. The most important field is `RerunOnResume *bool`, which controls what happens when a workflow resumes after a human-in-the-loop pause:
- **`&true` (re-entry mode)**: the interrupted node is re-run from the beginning on resume. Use this for dynamic orchestrator nodes that call `workflow.RunNode` in a loop — the body re-executes and already-completed child activations are skipped automatically (checkpointing). This mirrors Python's `@node(rerun_on_resume=True)`.
- **`&false` (handoff mode)**: the resume payload is routed directly to the node's successor as input, bypassing the interrupted node entirely. Use this for leaf nodes that simply emit a pause event and expect the human response to flow to the next step.
- **`nil`**: the default depends on node type. `workflow.NewDynamicNode` automatically sets `nil → &true` (re-entry mode), because an orchestrator body must be re-entered on resume to deliver cached child results. `workflow.NewFunctionNode` and other leaf node constructors leave `nil` as-is, which the engine treats as handoff (`&false`). Explicit `&false` is always respected on any node type.
```go
// NewDynamicNode: nil RerunOnResume is automatically set to &true.
// Passing &rerun explicitly is equivalent and makes the intent clear.
rerun := true
orchestratorNode := workflow.NewDynamicNode[string, string]("my_workflow",
myOrchestratorfn,
workflow.NodeConfig{RerunOnResume: &rerun}, // re-entry: node body re-runs on resume
)
// NewFunctionNode: nil RerunOnResume stays nil → engine treats as handoff.
handoffNode := workflow.NewFunctionNode("leaf_node",
myLeafFn,
workflow.NodeConfig{}, // nil RerunOnResume → handoff for FunctionNode
)
```
### Workflows
In an ADK dynamic workflow, you use a dynamic node as the primary orchestrator for nodes. A dynamic node manages running child nodes and the execution logic (order and paths) for those nodes.
```python
@node(rerun_on_resume=True)
async def my_workflow(ctx):
# run_node executes a node and returns its output
result = await ctx.run_node(my_function_node, node_input="Hello")
result_formatted = await ctx.run_node(my_formatting_node, node_input=result)
return result_formatted
# Run the workflow
root_agent = Workflow(
name="root_agent",
edges=[("START", my_workflow)],
)
```
The orchestrator is an async function that awaits `ctx.runNode()` for each child step. Wrap it as a node with `rerunOnResume: true` and use it as the graph's only edge:
```typescript
const myFormattingNode = node(
(_ctx: NodeContext, nodeInput: string) => `>> ${nodeInput.trim()} <<`,
{ name: 'my_formatting_node' },
);
const myWorkflow = node(
async (ctx: NodeContext, nodeInput: unknown) => {
const greeted = await ctx.runNode(helloNode, nodeInput);
const again = await ctx.runNode(successNode, greeted.output);
const formatted = await ctx.runNode(myFormattingNode, again.output);
return formatted.output;
},
{ name: 'my_workflow', rerunOnResume: true },
);
export const rootAgent = new Workflow({
name: 'root_agent',
edges: [['START', myWorkflow]],
});
```
`workflow.NewDynamicNode` creates an orchestrator whose body calls `workflow.RunNode` for each child step. `workflowagent.New` with `workflow.Chain(workflow.Start, myWorkflow)` is the equivalent of `Workflow(edges=[("START", my_workflow)])`:
```go
// orchestratorWorkflow is a dynamic node that schedules two children in
// sequence via workflow.RunNode, equivalent to:
//
// @node(rerun_on_resume=True)
// async def my_workflow(ctx):
// result = await ctx.run_node(my_function_node, node_input="Hello")
// result_formatted = await ctx.run_node(my_formatting_node, node_input=result)
// return result_formatted
var orchestratorWorkflow = workflow.NewDynamicNode[string, string]("my_workflow",
func(ctx agent.Context, _ string, _ func(*session.Event) error) (string, error) {
result, err := workflow.RunNode[string](ctx, myFunctionNode, "Hello")
if err != nil {
return "", err
}
return workflow.RunNode[string](ctx, myFormattingNode, result)
},
workflow.NodeConfig{},
)
```
## Data handling
When using dynamic workflows with ADK, passing data is simpler than [graph-based workflows](/graphs/) because running a child node returns that node's output directly to the caller — eliminating the need to manually read and write session state keys for data transfer.
```python
from google.adk import Context
from google.adk.workflow import node
@node(rerun_on_resume=True)
async def editorial_workflow(ctx: Context, node_input: str):
# Agent Node generates output
raw_draft = await ctx.run_node(draft_agent, node_input)
# Function Node formats text
formatted_text = await ctx.run_node(format_function_node, raw_draft)
return formatted_text
```
You can also pass specific data schemas using a defined class and configure input and output schemas, similar to graph-based workflow nodes:
```python
from google.adk import Agent
from google.adk import Context
from google.adk.workflow import node
from pydantic import BaseModel
class CityTime(BaseModel):
time_info: str # time information
city: str # city name
@node
def city_time_function(node_input: str):
"""Simulate returning the current time in a specified city."""
return CityTime(time_info="10:10 AM", city=node_input)
city_report_agent = Agent(
name="city_report_agent",
model="gemini-flash-latest",
input_schema=CityTime,
instruction="""output the data provided by the previous node.""",
)
@node(rerun_on_resume=True) # workflow node
async def city_workflow(ctx: Context):
city_time = await ctx.run_node(city_time_function, "Paris")
report_text = await ctx.run_node(city_report_agent, city_time)
return report_text
```
The `ctx.runNode()` function returns the child's result directly, so there are no session-state keys to read and write to move a value one step downstream. This function accepts any node-like value, including an `LlmAgent`, without wrapping it in `node()` first:
```typescript
import { LlmAgent, node, NodeContext, Workflow } from '@google/adk';
const draftAgent = new LlmAgent({
name: 'draft_agent',
model: 'gemini-flash-latest',
instruction: 'Write a short draft for the user request.',
});
const formatFunctionNode = node(
(_ctx: NodeContext, rawDraft: string) =>
rawDraft
.split('\n')
.map((line) => line.trim())
.filter(Boolean)
.map((line) => `| ${line}`)
.join('\n'),
{ name: 'format_function_node' },
);
const editorialWorkflow = node(
async (ctx: NodeContext, userRequest: string) => {
const rawDraft = await ctx.runNode(draftAgent, userRequest);
const formattedText = await ctx.runNode(
formatFunctionNode,
rawDraft.output,
);
return formattedText.output;
},
{ name: 'editorial_workflow', rerunOnResume: true },
);
export const rootAgent = new Workflow({
name: 'root_agent',
edges: [['START', editorialWorkflow]],
});
```
Schemas work the same way as in a graph. Attach them to the nodes you run, as shown in the [sequence route](#sequence-route) section.
In Go, `workflow.NewAgentNode` wraps an `agent.Agent` so it can be invoked via `workflow.RunNode` inside a dynamic orchestrator. The output of each `RunNode` call is returned as a typed value — no session state reads are required:
```go
// newDataHandlingWorkflow demonstrates how to pass data between a dynamic
// orchestrator and an LlmAgent-backed node. workflow.NewAgentNode wraps an
// agent.Agent so it can be invoked via workflow.RunNode.
//
// In Python this mirrors:
//
// city_report_agent = Agent(name="city_report_agent", ...)
// @node
// async def city_workflow(ctx: Context):
// city_time = await ctx.run_node(city_time_function, "Paris")
// report_text = await ctx.run_node(city_report_agent, city_time)
// return report_text
func newDataHandlingWorkflow(ctx context.Context) (agent.Agent, error) {
model, err := gemini.NewModel(ctx, "gemini-flash-latest", &genai.ClientConfig{})
if err != nil {
return nil, fmt.Errorf("gemini.NewModel: %w", err)
}
// cityTimeNode is a FunctionNode that returns a formatted city-time string.
cityTimeNode := workflow.NewFunctionNode("city_time_function",
func(_ agent.Context, city string) (string, error) {
return fmt.Sprintf("10:10 AM in %s", city), nil
},
workflow.NodeConfig{},
)
// cityReportAgent is an LlmAgent that receives the city-time string and
// produces a human-friendly report.
cityReportAgent, err := llmagent.New(llmagent.Config{
Name: "city_report_agent",
Model: model,
Description: "Reports city time information.",
Instruction: "Output the data provided by the previous node in a friendly sentence.",
})
if err != nil {
return nil, fmt.Errorf("llmagent.New (cityReport): %w", err)
}
// workflow.NewAgentNode wraps cityReportAgent so it can be called from
// inside a dynamic node via workflow.RunNode.
cityReportNode, err := workflow.NewAgentNode(cityReportAgent, workflow.NodeConfig{})
if err != nil {
return nil, fmt.Errorf("workflow.NewAgentNode: %w", err)
}
cityWorkflow := workflow.NewDynamicNode[string, string]("city_workflow",
func(ctx agent.Context, _ string, _ func(*session.Event) error) (string, error) {
cityTime, err := workflow.RunNode[string](ctx, cityTimeNode, "Paris")
if err != nil {
return "", err
}
return workflow.RunNode[string](ctx, cityReportNode, cityTime)
},
workflow.NodeConfig{},
)
return workflowagent.New(workflowagent.Config{
Name: "data_handling_workflow",
SubAgents: []agent.Agent{cityReportAgent},
Edges: workflow.Chain(workflow.Start, cityWorkflow),
})
}
```
For more information on data handling between workflow nodes, see [Data handling for agent workflows](/graphs/data-handling/).
## Workflow routes
Dynamic workflows in ADK provide more flexibility in terms of routing logic compared to [graph-based workflows](/graphs/), including iterative loops or more complex branching logic. This section describes some of the techniques that you can use for routing.
### Sequence route
You can create sequential task processing with dynamic workflows in ADK, just as you can with graph-based workflows.
The following code snippet shows a dynamic workflow with an agent, a function node, and a second agent:
```python
@node(rerun_on_resume=True) # workflow node
async def city_workflow(ctx: Context):
city = await ctx.run_node(city_generator_agent)
city_time = await ctx.run_node(city_time_function, city)
report_text = await ctx.run_node(city_report_agent, city_time)
return report_text
```
A sequential route awaits `ctx.runNode()` calls one after another. Each call finishes before the next one starts:
```typescript
import { LlmAgent, node, NodeContext, Workflow } from '@google/adk';
import { z } from 'zod';
const cityTimeSchema = z.object({
timeInfo: z.string().describe('Time information.'),
city: z.string().describe('City name.'),
});
type CityTime = z.infer;
const cityGeneratorAgent = new LlmAgent({
name: 'city_generator_agent',
model: 'gemini-flash-latest',
instruction: 'Return the name of a random city. Return only the name.',
});
/** Simulates returning the current time in a specified city. */
const cityTimeFunction = node(
(_ctx: NodeContext, city: string): CityTime => ({
timeInfo: '10:10 AM',
city: city.trim(),
}),
{ name: 'city_time_function', outputSchema: cityTimeSchema },
);
const cityReportAgent = node(
new LlmAgent({
name: 'city_report_agent',
model: 'gemini-flash-latest',
instruction: 'Output the data provided by the previous node as a sentence.',
}),
{ inputSchema: cityTimeSchema },
);
const cityWorkflow = node(
async (ctx: NodeContext) => {
const city = await ctx.runNode(cityGeneratorAgent);
const cityTime = await ctx.runNode(cityTimeFunction, city.output);
const reportText = await ctx.runNode(cityReportAgent, cityTime.output);
return reportText.output;
},
{ name: 'city_workflow', rerunOnResume: true },
);
export const rootAgent = new Workflow({
name: 'root_agent',
edges: [['START', cityWorkflow]],
});
```
Call `workflow.RunNode` sequentially inside a `NewDynamicNode` body — each call awaits the child before the next one starts. The [data handling example above](#data-handling) demonstrates exactly this pattern: `cityWorkflow` calls `workflow.RunNode` for `cityTimeNode` and then `cityReportNode` in order, passing each node's typed output to the next.
### Loop route
For workflows where you want to use an iterative loop for a task, dynamic workflows offer much more flexibility to define the routing logic you need.
The following code example shows how to use dynamic workflows to construct a workflow loop for generating, reviewing, and updating code:
```python
from google.adk import Context
from google.adk import Event
from google.adk.agents import LlmAgent
from google.adk.workflow import node
coder_agent = LlmAgent(
name="generator_agent",
model="gemini-flash-latest",
instruction="Write python code for user request.",
)
@node(name="lint_reviewer")
async def compile_lint_check(ctx: Context, node_input: str):
# Simulate API call or lint check
class Response:
findings = ""
return Response()
fixer_agent = LlmAgent(
name="fixer_agent",
model="gemini-flash-latest",
instruction="""Refactor current code {code}.
Based on compile & lint review: {findings}""",
)
@node(rerun_on_resume=True) # workflow node
async def code_workflow(ctx: Context, node_input: str):
code = await ctx.run_node(coder_agent, node_input)
check_resp = await ctx.run_node(compile_lint_check, code)
while check_resp.findings:
yield Event(state={"code": code, "findings": check_resp.findings})
code = await ctx.run_node(fixer_agent, {"code": code, "findings": check_resp.findings})
check_resp = await ctx.run_node(compile_lint_check, code)
yield Event(output=code)
```
Dynamic workflows can help keep workflow logic simple by defining an iteration as an ordinary loop rather than a back-edge in a graph. Values are held in local variables, and state is written only where an agent's instruction template needs to read it back. Unlike a graph cycle, the loop is bounded by its loop condition:
```typescript
import { LlmAgent, node, NodeContext, Workflow } from '@google/adk';
/** Safety bound on the refine loop. */
const MAX_FIX_ROUNDS = 3;
const coderAgent = new LlmAgent({
name: 'generator_agent',
model: 'gemini-flash-latest',
instruction: 'Write TypeScript code for the user request. Output code only.',
});
/** Simulates a compile / lint pass. Empty findings means "clean". */
const compileLintCheck = node(
(_ctx: NodeContext, code: string) => {
const findings: string[] = [];
if (!/\/\*\*/.test(code)) {
findings.push('every function needs a JSDoc comment');
}
if (!/\)\s*:\s*\w/.test(code)) {
findings.push('add return type annotations');
}
return { findings: findings.join('; ') };
},
{ name: 'lint_reviewer' },
);
const fixerAgent = new LlmAgent({
name: 'fixer_agent',
model: 'gemini-flash-latest',
instruction: `Refactor current code {code}.
Based on compile & lint review: {findings}
Output code only.`,
});
const codeWorkflow = node(
async (ctx: NodeContext, userRequest: string) => {
let code = (await ctx.runNode(coderAgent, userRequest)).output as string;
let checkResp = (await ctx.runNode(compileLintCheck, code)).output as {
findings: string;
};
for (let round = 0; checkResp.findings && round < MAX_FIX_ROUNDS; round++) {
ctx.state.set('code', code);
ctx.state.set('findings', checkResp.findings);
code = (
await ctx.runNode(fixerAgent, { code, findings: checkResp.findings })
).output as string;
checkResp = (await ctx.runNode(compileLintCheck, code)).output as {
findings: string;
};
}
return code;
},
{ name: 'code_workflow', rerunOnResume: true },
);
export const rootAgent = new Workflow({
name: 'root_agent',
edges: [['START', codeWorkflow]],
});
```
In Go, the loop is a plain `for` loop inside the dynamic node body. The lint check node returns an empty string when there are no findings, which signals the loop to exit:
```go
// newLoopWorkflow demonstrates an iterative loop inside a dynamic node.
// The orchestrator body uses a plain Go for loop to keep calling the
// lintCheckNode until there are no findings — equivalent to Python's:
//
// @node
// async def code_workflow(ctx: Context, user_request: str):
// code = await ctx.run_node(coder_agent, user_request)
// check_resp = await ctx.run_node(compile_lint_check, code)
// while check_resp.findings:
// code = await ctx.run_node(fixer_agent, ...)
// check_resp = await ctx.run_node(compile_lint_check, code)
// return code
func newLoopWorkflow(ctx context.Context) (agent.Agent, error) {
model, err := gemini.NewModel(ctx, "gemini-flash-latest", &genai.ClientConfig{})
if err != nil {
return nil, fmt.Errorf("gemini.NewModel: %w", err)
}
coderAgent, err := llmagent.New(llmagent.Config{
Name: "generator_agent",
Model: model,
Description: "Writes Go code for the user request.",
Instruction: "Write Go code for the user request. Output only the code.",
OutputKey: "generated_code",
})
if err != nil {
return nil, fmt.Errorf("llmagent.New (coder): %w", err)
}
coderNode, err := workflow.NewAgentNode(coderAgent, workflow.NodeConfig{})
if err != nil {
return nil, fmt.Errorf("workflow.NewAgentNode (coder): %w", err)
}
// lintCheckNode simulates a lint/compile check. It returns an empty
// string when there are no findings, signalling the loop to exit.
lintCheckNode := workflow.NewFunctionNode("lint_reviewer",
func(_ agent.Context, code string) (string, error) {
// Simulate a lint check: return findings or empty string when clean.
if len(code) < 50 {
return "Code is too short; add error handling.", nil
}
return "", nil // no findings — loop exits
},
workflow.NodeConfig{},
)
fixerAgent, err := llmagent.New(llmagent.Config{
Name: "fixer_agent",
Model: model,
Description: "Refactors code based on lint findings.",
Instruction: "Refactor the provided code to address the review findings. Output only the improved code.",
})
if err != nil {
return nil, fmt.Errorf("llmagent.New (fixer): %w", err)
}
fixerNode, err := workflow.NewAgentNode(fixerAgent, workflow.NodeConfig{})
if err != nil {
return nil, fmt.Errorf("workflow.NewAgentNode (fixer): %w", err)
}
codeWorkflow := workflow.NewDynamicNode[string, string]("code_workflow",
func(ctx agent.Context, userRequest string, _ func(*session.Event) error) (string, error) {
code, err := workflow.RunNode[string](ctx, coderNode, userRequest)
if err != nil {
return "", err
}
findings, err := workflow.RunNode[string](ctx, lintCheckNode, code)
if err != nil {
return "", err
}
// Loop until the lint check reports no findings.
for findings != "" {
code, err = workflow.RunNode[string](ctx, fixerNode, code)
if err != nil {
return "", err
}
findings, err = workflow.RunNode[string](ctx, lintCheckNode, code)
if err != nil {
return "", err
}
}
return code, nil
},
workflow.NodeConfig{},
)
return workflowagent.New(workflowagent.Config{
Name: "code_pipeline",
SubAgents: []agent.Agent{coderAgent, fixerAgent},
Edges: workflow.Chain(workflow.Start, codeWorkflow),
})
}
```
### Parallel execution routes
Dynamic workflows in ADK can support parallel execution.
In Python, you can use `asyncio.gather` to build parallel execution:
```python
import asyncio
from typing import Any
from google.adk import Context
from google.adk.workflow import node
@node
def worker_node(node_input: Any):
"""Processes a single item."""
return f"done {node_input}"
@node(rerun_on_resume=True)
async def parallel_supervisor(ctx: Context, node_input: list[Any]):
"""Runs worker_node in parallel for each item in the input list."""
tasks = []
for item in node_input:
# ctx.run_node returns a coroutine. Append instead of awaiting
# immediately, and use a sub-branch to isolate each run's events.
tasks.append(ctx.run_node(worker_node, item, use_sub_branch=True))
# Collect all results in parallel
results = await asyncio.gather(*tasks)
return results
```
Tip: Resuming parallel nodes
The workflow framework ensures that if a dynamic workflow is resumed, only failed or interrupted worker nodes are re-executed, including parallel worker nodes.
The `ctx.runNode()` method returns a promise, so starting every child before awaiting any of them runs the children concurrently, and `Promise.all` collects the results. Run IDs are assigned in call order, so start the children in a synchronous loop to keep the IDs deterministic across a resume:
```typescript
import { node, NodeContext, Workflow } from '@google/adk';
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
/** The worker run once per list item. */
const realNode = node(
async (_ctx: NodeContext, item: string) => {
await sleep(200);
return { item, length: item.length };
},
{ name: 'analyze_item' },
);
const parallelSupervisor = node(
async (ctx: NodeContext, nodeInput: string) => {
const items = nodeInput
.split(',')
.map((item) => item.trim())
.filter(Boolean);
const tasks = items.map((item) => ctx.runNode(realNode, item));
const results = await Promise.all(tasks);
return results.map((result) => result.output);
},
{ name: 'parallel_supervisor', rerunOnResume: true },
);
const summarize = node(
(_ctx: NodeContext, results: Array<{ item: string; length: number }>) =>
results.map((r) => `${r.item}: ${r.length} chars`).join('\n'),
{ name: 'summarize' },
);
export const rootAgent = new Workflow({
name: 'root_agent',
edges: [['START', parallelSupervisor, summarize]],
});
```
Tip: prefer the built-in parallel worker
To run one node over each item in a list, use `node(worker, {parallelWorker: true, maxParallelWorkers: 4})`. This option performs the fan-out and bounds concurrency, which defaults to 8. Use the manual approach shown above when you need custom scheduling or partial-failure handling. On resume, only failed or interrupted workers re-execute in both cases.
In Go, `workflow.NewParallelWorker` wraps a child node and runs it concurrently for each element of a list input, collecting results into a single output slice. The `maxConcurrency` parameter caps how many concurrent activations may run simultaneously; `0` means unlimited:
```go
// newParallelWorkflow demonstrates parallel execution using
// workflow.NewParallelWorker. The worker node runs a wrapped child node
// concurrently for each element in a list input, collecting results.
//
// This is the Go equivalent of using asyncio.gather in Python:
//
// @node(rerun_on_resume=True)
// async def parallel_supervisor(ctx, node_input, real_node):
// tasks = [ctx.run_node(real_node, item) for item in node_input]
// results = await asyncio.gather(*tasks)
// return results
func newParallelWorkflow() (agent.Agent, error) {
// workerNode processes a single item. NewParallelWorker will call it
// once per element of the list input, concurrently.
workerNode := workflow.NewFunctionNode("worker",
func(_ agent.Context, item string) (string, error) {
return fmt.Sprintf("processed: %s", item), nil
},
workflow.NodeConfig{},
)
// NewParallelWorker wraps workerNode so it runs concurrently for each
// element of a []string input. maxConcurrency=0 means unlimited.
parallelWorker, err := workflow.NewParallelWorker(
"parallel_supervisor",
workerNode,
0, // maxConcurrency: 0 = unlimited
workflow.NodeConfig{},
)
if err != nil {
return nil, fmt.Errorf("workflow.NewParallelWorker: %w", err)
}
return workflowagent.New(workflowagent.Config{
Name: "parallel_workflow",
Description: "Runs a worker node in parallel for each item in the input list.",
Edges: workflow.Chain(workflow.Start, parallelWorker),
})
}
```
Tip: Resuming parallel nodes
The workflow framework ensures that if a dynamic workflow is resumed, only failed or interrupted worker nodes are re-executed, including parallel worker nodes managed by `NewParallelWorker`.
## Human input
Dynamic workflows in ADK can also include human input or human in the loop (HITL) steps.
You build human input into workflows by yielding a ***RequestInput*** from a node, which pauses the workflow and waits for user input. The following code example shows how to build a human input node and include it in a workflow:
```python
from typing import Any
from google.adk import Context
from google.adk.events import RequestInput
from google.adk.workflow import node
@node(rerun_on_resume=False)
async def get_user_approval(ctx: Context, node_input: Any):
"""Yields a RequestInput to pause the workflow and wait for user input."""
yield RequestInput(message="Please approve this request (Yes/No)")
@node(rerun_on_resume=True)
async def handle_process(ctx: Context, node_input: Any):
"""The orchestrator calling the interactive step."""
user_response = await ctx.run_node(get_user_approval)
if user_response.lower() == "yes":
return "Approved"
return "Denied"
```
Important: Parent nodes with `ctx.run_node`
Parent nodes in dynamic workflows that call `ctx.run_node` must set `rerun_on_resume=True` to handle interruptions properly.
The leaf node returns a `RequestInput` to pause the workflow, and keeps the default `rerunOnResume: false` so the reply becomes its output. The orchestrator that calls it must set `rerunOnResume: true`:
```typescript
import { node, NodeContext, RequestInput, Workflow } from '@google/adk';
/**
* Pauses the workflow and waits for user input.
*
* `rerunOnResume: false` (the default, spelled out here because it is the
* point) is what makes this a one-liner: the reply is handed to the node as
* its output instead of the body running a second time to collect it.
*/
const getUserApproval = node(
() => new RequestInput({ message: 'Please approve this request (Yes/No)' }),
{ name: 'get_user_approval', rerunOnResume: false },
);
/** The orchestrator calling the interactive step. */
const handleProcess = node(
async (ctx: NodeContext, nodeInput: unknown) => {
const approval = await ctx.runNode(getUserApproval, nodeInput);
if (approval.interruptIds.length > 0) {
return undefined;
}
const userResponse = String(approval.output ?? '')
.trim()
.toLowerCase();
if (userResponse === 'yes') {
return 'Approved';
}
return 'Denied';
},
{ name: 'handle_process', rerunOnResume: true },
);
export const rootAgent = new Workflow({
name: 'root_agent',
edges: [['START', handleProcess]],
});
```
Important: check `interruptIds` before deciding
The `ctx.runNode()` method does **not** throw an error when a child node interrupts. It returns normally, with the `interruptIds` property of the result populated and the `output` property still `undefined`. Check `interruptIds` before you use the result. An orchestrator that skips this check treats the missing output as an answer and continues with a value the user never supplied.
In Go, use `workflow.NewEmittingFunctionNode` with `workflow.ResumeOrRequestInput` to implement the re-entry HITL pattern. On the first pass `ResumeOrRequestInput` emits a `session.RequestInput` event and returns `ErrNodeInterrupted`, pausing the workflow. After the human replies, the node is re-run from the top (`RerunOnResume: &true`) and `ResumeOrRequestInput` returns the human's reply directly:
```go
// newHITLWorkflow demonstrates the re-entry HITL pattern using
// workflow.ResumeOrRequestInput. On the first pass the node emits a
// RequestInput event and returns ErrNodeInterrupted (pausing the workflow).
// After the human replies, the same node is re-run from the top
// (RerunOnResume=&true) and ResumeOrRequestInput returns the human's reply.
//
// In Python this is equivalent to:
//
// @node(rerun_on_resume=True)
// async def get_user_approval(ctx, node_input):
// yield RequestInput(message="Please approve this request (Yes/No)")
//
// @node(rerun_on_resume=True)
// async def handle_process(ctx, node_input):
// user_response = await ctx.run_node(get_user_approval)
// if user_response.lower() == "yes":
// return "Approved"
// return "Denied"
func newHITLWorkflow() (agent.Agent, error) {
rerun := true
// approvalNode pauses on the first pass to ask the user for a Yes/No
// approval, then resolves their decision on resume.
// workflow.ResumeOrRequestInput handles both phases.
approvalNode := workflow.NewEmittingFunctionNode[any, any]("get_user_approval",
func(nc agent.Context, _ any, emit func(*session.Event) error) (any, error) {
// ResumeOrRequestInput: on first pass, emits the prompt and
// returns ErrNodeInterrupted. On re-run after the human replies,
// it returns the reply payload directly.
reply, err := workflow.ResumeOrRequestInput(nc, emit, session.RequestInput{
InterruptID: "user_approval",
Message: "Please approve this request (Yes/No)",
})
if err != nil {
return nil, err
}
response, _ := reply.(string)
if response == "" {
response = "No"
}
if response == "yes" || response == "Yes" {
return "Approved", nil
}
return "Denied", nil
},
workflow.NodeConfig{RerunOnResume: &rerun},
)
return workflowagent.New(workflowagent.Config{
Name: "hitl_workflow",
Description: "Pauses for user approval before completing a task.",
Edges: workflow.Chain(workflow.Start, approvalNode),
})
}
```
## Advanced features
Dynamic workflows offer some advanced features designed to handle more complex development scenarios. These capabilities allow for finer control over execution and better integration with existing technical infrastructure.
### Execution IDs
The ADK framework generates a deterministic identifier (ID) for child node executions based on the parent ID and a counter. ADK workflows use deterministic IDs for each scheduled node to identify previous results. These IDs are generated based on the order of dynamic node schedules, and are used for checkpointing and to re-run tasks in the correct order in the case of a resumed or re-run workflow.
#### Custom execution IDs
In some rare cases, you may need to have stable identifiers, such as when processing a reorderable list. In general, you should avoid this due to the impacts to workflow task retries and process resumes. Specifically, these IDs are used to check node states and skip execution if a node was already run. If you provide custom IDs, make sure they are deterministic for workflow re-runs and logically remain the same for the input.
Warning: Custom execution IDs
Avoid creating custom execution IDs. Since execution IDs are used to determine the execution order of nodes, custom execution IDs can cause problems when the system attempts to re-run those nodes in your workflow.
```python
from google.adk import Context
from google.adk.workflow import node
from pydantic import BaseModel
from typing import Any
import asyncio
class Product(BaseModel):
sku: str
quantity: int
class Order(BaseModel):
order_id: str
cart_items: list[Product]
async def get_orders() -> list[Order]:
"""Fetch the orders to process from your own data source."""
...
@node
async def process_order(node_input: Order):
"""Processes a single order."""
return f"processed {node_input.order_id}"
@node(rerun_on_resume=True)
async def process_all_orders(ctx: Context, node_input: Any):
orders = await get_orders()
process_tasks = []
for order in orders:
# Use run_id to provide a custom identifier.
# Custom run_ids must contain at least one non-numeric character
# to avoid collision with auto-generated sequential numeric IDs.
task = ctx.run_node(process_order, order, run_id=f"order-{order.order_id}")
process_tasks.append(task)
results = await asyncio.gather(*process_tasks)
return results
```
By default, auto-generated run IDs are sequential integers starting from `"1"` (represented as strings). Custom `run_id` values must contain at least one non-numeric character to avoid collisions with these auto-generated IDs.
Pass a `runId` as a trailing option to `ctx.runNode()`. The ID must contain at least one non-numeric character so it does not collide with the auto-generated sequential IDs:
```typescript
import { node, NodeContext, Workflow } from '@google/adk';
interface Order {
orderId: string;
cartItems: string[];
}
/** Stands in for loading orders from a database. */
async function getOrders(): Promise {
return [
{ orderId: 'a91', cartItems: ['keyboard', 'mouse'] },
{ orderId: 'b02', cartItems: ['monitor'] },
{ orderId: 'c73', cartItems: ['dock', 'cable', 'hub'] },
];
}
const processOrder = node(
(_ctx: NodeContext, order: Order) =>
`order ${order.orderId}: ${order.cartItems.length} item(s) shipped`,
{ name: 'process_order' },
);
const processAllOrders = node(
async (ctx: NodeContext) => {
const orders = await getOrders();
const processTasks = orders.map((order) =>
ctx.runNode(processOrder, order, { runId: `order-${order.orderId}` }),
);
const results = await Promise.all(processTasks);
return results.map((result) => result.output).join('\n');
},
{ name: 'process_all_orders', rerunOnResume: true },
);
export const rootAgent = new Workflow({
name: 'root_agent',
edges: [['START', processAllOrders]],
});
```
In Go, pass `workflow.WithRunID("order-x")` as a trailing option to `workflow.RunNode`. The ID must contain at least one non-numeric character to avoid collision with the auto-generated sequential counter IDs:
```go
// newCustomIDWorkflow demonstrates supplying stable custom run IDs via
// workflow.WithRunID — equivalent to Python's:
//
// task = ctx.run_node(process_order, order, run_id=f"order-{order.order_id}")
//
// Custom run IDs must contain at least one non-numeric character to avoid
// collision with auto-generated sequential integer IDs.
func newCustomIDWorkflow() (agent.Agent, error) {
processOrderNode := workflow.NewFunctionNode("process_order",
func(_ agent.Context, orderID string) (string, error) {
return fmt.Sprintf("processed order %s", orderID), nil
},
workflow.NodeConfig{},
)
orders := []string{"ord-001", "ord-002", "ord-003"}
processAllOrders := workflow.NewDynamicNode[any, []string]("process_all_orders",
func(ctx agent.Context, _ any, _ func(*session.Event) error) ([]string, error) {
results := make([]string, 0, len(orders))
for _, orderID := range orders {
// WithRunID supplies a stable, deterministic identifier for
// each child invocation. IDs must contain at least one
// non-numeric character to avoid collision with the
// auto-generated sequential counter IDs.
result, err := workflow.RunNode[string](
ctx,
processOrderNode,
orderID,
workflow.WithRunID(fmt.Sprintf("order-%s", orderID)),
)
if err != nil {
return nil, fmt.Errorf("process order %s: %w", orderID, err)
}
results = append(results, result)
}
return results, nil
},
workflow.NodeConfig{},
)
return workflowagent.New(workflowagent.Config{
Name: "custom_id_workflow",
Description: "Processes orders with stable per-order execution IDs.",
Edges: workflow.Chain(workflow.Start, processAllOrders),
})
}
```
# Human input for agent workflows
Supported in ADKPython v2.0.0TypeScript v2.0.0Go v2.0.0
Being able to request human input for data input, decision verification, or action permission is an important part of many agent-powered workflows. Graph-based workflows in ADK can include human in the loop (HITL) nodes specifically built for obtaining input from humans as part of a workflow. These nodes do not require artificial intelligence (AI) models to run, which can make the input process more predictable and reliable.
## Get started
You can implement a human input node in a graph using the ***RequestInput*** class and a text prompt for the user. The following code example shows how to add a human input node to a Workflow graph:
```python
from google.adk.events import RequestInput
from google.adk import Workflow
def step1(): # Human input step
yield RequestInput(message="Enter a number:")
def step2(node_input):
return node_input * 2
root_agent = Workflow(
name="root_agent",
edges=[('START', step1, step2)],
)
```
In this code example, `step1` pauses the execution of the agent until the system receives an input from a user. Once the system receives input from the user, that input is passed to the next node.
In ADK TypeScript, a human input node yields a `RequestInput`. The `step1` node pauses the workflow until the user replies, and the reply is passed to the next node as its input. A human-in-the-loop node does not require a model, which makes the pause deterministic.
```typescript
import { node, NodeContext, RequestInput, Workflow } from '@google/adk';
const step1 = node(
async function* () {
yield new RequestInput({ message: 'Enter a number:' });
},
{ name: 'step1' },
);
const step2 = node(
(_ctx: NodeContext, nodeInput: string | number) => {
const value = Number(nodeInput);
return Number.isFinite(value)
? value * 2
: `"${nodeInput}" is not a number.`;
},
{ name: 'step2' },
);
export const rootAgent = new Workflow({
name: 'root_agent',
edges: [['START', step1, step2]],
});
```
This implementation shows the default `rerunOnResume: false` handoff: the interrupted node does not re-run. It completes with the user's reply as its output. A node that calls `ctx.runNode()` needs `rerunOnResume: true` instead. For more information, see [human input in dynamic workflows](/graphs/dynamic/#human-input).
In ADK Go, a human input node emits a request with `workflow.NewRequestInputEvent` and returns `workflow.ErrNodeInterrupted`. The `step1` node pauses the workflow until the user replies. The node does not run again. The reply becomes its output and is passed to the next node.
```go
// newGraphHITLHandoffWorkflow demonstrates the handoff HITL pattern, which
// matches the Python example. step1Node emits a RequestInput event and pauses
// the workflow. RerunOnResume is left unset, so step1Node does not run again
// on resume. The human's reply becomes step1Node's output and flows to
// step2Node as its input.
func newGraphHITLHandoffWorkflow() (agent.Agent, error) {
step1Node := workflow.NewEmittingFunctionNode[any, string]("step1",
func(ctx agent.Context, _ any, emit func(*session.Event) error) (string, error) {
// Leaving InterruptID empty generates a new UUID automatically.
if err := emit(workflow.NewRequestInputEvent(ctx, session.RequestInput{
Message: "Enter a number:",
})); err != nil {
return "", err
}
return "", workflow.ErrNodeInterrupted
},
workflow.NodeConfig{},
)
// step2Node takes any, because a reply that parses as JSON, such as "42",
// arrives decoded (here as a float64).
step2Node := workflow.NewFunctionNode("step2",
func(_ agent.Context, input any) (string, error) {
return fmt.Sprintf("You entered: %v", input), nil
},
workflow.NodeConfig{},
)
return workflowagent.New(workflowagent.Config{
Name: "root_agent",
Description: "Pauses for a number from the user, then hands it to step2.",
Edges: workflow.Chain(workflow.Start, step1Node, step2Node),
})
}
```
A node that needs to run again after the reply sets `RerunOnResume` and calls `workflow.ResumeOrRequestInput` instead. For more information, see [human input in dynamic workflows](/graphs/dynamic/#human-input).
## Configuration options
Human input nodes can use the ***RequestInput*** class with the following configuration options:
- **`message`:** Text provided to the user to explain the human input request.
- **`payload`:** Structured data to be used as part of the human input request.
- **`response_schema`:** A data structure the human response must conform to.
The `RequestInput` class takes the following configuration options:
- **`message`:** Text shown to the user explaining what is being asked.
- **`payload`:** Structured data sent with the prompt, so a client can render additional context.
- **`responseSchema`:** The shape the reply is expected to take. The schema travels on the interrupt as `functionCall.args.response_schema`, which a client reads to render a form for the reply.
The `rerunOnResume` option on the node controls what happens when the reply arrives:
- **`false`** (the leaf default): the reply is routed to the node's successor as input, bypassing the interrupted node.
- **`true`**: the node body re-runs from the top. This setting is required for any node that calls `ctx.runNode()`, so it can deliver cached child results on resume.
`session.RequestInput` carries the following fields, which map directly to Python's `RequestInput` parameters:
- **`InterruptID`** (`string`): A unique identifier for this pause point. Use a stable prefix plus a UUID to avoid collision across workflow runs. Equivalent to the implicit interrupt ID in Python.
- **`Message`** (`string`): Human-readable prompt displayed to the user. Equivalent to Python's `message` parameter.
- **`Payload`** (`any`): Optional structured data sent alongside the prompt so the client can render additional context. Equivalent to Python's `payload` parameter.
- **`ResponseSchema`** (`*jsonschema.Schema`): Optional JSON schema the reply must conform to. Equivalent to Python's `response_schema` parameter.
The `workflow.NodeConfig.RerunOnResume` field controls what happens on resume:
- **`&true`**: the node body is re-run from the top; `ResumeOrRequestInput` returns the human's reply on the second pass. Required for nodes that use `ResumeOrRequestInput`.
- **`&false`** or **`nil`** (leaf default): the reply is routed to the node's successor as input, bypassing the interrupted node.
Note: Response schema input limitations
A response schema does not reformat a human reply to fit the specified structure. The reply must already be in that format. For a better user experience, collect structured data in your client interface, or place an agent node after the pause to convert the reply into the required format.
## Human input examples
The following code examples demonstrate more detailed human input requests.
### Request input with a message and payload
The following code sample shows how to construct a ***RequestInput*** object in a workflow node, including a ***payload*** and ***response schema***. In this example, the `ActivitiesList` is expected to be completed by an agent node that composes a list of activities, and the `get_user_feedback()` node requests feedback from the user.
```python
class ActivitiesList(BaseModel):
"""Itinerary should be a list of dictionaries for each activity. Each
activity has a name and a description"""
itinerary: List[Dict[str, str]]
class UserFeedback(BaseModel):
"""Expected response structure from the user."""
user_response: str
async def get_user_feedback(node_input: ActivitiesList):
"""
Retrieves the user's thoughts on the agents initial itinerary in order to
either expand on, change the list, or exit the loop
"""
message = (
f"""
Here is your recommended base itinerary:\n{node_input}\n\n
Which of these items appeal to you (if any)?
"""
)
yield RequestInput(
message=message,
payload=node_input,
response_schema=UserFeedback,
)
```
The following three-node graph builds a structured itinerary, sends it as `payload` with the prompt so a client can render it, and then acts on the user's feedback:
```typescript
import { node, NodeContext, RequestInput, Workflow } from '@google/adk';
import { z } from 'zod';
/**
* Itinerary is a list of activities. Each activity has a name and a
* description.
*/
const activitiesListSchema = z.object({
itinerary: z.array(z.object({ name: z.string(), description: z.string() })),
});
type ActivitiesList = z.infer;
/** Expected response structure from the user. */
const userFeedbackSchema = z.object({
userResponse: z.string(),
});
const buildItinerary = node(
(_ctx: NodeContext, city: string): ActivitiesList => {
const place = city.trim() || 'your city';
return {
itinerary: [
{ name: 'Morning walk', description: `A stroll through old ${place}.` },
{ name: 'Local lunch', description: `Regional food in ${place}.` },
{ name: 'Museum visit', description: `The main museum of ${place}.` },
],
};
},
{ name: 'build_itinerary', outputSchema: activitiesListSchema },
);
/**
* Retrieves the user's thoughts on the agent's initial itinerary in order to
* either expand on it, change the list, or exit the loop.
*/
const getUserFeedback = node(
async function* (_ctx: NodeContext, nodeInput: ActivitiesList) {
const rendered = nodeInput.itinerary
.map((a, i) => ` ${i + 1}. ${a.name} — ${a.description}`)
.join('\n');
yield new RequestInput({
message:
`Here is your recommended base itinerary:\n${rendered}\n\n` +
'Which of these items appeal to you (if any)?',
payload: nodeInput,
responseSchema: userFeedbackSchema,
});
},
{ name: 'get_user_feedback' },
);
const applyFeedback = node(
(_ctx: NodeContext, nodeInput: unknown) => {
const feedback =
typeof nodeInput === 'string'
? nodeInput
: String(
(nodeInput as { userResponse?: unknown } | null)?.userResponse ??
JSON.stringify(nodeInput),
);
return `Noted. Building the final itinerary around: ${feedback}`;
},
{ name: 'apply_feedback' },
);
export const rootAgent = new Workflow({
name: 'concierge_workflow',
edges: [['START', buildItinerary, getUserFeedback, applyFeedback]],
});
```
The following code sample shows a three-node graph: a builder node generates a structured itinerary, a HITL node sends it as `Payload` alongside the prompt, and a final node acts on the user's feedback. The `Payload` field lets the client render the full itinerary for the user before they respond:
```go
// ItineraryItem represents a single activity in a travel plan.
type ItineraryItem struct {
Name string `json:"name"`
Description string `json:"description"`
}
// newItineraryReviewWorkflow demonstrates a graph HITL node that sends a
// structured payload alongside the input prompt so the client can render
// additional context for the user. This mirrors Python's:
//
// async def get_user_feedback(node_input: ActivitiesList):
// yield RequestInput(
// message="Which items appeal to you?",
// payload=node_input,
// response_schema=UserFeedback,
// )
func newItineraryReviewWorkflow() (agent.Agent, error) {
rerun := true
// buildItineraryNode: generates an itinerary and passes it to the HITL
// node as its typed output via event.Output.
buildItineraryNode := workflow.NewFunctionNode("build_itinerary",
func(_ agent.Context, _ any) ([]ItineraryItem, error) {
return []ItineraryItem{
{Name: "Eiffel Tower", Description: "Iconic iron lattice tower."},
{Name: "Louvre Museum", Description: "World's largest art museum."},
{Name: "Seine River Cruise", Description: "Scenic boat tour of Paris."},
}, nil
},
workflow.NodeConfig{},
)
// reviewNode: sends the itinerary as payload alongside the prompt so the
// client can display it. On resume, the human's selection is returned.
reviewNode := workflow.NewEmittingFunctionNode[[]ItineraryItem, string]("get_user_feedback",
func(ctx agent.Context, itinerary []ItineraryItem, emit func(*session.Event) error) (string, error) {
reply, err := workflow.ResumeOrRequestInput(ctx, emit, session.RequestInput{
InterruptID: "itinerary_review",
Message: fmt.Sprintf("Here is your recommended itinerary (%d activities). Which items appeal to you?", len(itinerary)),
Payload: itinerary, // structured payload rendered by the client
})
if err != nil {
// ErrNodeInterrupted on first pass — workflow pauses here.
return "", err
}
feedback, _ := reply.(string)
return feedback, nil
},
workflow.NodeConfig{RerunOnResume: &rerun},
)
// finalNode: receives the user's feedback and produces a confirmation.
finalNode := workflow.NewFunctionNode("finalize",
func(_ agent.Context, feedback string) (string, error) {
return fmt.Sprintf("Itinerary finalised with your feedback: %q", feedback), nil
},
workflow.NodeConfig{},
)
return workflowagent.New(workflowagent.Config{
Name: "concierge_workflow",
Description: "Builds an itinerary, asks the user for feedback, then finalises.",
Edges: workflow.Chain(workflow.Start, buildItineraryNode, reviewNode, finalNode),
})
}
```
## Tool-confirmation: approval prompts in LLM agents
Tool-confirmation is a separate, LLM-agent–level mechanism for yes/no approval prompts. Unlike graph HITL nodes, tool-confirmation works inside an `llmagent` tool function rather than as a standalone graph node. It is useful when you want an LLM agent to pause and ask for approval before executing a specific tool call.
Wrap the tool with ***FunctionTool*** and set `require_confirmation=True` for a static yes/no approval before the tool runs, or call `tool_context.request_confirmation()` from inside the tool for a custom hint message. ADK only rejects the call for you on the `require_confirmation=True` path, so a tool that asks for its own confirmation must check both: `tool_context.tool_confirmation` to ask once, then `tool_confirmation.confirmed` to handle a rejection:
```python
from google.adk.agents import Agent
from google.adk.tools import FunctionTool, ToolContext
def book_flight(flight_id: str) -> str:
"""Books the selected flight."""
...
def refund(amount: float, tool_context: ToolContext) -> str:
"""Refunds an order."""
if not tool_context.tool_confirmation:
tool_context.request_confirmation(
hint=f"Approve a refund of {amount}?"
)
return "Waiting for approval."
elif not tool_context.tool_confirmation.confirmed:
return "Refund rejected by user."
...
root_agent = Agent(
name="concierge",
model="gemini-flash-latest",
tools=[FunctionTool(book_flight, require_confirmation=True), refund],
)
```
Set `requireConfirmation: true` on a `FunctionTool` to make the agent pause for approval before that tool runs. A graph human-in-the-loop node serves a different purpose: instead of confirming a tool call, it can start the workflow by asking the user for input. The `responseSchema: z.string()` option requests a plain text reply:
```typescript
import { node, NodeContext, RequestInput, Workflow } from '@google/adk';
import { z } from 'zod';
/** Asks the user for itinerary information. */
const initialPrompt = node(
async function* () {
const inputMessage = `
This is an interactive concierge workflow tasked with making you a great
itinerary for you in your city of choice. If you give some details about
yourself or what you are generally looking for I can better personalize
your itinerary.
For example, input your:
City (Required),
Age,
Hobby,
Example of attraction you liked
`;
yield new RequestInput({
message: inputMessage,
responseSchema: z.string(),
});
},
{ name: 'initial_prompt' },
);
const buildItinerary = node(
(_ctx: NodeContext, nodeInput: string) => {
const [city = 'your city'] = nodeInput.split(',');
return (
`Personalized itinerary for ${city.trim()}:\n` +
' 1. Morning walk through the old town\n' +
' 2. Lunch at a neighbourhood favourite\n' +
' 3. An afternoon activity matched to your hobby\n\n' +
`(based on: ${nodeInput.trim()})`
);
},
{ name: 'build_itinerary' },
);
export const rootAgent = new Workflow({
name: 'concierge_workflow',
edges: [['START', initialPrompt, buildItinerary]],
});
```
Set `RequireConfirmation: true` in `functiontool.Config` for a static yes/no approval before a tool executes, or call `ctx.RequestConfirmation` from inside the tool for a custom hint message:
```go
// DoubleNumberArgs holds the input for the doubleNumber tool.
type DoubleNumberArgs struct {
Number int `json:"number" jsonschema:"The number to double."`
}
// DoubleNumberResults holds the output of the doubleNumber tool.
type DoubleNumberResults struct {
Result int `json:"result"`
}
// doubleNumber is a tool that doubles the given number.
// Because RequireConfirmation is true, the framework automatically pauses
// execution and emits an "adk_request_confirmation" event to the client before
// running the tool. The client must reply with a FunctionResponse confirming
// or denying the action.
func doubleNumber(_ agent.Context, args DoubleNumberArgs) (DoubleNumberResults, error) {
return DoubleNumberResults{Result: args.Number * 2}, nil
}
// newSimpleHITLAgent creates an LLM agent with a tool that always requires
// user confirmation before it executes (tool-confirmation pattern).
func newSimpleHITLAgent(ctx context.Context) (agent.Agent, error) {
model, err := gemini.NewModel(ctx, modelName, &genai.ClientConfig{})
if err != nil {
return nil, fmt.Errorf("failed to create model: %w", err)
}
doubleNumberTool, err := functiontool.New(
functiontool.Config{
Name: "double_number",
Description: "Doubles the given number. Requires user approval before running.",
RequireConfirmation: true,
},
doubleNumber,
)
if err != nil {
return nil, fmt.Errorf("failed to create tool: %w", err)
}
return llmagent.New(llmagent.Config{
Name: "double_number_agent",
Model: model,
Instruction: "You are a helpful assistant. When asked to double a number, use the double_number tool.",
Tools: []tool.Tool{doubleNumberTool},
})
}
```
For a custom hint with manual re-entry handling:
```go
// BookFlightArgs holds the input for the bookFlight tool.
type BookFlightArgs struct {
Origin string `json:"origin" jsonschema:"Departure airport code."`
Destination string `json:"destination" jsonschema:"Arrival airport code."`
Date string `json:"date" jsonschema:"Travel date in YYYY-MM-DD format."`
}
// BookFlightResults holds the outcome of the bookFlight tool.
type BookFlightResults struct {
Status string `json:"status"`
ConfirmNumber string `json:"confirm_number,omitempty"`
}
// bookFlight is a tool that pauses for human approval before completing a
// booking (tool-confirmation pattern with a custom hint message).
func bookFlight(ctx agent.Context, args BookFlightArgs) (BookFlightResults, error) {
if confirmation := ctx.ToolConfirmation(); confirmation != nil {
if !confirmation.Confirmed {
return BookFlightResults{Status: "Booking cancelled by user."}, nil
}
return BookFlightResults{
Status: "Booking confirmed.",
ConfirmNumber: "FLT-20251031",
}, nil
}
hint := fmt.Sprintf(
"The agent wants to book a flight from %s to %s on %s. Do you approve?",
args.Origin, args.Destination, args.Date,
)
if err := ctx.RequestConfirmation(hint, nil); err != nil {
return BookFlightResults{}, fmt.Errorf("failed to request confirmation: %w", err)
}
return BookFlightResults{Status: "Awaiting user approval."}, nil
}
// newHITLWithHintAgent creates an LLM agent whose bookFlight tool manually
// requests confirmation with a descriptive hint (tool-confirmation pattern).
func newHITLWithHintAgent(ctx context.Context) (agent.Agent, error) {
model, err := gemini.NewModel(ctx, modelName, &genai.ClientConfig{})
if err != nil {
return nil, fmt.Errorf("failed to create model: %w", err)
}
bookFlightTool, err := functiontool.New(
functiontool.Config{
Name: "book_flight",
Description: "Books a flight between two airports on a given date.",
},
bookFlight,
)
if err != nil {
return nil, fmt.Errorf("failed to create tool: %w", err)
}
return llmagent.New(llmagent.Config{
Name: "flight_booking_agent",
Model: model,
Instruction: "You are a flight booking assistant. Help the user book flights.",
Tools: []tool.Tool{bookFlightTool},
})
}
```
For the full tool-confirmation workflow, including dynamic confirmation and how the client replies, see [Get action confirmation for ADK Tools](/tools-custom/confirmation/).
# Handle node failures with retries and timeouts
Supported in ADKPython v2.0.0TypeScript v2.0.0Go v2.0.0Java v1.14.0Kotlin v1.2.0
Nodes in a graph-based workflow often call services that fail intermittently or respond slowly, such as model APIs, remote tools, and databases. Agent Development Kit (ADK) lets you attach a retry policy and a timeout to each node, so a node can recover from a transient failure without failing the whole workflow, and a stalled node fails after a set time instead of blocking the workflow. Use retries and timeouts functionality to:
- Retry a tool call that fails with a connection error, waiting longer between each attempt so the remote service has time to recover.
- Stop a model call that has not responded within 30 seconds, and try it again.
- Retry only the errors that are likely to succeed on a second attempt, and fail immediately on all other errors.
A node that has a retry policy runs again when it fails, until an attempt succeeds or the node reaches its maximum attempts. Only then does the failure reach the rest of the workflow. Nodes without a retry policy are not retried.
Experimental in TypeScript, Java, and Kotlin
In ADK TypeScript, ADK Java, and ADK Kotlin, the workflow APIs that node retries and timeouts depend on are experimental releases. These APIs may have limitations and are not recommended for production use in those languages.
## Get started
The following example builds a two-node workflow. The first node calls an unreliable forecast service, and its configuration allows three attempts in total, waits one second before the first retry, retries only service errors and timeouts, and limits each attempt to 10 seconds. The second node reports the result.
```python
import random
from google.adk import Context
from google.adk import Event
from google.adk import Workflow
from google.adk.workflow import NodeTimeoutError
from google.adk.workflow import RetryConfig
from google.adk.workflow import node
class ServiceUnavailableError(Exception):
"""Stands in for a transient failure from a remote service."""
@node(
retry_config=RetryConfig(
max_attempts=3, # 1 original attempt + 2 retries
initial_delay=1.0, # seconds before the first retry
exceptions=[ServiceUnavailableError, NodeTimeoutError],
),
timeout=10.0, # seconds allowed for each attempt
)
async def fetch_forecast(ctx: Context):
"""Simulates an unreliable call to a forecast service."""
yield Event(message=f"Fetching forecast, attempt {ctx.attempt_count}")
if random.random() < 0.5:
raise ServiceUnavailableError("Forecast service unavailable")
yield "sunny"
def report_forecast(node_input: str):
return Event(message=f"The forecast is {node_input}.")
root_agent = Workflow(
name="forecast_workflow",
edges=[("START", fetch_forecast, report_forecast)],
)
```
Pass `retryConfig` and `timeout` as options to `node()`. Delays and timeouts are in seconds.
```typescript
import {
createEvent,
node,
NodeContext,
NodeTimeoutError,
Workflow,
} from '@google/adk';
/** Stands in for a transient failure from a remote service. */
class ServiceUnavailableError extends Error {
constructor(message: string) {
super(message);
this.name = 'ServiceUnavailableError';
}
}
/** Simulates an unreliable call to a forecast service. */
async function* fetchForecast(ctx: NodeContext) {
yield createEvent({
content: {
role: 'model',
parts: [{ text: `Fetching forecast, attempt ${ctx.attemptCount}` }],
},
});
if (Math.random() < 0.5) {
throw new ServiceUnavailableError('Forecast service unavailable');
}
yield 'sunny';
}
function reportForecast(_ctx: NodeContext, forecast: string) {
return `The forecast is ${forecast}.`;
}
export const rootAgent = new Workflow({
name: 'forecast_workflow',
edges: [
[
'START',
node(fetchForecast, {
name: 'fetch_forecast',
retryConfig: {
maxAttempts: 3, // 1 original attempt + 2 retries
initialDelay: 1, // seconds before the first retry
exceptions: [ServiceUnavailableError, NodeTimeoutError],
},
timeout: 10, // seconds allowed for each attempt
}),
node(reportForecast, { name: 'report_forecast' }),
],
],
});
```
Set `RetryConfig` and `Timeout` in the `workflow.NodeConfig` that you pass to the node constructor. Start from `workflow.DefaultRetryConfig()` and change only the fields you need.
```go
// errServiceUnavailable stands in for a transient failure from a remote service.
var errServiceUnavailable = errors.New("forecast service unavailable")
func newForecastWorkflow() (agent.Agent, error) {
// fetchForecast simulates a slow, unreliable service call. It watches
// ctx.Done() so that the node's timeout can stop it.
fetchForecast := func(ctx agent.Context, _ any) (string, error) {
select {
case <-time.After(200 * time.Millisecond):
case <-ctx.Done():
return "", ctx.Err()
}
if rand.Float64() < 0.5 {
return "", errServiceUnavailable
}
return "sunny", nil
}
// Start from the default policy and override only what you need.
retry := workflow.DefaultRetryConfig()
retry.MaxAttempts = 3 // 1 original attempt + 2 retries
retry.InitialDelay = time.Second
// Retry only transient failures: the service error and the node's own
// timeout, which surfaces as context.DeadlineExceeded.
retry.ShouldRetry = func(err error) bool {
return errors.Is(err, errServiceUnavailable) ||
errors.Is(err, context.DeadlineExceeded)
}
fetchNode := workflow.NewFunctionNode("fetch_forecast", fetchForecast,
workflow.NodeConfig{
RetryConfig: retry,
Timeout: 10 * time.Second, // limit for each attempt
},
)
reportNode := workflow.NewFunctionNode("report_forecast",
func(_ agent.Context, forecast string) (string, error) {
return "The forecast is " + forecast + ".", nil
},
workflow.NodeConfig{},
)
return workflowagent.New(workflowagent.Config{
Name: "forecast_workflow",
Edges: workflow.Chain(workflow.Start, fetchNode, reportNode),
})
}
```
Return a `NodeConfig` from the `config()` method of your `Node` implementation.
```java
import com.google.adk.agents.Context;
import com.google.adk.workflow.Edge;
import com.google.adk.workflow.Node;
import com.google.adk.workflow.NodeConfig;
import com.google.adk.workflow.NodeTimeoutException;
import com.google.adk.workflow.RetryConfig;
import com.google.adk.workflow.Start;
import com.google.adk.workflow.Workflow;
import io.reactivex.rxjava3.core.Flowable;
import java.time.Duration;
import java.util.concurrent.ThreadLocalRandom;
public final class ForecastWorkflow {
/** Stands in for a transient failure from a remote service. */
static final class ServiceUnavailableException extends RuntimeException {
ServiceUnavailableException(String message) {
super(message);
}
}
/** Simulates an unreliable call to a forecast service. */
static final class FetchForecastNode implements Node {
private static final NodeConfig CONFIG =
NodeConfig.builder()
.retryConfig(
RetryConfig.builder()
.maxAttempts(3) // 1 original attempt + 2 retries
.initialDelay(Duration.ofSeconds(1))
.exceptionClasses(
ServiceUnavailableException.class, NodeTimeoutException.class)
.build())
.timeout(Duration.ofSeconds(10)) // limit for each attempt
.build();
@Override
public String name() {
return "fetch_forecast";
}
@Override
public NodeConfig config() {
return CONFIG;
}
@Override
public Flowable> runNode(Context context, Object nodeInput) {
if (ThreadLocalRandom.current().nextDouble() < 0.5) {
return Flowable.error(
new ServiceUnavailableException(
"Forecast service unavailable, attempt " + context.attemptCount()));
}
return Flowable.just("sunny");
}
}
public static Workflow create() {
return Workflow.builder()
.name("forecast_workflow")
.edges(new Edge(Start.INSTANCE, new FetchForecastNode()))
.build();
}
}
```
Pass a `NodeConfig` to `node()`. The workflow APIs require an opt-in to `ExperimentalWorkflowApi`.
```kotlin
import com.google.adk.kt.annotations.ExperimentalWorkflowApi
import com.google.adk.kt.types.Content
import com.google.adk.kt.types.Role
import com.google.adk.kt.workflow.NodeConfig
import com.google.adk.kt.workflow.RetryConfig
import com.google.adk.kt.workflow.Start
import com.google.adk.kt.workflow.Workflow
import com.google.adk.kt.workflow.emit
import com.google.adk.kt.workflow.node
import com.google.adk.kt.workflow.workflow
import kotlin.random.Random
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.delay
/** Stands in for a transient failure from a remote service. */
class ServiceUnavailableException(message: String) : Exception(message)
/** Simulates an unreliable call to a forecast service. */
val fetchForecast =
node(
"fetch_forecast",
config =
NodeConfig(
retryConfig =
RetryConfig(
maxAttempts = 3, // 1 original attempt + 2 retries
initialDelay = 1.seconds,
// A timed-out attempt is reported as "NodeTimeoutError".
exceptions = listOf("ServiceUnavailableException", "NodeTimeoutError"),
),
timeout = 10.seconds, // limit for each attempt
),
) { context, _ ->
emit(Content.fromText(Role.MODEL, "Fetching forecast, attempt ${context.attemptCount}"))
delay(200.milliseconds)
if (Random.nextDouble() < 0.5) throw ServiceUnavailableException("Forecast service unavailable")
"sunny"
}
val reportForecast = node("report_forecast") { _, forecast -> "The forecast is $forecast." }
fun createForecastWorkflow(): Workflow =
workflow("forecast_workflow") { chain(Start, fetchForecast, reportForecast) }
```
When `fetch_forecast` fails with a service error or exceeds its timeout, the ADK agent waits, then runs the node again with the same input. The next node, `report_forecast`, runs only after an attempt succeeds. If all three attempts fail, the node fails with the error from the last attempt, and the workflow fails with it. In all languages except Go, the node context provides an attempt count, which starts at 1, so your code can tell which attempt is running.
## Configure retries
A retry policy controls three things: how many times a node runs, how long ADK waits between attempts, and which errors start a retry.
- **Attempts:** The maximum attempts value counts the original attempt, so a value of 3 allows 2 retries. A value of 0 or 1 disables retries.
- **Delay:** The wait before the first retry is the initial delay. ADK multiplies each later wait by the backoff factor, so the defaults of 1 second and 2.0 give base waits of 1, 2, 4, and 8 seconds before jitter. The maximum delay caps each wait.
- **Jitter:** The system adds or subtracts a random amount, up to the jitter factor times the delay, from each wait, so that many nodes that fail at the same time do not all retry at the same moment. Set jitter to 0 for fixed delays, for example in tests.
The following decisions have the most effect on how well retries work for your workflow:
- **Retry only transient errors.** By default, a retry policy retries almost every error. An error caused by bad input or a bug fails the same way on every attempt, so retrying it only delays the failure. List the error types that can succeed on a later attempt, such as connection errors, rate limit errors, and timeouts. For details on how each language matches errors, see [Configuration options](#configuration-options).
- **Set the policy on the node that can fail.** In Python, Java, and Kotlin, you can also attach a retry policy to a nested workflow, and a failure of any node inside it then retries the nested workflow. Setting the policy on the individual node that calls the unreliable service retries less work and gives you more control.
- **Make retried work safe to repeat.** A retry runs the whole node again. If the node writes to an external system before it fails, the write can happen more than once. Design those operations so that repeating them has no extra effect, or move the side effect into a separate node that runs after the unreliable call succeeds.
## Set node timeouts
A timeout limits how long a single attempt of a node can run. The limit applies to each attempt separately, not to the total time across retries, and the wait between attempts does not count toward it. When an attempt runs past its timeout, ADK stops the attempt and treats it as a failure:
- In Python, TypeScript, Java, and Kotlin, the attempt fails with a node timeout error, as listed in the following table.
- In Go, the node's context reaches its deadline, and the attempt fails with `context.DeadlineExceeded`.
ADK retries a timed-out attempt like any other failure when the node has a retry policy that covers the timeout error. If you list specific error types in the policy, include the timeout error, as the [Get started](#get-started) example does. A timeout without a retry policy fails the node the first time it fires.
| Language | Timeout setting | Timeout error | Name to list in a retry policy |
| ---------- | ----------------------------------------- | -------------------------- | ------------------------------------------ |
| Python | `timeout` in seconds | `NodeTimeoutError` | `NodeTimeoutError` |
| TypeScript | `timeout` in seconds | `NodeTimeoutError` | `NodeTimeoutError` |
| Go | `NodeConfig.Timeout` as a `time.Duration` | `context.DeadlineExceeded` | Match it in `ShouldRetry` with `errors.Is` |
| Java | `NodeConfig.timeout` as a `Duration` | `NodeTimeoutException` | `NodeTimeoutException` |
| Kotlin | `NodeConfig.timeout` as a `Duration` | `NodeTimeoutError` | `NodeTimeoutError` |
Stopping an attempt relies on cancellation, and how cancellation reaches your code depends on the language:
- **Go:** The node's `agent.Context` carries the deadline. Pass it to the calls your node makes, or check `ctx.Done()`, so the node returns when the deadline passes.
- **TypeScript:** ADK stops reading events from the node when the timeout fires. To also cancel work the node has started, such as an HTTP request, pass `ctx.abortSignal` to that work.
- **Kotlin:** ADK runs the attempt in a coroutine and cancels the coroutine when the timeout fires. Code that does not reach a suspension point keeps running until it does.
## Configuration options
The retry policy has the same options in every language. Durations are in seconds in Python and TypeScript, and use the language's duration type in Go, Java, and Kotlin.
| Option | Default | Description |
| ---------------- | ---------- | ------------------------------------------------------------------------------------- |
| Maximum attempts | `5` | Attempts including the original one. A value of 0 or 1 disables retries. |
| Initial delay | 1 second | Wait before the first retry. |
| Maximum delay | 60 seconds | Upper limit on the wait between attempts. |
| Backoff factor | `2.0` | Multiplier applied to the wait after each attempt. |
| Jitter | `1.0` | Random spread applied to each wait, as a fraction of the wait. `0` gives fixed waits. |
| Errors to retry | All errors | Errors that start a retry. See the following list. |
Each language selects the errors to retry in its own way:
- **Python:** The `exceptions` setting takes exception classes or class names. A listed class also matches its subclasses.
- **TypeScript:** The `exceptions` setting takes error classes or class names. An error matches when its class name or its `name` property is in the list. ADK does not check the class hierarchy.
- **Go:** The `ShouldRetry` setting is a function that receives the error and returns `true` to retry it. The default retries every error except `workflow.ErrInputValidation`.
- **Java:** The `exceptions` setting takes simple class names, which match exactly. The `exceptionClasses` setting takes classes, which also match their subclasses. ADK never retries a `WorkflowConfigurationException`.
- **Kotlin:** The `exceptions` setting takes simple class names, which match exactly. ADK never retries a `WorkflowConfigurationException`.
Go: unset fields are zero, not defaults
In Go, a `RetryConfig` built as a struct literal uses the zero value for every field that you do not set, not the default value. For example, `&workflow.RetryConfig{MaxAttempts: 3}` retries with no delay between attempts. Start from `workflow.DefaultRetryConfig()` and override fields instead. Also, Go applies jitter after it applies `MaxDelay`, so a single wait can be longer than `MaxDelay` by up to the jitter fraction.
## Limitations
- **Attempt count is not saved.** ADK keeps the attempt count in memory for the current run of the node. If the workflow pauses, for example for [human input](/graphs/human-input/), or the application restarts, a node that runs again after the workflow resumes starts from attempt 1 with all of its attempts available.
## Next steps
- [Build graph routes for agent workflows](/graphs/routes/): connect nodes with sequences, branches, and parallel paths.
- [Data handling for agent workflows](/graphs/data-handling/): pass data between nodes.
- [Human input for agent workflows](/graphs/human-input/): pause a workflow to ask a person for input.
# Build graph routes for agent workflows
Supported in ADKPython v2.0.0TypeScript v2.0.0Go v2.0.0Kotlin v1.5.0
Graph-based workflows in ADK define agent logic as a graph of execution nodes and edges, allowing you to build more reliable processes that combine artificial intelligence (AI) reasoning and code logic. These workflows allow you to create logical routes of execution nodes that can encapsulate code functions, AI-powered agents, Tools, and human input. By explicitly mapping out routing logic, this approach allows you to define a specific, step-wise process workflow in code, providing improved precision and reliability over purely prompt-based agents.
**Figure 1.** Visualization of a task graph and the routing code to implement it.
```python
root_agent = Workflow(
name="routing_workflow",
edges=[
("START", process_message, router),
(router,
{
"output-1": response_1,
"output-2": response_2,
"output-3": response_3,
},
),
],
)
```
```typescript
export const rootAgent = new Workflow({
name: 'routing_workflow',
edges: [
['START', processMessage, router],
[
router,
{
'output-1': response1,
'output-2': response2,
'output-3': response3,
},
],
],
});
```
ADK Go v2.0.0 provides the following approach to graph-based workflows:
**Graph engine** (`workflowagent` + `workflow.Edge`): A node-and-edges graph API that maps directly to Python's `Workflow(edges=[...])`. Nodes are defined with `workflow.NewFunctionNode`, `workflow.NewAgentNode`, or `workflow.NewDynamicNode`, edges are declared as `[]workflow.Edge`, and the whole graph is wrapped in a `workflowagent.New` call:
```go
edges := workflow.Concat(
workflow.Chain(workflow.Start, classifyNode),
[]workflow.Edge{
{From: classifyNode, To: responseA, Route: workflow.StringRoute("output-1")},
{From: classifyNode, To: responseB, Route: workflow.StringRoute("output-2")},
{From: classifyNode, To: responseC, Route: workflow.StringRoute("output-3")},
},
)
rootAgent, _ := workflowagent.New(workflowagent.Config{
Name: "routing_workflow",
Edges: edges,
})
```
```kotlin
val rootAgent = workflow("routing_workflow") {
chain(Start, processMessage, router).route {
"output-1" then response1
"output-2" then response2
"output-3" then response3
}
}
```
The advantage of using a graph-based agent workflow is the significant increase in control, predictability, and reliability over prompt-based agents. By defining the overall process workflow in code, you gain more control over how tasks are routed and executed. This structured node definition improves the predictability of agents and enhances reliability for complex tasks that require defined steps and process management.
Get started with graph-based workflows in ADK by checking out [Graph-based agent workflows](/graphs/).
## Nodes
A graph is composed of execution nodes. These *nodes* can be ***Agents***, ADK ***Tools***, human input tasks, or code functions you write. Nodes can take inputs from previously executed nodes, and emit data through ***Event*** objects.
The following shows a simple ***FunctionNode*** that handles text inputs and sends a text output:
```python
from google.adk import Event
def my_function_node(node_input: str):
input_text_modified = node_input.upper()
return Event(output=input_text_modified)
```
In ADK TypeScript v2.0.0, the primary node type is a `FunctionNode`, created by passing a function to `node()`. A handler always takes `(ctx, input)` parameters; ADK does not inject values by parameter name. Returning a value directly wraps it in the event's `output` field. Returning `createEvent({output})` is the explicit form, which you need when you also set `route` or `content`:
```typescript
import {
createEvent,
node,
NodeContext,
Workflow,
type FunctionNodeHandler,
} from '@google/adk';
/** A bare return value: boxed into an event's `output` for you. */
const myFunctionNode: FunctionNodeHandler = (
_ctx: NodeContext,
nodeInput: string,
) => {
const inputTextModified = nodeInput.toUpperCase();
return inputTextModified;
};
/** The explicit form — identical behaviour, useful when you also set `route`. */
const myExplicitEventNode = (_ctx: NodeContext, nodeInput: string) =>
createEvent({ output: `${nodeInput} IS AWESOME!` });
export const rootAgent = new Workflow({
name: 'function_node_pipeline',
edges: [
[
'START',
node(myFunctionNode, { name: 'my_function_node' }),
node(myExplicitEventNode, { name: 'add_suffix' }),
],
],
});
```
In ADK Go v2.0.0, the primary node type is `workflow.NewFunctionNode`. A `FunctionNode` wraps a plain Go function: the function returns a typed value, and the framework automatically wraps it in a `session.Event`, setting `event.Output`. The successor node receives this value as its typed `input` parameter — no manual state writes or event construction needed:
```go
// newFunctionNodePipeline demonstrates workflow.NewFunctionNode as the primary
// v2 node type. A FunctionNode wraps a plain Go function: the function returns
// a typed value, and the framework automatically wraps it in a session.Event,
// setting event.Output. The successor node receives this value as its typed
// input parameter.
//
// This is the direct Go equivalent of the Python FunctionNode:
//
// def my_function_node(node_input: str):
// return Event(output=node_input.upper())
func newFunctionNodePipeline() (agent.Agent, error) {
upperFn := func(_ agent.Context, input string) (string, error) {
return strings.ToUpper(input), nil
}
suffixFn := func(_ agent.Context, input string) (string, error) {
return input + " IS AWESOME!", nil
}
// workflow.NewFunctionNode wraps each function as a graph node.
// workflow.Chain wires them in order: START → upper → suffix.
// The output of upperFn is delivered as the typed input of suffixFn
// via event.Output — no session state writes are needed.
nodeA := workflow.NewFunctionNode("upper", upperFn, workflow.NodeConfig{})
nodeB := workflow.NewFunctionNode("suffix", suffixFn, workflow.NodeConfig{})
return workflowagent.New(workflowagent.Config{
Name: "function_node_pipeline",
Description: "Demonstrates workflow.NewFunctionNode data flow via Event.Output.",
Edges: workflow.Chain(workflow.Start, nodeA, nodeB),
})
}
```
In ADK Kotlin, the `node()` factory creates a `FunctionNode` from a suspending block that takes `(context, input)` and returns the node's output. The type arguments set the input and output port types, and the successor node receives the returned value as its typed input. The workflow APIs are experimental, so the file opts in with `@file:OptIn(ExperimentalWorkflowApi::class)`:
```kotlin
import com.google.adk.kt.annotations.ExperimentalWorkflowApi
import com.google.adk.kt.workflow.Start
import com.google.adk.kt.workflow.node
import com.google.adk.kt.workflow.workflow
// The returned value becomes the node's output.
private val myFunctionNode =
node("my_function_node") { _, nodeInput ->
val inputTextModified = nodeInput.uppercase()
inputTextModified
}
// The successor receives my_function_node's output as its typed input.
private val addSuffix =
node("add_suffix") { _, nodeInput ->
"$nodeInput IS AWESOME!"
}
val functionNodePipeline =
workflow("function_node_pipeline") {
chain(Start, myFunctionNode, addSuffix)
}
```
For more information about transferring data between nodes, see [Data handling for agent workflows](/graphs/data-handling/).
## Workflow graphs syntax
You define a graph by composing workflow agents. This section provides an overview of the common routing patterns.
Caution: Workflow agent limitations
You can add ***LlmAgents*** to graph-based workflows. An agent used as a graph node defaults to `single_turn` mode; a `chat` mode agent is only allowed directly after `START`, because it consumes conversational history rather than a node input. For more information about agent modes, see [Build collaborative agent teams](/workflows/collaboration/#mode-configuration-and-behaviors).
### Route sequences
A sequential route runs each node once, in the listed order.
The `edges` array uses the `START` keyword to indicate the beginning of a graph execution, with each listed node executed in sequence:
```python
edges=[("START", task_A_node)] # single node run
edges=[("START",
task_A_node,
task_B_node,
task_C_node)] # 3 nodes run in order
```
An `edges` row that starts with `'START'` runs each listed node once, in order, and passes every node's return value to the next node:
```typescript
edges: [['START', taskANode]] // a single node
edges: [['START', taskANode, taskBNode, taskCNode]] // three, in order
```
Listing `'START'` in more than one row creates parallel paths instead. For more information, see [fan out and join](#parallel-tasks-fan-out-and-join-paths).
```typescript
import { node, NodeContext, Workflow } from '@google/adk';
const taskANode = node(
(_ctx: NodeContext, nodeInput: string) => `Summary: ${nodeInput.trim()}`,
{ name: 'task_A_node' },
);
const taskBNode = node(
(_ctx: NodeContext, summary: string) => summary.toUpperCase(),
{ name: 'task_B_node' },
);
const taskCNode = node(
(_ctx: NodeContext, shouted: string) => `${shouted} (done)`,
{ name: 'task_C_node' },
);
export const rootAgent = new Workflow({
name: 'sequential_workflow',
edges: [['START', taskANode, taskBNode, taskCNode]],
});
```
`workflow.Chain(workflow.Start, nodeA, nodeB, nodeC)` wires nodes into a sequential edge slice. Each node's typed return value is forwarded to the next node via `event.Output` — no session state writes needed:
```go
// newSequentialNodes builds a two-step sequential workflow using the v2 graph
// engine. workflow.Chain wires the nodes in order; each node's typed return
// value is forwarded to the next node via event.Output.
//
// This is the Go equivalent of:
//
// edges=[("START", task_A_node, task_B_node)]
func newSequentialNodes() (agent.Agent, error) {
// task_A_node: transforms the user's input.
taskANode := workflow.NewFunctionNode("task_A_node",
func(_ agent.Context, input string) (string, error) {
return "Summary: " + strings.TrimSpace(input), nil
},
workflow.NodeConfig{},
)
// task_B_node: receives task A's output as its typed input and produces
// the final result. No session state reads needed.
taskBNode := workflow.NewFunctionNode("task_B_node",
func(_ agent.Context, summary string) (string, error) {
return strings.ToUpper(summary), nil
},
workflow.NodeConfig{},
)
return workflowagent.New(workflowagent.Config{
Name: "sequential_workflow",
Description: "Runs task A then task B in order via workflow.Chain.",
Edges: workflow.Chain(workflow.Start, taskANode, taskBNode),
})
}
```
Inside a `workflow { }` block, `chain(Start, taskANode, taskBNode, taskCNode)` connects the nodes in order, and `Start.then(taskANode)` runs a single node. Each node's return value is delivered to the next node as its input:
```kotlin
import com.google.adk.kt.annotations.ExperimentalWorkflowApi
import com.google.adk.kt.workflow.Start
import com.google.adk.kt.workflow.node
import com.google.adk.kt.workflow.workflow
private val taskANode =
node("task_A_node") { _, nodeInput ->
"Summary: ${nodeInput.trim()}"
}
private val taskBNode =
node("task_B_node") { _, summary ->
summary.uppercase()
}
private val taskCNode =
node("task_C_node") { _, shouted ->
"$shouted (done)"
}
val sequentialWorkflow =
workflow("sequential_workflow") {
chain(Start, taskANode, taskBNode, taskCNode)
}
```
### Route branches and conditional execution
In Python, branching is handled by a `FunctionNode` that returns an `Event(route=...)` value, which the `edges` dict dispatches to different nodes.
```python
from google.adk import Event, Workflow
from google.adk.agents import Agent
def router(node_input: str):
"""Route to task B or C based on node_input."""
if condition(node_input):
return Event(route="RUN_TASK_C")
return Event(route="RUN_TASK_B")
task_B_node = Agent(name="task_B_agent") # An agent to execute node B
def task_C_node(node_input: str):
"""A FunctionNode to execute node C."""
return Event(output="Task C completed")
root_agent = Workflow(
name="routing_workflow",
edges=[
("START", task_A_node, router),
(router,
{
# "route value": node_to_run
"RUN_TASK_B": task_B_node,
"RUN_TASK_C": task_C_node,
},
),
],
)
```
Branching requires a node that emits a `route` value, and an edge row that maps each route value to the node that handles it. Route values can be strings, numbers, or booleans. The `DEFAULT_ROUTE` setting matches when no other route on the same source node matches. A branch target can be any node-like value: in this example, `taskBNode` is an `LlmAgent` and `taskCNode` is a function.
```typescript
import {
createEvent,
LlmAgent,
node,
NodeContext,
Workflow,
} from '@google/adk';
const taskANode = node(
(_ctx: NodeContext, nodeInput: string) => nodeInput.trim(),
{ name: 'task_A_node' },
);
/** Stands in for an application-specific branch condition. */
const condition = (nodeInput: string) => /\d/.test(nodeInput);
/** Routes to task B or C based on nodeInput. */
const router = node(
(_ctx: NodeContext, nodeInput: string) =>
condition(nodeInput)
? createEvent({ route: 'RUN_TASK_C', output: nodeInput })
: createEvent({ route: 'RUN_TASK_B', output: nodeInput }),
{ name: 'router' },
);
const taskBNode = new LlmAgent({
name: 'task_B_agent',
model: 'gemini-flash-latest',
instruction: 'Answer the user in a single short sentence.',
});
const taskCNode = node(() => 'Task C completed', { name: 'task_C_node' });
export const rootAgent = new Workflow({
name: 'routing_workflow',
edges: [
['START', taskANode, router],
[
router,
{
RUN_TASK_B: taskBNode,
RUN_TASK_C: taskCNode,
},
],
],
});
```
In ADK Go v2.0.0, conditional dispatch uses the `workflow` graph engine. A node sets `Event.Routes` to one or more string route keys, and each `workflow.Edge` selects its successor using a `workflow.Route` matcher:
- `workflow.StringRoute("category")` — matches a single string value
- `workflow.IntRoute(n)` or `workflow.MultiRoute[int]{1, 2, 3}` — matches integer values
- `workflow.BoolRoute(true)` — matches a boolean value
- `workflow.Default` — matches when no other route on the same source node matches
ADK Go compares routes as strings, so a node emits integer and boolean route values in their string form, such as `Routes: []string{"2"}` for `workflow.IntRoute(2)` or `[]string{"true"}` for `workflow.BoolRoute(true)`.
The following pattern is the Go equivalent of the Python router:
```go
// classifyNode emits an Event with Routes=[]string{"BUG"},
// ["CUSTOMER_SUPPORT"], or ["LOGISTICS"] based on the message.
edges := workflow.Concat(
workflow.Chain(workflow.Start, processMessage, classifyNode),
[]workflow.Edge{
{From: classifyNode, To: bugHandler, Route: workflow.StringRoute("BUG")},
{From: classifyNode, To: supportHandler, Route: workflow.StringRoute("CUSTOMER_SUPPORT")},
{From: classifyNode, To: logisticsHandler, Route: workflow.StringRoute("LOGISTICS")},
},
)
rootAgent, _ := workflowagent.New(workflowagent.Config{
Name: "routing_workflow",
Edges: edges,
})
```
`workflow.EdgeBuilder` provides a fluent alternative to assembling the `[]workflow.Edge` slice by hand. The builder's `Add`, `AddFanOut`, and `AddFanIn` methods express the same topology with less repetition:
```go
eb := workflow.NewEdgeBuilder()
eb.Add(workflow.Start, processMessage)
eb.Add(processMessage, classifyNode)
eb.AddRoute(classifyNode, bugHandler, workflow.StringRoute("BUG"))
eb.AddRoute(classifyNode, supportHandler, workflow.StringRoute("CUSTOMER_SUPPORT"))
eb.AddRoute(classifyNode, logisticsHandler, workflow.StringRoute("LOGISTICS"))
rootAgent, _ := workflowagent.New(workflowagent.Config{
Name: "routing_workflow",
Edges: eb.Build(),
})
```
For complete, runnable routing examples see: [string routing](https://github.com/google/adk-go/tree/v2/examples/workflow/routing/string), [int / multi-value routing](https://github.com/google/adk-go/tree/v2/examples/workflow/routing/int), and [LLM-driven routing](https://github.com/google/adk-go/tree/v2/examples/workflow/routing/llm).
Prebuilt agents: encoding routing in state
When using `sequentialagent` / `parallelagent` / `loopagent` instead of the graph engine, there is no `Event.Routes` dispatch. Encode the routing decision in session state via `OutputKey` and let downstream agents inspect it in their `Instruction` template, or use a `loopagent` with an `Escalate`-based exit — see the [loop and escalation example](#loop-and-escalation-exit) below.
A node selects a route by setting `context.routes`, and a `route { }` block maps each route value to the node that handles it. Route values can be strings (`Route.Tag`), integers (`Route.Num`), or booleans (`Route.Flag`). An `otherwise(node)` entry matches when no other route on the same source node matches. A branch target can be any node: in this example, `taskBNode` is an `LlmAgent` and `taskCNode` is a function node.
```kotlin
import com.google.adk.kt.agents.Instruction
import com.google.adk.kt.agents.LlmAgent
import com.google.adk.kt.annotations.ExperimentalWorkflowApi
import com.google.adk.kt.models.Gemini
import com.google.adk.kt.workflow.Route
import com.google.adk.kt.workflow.Start
import com.google.adk.kt.workflow.node
import com.google.adk.kt.workflow.workflow
private val taskANode =
node("task_A_node") { _, nodeInput -> nodeInput.trim() }
/** Stands in for an application-specific branch condition. */
private fun condition(nodeInput: String) = nodeInput.any { it.isDigit() }
/** Routes to task B or C based on nodeInput. */
private val router =
node("router") { context, nodeInput ->
val route = if (condition(nodeInput)) "RUN_TASK_C" else "RUN_TASK_B"
context.routes = listOf(Route.Tag(route))
nodeInput
}
private val taskBNode =
LlmAgent(
name = "task_B_agent",
model = Gemini(name = "gemini-flash-latest"),
instruction = Instruction("Answer the user in a single short sentence."),
)
private val taskCNode =
node("task_C_node") { _, _ -> "Task C completed" }
val routingWorkflow =
workflow("routing_workflow") {
chain(Start, taskANode, router).route {
// "route value" then nodeToRun
"RUN_TASK_B" then taskBNode
"RUN_TASK_C" then taskCNode
}
}
```
## Parallel tasks: fan out and join paths
You can create graphs that split execution across multiple, parallel nodes, and typically you need to assemble the output of each node for further processing. This task execution pattern has two stages. The workflow first fans out when it starts multiple parallel tasks, and then it re-joins those paths when those tasks are completed before proceeding to the next step.
**Figure 2.** The output of parallel task nodes can be assembled and joined before passing results to the next step.
You accomplish the join step by using a ***JoinNode*** object, which waits for each parallel task to complete and then passes the collection of outputs from these nodes to the next node.
```python
from google.adk.workflow import JoinNode
my_join_node = JoinNode(name="my_join_node")
edges=[
("START", parallel_task_A, my_join_node),
("START", parallel_task_B, my_join_node),
("START", parallel_task_C, my_join_node),
(my_join_node, final_task_D),
]
```
A `JoinNode` is the fan-in barrier. This logic mechanism waits for every predecessor task to complete, and then passes its successor a record keyed by predecessor node name:
```typescript
import { JoinNode, node, NodeContext, Workflow } from '@google/adk';
const parallelTaskA = node(
(_ctx: NodeContext, text: string) => text.toUpperCase(),
{ name: 'parallel_task_A' },
);
const parallelTaskB = node((_ctx: NodeContext, text: string) => text.length, {
name: 'parallel_task_B',
});
const parallelTaskC = node(
(_ctx: NodeContext, text: string) => text.split('').reverse().join(''),
{ name: 'parallel_task_C' },
);
const myJoinNode = new JoinNode({ name: 'my_join_node' });
const finalTaskD = node(
(_ctx: NodeContext, results: Record) =>
[
`Uppercase: ${results['parallel_task_A']}`,
`Length: ${results['parallel_task_B']}`,
`Reversed: ${results['parallel_task_C']}`,
].join('\n'),
{ name: 'final_task_D' },
);
export const rootAgent = new Workflow({
name: 'fan_out_workflow',
edges: [
['START', parallelTaskA, myJoinNode],
['START', parallelTaskB, myJoinNode],
['START', parallelTaskC, myJoinNode],
[myJoinNode, finalTaskD],
],
});
```
ADK Go v2.0.0 provides `workflow.NewJoinNode` for true fan-in in the graph engine: fan-out edges from `workflow.Start` (or any shared source node) feed in parallel to the join node, which waits for all of them to complete before emitting a `map[string]any` keyed by predecessor node name to the next node.
`workflow.EdgeBuilder` makes the fan-out / fan-in wiring concise with its dedicated `AddFanOut` and `AddFanIn` helpers (as shown in the [complex workflow example](https://github.com/google/adk-go/tree/v2/examples/workflow/complex)):
```go
gatherNode := workflow.NewJoinNode("gather")
eb := workflow.NewEdgeBuilder()
eb.AddFanOut(workflow.Start, researchNodeA, researchNodeB, researchNodeC)
eb.AddFanIn(gatherNode, researchNodeA, researchNodeB, researchNodeC)
eb.Add(gatherNode, formatNode)
eb.Add(formatNode, synthesisNode)
rootAgent, _ := workflowagent.New(workflowagent.Config{
Name: "research_pipeline",
Edges: eb.Build(),
})
```
The following snippet shows the complete fan-out / join pattern using `workflow.NewJoinNode` and `EdgeBuilder.AddFanOut` / `AddFanIn`:
```go
// newParallelFanOut builds a fan-out / join workflow using the v2 graph engine.
// Three research nodes run in parallel from Start; workflow.NewJoinNode waits
// for all of them to complete and emits a map[nodeName]output to the format
// node, which assembles the results for a synthesis node.
//
// Graph topology:
//
// START ─┬─> research_A ──┐
// ├─> research_B ──┼─> gather (JoinNode) ─> format ─> synthesis
// └─> research_C ──┘
//
// Python equivalent:
//
// edges=[
// ("START", research_A, my_join_node),
// ("START", research_B, my_join_node),
// ("START", research_C, my_join_node),
// (my_join_node, format_node),
// (format_node, synthesis_node),
// ]
func newParallelFanOut() (agent.Agent, error) {
researchA := workflow.NewFunctionNode("research_A",
func(_ agent.Context, _ any) (string, error) {
return "Fact about renewable energy.", nil
},
workflow.NodeConfig{},
)
researchB := workflow.NewFunctionNode("research_B",
func(_ agent.Context, _ any) (string, error) {
return "Fact about electric vehicles.", nil
},
workflow.NodeConfig{},
)
researchC := workflow.NewFunctionNode("research_C",
func(_ agent.Context, _ any) (string, error) {
return "Fact about carbon capture.", nil
},
workflow.NodeConfig{},
)
// workflow.NewJoinNode waits for all predecessors (research_A, research_B,
// research_C) to complete and emits a map[nodeName]output to its successor.
gatherNode := workflow.NewJoinNode("gather")
// formatNode receives map[string]any from gatherNode and assembles a
// combined prompt string.
formatNode := workflow.NewFunctionNode("format",
func(_ agent.Context, results map[string]any) (string, error) {
return fmt.Sprintf("A: %v\nB: %v\nC: %v",
results["research_A"],
results["research_B"],
results["research_C"],
), nil
},
workflow.NodeConfig{},
)
synthesisNode := workflow.NewFunctionNode("synthesis",
func(_ agent.Context, prompt string) (string, error) {
return "Combined report: " + prompt, nil
},
workflow.NodeConfig{},
)
// EdgeBuilder.AddFanOut fans workflow.Start out to all three research nodes.
// EdgeBuilder.AddFanIn routes all three research nodes into gatherNode.
eb := workflow.NewEdgeBuilder()
eb.AddFanOut(workflow.Start, researchA, researchB, researchC)
eb.AddFanIn(gatherNode, researchA, researchB, researchC)
eb.Add(gatherNode, formatNode)
eb.Add(formatNode, synthesisNode)
return workflowagent.New(workflowagent.Config{
Name: "fan_out_workflow",
Description: "Parallel research fan-out with JoinNode barrier and synthesis.",
Edges: eb.Build(),
})
}
```
`then(a, b, c)` fans out to nodes that run concurrently, and `joinTo` connects them to a `JoinNode`. The join waits for every predecessor to complete, and then passes its successor a map keyed by predecessor node name:
```kotlin
import com.google.adk.kt.annotations.ExperimentalWorkflowApi
import com.google.adk.kt.workflow.JoinNode
import com.google.adk.kt.workflow.Start
import com.google.adk.kt.workflow.node
import com.google.adk.kt.workflow.workflow
private val parallelTaskA =
node("parallel_task_A") { _, text -> text.uppercase() }
private val parallelTaskB =
node("parallel_task_B") { _, text -> text.length }
private val parallelTaskC =
node("parallel_task_C") { _, text -> text.reversed() }
private val myJoinNode = JoinNode("my_join_node")
private val finalTaskD =
node