> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hexr.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Agent-to-Agent Communication

> Enable AI agents to discover, delegate tasks, and collaborate using the A2A protocol.

## Overview

Hexr implements Google's [Agent-to-Agent (A2A) protocol](https://google.github.io/A2A/) with SPIFFE identity extensions, enabling agents to:

* **Discover** other agents via Agent Cards
* **Delegate** tasks to specialized agents
* **Fan out** work to multiple agents in parallel
* **Pipeline** sequential processing across agents

***

## Enable A2A

Add `a2a=True` to your agent decorator:

```python theme={null}
from hexr import hexr_agent
from hexr.a2a import A2AClient

@hexr_agent(name="coordinator", tenant="acme-corp", a2a=True)
def main():
    a2a = A2AClient()
    
    # Discover agents in the same namespace
    agents = a2a.discover()
    for agent in agents:
        print(f"Found: {agent.name} - {agent.description}")
    
    # Send a task to another agent
    task = a2a.send(
        agent="research-analyst",
        message="Research the latest AI agent frameworks",
    )
    
    # Get the result
    result = a2a.get_task(task.id)
    print(result.output)
```

***

## Fan-Out Pattern

Send tasks to multiple agents in parallel:

```python theme={null}
@hexr_agent(name="coordinator", tenant="acme-corp", a2a=True)
def main():
    a2a = A2AClient()
    
    topics = ["quantum computing", "robotics", "biotech"]
    tasks = []
    
    for topic in topics:
        task = a2a.send(
            agent="research-analyst",
            message=f"Research {topic} trends for 2026",
        )
        tasks.append(task)
    
    # Collect results
    results = [a2a.get_task(t.id) for t in tasks]
    
    # Synthesize
    from hexr import hexr_llm
    synthesis = hexr_llm(
        provider="openai",
        model="gpt-4o",
        prompt=f"Synthesize these research results: {results}",
    )
    print(synthesis)
```

***

## Pipeline Pattern

Chain agents sequentially:

```python theme={null}
@hexr_agent(name="pipeline-coordinator", tenant="acme-corp", a2a=True)
def main():
    a2a = A2AClient()
    
    # Step 1: Research
    research = a2a.send(agent="researcher", message="AI agent trends 2026")
    research_result = a2a.get_task(research.id)
    
    # Step 2: Write (using research output)
    article = a2a.send(
        agent="writer",
        message=f"Write a blog post about: {research_result.output}",
    )
    article_result = a2a.get_task(article.id)
    
    # Step 3: Edit  
    edited = a2a.send(
        agent="editor",
        message=f"Edit this article: {article_result.output}",
    )
    final = a2a.get_task(edited.id)
    print(final.output)
```

***

## Security

All A2A communication is:

* **mTLS encrypted** — using SPIFFE SVIDs
* **Identity verified** — each agent's SPIFFE ID is validated
* **OPA authorized** — policies control which agents can communicate
* **Audited** — every message is traced via OpenTelemetry

<Steps>
  <Step title="Agent A sends task via mTLS">
    Agent A (coordinator) sends a task to Agent B (researcher) through the Envoy proxy, authenticated with its SPIFFE identity.
  </Step>

  <Step title="OPA authorizes the request">
    Envoy checks with OPA: "Can `coordinator` communicate with `researcher`?" OPA returns ALLOW.
  </Step>

  <Step title="Task delivered over mTLS">
    Envoy delivers the task to Agent B over mTLS. Agent B processes it and returns the result.
  </Step>
</Steps>

```
Agent A (coordinator) → Envoy (mTLS) → OPA (authorize) → Agent B (researcher) → Result → A
```
