207 lines
6.1 KiB
Markdown
207 lines
6.1 KiB
Markdown
# A2A Swarm Agent Guide
|
|
|
|
Run a multi-agent LLM swarm where agents discover each other via the DHT and communicate using the A2A protocol. No central server required beyond a bootstrap node.
|
|
|
|
## Architecture
|
|
|
|
```
|
|
DHT Network
|
|
┌────────────┐
|
|
┌─────────────│ Topic Key │─────────────┐
|
|
│ └────────────┘ │
|
|
│ dhtStore() │ dhtFindValue() │
|
|
│ │ │
|
|
┌────▼────┐ ┌─────▼─────┐ ┌─────▼───┐
|
|
│ Agent A │◄──────►│ Bootstrap │◄──────►│ Agent B │
|
|
│ llama3 │ A2A │ (seed) │ A2A │ mistral │
|
|
└─────────┘ msgs └───────────┘ msgs └─────────┘
|
|
port 9001 port 9000 port 9002
|
|
```
|
|
|
|
**How it works:**
|
|
|
|
1. A bootstrap agent starts and prints its DHT node ID
|
|
2. Other agents join the swarm by connecting to the bootstrap node
|
|
3. Each agent advertises itself on the DHT under a topic key (`blake2b("a2a-topic:" + topic)`)
|
|
4. Agents discover each other by querying the DHT for that topic key
|
|
5. Agents exchange A2A chat messages over the P2P network
|
|
|
|
## Prerequisites
|
|
|
|
- [Zig](https://ziglang.org/download/) (0.15+)
|
|
- [Ollama](https://ollama.ai/) running on each machine (or a shared endpoint)
|
|
- A model pulled: `ollama pull llama3.2`
|
|
|
|
## Setup
|
|
|
|
### 1. Build on each server
|
|
|
|
```bash
|
|
zig build
|
|
```
|
|
|
|
Or cross-compile for Linux and copy the binary:
|
|
|
|
```bash
|
|
zig build -Doptimize=ReleaseFast -Dtarget=x86_64-linux
|
|
scp zig-out/bin/swarm-agent user@server:/usr/local/bin/
|
|
```
|
|
|
|
### 2. Install Ollama on each server
|
|
|
|
```bash
|
|
curl -fsSL https://ollama.ai/install.sh | sh
|
|
ollama pull llama3.2
|
|
```
|
|
|
|
### 3. Ensure the P2P port (default 9000) is open
|
|
|
|
```bash
|
|
# Example: UFW
|
|
sudo ufw allow 9000/udp
|
|
sudo ufw allow 9000/tcp
|
|
```
|
|
|
|
## Deployment
|
|
|
|
### Server A — Bootstrap node (203.0.113.10)
|
|
|
|
```bash
|
|
swarm-agent \
|
|
--name bootstrap \
|
|
--port 9000 \
|
|
--public-ip 203.0.113.10 \
|
|
--model llama3.2
|
|
```
|
|
|
|
Output:
|
|
```
|
|
=== A2A Swarm Agent ===
|
|
Name: bootstrap
|
|
Port: 9000
|
|
Model: llama3.2
|
|
Topic: a2a-swarm
|
|
DHT ID: a1b2c3d4e5f6... <-- copy this value
|
|
|
|
Other agents can join with:
|
|
--bootstrap 203.0.113.10:9000 --dht-id a1b2c3d4e5f6...
|
|
```
|
|
|
|
Copy the 64-character DHT ID — every other server needs it.
|
|
|
|
### Server B — Agent (198.51.100.20)
|
|
|
|
```bash
|
|
swarm-agent \
|
|
--name server-b \
|
|
--port 9000 \
|
|
--public-ip 198.51.100.20 \
|
|
--model mistral \
|
|
--bootstrap 203.0.113.10:9000 \
|
|
--dht-id <bootstrap_dht_id>
|
|
```
|
|
|
|
### Server C — Agent (192.0.2.30)
|
|
|
|
```bash
|
|
swarm-agent \
|
|
--name server-c \
|
|
--port 9000 \
|
|
--public-ip 192.0.2.30 \
|
|
--model llama3.2 \
|
|
--bootstrap 203.0.113.10:9000 \
|
|
--dht-id <bootstrap_dht_id>
|
|
```
|
|
|
|
### Query from any machine
|
|
|
|
Send a one-shot question to the first discovered agent and exit:
|
|
|
|
```bash
|
|
swarm-agent \
|
|
--name client \
|
|
--port 9003 \
|
|
--public-ip 10.0.0.50 \
|
|
--bootstrap 203.0.113.10:9000 \
|
|
--dht-id <bootstrap_dht_id> \
|
|
--ask "What is quantum computing?"
|
|
```
|
|
|
|
## CLI Reference
|
|
|
|
```
|
|
swarm-agent [options]
|
|
|
|
Options:
|
|
--name <name> Agent name (default: agent)
|
|
--port <port> P2P listen port (default: 9000)
|
|
--model <model> LLM model name (default: llama3.2)
|
|
--ollama-url <url> Ollama endpoint (default: http://localhost:11434)
|
|
--bootstrap <ip:port> Bootstrap node address
|
|
--dht-id <hex> Bootstrap node's DHT node ID (64 hex chars)
|
|
--public-ip <ip> Explicit public IP for NAT environments
|
|
--topic <topic> Agent topic to advertise (default: a2a-swarm)
|
|
--ask <message> Send message to first discovered agent and exit
|
|
--help, -h Show this help
|
|
```
|
|
|
|
## Agent Discovery via DHT
|
|
|
|
Each agent advertises a JSON payload on the DHT:
|
|
|
|
```json
|
|
{
|
|
"name": "alice",
|
|
"port": 9001,
|
|
"ip": "203.0.113.10",
|
|
"model": "llama3.2",
|
|
"capability": "chat"
|
|
}
|
|
```
|
|
|
|
The DHT key is derived from the topic name:
|
|
|
|
```
|
|
key = blake2b("a2a-topic:" + topic_name) // 32 bytes
|
|
```
|
|
|
|
All agents using the same `--topic` value will share the same DHT key. The DHT entry has a TTL of 300 seconds and is re-advertised every 60 seconds.
|
|
|
|
## A2A Message Format
|
|
|
|
Messages use the existing A2A wire protocol (`examples/a2a/protocol.zig`):
|
|
|
|
- **Request** (type `0x01`): JSON with `request_id`, `model`, `messages[]`, `temperature`, `capability`
|
|
- **Response** (type `0x02`): JSON with `request_id`, `success`, `content`, `model`, `agent_id`
|
|
|
|
Messages are sent as P2P messages with protocol ID `0x42`.
|
|
|
|
## NAT Traversal
|
|
|
|
The swarm agent supports several address discovery methods (in priority order):
|
|
|
|
1. `--public-ip` flag (explicit, recommended for cloud servers)
|
|
2. IP Reflection / Bundled STUN (learned from DHT peers automatically)
|
|
3. Localhost fallback (for local testing)
|
|
|
|
For servers behind NAT, always use `--public-ip` with the server's public IP and ensure the listen port is forwarded.
|
|
|
|
## Scaling to N Agents
|
|
|
|
- All agents bootstrap to the same node (or any existing swarm member)
|
|
- Each agent advertises on the same topic key
|
|
- The DHT propagates entries across the network
|
|
- DHT entries are refreshed every 60 seconds
|
|
- Current DHT stores a single value per key (last-writer-wins); for multi-agent discovery, use distinct topics or connect via the bootstrap peer list
|
|
|
|
## Troubleshooting
|
|
|
|
| Symptom | Fix |
|
|
|---------|-----|
|
|
| "No agents discovered" | Ensure `--dht-id` matches the bootstrap node's printed DHT ID |
|
|
| "Failed to connect to bootstrap" | Check firewall/port forwarding; ensure bootstrap is running |
|
|
| "LLM call failed" | Verify Ollama is running: `curl http://localhost:11434/api/tags` |
|
|
| "Timeout" waiting for response | The responding agent may be slow (large model) — increase timeout or use a smaller model |
|
|
| Agent not found via DHT | Wait a few seconds for DHT propagation; try `--ask` again |
|
|
| Wrong IP advertised | Use `--public-ip` to set the correct reachable address |
|