Skip to content

Add AI agent communication example using GossipSub#45

Open
Harshit-Mishra2212 wants to merge 1 commit into
seetadev:mainfrom
Harshit-Mishra2212:add-ai-agent-example
Open

Add AI agent communication example using GossipSub#45
Harshit-Mishra2212 wants to merge 1 commit into
seetadev:mainfrom
Harshit-Mishra2212:add-ai-agent-example

Conversation

@Harshit-Mishra2212

@Harshit-Mishra2212 Harshit-Mishra2212 commented Jun 2, 2026

Copy link
Copy Markdown

What this PR does

Looking through the examples folder, there is no example showing how AI agents can use py-libp2p to communicate with each other. All existing examples cover the networking primitives (chat, echo, ping) but none ofthem connect this to an actual AI agent use case. Given issues like #20 this felt like a useful gap to fill.

The example implements a dispatcher-worker pattern over GossipSub. The dispatcher broadcasts tasks on the agent/tasks/v1 topic. Worker agents subscribe to that topic, process the task, and publish their response back on agent/responses/v1. Using two separate topics keeps the message flow clean - the dispatcher only sees responses, workers only see tasks. Multiple workers can connect to the same dispatcher simultaneously and each one responds independently. No central server is involved at any point.

The task processing is intentionally kept simple to keep the focus on the p2p communication layer. In a real deployment this is where we would plug in an LLM or run local inference. The architecture stays the same regardless of what runs inside process_task.

Changes

examples/ai_agent/agent.py

  • Dispatcher publishes tasks over GossipSub and listens for worker responses
  • Worker subscribes to task topic, processes task, publishes response back
  • Two separate topics so dispatcher and worker don't receive their own messages
  • Task processing runs in a thread so it does not block the event loop
  • Multiple workers can connect to the same dispatcher and respond in parallel

How to verify

pip install libp2p

Terminal 1

python examples/ai_agent/agent.py

Terminal 2 (paste address printed by Terminal 1)

python examples/ai_agent/agent.py -d (address) --worker

Type a task in Terminal 1. The worker in Terminal 2 receives it, processes it, and the response appears back in Terminal 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant