DocsCookbook: Swarm Orchestration
Cookbook

Scaling to 1,000 Agents

Discover how to use Modexia's Gateway Nanopayments to securely fund and orchestrate a massive swarm of AI agents without distributing your API keys or funding hundreds of separate wallets.

The Multi-Agent Problem

Imagine deploying a swarm of 200 distributed worker agents that need to autonomously purchase API data. Giving all 200 agents your Modexia API key (which grants access to your entire SCA wallet) is a massive security risk. Conversely, creating and funding 200 separate agent wallets with $5 each is highly inefficient, locking up $1,000 in unused liquidity.

The "Swarm Orchestrator" Architecture

The solution is to use a Swarm Orchestrator (or Master Agent) combined with Modexia's off-chain EIP-3009 signatures.

1. The Orchestrator Deposits Liquidity

Only the Swarm Orchestrator holds the Modexia API key. The Orchestrator calls client.nanopay_deposit(50.0) to pool $50 into the Modexia Gateway.

2. Worker Hits a Paywall

A worker agent requests data and hits an HTTP 402 Payment Required response. The worker does not pay it. Instead, it sends an intent to the Orchestrator: "I need to pay 0.05 USDC to address 0xABC."

3. Zero-Gas Signature Dispensing

The Orchestrator, using its Gateway balance, pings the Modexia backend to generate an off-chain EIP-3009 signature for 0.05 USDC. It then sends this raw cryptographic signature back to the worker.

4. Worker Execution

The worker attaches the signature to its HTTP request and successfully retrieves the premium data. The worker never needed a wallet, never paid gas, and never had access to your API keys.


Implementation Concept (Pseudocode)

Orchestrator Agent (Holds API Key)

python
from modexia import ModexiaClient

client = ModexiaClient(api_key="mx_live_...")
client.nanopay_deposit(50.0) # Fund the gateway

def handle_worker_request(worker_id, amount_wei, recipient_address):
    # 1. Evaluate if worker is allowed to spend
    if not is_authorized(worker_id, amount_wei):
        return {"error": "Budget exceeded"}
        
    # 2. Generate EIP-3009 signature using Gateway balance
    # (Using the internal endpoint to generate a signature without fetching a URL)
    res = client._request("POST", "/api/v2/nanopay/sign", json={
        "to": recipient_address,
        "value": str(amount_wei)
    })
    
    # 3. Dispense signature to worker
    return {"signature": res['data']['signature']}

Worker Agent (No API Key)

python
import requests

def fetch_data():
    # 1. Hit the paywall
    resp = requests.get("https://api.premium-data.com/feed")
    if resp.status_code == 402:
        pay_info = resp.json()
        
        # 2. Ask Orchestrator for a signature
        sig_data = request_signature_from_orchestrator(
            amount_wei=pay_info['amount'], 
            recipient_address=pay_info['payTo']
        )
        
        # 3. Retry with signature
        headers = {"PAYMENT-SIGNATURE": sig_data['signature']}
        return requests.get("https://api.premium-data.com/feed", headers=headers)
        
    return resp

Why this is enterprise-grade

By centralizing liquidity in the Orchestrator, you achieve Zero Trust (workers have no wallet access), Shared Liquidity (no fragmented balances), and Zero Gas Distribution (signatures cost nothing to send to workers).