Introduction to A2A¶
A2A 简介¶
As you build more complex agentic systems, you will find that a single agent is often not enough. You will want to create specialized agents that can collaborate to solve a problem. The Agent2Agent (A2A) Protocol is the standard that allows these agents to communicate with each other.
当您构建更复杂的智能体系统时,您会发现单个智能体往往不够。您将希望创建能够协作解决问题的专业智能体。Agent2Agent (A2A) 协议 是允许这些智能体相互通信的标准。
When to Use A2A vs. Local Sub-Agents¶
何时使用 A2A 与本地子智能体¶
-
Local Sub-Agents: These are agents that run within the same application process as your main agent. They are like internal modules or libraries, used to organize your code into logical, reusable components. Communication between a main agent and its local sub-agents is very fast because it happens directly in memory, without network overhead.
-
本地子智能体: 这些智能体在与主智能体相同的应用程序进程内运行。它们就像内部模块或库,用于将代码组织成逻辑的、可重用的组件。主智能体与其本地子智能体之间的通信非常快,因为它直接在内存中进行,没有网络开销。
-
Remote Agents (A2A): These are independent agents that run as separate services, communicating over a network. A2A defines the standard protocol for this communication.
-
远程智能体 (A2A): 这些是作为独立服务运行的独立智能体,通过网络进行通信。A2A 定义了这种通信的标准协议。
Consider using A2A when:
考虑在以下情况下使用 A2A:
- The agent you need to talk to is a separate, standalone service (e.g., a specialized financial modeling agent).
- The agent is maintained by a different team or organization.
- You need to connect agents written in different programming languages or agent frameworks.
-
You want to enforce a strong, formal contract (the A2A protocol) between your system's components.
-
您需要与之通信的智能体是一个独立的、单独的服务(例如,专业的金融建模智能体)。
- 该智能体由不同的团队或组织维护。
- 您需要连接使用不同编程语言或智能体框架编写的智能体。
- 您希望在系统的组件之间强制执行强正式合约(A2A 协议)。
When to Use A2A: Concrete Examples¶
何时使用 A2A:具体示例¶
- Integrating with a Third-Party Service: Your main agent needs to get real-time stock prices from an external financial data provider. This provider exposes its data through an A2A-compatible agent.
- Microservices Architecture: You have a large system broken down into smaller, independent services (e.g., an Order Processing Agent, an Inventory Management Agent, a Shipping Agent). A2A is ideal for these services to communicate with each other across network boundaries.
- Cross-Language Communication: Your core business logic is in a Python agent, but you have a legacy system or a specialized component written in Java that you want to integrate as an agent. A2A provides the standardized communication layer.
-
Formal API Enforcement: You are building a platform where different teams contribute agents, and you need a strict contract for how these agents interact to ensure compatibility and stability.
-
与第三方服务集成: 您的主智能体需要从外部金融数据提供商获取实时股票价格。该提供商通过兼容 A2A 的智能体公开其数据。
- 微服务架构: 您有一个大型系统,分解为更小的、独立的服务(例如,订单处理智能体、库存管理智能体、发货智能体)。A2A 非常适合这些服务跨越网络边界进行相互通信。
- 跨语言通信: 您的核心业务逻辑在 Python 智能体中,但您有一个遗留系统或用 Java 编写的专业组件,您希望将其集成为智能体。A2A 提供了标准化的通信层。
- 正式 API 强制执行: 您正在构建一个平台,不同团队在其中贡献智能体,您需要这些智能体如何交互的严格合约,以确保兼容性和稳定性。
When NOT to Use A2A: Concrete Examples (Prefer Local Sub-Agents)¶
何时不使用 A2A:具体示例(优先使用本地子智能体)¶
- Internal Code Organization: You are breaking down a complex task within a
single agent into smaller, manageable functions or modules (e.g., a
DataValidatorsub-agent that cleans input data before processing). These are best handled as local sub-agents for performance and simplicity. - Performance-Critical Internal Operations: A sub-agent is responsible for a
high-frequency, low-latency operation that is tightly coupled with the main
agent's execution (e.g., a
RealTimeAnalyticssub-agent that processes data streams within the same application). - Shared Memory/Context: When sub-agents need direct access to the main agent's internal state or shared memory for efficiency, A2A's network overhead and serialization/deserialization would be counterproductive.
-
Simple Helper Functions: For small, reusable pieces of logic that don't require independent deployment or complex state management, a simple function or class within the same agent is more appropriate than a separate A2A agent.
-
内部代码组织: 您正在将单个智能体内的复杂任务分解为更小的、可管理的函数或模块(例如,一个
DataValidator子智能体,用于在处理之前清理输入数据)。这些最好作为本地子智能体处理,以提高性能和简洁性。 - 性能关键型内部操作: 子智能体负责一个与主智能体执行紧密耦合的高频、低延迟操作(例如,一个在同一应用程序内处理数据流的
RealTimeAnalytics子智能体)。 - 共享内存/上下文: 当子智能体需要直接访问主智能体的内部状态或共享内存以提高效率时,A2A 的网络开销和序列化/反序列化会适得其反。
- 简单的辅助函数: 对于不需要独立部署或复杂状态管理的小型、可重用的逻辑片段,同一智能体内的简单函数或类比单独的 A2A 智能体更合适。
The A2A Workflow in ADK: A Simplified View¶
ADK 中的 A2A 工作流:简化视图¶
Agent Development Kit (ADK) simplifies the process of building and connecting agents using the A2A protocol. Here's a straightforward breakdown of how it works:
Agent Development Kit (ADK) 简化了使用 A2A 协议构建和连接智能体的过程。以下是其工作原理的简单分解:
-
Making an Agent Accessible (Exposing): You start with an existing ADK agent that you want other agents to be able to interact with. The ADK provides a simple way to "expose" this agent, turning it into an A2AServer. This server acts as a public interface, allowing other agents to send requests to your agent over a network. Think of it like setting up a web server for your agent.
-
使智能体可访问(暴露): 您从现有的 ADK 智能体开始,希望其他智能体能够与之交互。ADK 提供了一种简单的方法来"暴露"这个智能体,将其转换为 A2AServer。这个服务器充当公共接口,允许其他智能体通过网络向您的智能体发送请求。将其想象成为您的智能体设置一个 Web 服务器。
-
Connecting to an Accessible Agent (Consuming): In a separate agent (which could be running on the same machine or a different one), you'll use a special ADK component called
RemoteA2aAgent. ThisRemoteA2aAgentacts as a client that knows how to communicate with the A2AServer you exposed earlier. It handles all the complexities of network communication, authentication, and data formatting behind the scenes. -
连接到可访问的智能体(消费): 在一个单独的智能体(可以在同一台机器或不同的机器上运行)中,您将使用一个名为
RemoteA2aAgent的特殊 ADK 组件。这个RemoteA2aAgent充当客户端,知道如何与您之前暴露的 A2AServer 通信。它在后台处理网络通信、身份验证和数据格式化的所有复杂性。
From your perspective as a developer, once you've set up this connection, interacting with the remote agent feels just like interacting with a local tool or function. The ADK abstracts away the network layer, making distributed agent systems as easy to work with as local ones.
从开发人员的角度来看,一旦您设置了这个连接,与远程智能体交互的感觉就像与本地工具或函数交互一样。ADK 抽象化了网络层,使分布式智能体系统像本地系统一样易于使用。
Visualizing the A2A Workflow¶
可视化 A2A 工作流¶
To further clarify the A2A workflow, let's look at the "before and after" for both exposing and consuming agents, and then the combined system.
为了进一步阐明 A2A 工作流程,让我们看看暴露和消费智能体的"前后对比",然后是组合系统。
Exposing an Agent¶
暴露智能体¶
Before Exposing: Your agent code runs as a standalone component, but in this scenario, you want to expose it so that other remote agents can interact with your agent.
暴露前: 您的智能体代码作为独立组件运行,但在这种情况下,您希望暴露它,以便其他远程智能体可以与您的智能体交互。
After Exposing:
Your agent code is integrated with an A2AServer (an ADK component), making it
accessible over a network to other remote agents.
暴露后:
您的智能体代码与 A2AServer(一个 ADK 组件)集成,使其可以通过网络被其他远程智能体访问。
+-----------------+
| A2A Server |
| (ADK Component) |<--------+
+-----------------+ |
| |
v |
+-------------------+ |
| Your Agent Code | |
| (Now Accessible) | |
+-------------------+ |
|
| (Network Communication)
v
+-----------------------------+
| Remote Agent(s) |
| (Can now communicate) |
+-----------------------------+
Consuming an Agent¶
消费智能体¶
Before Consuming: Your agent (referred to as the "Root Agent" in this context) is the application you are developing that needs to interact with a remote agent. Before consuming, it lacks the direct mechanism to do so.
消费前: 您的智能体(在此上下文中称为"根智能体")是您正在开发的、需要与远程智能体交互的应用程序。在消费之前,它缺乏直接这样做的机制。
+----------------------+ +-------------------------------------------------------------+
| Root Agent | | Remote Agent |
| (Your existing code) | | (External Service that you want your Root Agent to talk to) |
+----------------------+ +-------------------------------------------------------------+
After Consuming:
Your Root Agent uses a RemoteA2aAgent (an ADK component that acts as a
client-side proxy for the remote agent) to establish communication with the
remote agent.
消费后:
您的根智能体使用 RemoteA2aAgent(充当远程智能体的客户端代理的 ADK 组件)与远程智能体建立通信。
+----------------------+ +-----------------------------------+
| Root Agent | | RemoteA2aAgent |
| (Your existing code) |<------->| (ADK Client Proxy) |
+----------------------+ | |
| +-----------------------------+ |
| | Remote Agent | |
| | (External Service) | |
| +-----------------------------+ |
+-----------------------------------+
(Now talks to remote agent via RemoteA2aAgent)
Final System (Combined View)¶
最终系统(组合视图)¶
This diagram shows how the consuming and exposing parts connect to form a complete A2A system.
此图显示了消费和暴露部分如何连接以形成完整的 A2A 系统。
Consuming Side:
+----------------------+ +-----------------------------------+
| Root Agent | | RemoteA2aAgent |
| (Your existing code) |<------->| (ADK Client Proxy) |
+----------------------+ | |
| +-----------------------------+ |
| | Remote Agent | |
| | (External Service) | |
| +-----------------------------+ |
+-----------------------------------+
|
| (Network Communication)
v
Exposing Side:
+-----------------+
| A2A Server |
| (ADK Component) |
+-----------------+
|
v
+-------------------+
| Your Agent Code |
| (Exposed Service) |
+-------------------+
Concrete Use Case: Customer Service and Product Catalog Agents¶
具体用例:客户服务和产品目录智能体¶
Let's consider a practical example: a Customer Service Agent that needs to retrieve product information from a separate Product Catalog Agent.
让我们考虑一个实际示例:一个需要从单独的 产品目录智能体 检索产品信息的 客户服务智能体。
Before A2A¶
使用 A2A 之前¶
Initially, your Customer Service Agent might not have a direct, standardized way to query the Product Catalog Agent, especially if it's a separate service or managed by a different team.
最初,您的客户服务智能体可能没有直接、标准化的方式来查询产品目录智能体,特别是如果它是单独的服务或由不同的团队管理。
+-------------------------+ +--------------------------+
| Customer Service Agent | | Product Catalog Agent |
| (Needs Product Info) | | (Contains Product Data) |
+-------------------------+ +--------------------------+
(No direct, standardized communication)
After A2A¶
使用 A2A 之后¶
By using the A2A Protocol, the Product Catalog Agent can expose its
functionality as an A2A service. Your Customer Service Agent can then easily
consume this service using ADK's RemoteA2aAgent.
通过使用 A2A 协议,产品目录智能体可以将其功能作为 A2A 服务公开。您的客户服务智能体然后可以使用 ADK 的 RemoteA2aAgent 轻松消费此服务。
+-------------------------+ +-----------------------------------+
| Customer Service Agent | | RemoteA2aAgent |
| (Your Root Agent) |<------->| (ADK Client Proxy) |
+-------------------------+ | |
| +-----------------------------+ |
| | Product Catalog Agent | |
| | (External Service) | |
| +-----------------------------+ |
+-----------------------------------+
|
| (Network Communication)
v
+-----------------+
| A2A Server |
| (ADK Component) |
+-----------------+
|
v
+------------------------+
| Product Catalog Agent |
| (Exposed Service) |
+------------------------+
In this setup, first, the Product Catalog Agent needs to be exposed via an A2A
Server. Then, the Customer Service Agent can simply call methods on the
RemoteA2aAgent as if it were a tool, and the ADK handles all the underlying
communication to the Product Catalog Agent. This allows for clear separation of
concerns and easy integration of specialized agents.
在此设置中,首先,产品目录智能体需要通过 A2A 服务器公开。然后,客户服务智能体可以简单地调用 RemoteA2aAgent 上的方法,就像调用工具一样,ADK 处理与产品目录智能体的所有底层通信。这允许明确的关注点分离和易于集成专业智能体。
Next Steps¶
下一步¶
Now that you understand the "why" of A2A, let's dive into the "how."
既然您了解了 A2A 的"原因",让我们深入了解"如何"。
-
Continue to the next guide: Quickstart: Exposing Your Agent
-
继续下一个指南: 快速入门:暴露您的智能体