A peer-to-peer network of AI agents that demonstrate libp2p's universal connectivity through seamless discovery, NAT traversal, and collaborative intelligence. Agents autonomously discover each other, advertise capabilities, and collaborate on complex tasks that require distributed problem-solving.
- Decentralized Discovery: Agents discover each other via Kademlia DHT
- Secure Communication: Noise encryption and Yamux multiplexing
- Pub/Sub Messaging: Gossipsub for topic-based communication
- AI Task Execution: Execute tasks using Anthropic Claude API
- Multi-Agent Collaboration: Decompose complex tasks and distribute across agents
- Universal Connectivity: NAT traversal and peer-to-peer networking
- Python 3.10 or higher
- libp2p 0.4.0+ (installed from local repository)
- Anthropic API key (for AI task execution)
- Clone and setup the repository:
git clone https://github.com/bomanaps/SwarmMind-AI.git
cd SwarmMind-AI- Create a virtual environment:
python3 -m venv venv
source venv/bin/activate - Install dependencies:
pip install -r requirements.txt- Install libp2p from local repository:
# Make sure you have py-libp2p cloned at ../py-libp2p or update the path
pip install -e /path/to/py-libp2p- Set up Anthropic API key:
export ANTHROPIC_API_KEY="sk-ant-api03-..."The easiest way to get started is using the interactive demo helper script:
./run_demo.shThis script will:
- Check your environment setup (virtual environment, API key)
- Provide you with pre-configured demo scenarios
- Show you the exact commands to run in each terminal window
Available demo scenarios:
- Quick Test - Basic connectivity (2 agents)
- Discovery Test - DHT discovery (3 agents)
- AI Task Execution - Single task execution (2 agents with API key)
- Full Demo - Multi-agent collaboration (4+ agents with API key)
- Manual - Run commands manually (for advanced users)
Note: Make sure you have exported your ANTHROPIC_API_KEY before running AI task demos (scenarios 3 and 4). The script will check for this and warn you if it's missing.
Terminal 1 - Start an executor agent:
python swarm_demo.py \
--agent-id executor1 \
--port 8000 \
--capabilities problem_solving code_generationThis will start an agent that can execute AI tasks. Note the peer ID from the output - you'll need it for other agents to connect.
Terminal 2 - Start another executor agent:
python swarm_demo.py \
--agent-id executor2 \
--port 8001 \
--capabilities data_analysis research \
--bootstrap /ip4/127.0.0.1/tcp/8000/p2p/<PEER_ID_FROM_TERMINAL1>Replace <PEER_ID_FROM_TERMINAL1> with the actual peer ID shown in Terminal 1.
Terminal 3 - Search for agents:
python swarm_demo.py \
--agent-id searcher \
--port 8002 \
--bootstrap /ip4/127.0.0.1/tcp/8000/p2p/<PEER_ID_FROM_TERMINAL1> \
--search-capability problem_solvingThis agent will search for and discover agents with the problem_solving capability.
Start an agent that can execute AI tasks:
ANTHROPIC_API_KEY="sk-ant-api03-..." python swarm_demo.py \
--agent-id executor \
--port 8000 \
--capabilities problem_solving code_generation data_analysisOutput will show:
- Peer ID (needed for other agents to connect)
- Listening addresses
- DHT routing table size
- Agent is ready to receive tasks
Find agents with specific capabilities:
python swarm_demo.py \
--agent-id searcher \
--port 8001 \
--bootstrap /ip4/127.0.0.1/tcp/8000/p2p/<PEER_ID> \
--search-capability problem_solvingThis will:
- Connect to the bootstrap node
- Search the DHT for agents with
problem_solvingcapability - Display found agents
- Broadcast a test task request via Gossipsub
Send a task request to an executor agent:
python swarm_demo.py \
--agent-id requester \
--port 8002 \
--bootstrap /ip4/127.0.0.1/tcp/8000/p2p/<PEER_ID> \
--send-task problem_solving "Solve: What is 2+2?"This will:
- Connect to the bootstrap node
- Find an agent with
problem_solvingcapability - Send the task request
- Wait for the result
Distribute a complex task across multiple agents:
python swarm_demo.py \
--agent-id coordinator \
--port 8003 \
--bootstrap /ip4/127.0.0.1/tcp/8000/p2p/<PEER_ID> \
--distribute-task problem_solving "Solve: What is 2+2? and What is 5*3?" \
--decomposition-strategy parallelThis will:
- Decompose the task into sub-tasks
- Find multiple agents with required capabilities
- Distribute sub-tasks to different agents
- Aggregate results from all agents
Test basic peer-to-peer connectivity:
# Terminal 1
python swarm_demo.py --agent-id agent1 --port 8000
# Terminal 2 (after noting peer ID from Terminal 1)
python swarm_demo.py \
--agent-id agent2 \
--port 8001 \
--connect /ip4/127.0.0.1/tcp/8000/p2p/<PEER_ID_FROM_TERMINAL1>--agent-id: Unique identifier for the agent
--port PORT: Port to listen on (default: auto-find free port)--dht-mode {client,server}: DHT mode (default: server)--capabilities CAP1 CAP2 ...: Agent capabilities--bootstrap ADDR [ADDR ...]: Bootstrap node multiaddresses--api-key KEY: Anthropic API key (or use ANTHROPIC_API_KEY env var)--max-concurrent-tasks N: Max concurrent tasks (default: 3)--connect ADDR: Connect to a specific peer (basic connectivity test)--search-capability CAP: Search for agents with capability--send-task TYPE "DESCRIPTION": Send a single task--distribute-task TYPE "DESCRIPTION": Distribute a complex task--decomposition-strategy {parallel,sequential,hierarchical,auto}: Task decomposition strategy-v, --verbose: Enable verbose logging
Available capability types:
problem_solving: General problem-solving and reasoningcode_generation: Generate code solutionscode_review: Review and improve codedata_analysis: Analyze data and generate insightsresearch: Research and information gatheringtranslation: Language translationsummarization: Text summarization
┌─────────────────────────────────────────────────────────┐
│ SwarmMind-AI Agent │
├─────────────────────────────────────────────────────────┤
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Identity │ │ Transport │ │ Protocol │ │
│ │ (Ed25519) │ │ (TCP/QUIC) │ │ Handler │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│ │
│ ┌──────────────────────────────────────────────────┐ │
│ │ libp2p Host (py-libp2p) │ │
│ │ • Noise Encryption • Yamux Multiplexing │ │
│ │ • Universal Connectivity (NAT Traversal) │ │
│ └──────────────────────────────────────────────────┘ │
│ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Kademlia │ │ Gossipsub │ │ Custom │ │
│ │ DHT │ │ Pub/Sub │ │ Protocols │ │
│ │ (Discovery) │ │ (Broadcast) │ │ (Task Exec) │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│ │
│ ┌──────────────────────────────────────────────────┐ │
│ │ AI Agent Core │ │
│ │ • Capability Registry • Task Queue │ │
│ │ • AI Integration • Result Aggregation │ │
│ └──────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────┘
Here's a complete demo scenario with multiple agents:
ANTHROPIC_API_KEY="sk-ant-api03-..." python swarm_demo.py \
--agent-id executor1 \
--port 8000 \
--capabilities problem_solvingNote the Peer ID from output, e.g., 12D3KooW...
ANTHROPIC_API_KEY="sk-ant-api03-..." python swarm_demo.py \
--agent-id executor2 \
--port 8001 \
--capabilities code_generation data_analysis \
--bootstrap /ip4/127.0.0.1/tcp/8000/p2p/12D3KooW...ANTHROPIC_API_KEY="sk-ant-api03-..." python swarm_demo.py \
--agent-id executor3 \
--port 8002 \
--capabilities problem_solving research \
--bootstrap /ip4/127.0.0.1/tcp/8000/p2p/12D3KooW...python swarm_demo.py \
--agent-id searcher \
--port 8003 \
--bootstrap /ip4/127.0.0.1/tcp/8000/p2p/12D3KooW... \
--search-capability problem_solvingYou should see executor1 and executor3 discovered.
python swarm_demo.py \
--agent-id requester \
--port 8004 \
--bootstrap /ip4/127.0.0.1/tcp/8000/p2p/12D3KooW... \
--send-task problem_solving "Solve: What is 2+2?"The task will be executed by one of the agents with problem_solving capability.
python swarm_demo.py \
--agent-id coordinator \
--port 8005 \
--bootstrap /ip4/127.0.0.1/tcp/8000/p2p/12D3KooW... \
--distribute-task problem_solving "Solve: What is 2+2? and What is 5*3? and What is 10/2?" \
--decomposition-strategy parallelThis will decompose the task and distribute it across multiple agents.
- Check if port is already in use
- Verify libp2p is installed correctly
- Check Python version (3.10+ required)
- Ensure bootstrap node is running and accessible
- Check network connectivity
- Wait a few seconds for DHT to propagate
- Try using
--search-capabilityto trigger discovery
- Verify ANTHROPIC_API_KEY is set correctly
- Check API key has sufficient credits - Important: Anthropic API requires credits to be purchased/added to your account. If you see "Your credit balance is too low to access the Anthropic API", you need to add credits to your Anthropic account at https://console.anthropic.com/
- Ensure executor agent has required capability
- Check executor agent logs for errors
Note about API Credits: The SwarmMind-AI system architecture is fully functional end-to-end. Tasks are properly accepted, queued, and distributed. However, to execute actual AI tasks, your Anthropic API key must have credits available. If credits are exhausted, you'll see HTTP 400 errors in the executor logs. This is a billing/account issue with Anthropic, not a code issue - the distributed architecture and agent communication work correctly regardless of API availability.
- Verify peer ID is correct in multiaddress
- Check firewall settings
- For NAT traversal, ensure UPnP is enabled
- Try using
--verboseflag for detailed logs
SwarmMind-AI/
├── src/
│ ├── __init__.py
│ ├── agent.py # Main agent class
│ ├── identity.py # Agent identity management
│ ├── transport.py # Transport layer (TCP/QUIC)
│ ├── discovery.py # DHT discovery
│ ├── capability_registry.py # Capability management
│ ├── broadcast.py # Gossipsub pub/sub
│ ├── messaging.py # Message serialization
│ ├── task_protocol.py # Custom task protocol
│ ├── ai_provider.py # AI provider abstraction
│ ├── task_queue.py # Task queue management
│ ├── task_executor.py # Task execution engine
│ ├── task_decomposition.py # Task decomposition
│ ├── result_aggregator.py # Result aggregation
│ └── collaboration.py # Multi-agent collaboration
├── swarm_demo.py # Unified demo script
├── run_demo.sh # Interactive demo helper script
├── requirements.txt # Python dependencies
└── README.md # This file
Use the unified demo script with different modes:
# Basic connectivity test
python swarm_demo.py --agent-id test1 --port 8000
python swarm_demo.py --agent-id test2 --port 8001 \
--connect /ip4/127.0.0.1/tcp/8000/p2p/<PEER_ID>
# Discovery test
python swarm_demo.py --agent-id test1 --port 8000 --capabilities test
python swarm_demo.py --agent-id test2 --port 8001 \
--bootstrap /ip4/127.0.0.1/tcp/8000/p2p/<PEER_ID> \
--search-capability testEnable verbose logging for debugging:
python swarm_demo.py --agent-id test --port 8000 -vThis project is open source. Please check the repository for license information.
Contributions are welcome! This project was built for the libp2p Universal Connectivity Hackathon. For contributions:
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
- Built for the libp2p Universal Connectivity Hackathon
- Uses py-libp2p for peer-to-peer networking
- AI capabilities powered by Anthropic Claude