adk-skill
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGoogle Agent Development Kit (ADK) Guide
Google Agent Development Kit (ADK) 指南
Overview
概述
ADK is Google's open-source framework for building AI agents powered by Gemini models. It supports single-agent and multi-agent architectures with built-in tool integration, state management, callbacks, guardrails, and deployment options.
ADK是Google推出的开源框架,用于构建基于Gemini模型的AI Agent。它支持单Agent和多Agent架构,内置工具集成、状态管理、回调机制、防护措施以及部署选项。
Documentation & Resources
文档与资源
For up-to-date API references and detailed guides beyond this skill, always consult:
- ADK Docs Index: https://google.github.io/adk-docs/llms.txt
- Official Samples: https://github.com/google/adk-samples (Python, Java, TypeScript)
如需获取本技能之外的最新API参考和详细指南,请务必查阅:
Supported Languages
支持的语言
| Language | Package | Install |
|---|---|---|
| Python | | |
| Java | | Maven/Gradle |
| Go | | |
| TypeScript | | |
This guide shows Python examples. For Java, Go, and TypeScript patterns, see references/multi-language.md.
| 语言 | 包名 | 安装方式 |
|---|---|---|
| Python | | |
| Java | | Maven/Gradle |
| Go | | |
| TypeScript | | |
本指南展示Python示例。Java、Go和TypeScript的实现模式,请参阅references/multi-language.md。
Quick Reference
快速参考
| Task | Approach |
|---|---|
| Single agent | |
| Sequential pipeline | |
| Parallel execution | |
| Iterative refinement | |
| Agent-as-tool | Wrap agent with |
| Remote agent (A2A) | |
| Custom tools | Python functions with type hints + docstrings |
| Structured output | Pydantic model via |
| State management | |
| MCP integration | |
| Testing | |
| Evaluation | EvalSet with |
| 任务 | 实现方式 |
|---|---|
| 单Agent | 使用带工具和指令的 |
| 顺序流水线 | 使用包含有序子Agent的 |
| 并行执行 | 使用包含独立子Agent的 |
| 迭代优化 | 使用带max_iterations或检查器Agent的 |
| Agent作为工具 | 使用 |
| 远程Agent(A2A) | 使用 |
| 自定义工具 | 带类型提示和文档字符串的Python函数 |
| 结构化输出 | 通过 |
| 状态管理 | 使用 |
| MCP集成 | 使用带连接参数的 |
| 测试 | 使用 |
| 评估 | 使用带 |
Project Structure
项目结构
Every ADK project follows this layout:
my_agent/
├── my_agent/
│ ├── __init__.py # Must import agent module
│ ├── agent.py # Defines root_agent (entry point)
│ ├── prompts.py # Instruction strings (optional)
│ ├── tools.py # Custom tool functions (optional)
│ ├── sub_agents/ # Sub-agent packages (optional)
│ └── shared_libraries/ # Callbacks, utilities (optional)
├── tests/
│ └── test_agent.py
├── pyproject.toml
└── .env # GOOGLE_API_KEY or GOOGLE_CLOUD_PROJECT每个ADK项目都遵循以下布局:
my_agent/
├── my_agent/
│ ├── __init__.py # 必须导入agent模块
│ ├── agent.py # 定义root_agent(入口点)
│ ├── prompts.py # 指令字符串(可选)
│ ├── tools.py # 自定义工具函数(可选)
│ ├── sub_agents/ # 子Agent包(可选)
│ └── shared_libraries/ # 回调函数、工具库(可选)
├── tests/
│ └── test_agent.py
├── pyproject.toml
└── .env # GOOGLE_API_KEY或GOOGLE_CLOUD_PROJECTCritical: init.py
关键文件:init.py
python
undefinedpython
undefinedmy_agent/init.py
my_agent/init.py
from . import agent
undefinedfrom . import agent
undefinedCritical: root_agent
关键:root_agent
The variable at module level is the framework entry point:
root_agentpython
undefined模块级别的变量是框架的入口点:
root_agentpython
undefinedmy_agent/agent.py
my_agent/agent.py
from google.adk.agents import Agent
root_agent = Agent(
name="my_agent",
model="gemini-2.5-flash",
description="Brief description for agent discovery",
instruction="Detailed system prompt...",
tools=[...],
)
undefinedfrom google.adk.agents import Agent
root_agent = Agent(
name="my_agent",
model="gemini-2.5-flash",
description="用于Agent发现的简短描述",
instruction="详细的系统提示词...",
tools=[...],
)
undefinedpyproject.toml
pyproject.toml
toml
[project]
name = "my-agent"
version = "0.1.0"
requires-python = ">=3.10"
dependencies = ["google-adk"]
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"toml
[project]
name = "my-agent"
version = "0.1.0"
requires-python = ">=3.10"
dependencies = ["google-adk"]
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"Agent Types
Agent类型
1. LlmAgent (Single Agent)
1. LlmAgent(单Agent)
The fundamental building block. Wraps a single LLM call with tools and instructions.
python
from google.adk.agents import LlmAgent
agent = LlmAgent(
name="assistant",
model="gemini-2.5-flash",
description="General assistant",
instruction="""You are a helpful assistant.
Use the search tool when you need current information.""",
tools=[search_tool],
output_schema=ResponseModel, # Optional structured output
output_key="response", # State key for output
generate_content_config=types.GenerateContentConfig(
temperature=0.7,
),
)基础构建模块。将单次LLM调用与工具和指令封装在一起。
python
from google.adk.agents import LlmAgent
agent = LlmAgent(
name="assistant",
model="gemini-2.5-flash",
description="通用助手",
instruction="""你是一个乐于助人的助手。
需要当前信息时,请使用搜索工具。""",
tools=[search_tool],
output_schema=ResponseModel, # 可选的结构化输出
output_key="response", # 用于存储输出的状态键
generate_content_config=types.GenerateContentConfig(
temperature=0.7,
),
)2. SequentialAgent
2. SequentialAgent
Runs sub-agents in order. Output of each flows to the next via shared state.
python
from google.adk.agents import SequentialAgent
pipeline = SequentialAgent(
name="research_pipeline",
description="Research then summarize",
sub_agents=[
researcher_agent, # Step 1: writes to state["research"]
summarizer_agent, # Step 2: reads state["research"]
],
)按顺序运行子Agent。每个Agent的输出通过共享状态传递给下一个。
python
from google.adk.agents import SequentialAgent
pipeline = SequentialAgent(
name="research_pipeline",
description="先研究再总结",
sub_agents=[
researcher_agent, # 步骤1:写入state["research"]
summarizer_agent, # 步骤2:读取state["research"]
],
)3. ParallelAgent
3. ParallelAgent
Runs sub-agents concurrently. Use for independent tasks.
python
from google.adk.agents import ParallelAgent
parallel = ParallelAgent(
name="multi_channel",
description="Send to all channels simultaneously",
sub_agents=[
email_agent,
slack_agent,
calendar_agent,
],
)并发运行子Agent。适用于独立任务。
python
from google.adk.agents import ParallelAgent
parallel = ParallelAgent(
name="multi_channel",
description="同时发送到所有渠道",
sub_agents=[
email_agent,
slack_agent,
calendar_agent,
],
)4. LoopAgent
4. LoopAgent
Repeats sub-agents until termination. Two termination patterns:
Pattern A: Fixed iterations
python
from google.adk.agents import LoopAgent
loop = LoopAgent(
name="refinement_loop",
description="Iteratively refine output",
sub_agents=[writer_agent, critic_agent],
max_iterations=3,
)Pattern B: Checker agent with escalate
python
undefined重复运行子Agent直到终止。有两种终止模式:
模式A:固定迭代次数
python
from google.adk.agents import LoopAgent
loop = LoopAgent(
name="refinement_loop",
description="迭代优化输出",
sub_agents=[writer_agent, critic_agent],
max_iterations=3,
)模式B:带升级机制的检查器Agent
python
undefinedThe checker agent uses tool_context.actions.escalate = True to stop
检查器Agent使用tool_context.actions.escalate = True来停止循环
def check_quality(score: float, tool_context: ToolContext) -> str:
"""Check if quality meets threshold."""
if score >= 0.9:
tool_context.actions.escalate = True
return "Quality threshold met, stopping loop."
return "Quality below threshold, continuing refinement."
checker = Agent(
name="checker",
model="gemini-2.5-flash",
instruction="Evaluate the output quality and call check_quality.",
tools=[check_quality],
)
loop = LoopAgent(
name="quality_loop",
sub_agents=[generator_agent, checker],
)
undefineddef check_quality(score: float, tool_context: ToolContext) -> str:
"""检查质量是否达到阈值。"""
if score >= 0.9:
tool_context.actions.escalate = True
return "质量阈值已满足,停止循环。"
return "质量未达阈值,继续优化。"
checker = Agent(
name="checker",
model="gemini-2.5-flash",
instruction="评估输出质量并调用check_quality。",
tools=[check_quality],
)
loop = LoopAgent(
name="quality_loop",
sub_agents=[generator_agent, checker],
)
undefined5. Composing Agent Types
5. 组合Agent类型
Nest agent types freely for complex workflows. Example: containing a containing s. See references/advanced-patterns.md for hierarchical workflow examples.
SequentialAgentParallelAgentLlmAgent可自由嵌套Agent类型以实现复杂工作流。例如:包含的,而中又包含多个。有关分层工作流示例,请参阅references/advanced-patterns.md。
ParallelAgentSequentialAgentParallelAgentLlmAgentTools
工具
Function Tools
函数工具
Any Python function with type hints and a docstring becomes a tool:
python
def get_weather(city: str, units: str = "celsius") -> dict:
"""Get current weather for a city.
Args:
city: The city name to look up weather for.
units: Temperature units - 'celsius' or 'fahrenheit'.
Returns:
dict with temperature, conditions, and humidity.
"""
# Implementation
return {"temperature": 22, "conditions": "sunny", "humidity": 45}
agent = Agent(
name="weather_agent",
model="gemini-2.5-flash",
instruction="Help users check the weather.",
tools=[get_weather],
)Requirements:
- Type hints on all parameters
- Docstring with description and Args section
- Return type annotation
任何带类型提示和文档字符串的Python函数都可以成为工具:
python
def get_weather(city: str, units: str = "celsius") -> dict:
"""获取城市当前天气。
参数:
city: 要查询天气的城市名称。
units: 温度单位 - 'celsius'或'fahrenheit'。
返回:
包含温度、天气状况和湿度的字典。
"""
# 实现代码
return {"temperature": 22, "conditions": "sunny", "humidity": 45}
agent = Agent(
name="weather_agent",
model="gemini-2.5-flash",
instruction="帮助用户查询天气。",
tools=[get_weather],
)要求:
- 所有参数都有类型提示
- 包含描述和参数部分的文档字符串
- 有返回类型注解
Tools with State Access
带状态访问的工具
Use to read/write session state:
ToolContextpython
from google.adk.tools import ToolContext
def add_to_cart(item: str, quantity: int, tool_context: ToolContext) -> dict:
"""Add an item to the shopping cart."""
cart = tool_context.state.get("cart", [])
cart.append({"item": item, "quantity": quantity})
tool_context.state["cart"] = cart
return {"status": "added", "cart_size": len(cart)}使用读取/写入会话状态:
ToolContextpython
from google.adk.tools import ToolContext
def add_to_cart(item: str, quantity: int, tool_context: ToolContext) -> dict:
"""将商品添加到购物车。"""
cart = tool_context.state.get("cart", [])
cart.append({"item": item, "quantity": quantity})
tool_context.state["cart"] = cart
return {"status": "added", "cart_size": len(cart)}AgentTool (Agent-as-Tool)
AgentTool(Agent作为工具)
Wrap an agent to use it as a tool for another agent:
python
from google.adk.tools.agent_tool import AgentTool
specialist = Agent(
name="code_reviewer",
model="gemini-2.5-pro",
instruction="Review code for bugs and best practices.",
)
coordinator = Agent(
name="coordinator",
model="gemini-2.5-flash",
instruction="Coordinate tasks. Use code_reviewer for code reviews.",
tools=[AgentTool(agent=specialist)],
)包装一个Agent,使其成为另一个Agent的工具:
python
from google.adk.tools.agent_tool import AgentTool
specialist = Agent(
name="code_reviewer",
model="gemini-2.5-pro",
instruction="检查代码中的漏洞和最佳实践。",
)
coordinator = Agent(
name="coordinator",
model="gemini-2.5-flash",
instruction="协调任务。代码评审请使用code_reviewer。",
tools=[AgentTool(agent=specialist)],
)Built-in Tools
内置工具
python
from google.adk.tools import google_search
agent = Agent(
name="researcher",
tools=[google_search],
)python
from google.adk.tools import google_search
agent = Agent(
name="researcher",
tools=[google_search],
)MCP Tools
MCP工具
python
from google.adk.tools.mcp_tool import MCPToolset, StdioConnectionParams
from mcp import StdioServerParameters
agent = Agent(
name="db_agent",
tools=[
MCPToolset(
connection_params=StdioConnectionParams(
server_params=StdioServerParameters(
command="npx",
args=["-y", "some-mcp-server"],
),
),
),
],
)For advanced tool patterns (FunctionTool, ToolboxToolset, long-running tools), see references/tools-reference.md.
python
from google.adk.tools.mcp_tool import MCPToolset, StdioConnectionParams
from mcp import StdioServerParameters
agent = Agent(
name="db_agent",
tools=[
MCPToolset(
connection_params=StdioConnectionParams(
server_params=StdioServerParameters(
command="npx",
args=["-y", "some-mcp-server"],
),
),
),
],
)有关高级工具模式(FunctionTool、ToolboxToolset、长运行工具),请参阅references/tools-reference.md。
Callbacks
回调函数
Callbacks intercept the agent lifecycle. Return to proceed, return a value to short-circuit.
None| Callback | Signature | Use Case |
|---|---|---|
| | Initialize state |
| | Validate inputs, auto-approve |
| | Post-process results |
| | Rate limit, safety filter |
python
def before_tool(tool, args, tool_context) -> dict | None:
"""Return dict to skip tool execution with that response."""
if tool.name == "approve_discount" and args.get("value", 0) > 50:
return {"status": "rejected", "reason": "Discount too large"}
return None # Proceed normally
agent = Agent(
name="guarded_agent",
model="gemini-2.5-flash",
instruction="...",
before_tool_callback=before_tool,
)For rate limiting, input validation, and safety callbacks, see references/advanced-patterns.md.
回调函数可拦截Agent的生命周期。返回表示继续执行,返回值则表示短路执行(跳过后续步骤)。
None| 回调函数 | 签名 | 使用场景 |
|---|---|---|
| | 初始化状态 |
| | 验证输入、自动批准 |
| | 后处理结果 |
| | 速率限制、安全过滤 |
python
def before_tool(tool, args, tool_context) -> dict | None:
"""返回字典以跳过工具执行,直接使用该响应。"""
if tool.name == "approve_discount" and args.get("value", 0) > 50:
return {"status": "rejected", "reason": "折扣过大"}
return None # 正常继续执行
agent = Agent(
name="guarded_agent",
model="gemini-2.5-flash",
instruction="...",
before_tool_callback=before_tool,
)有关速率限制、输入验证和安全回调的内容,请参阅references/advanced-patterns.md。
State Management
状态管理
State is a shared dictionary across agents, tools, and callbacks. Scopes: (session), (app-wide), (user-wide).
state["key"]app:keyuser:keyPassing data between agents: Use to write to state, read in next agent's instruction:
output_keypython
researcher = Agent(name="researcher", output_key="findings", output_schema=ResearchOutput, ...)
writer = Agent(name="writer", instruction="Write report based on state['findings'].", ...)
pipeline = SequentialAgent(name="pipeline", sub_agents=[researcher, writer])状态是Agent、工具和回调函数之间共享的字典。作用域包括:(会话级)、(应用级)、(用户级)。
state["key"]app:keyuser:key在Agent之间传递数据: 使用写入状态,在下一个Agent的指令中读取:
output_keypython
researcher = Agent(name="researcher", output_key="findings", output_schema=ResearchOutput, ...)
writer = Agent(name="writer", instruction="基于state['findings']撰写报告。", ...)
pipeline = SequentialAgent(name="pipeline", sub_agents=[researcher, writer])Structured Output
结构化输出
Use Pydantic models for typed, validated agent output:
python
from pydantic import BaseModel
class AnalysisResult(BaseModel):
summary: str
key_findings: list[str]
confidence: float
recommendations: list[str]
agent = Agent(
name="analyzer",
model="gemini-2.5-flash",
instruction="Analyze the provided data and return structured results.",
output_schema=AnalysisResult,
output_key="analysis", # Stored in state["analysis"]
)使用Pydantic模型获取类型化、经过验证的Agent输出:
python
from pydantic import BaseModel
class AnalysisResult(BaseModel):
summary: str
key_findings: list[str]
confidence: float
recommendations: list[str]
agent = Agent(
name="analyzer",
model="gemini-2.5-flash",
instruction="分析提供的数据并返回结构化结果。",
output_schema=AnalysisResult,
output_key="analysis", # 存储在state["analysis"]中
)Running and Testing
运行与测试
Local Development
本地开发
bash
undefinedbash
undefinedInstall
安装
pip install google-adk
pip install google-adk
Set API key
设置API密钥
export GOOGLE_API_KEY="your-key"
export GOOGLE_API_KEY="your-key"
Run interactively
交互式运行
adk run my_agent
adk run my_agent
Run with web UI
带Web UI运行
adk web my_agent
undefinedadk web my_agent
undefinedTesting with InMemoryRunner
使用InMemoryRunner测试
python
import pytest
from google.adk.runners import InMemoryRunner
from google.genai import types
@pytest.mark.asyncio
async def test_agent():
runner = InMemoryRunner(agent=root_agent, app_name="test")
session = await runner.session_service.create_session(
user_id="test_user", app_name="test",
)
content = types.Content(
role="user", parts=[types.Part.from_text(text="Hello")],
)
events = []
async for event in runner.run_async(
user_id="test_user", session_id=session.id, new_message=content,
):
events.append(event)
assert "expected" in events[-1].content.parts[0].text.lower()python
import pytest
from google.adk.runners import InMemoryRunner
from google.genai import types
@pytest.mark.asyncio
async def test_agent():
runner = InMemoryRunner(agent=root_agent, app_name="test")
session = await runner.session_service.create_session(
user_id="test_user", app_name="test",
)
content = types.Content(
role="user", parts=[types.Part.from_text(text="Hello")],
)
events = []
async for event in runner.run_async(
user_id="test_user", session_id=session.id, new_message=content,
):
events.append(event)
assert "expected" in events[-1].content.parts[0].text.lower()Evaluation
评估
ADK provides built-in evaluation for tool correctness, response quality, and safety. Define eval cases in files and run with , , or the web UI. See references/evaluation.md for eval data formats, all 8 metrics, and patterns.
.test.jsonadk evalpytestADK内置了工具正确性、响应质量和安全性的评估功能。在文件中定义评估用例,然后使用、或Web UI运行评估。有关评估数据格式、全部8项指标和模式,请参阅references/evaluation.md。
.test.jsonadk evalpytestModel Selection
模型选择
- - Default, fast, cost-effective (temperature 0.1-0.7)
gemini-2.5-flash - - Complex reasoning, code review (temperature 0.1-0.4)
gemini-2.5-pro
Configure per-agent via .
generate_content_config=types.GenerateContentConfig(temperature=0.2)- - 默认选项,速度快、成本低(温度值0.1-0.7)
gemini-2.5-flash - - 适用于复杂推理、代码评审(温度值0.1-0.4)
gemini-2.5-pro
可通过为每个Agent单独配置。
generate_content_config=types.GenerateContentConfig(temperature=0.2)Design Patterns
设计模式
| Pattern | When to Use | ADK Implementation |
|---|---|---|
| Sequential pipeline | Multi-step tasks with dependencies | |
| Fan-out / Fan-in | Independent tasks then synthesis | |
| Reflection loop | Quality matters more than speed | |
| Dynamic routing | Diverse inputs need different handling | Parent |
| Layered fallback | Tool failures need graceful recovery | |
| Guardrailed agent | Safety/compliance requirements | |
| Resource tiering | Cost optimization under constraints | Different |
Key design rules:
- One agent = one responsibility. Split agents with 5+ tools into specialists.
- Use for structured data flow between agents via state -- not free text.
output_key - Always set on
max_iterationsto prevent unbounded execution.LoopAgent - Separate generation from evaluation -- use a different agent to critique output (avoids self-review bias).
- Write sub-agent fields carefully -- they drive Auto-Flow routing decisions.
description - Use structured output (JSON/Pydantic) between pipeline stages for reliable data passing.
- Embed reasoning steps in agent instructions: "1. Analyze → 2. Plan → 3. Execute → 4. Verify".
- Always include a fallback route for unclear inputs -- ambiguous requests must not be silently misrouted.
For hierarchical workflows, agent transfer, and deployment patterns, see references/advanced-patterns.md.
For comprehensive design patterns (chaining, reflection, planning, memory, error handling, HITL, guardrails, RAG, resource optimization, reasoning, evaluation, prompting), see references/design-patterns.md.
For A2A (Agent-to-Agent) protocol patterns -- exposing agents as A2A servers, consuming remote agents with , agent cards, and cross-service communication -- see references/a2a-protocol.md.
RemoteA2aAgent| 模式 | 使用场景 | ADK实现方式 |
|---|---|---|
| 顺序流水线 | 有依赖关系的多步骤任务 | 包含有序子Agent的 |
| 扇出/扇入 | 独立任务后进行结果合成 | |
| 反射循环 | 质量优先于速度的场景 | 包含生成器+评审Agent的 |
| 动态路由 | 多样化输入需要不同处理方式 | 包含 |
| 分层降级 | 工具故障时需要优雅恢复 | |
| 防护型Agent | 有安全/合规要求的场景 | |
| 资源分层 | 约束条件下的成本优化 | 为不同Agent使用不同 |
关键设计规则:
- 一个Agent对应一个职责。将包含5个以上工具的Agent拆分为多个专业Agent。
- 使用通过状态在Agent之间传递结构化数据——而非自由文本。
output_key - 始终为设置
LoopAgent以防止无限执行。max_iterations - 将生成与评估分离——使用不同的Agent评审输出(避免自我评审偏差)。
- 仔细编写子Agent的字段——它们会驱动自动流的路由决策。
description - 在流水线阶段之间使用结构化输出(JSON/Pydantic)以确保可靠的数据传递。
- 在Agent指令中嵌入推理步骤:"1. 分析 → 2. 规划 → 3. 执行 → 4. 验证"。
- 始终为不明确的输入提供降级路由——模糊请求不能被静默错误路由。
有关分层工作流、Agent迁移和部署模式,请参阅references/advanced-patterns.md。
有关全面的设计模式(链式调用、反射、规划、记忆、错误处理、人在回路、防护措施、RAG、资源优化、推理、评估、提示词工程),请参阅references/design-patterns.md。
有关A2A(Agent-to-Agent)协议模式——将Agent暴露为A2A服务器、使用消费远程Agent、Agent卡片和跨服务通信,请参阅references/a2a-protocol.md。
RemoteA2aAgentDecision Guide
决策指南
When to use which agent type:
Single task, one LLM call? → Agent / LlmAgent
Steps must run in order? → SequentialAgent
Steps are independent? → ParallelAgent
Need iteration/refinement? → LoopAgent
Need on-demand delegation? → AgentTool
Remote agent, different service? → RemoteA2aAgent (A2A)
Complex multi-stage? → Compose agent typesWhen to use which tool type:
Simple function? → Python function with type hints
Need state access? → Add ToolContext parameter
Delegate to another agent? → AgentTool
Remote agent over network? → RemoteA2aAgent (A2A protocol)
External MCP server? → MCPToolset
Database access? → ToolboxToolset
Web search? → google_search (built-in)如何选择Agent类型:
单一任务,单次LLM调用? → Agent / LlmAgent
步骤必须按顺序执行? → SequentialAgent
步骤相互独立? → ParallelAgent
需要迭代/优化? → LoopAgent
需要按需委托? → AgentTool
远程Agent,不同服务? → RemoteA2aAgent(A2A)
复杂多阶段任务? → 组合多种Agent类型如何选择工具类型:
简单函数? → 带类型提示的Python函数
需要访问状态? → 添加ToolContext参数
委托给另一个Agent? → AgentTool
网络上的远程Agent? → RemoteA2aAgent(A2A协议)
外部MCP服务器? → MCPToolset
数据库访问? → ToolboxToolset
网页搜索? → google_search(内置工具)