This project simulates a decentralized peer-to-peer (P2P) energy grid using a multi-agent system. It features 10 Household Agents and 1 Utility Agent that can buy and sell energy using the Beckn Protocol. The entire system is built with modern, asynchronous Python technologies and runs in Docker containers.
- Docker and Docker Compose
- Python 3.12 (for development)
# Build and start all services
docker-compose up -d
# View logs
docker-compose logs -f
# Test the setup
python test_docker_setup.pyThe simulation consists of 13 services running in Docker containers:
- Mock Beckn Gateway (
gateway:9000) - Protocol gateway for agent discovery and message routing - Utility Agent (
utility_agent:8002) - Grid operator with unlimited capacity - 10 Household Agents (
household_agent_1throughhousehold_agent_10) - Individual energy prosumers - Reporter Service - Generates periodic reports every 2 minutes
| Agent | Port | Initial SoC | Role | Energy Model |
|---|---|---|---|---|
| household-agent-01 | 8001 | 15% | Buyer | Consumes -0.03 kWh/cycle |
| household-agent-02 | 8003 | 95% | Seller | Generates +0.02 kWh/cycle |
| household-agent-03 | 8005 | 15% | Buyer | Consumes -0.03 kWh/cycle |
| household-agent-04 | 8007 | 95% | Seller | Generates +0.02 kWh/cycle |
| household-agent-05 | 8009 | 15% | Buyer | Consumes -0.03 kWh/cycle |
| household-agent-06 | 8011 | 95% | Seller | Generates +0.02 kWh/cycle |
| household-agent-07 | 8013 | 15% | Buyer | Consumes -0.03 kWh/cycle |
| household-agent-08 | 8015 | 95% | Seller | Generates +0.02 kWh/cycle |
| household-agent-09 | 8017 | 15% | Buyer | Consumes -0.03 kWh/cycle |
| household-agent-10 | 8019 | 95% | Seller | Generates +0.02 kWh/cycle |
| utility-agent-01 | 8002 | 999,999 kWh | Grid Operator | Unlimited capacity |
The system implements the complete 5-step Beckn protocol flow:
- Search - Buyer initiates energy search
- Select - Buyer selects cheapest offer
- Init - Transaction initialization
- Confirm - Final confirmation
- Status - Transaction completion
- Price Competition: Household agents ($0.15/kWh) vs Utility agent ($0.25/kWh)
- Partial Participation: 30% chance agents are "offline" during searches
- Dynamic Energy Flow: Buyers consume, sellers generate energy over time
- Automatic Selection:
min(offers, key=lambda o: o.price_per_kwh)ensures cheapest wins
- Utility Agent automatically collects data from all household agents every 5 minutes
- Agent Discovery via Beckn Gateway registry
- SoC Data Collection using A2A protocol
- Data Storage accessible via
/admin/collected-dataendpoint
- Periodic Reports: Generated every 2 minutes
- Agent States: Complete energy levels and transaction history
- Market Analysis: Supply-demand balance and energy flow patterns
- Python 3.12: Core programming language
- FastAPI: High-performance ASGI web framework for API endpoints
- LangGraph: Stateful multi-agent orchestration and decision-making
- Pydantic: Data modeling and validation
- Docker: Containerized deployment
- Beckn Protocol: Decentralized transaction protocol
- A2A Protocol: Agent-to-agent communication
gemini-p2p/
βββ src/
β βββ agents/ # Agent logic and LangGraph workflows
β β βββ household/ # Household agent implementation
β β βββ utility/ # Utility agent implementation
β β βββ agent_graph.py # Shared LangGraph nodes and state
β βββ protocols/ # Beckn protocol implementation
β β βββ beckn/
β β βββ mock_gateway.py
β βββ reporting/ # Data collection and reporting
β β βββ reporter.py
β βββ shared/ # Shared models and configuration
β βββ config.py
β βββ models.py
βββ static/ # Agent configuration files
βββ reports/ # Generated simulation reports
βββ docker-compose.yml # Multi-service orchestration
βββ Dockerfile # Container configuration
βββ pyproject.toml # Project metadata and dependencies
# Run the test script
python test_docker_setup.py
# Check individual services
curl http://localhost:9000/registry
curl http://localhost:8001/profile
curl http://localhost:8002/profile# View logs from specific services
docker-compose logs -f household_agent_1
docker-compose logs -f utility_agent
docker-compose logs -f gateway
# Check reports
ls -la reports/
cat reports/report_*.json | jq .GET /registry- List all registered agentsPOST /register- Register an agentPOST /search- Broadcast search request
Each agent provides:
GET /profile- Get current agent statePOST /a2a- Handle A2A protocol tasksPOST /{action}- Handle Beckn protocol actions
POST /admin/request-data- Trigger data collectionGET /admin/collected-data- View collected data
# Install dependencies
uv venv
source .venv/bin/activate
uv pip install -e .
# Run individual services
uvicorn src.protocols.beckn.mock_gateway:app --port 9000
uvicorn src.agents.household.main:app --port 8001
uvicorn src.agents.utility.main:app --port 8002Edit environment variables in docker-compose.yml:
environment:
- INITIAL_SOC=50 # Change initial state of charge
- AGENT_ID=custom-agent-id- β Complete Beckn Protocol Implementation
- β Dynamic Energy Market with Price Competition
- β Automatic Data Collection via A2A Protocol
- β Real-time Reporting and Monitoring
- β Scalable Multi-Agent Architecture
- β Docker-based Deployment
- β Comprehensive Documentation
- DOCKER_README.md - Docker setup and deployment
- ENDPOINTS.md - API endpoint documentation
- WORKFLOW.md - Transaction workflow examples
- COMPREHENSIVE_GUIDE.md - Complete system overview
- 10 Household Agents + 1 Utility Agent actively trading
- Full Beckn Protocol implementation (search β select β init β confirm β status)
- Price Competition with automatic cheapest offer selection
- Dynamic Energy Model driving continuous transactions
- Automatic Data Collection every 5 minutes
- Agent Discovery via Beckn Gateway registry
- A2A Protocol for direct agent communication
- Data Storage and reporting system
The system provides a rock-solid foundation for building advanced P2P energy grid applications! π