Skip to content

A2A Client Usage#

The A2A client allows you to connect to remote A2A agent servers and interact with them as if they were local agents. This guide covers everything you need to know about using A2A clients.

Warning

A2aRemoteAgent supports only asynchronous methods - this is the limitation of the A2A client we use.

Basic Client Setup#

Generally, you can use A2aRemoteAgent like any other ConversableAgent. In the example below, we specify a remote agent and start a chat between a local agent and that remote agent, asking them to generate code.

from autogen.a2a import A2aRemoteAgent

# Connect to a remote agent
remote_agent = A2aRemoteAgent(
    url="http://localhost:8000",
    name="python_coder"
)

# Use it like any other agent
await local_agent.a_initiate_chat(
    recipient=remote_agent,
    message={
        "role": "user",
        "content": "Create a calculator function",
    }
)

Here's another example of a A2aRemoteAgent client agent working with a local agent, with the conversation printed out afterwards.

import asyncio
from autogen import ConversableAgent, LLMConfig
from autogen.a2a import A2aRemoteAgent

# Configure local agent
llm_config = LLMConfig({ "model": "gpt-4o-mini" })

reviewer = ConversableAgent(
    name="code_reviewer",
    system_message="You are a code reviewer...",
    llm_config=llm_config,
)

# Connect to remote agent
coder = A2aRemoteAgent(
    url="http://localhost:8000",
    name="python_coder"
)

# Start conversation
async def review_code():
    await reviewer.a_initiate_chat(
        recipient=coder,
        message={"role": "user", "content": "Create a Python calculator"}
    )

    # Process the response
    messages = reviewer.chat_messages[coder.name]
    for message in messages:
        print(f"{message['name']}: {message['content']}")

# Run the conversation
asyncio.run(review_code())

Advanced Usage#

Custom HTTP Client#

Commonly, you will need to set some custom options on the remote agent's HTTP client, such as headers, timeout, etc. To do this, pass your own HttpxClientFactory instance to the A2aRemoteAgent constructor.

import httpx
from autogen.a2a import A2aRemoteAgent, HttpxClientFactory

# Create custom HTTP client factory
http_client = HttpxClientFactory(
    timeout=30.0,
    headers={
        "User-Agent": "MyApp/1.0",
        "Authorization": f"Bearer {your_token}"
    }
)

remote_agent = A2aRemoteAgent(
    url="http://localhost:8000",
    name="python_coder",
    client=http_client
)

Client Configuration#

The A2A client has a few configuration options that you can pass to the A2aRemoteAgent constructor, use the client_config parameter and a ClientConfig to do that.

1
2
3
4
5
6
7
8
from a2a.client import ClientConfig
from autogen.a2a import A2aRemoteAgent

remote_agent = A2aRemoteAgent(
    url="http://localhost:8000",
    name="python_coder",
    client_config=ClientConfig(streaming=True),
)

A2aRemoteAgent testing#

To help with testing remote clients, you can mock remote agent replies by passing a MockClient instance to the A2aRemoteAgent constructor.

1
2
3
4
5
6
7
from autogen.a2a import A2aRemoteAgent, MockClient

remote_agent = A2aRemoteAgent(
    url="http://localhost:8000",
    name="python_coder",
    client=MockClient(response_message="Hello, user!"),
)

Interoperability with other frameworks#

A2A is also supported by other frameworks, so you can use A2aRemoteAgent with them, too.

As an example, you can connect your AG2 agents with Pydantic AI agents. Here we create a Pydantic AI agent:

server.py
1
2
3
4
5
6
7
from pydantic_ai import Agent

agent = Agent(
    "openai:gpt-4.1",
    instructions="You are an expert Python developer...",
)
app = agent.to_a2a()

And now, without any changes, your A2aRemoteAgent clients can interact with them:

client.py
1
2
3
4
5
6
7
from autogen.a2a import A2aRemoteAgent

# works correctly with other frameworks A2A servers
remote_agent = A2aRemoteAgent(
    url="http://localhost:8000",
    name="python_coder",
)