Agent2Agent (A2A) Crash Course: Full Walkthrough With Real Multi-Agent Examples

By aiwithbrandon

TechnologyAIBusiness
Share:

Key Concepts

  • A2A Protocol: Standardizes communication between AI agents, regardless of their underlying frameworks.
  • Agent Card: A "business card" for an agent, detailing its name, description, inputs, outputs, skills, and example queries.
  • Agent Executor: Wraps an agent and provides execute and cancel functions to trigger and stop agent execution.
  • Messages: Standardized data format for communication, containing a role (e.g., user, agent), text content, and metadata (message ID, task ID, context ID).
  • Tasks: Represents long-running agent processes, including artifacts (stored work), and status updates (submitted, in progress, completed, failed, canceled).
  • Remote Agent: An agent running on a separate server, accessible via HTTP/HTTPS using the A2A protocol.
  • Host Agent: The primary agent that initiates communication with remote agents.

Phase 1: Core Concepts of A2A

The Need for Standardization

  • Without A2A, integrating agents requires redundant work: determining agent capabilities, input/output formats, and response types for each agent.
  • A2A standardizes communication, minimizing integration effort and ensuring consistent interaction patterns.

Core Concepts Explained

  • Agent Card:
    • Acts as a business card, providing essential information about an agent.
    • Includes:
      • Name: Identifies the agent (e.g., "Hampton Hotel Agent").
      • Description: High-level overview of the agent's purpose (e.g., "Personal concierge for booking reservations").
      • Inputs/Outputs: Specifies expected data formats (text, audio).
      • Skills: Lists specific actions the agent can perform (e.g., "Book a room," "Change reservation," "Find available dates").
      • Example Queries: Demonstrates how to interact with the agent.
  • Standardized Communication:
    • A2A uses HTTP/HTTPS for message exchange.
    • Messages contain:
      • Role: Indicates the sender (e.g., "user," "agent").
      • Text: The actual content of the message.
      • Metadata: Unique identifiers for message, task, and context management.
  • Message Responses:
    • Success: Indicates successful message processing.
    • Message: A quick response containing the requested information.
    • Task: Used for long-running processes, providing:
      • Artifacts: Stored results of the agent's work (e.g., flight plans, car rental plans).
      • Status: Updates on the task's progress (submitted, in progress, completed, failed, canceled).
  • Agent Executor:
    • Wraps an agent, providing a standardized interface for execution.
    • Implements execute and cancel functions.
    • The execute function triggers the agent's logic (e.g., CrewAI.kickoff, agent.invoke).

End-to-End Workflow Example

  1. Client Request: User sends a request to a client agent (e.g., "Get the weather for Miami Florida this weekend").
  2. Agent Card Lookup: The client agent uses A2A to discover available agents and their capabilities by looking up their agent cards.
  3. Message Sending: The client agent sends a message to the appropriate remote agent (e.g., weather agent) using RPC.
  4. Server Processing: The A2A server receives the message and passes it to the weather agent executor.
  5. Agent Execution: The agent executor calls the execute function, triggering the weather agent.
  6. Response: The weather agent sends back a message containing the weather forecast.
  7. Client Display: The client agent formats and displays the information to the user.

Phase 2: Simple A2A Workflow with Code

Overview

  • Building a simple client-server application using A2A.
  • The client sends a request to a server with a dummy greeting agent.
  • The greeting agent responds with a predefined message ("Hello YouTube, make sure you like and subscribe").

Components

  • Client (client.py):
    • Fetches the agent card from the server.
    • Sends a message to the server.
    • Prints the server's response.
  • Server (main.py):
    • Runs a dummy greeting agent.
    • Listens for requests.
    • Uses an agent executor to handle messages.
  • Agent Card (agent.py):
    • Defines the greeting agent's capabilities (e.g., "greets users").

Code Details

  • Agent Card:
    • Defines the agent's name, description, inputs/outputs, and skills.
    • Includes capabilities (e.g., streaming, push notifications).
  • Server:
    • Uses A2A library to create a request handler and server.
    • Wraps the greeting agent in an agent executor.
    • Uses Uvicorn to run the server on localhost:9999.
  • Agent Executor:
    • Extends the AgentExecutor class.
    • Implements the execute function, which:
      • Adds events to an event queue.
      • Calls the agent's invoke function.
      • Creates a new message with the agent's response.
  • Client:
    • Uses A2ACardResolver to fetch the agent card from the server.
    • Creates an A2AClient to send messages.
    • Constructs a message with a text part ("Hey how are you?").
    • Sends the message to the server and prints the response.

Running the Code

  1. Create a virtual environment.
  2. Activate the virtual environment.
  3. Run the server (uvicorn main:app --reload).
  4. In a separate terminal, activate the virtual environment.
  5. Run the client (python client.py).

Results

  • The server receives a GET request for the agent card.
  • The client fetches the agent card and sends a message.
  • The server processes the message and responds with "Hello YouTube, make sure you like and subscribe."
  • The client prints the server's response.

Phase 3: Multi-Agent System for Pickleball Scheduling

Overview

  • Building a more complex A2A workflow with real AI agents.
  • A host agent uses A2A to communicate with remote agents to schedule pickleball games.
  • Remote agents use different frameworks (ADK, CrewAI, LangGraph).

Goal

  • Use agents to determine friends' availability and schedule a pickleball game.

Components

  • Host Agent (ADK):
    • Acts as the user's personal agent.
    • Has access to tools for listing court availabilities and booking courts.
    • Uses A2A to communicate with remote agents.
  • Remote Agents:
    • Represent friends with their own external-facing agents.
    • Each agent uses a different framework (ADK, CrewAI, LangGraph).
    • Each agent has access to a tool for getting availability.

Host Agent Workflow

  1. Initialization:
    • The host agent is created with remote URLs for each friend's agent.
    • The host agent fetches the agent card for each remote agent.
    • The host agent creates a remote connection for each agent.
  2. ADK Agent Creation:
    • The host agent creates an ADK agent.
    • The agent's root instructions include a list of available agents.
  3. Message Sending:
    • The host agent uses a "send message" tool to send requests to remote agents.
    • The tool takes the friend's name and the message as input.
    • The tool checks if the agent exists and sends the message using the A2A protocol.
  4. Response Handling:
    • The host agent receives a message from the remote agent.
    • The host agent extracts the message content and displays it to the user.

Remote Agent Blueprint

  1. Create Agent: Create a regular agent using LangGraph, CrewAI, or ADK.
  2. Add Tools: Add the necessary tools for the agent (e.g., "list availabilities").
  3. Wrap in Agent Executor: Wrap the agent in an agent executor to handle A2A communication.
  4. Create Agent Card: Create an agent card with the agent's skills and capabilities.
  5. Create Request Handler: Create a request handler that points to the agent executor.
  6. Run Remote Server: Create and run the remote agent server.

Remote Agent Details

  • ADK Agent (Carly):
    • Uses an ADK agent to get Carly's availability.
    • The agent executor processes the request, runs the agent, and converts the response to an A2A-compliant message.
  • CrewAI Agent (Nate):
    • Uses a CrewAI agent to get Nate's availability.
    • The agent executor processes the request, kicks off the CrewAI agent, and converts the response to an A2A-compliant message.
  • LangGraph Agent (Caitlyn):
    • Uses a LangGraph agent to get Caitlyn's availability.
    • The agent executor processes the request, streams the response from the LangGraph agent, and converts the response to an A2A-compliant message.

Running the Multi-Agent System

  1. Set up all remote agents (Caitlyn, Carly, Nate).
  2. Run the host agent (ADK).
  3. Interact with the host agent to schedule a pickleball game.

Results

  • The host agent connects to all remote agents.
  • The host agent sends messages to remote agents to get their availability.
  • The host agent checks court availabilities and books a court.
  • The host agent confirms the booking with the user.

Conclusion

The A2A protocol enables seamless communication between AI agents, regardless of their underlying frameworks. This allows for the creation of complex multi-agent systems that can solve real-world problems.

Chat with this Video

AI-Powered

Hi! I can answer questions about this video "Agent2Agent (A2A) Crash Course: Full Walkthrough With Real Multi-Agent Examples". What would you like to know?

Chat is based on the transcript of this video and may not be 100% accurate.

Related Videos

Ready to summarize another video?

Summarize YouTube Video