A hackathon-ready MVP that routes prompts to optimal LLM providers with USDC payments on Arc L1
An AI agent that:
- π§ Classifies prompts and determines quality requirements
- π° Selects the cheapest provider meeting policy constraints
- βοΈ Pays per request in USDC on Arc (EVM L1) with onchain receipts
- β Verifies results and escalates to premium providers when needed
- π Tracks spending with enforced guardrails
- Architecture
- Features
- Quick Start
- System Components
- API Documentation
- Demo Flow
- Configuration
- Security & Guardrails
- Threat Model
- Testing
- Production Deployment
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β USER / FRONTEND β
β (Next.js UI) β
ββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββ
β HTTP POST /api/route
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β ROUTER SERVICE (Agent) β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Classifier ββ β Selector ββ β Payment Svc β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β β β β
β βΌ βΌ βΌ β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Task Type β β Cost Optimizeβ β USDC Transferβ β
β β Token Est β β Policy Check β β on Arc L1 β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
ββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββ
β USDC Payment (onchain)
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β ARC NETWORK (EVM L1) β
β USDC ERC20 Transfer β Provider Address β
β TX Hash recorded as verifiable receipt β
ββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββ
β
ββββββββββββββββ΄βββββββββββββββ
βΌ βΌ
ββββββββββββββββββββββββ ββββββββββββββββββββββββ
β Gemini Provider β β Claude Provider β
β (Wrapper Service) β β (Wrapper Service) β
β β β β
β 1. Verify Payment β β 1. Verify Payment β
β 2. Check TX Hash β β 2. Check TX Hash β
β 3. Call Upstream β β 3. Call Upstream β
β 4. Return Result β β 4. Return Result β
ββββββββββββββββββββββββ ββββββββββββββββββββββββ
β β
βΌ βΌ
ββββββββββββββββββββββββ ββββββββββββββββββββββββ
β Gemini API β β Anthropic API β
β (gemini-1.5-flash) β β (claude-3-haiku) β
ββββββββββββββββββββββββ ββββββββββββββββββββββββ
β Prompt Classification
- Categorizes into: trivial_math, code, reasoning, writing, summarization, short_qa, other
- Estimates token requirements
- Derives quality tier needs (cheap/balanced/premium)
β Provider Selection
- Fetches real-time quotes from multiple providers
- Optimizes for cost while meeting constraints:
- Budget limits (per-request and daily)
- Latency requirements
- Quality tier preferences
- Provider allowlist
β USDC Payments on Arc
- Direct ERC20 transfers to provider addresses
- Generates unique payment nonces (replay protection)
- Returns verifiable transaction hashes
- Waits for onchain confirmation
β Payment Verification
- Providers verify payment before responding
- Checks: recipient, amount, tx hash, nonce
- Prevents replay attacks
β Progressive Escalation
- Verifies completion quality
- Automatically escalates to premium provider if initial attempt fails
- Only if within budget constraints
β Spending Guardrails
- Per-request spending cap (default: $0.02 USDC)
- Daily spending cap (default: $1.00 USDC)
- Provider allowlist enforcement
- Emergency stop mechanism
β Audit Logging
- Append-only JSONL logs
- Records: request_id, provider, payment tx, cost, verification results
- Full request traceability
- Node.js 18+
- npm or yarn
- (Optional) Arc testnet access for real blockchain transactions
# Clone or navigate to the project directory
cd AgenticCommerce
# Run setup script
chmod +x scripts/setup.sh
./scripts/setup.sh
# This will:
# - Install all dependencies
# - Build shared types
# - Create .env files from examples# Start everything at once
npm run dev
# Or use the convenience script
chmod +x scripts/start.sh
./scripts/start.shThis starts:
- Router Backend β
http://localhost:3000 - Gemini Provider β
http://localhost:4001 - Claude Provider β
http://localhost:4002 - Frontend UI β
http://localhost:5173
Open your browser to: http://localhost:5173
The main orchestration service.
Key Modules:
classifier.ts- Prompt analysis and task type detectionselector.ts- Provider selection algorithmpayment.ts- USDC payment handling via ethers.jsverifier.ts- Completion quality verificationspend-tracker.ts- Budget enforcement and statisticsprovider-client.ts- Communication with provider wrappers
Endpoints:
POST /api/route- Main routing endpointGET /api/stats- Usage statisticsGET /api/treasury- Treasury wallet infoGET /health- Health check
Simulate LLM provider services with payment verification.
Services:
gemini-provider.ts- Gemini Flash wrapper (cheap tier)claude-provider.ts- Claude Haiku wrapper (premium tier)
Endpoints (each provider):
GET /quote- Return pricing and capabilitiesPOST /complete- Process completion after payment verificationGET /health- Health check
Features:
- Payment verification via Arc blockchain
- Nonce-based replay protection
- Mock LLM responses for demo mode
- Real API integration ready (just add keys)
Next.js UI for interacting with the system.
Panels:
- Input Panel - Submit prompts with policy constraints
- Results Panel - Shows agent decision flow step-by-step:
- Classification
- Quotes received
- Provider selection + rationale
- Payment details with tx hash
- Completion result
- Verification outcome
- Escalation (if occurred)
- Stats Panel - Real-time usage metrics
TypeScript type definitions shared across all services.
Route a prompt to the optimal provider.
Request:
{
"prompt": "Calculate the square root of 144",
"policy": {
"max_cost_usdc": 0.02,
"max_latency_ms": 5000,
"quality_preference": "balanced",
"use_allowlist": true
}
}Response:
{
"request_id": "abc-123",
"classification": {
"task_type": "trivial_math",
"estimated_tokens": 25,
"requires_quality": "cheap"
},
"quotes_received": [
{
"provider_id": "gemini",
"model_name": "gemini-1.5-flash",
"price_per_1k_tokens": 0.00001,
"base_fee": 0.0001,
"est_latency_ms": 800,
"quality_tier": "cheap",
"arc_address": "0x742d..."
}
],
"selected_provider": {
"provider_id": "gemini",
"estimated_cost": 0.000125,
"rationale": "Selected gemini: Quality tier cheap matches..."
},
"payment": {
"amount_usdc": 0.000125,
"recipient_address": "0x742d...",
"tx_hash": "0xabc123...",
"block_number": 12345,
"payment_nonce": "uuid-v4"
},
"completion": "The square root of 144 is 12.",
"verification": {
"passed": true,
"score": 1.0,
"reason": "Contains numerical answer"
},
"total_cost_usdc": 0.000125,
"latency_ms": 1234
}Get usage statistics.
Response:
{
"total_requests": 42,
"total_spend_usdc": 0.0523,
"spend_by_provider": {
"gemini": 0.0321,
"claude": 0.0202
},
"average_cost_usdc": 0.001245,
"escalation_count": 3,
"requests_today": 15,
"spend_today": 0.0187
}Get treasury wallet information.
Response:
{
"address": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"balance_usdc": 5.23,
"daily_cap": 1.0,
"per_request_cap": 0.02
}-
User Input:
Prompt: "What is 2 + 2?" Max Cost: $0.02 USDC Quality: Balanced -
Classification:
- Task Type:
trivial_math - Estimated Tokens: 15
- Required Quality:
cheap
- Task Type:
-
Provider Quotes:
- Gemini: $0.00001/1K tokens, ~800ms, cheap tier
- Claude: $0.00025/1K tokens, ~1200ms, premium tier
-
Selection Decision:
- Chosen: Gemini
- Rationale: "Cheapest provider meeting quality requirements"
- Estimated Cost: $0.00015 USDC
-
Payment:
- Transfer $0.00015 USDC to Gemini's Arc address
- TX Hash:
0xabc123...(viewable on Arc explorer)
-
Completion:
- Gemini receives payment verification
- Returns: "The answer is 4."
-
Verification:
- β PASSED - Contains numerical answer
- No escalation needed
-
Result:
- Total Cost: $0.00015 USDC
- Latency: 1.2 seconds
- Receipt: onchain tx hash
# Arc Network
ARC_RPC_URL=https://rpc.arc.xyz
ARC_USDC_ADDRESS=0x1234... # USDC token contract
ARC_CHAIN_ID=1234
# Treasury (NEVER use test keys with real funds!)
TREASURY_PRIVATE_KEY=0xac09...
# Provider Addresses
PROVIDER_GEMINI_ADDRESS=0x742d...
PROVIDER_CLAUDE_ADDRESS=0x8626...
# Provider URLs
PROVIDER_GEMINI_URL=http://localhost:4001
PROVIDER_CLAUDE_URL=http://localhost:4002
# Spending Limits
DAILY_SPEND_CAP_USDC=1.0
PER_REQUEST_CAP_USDC=0.02
# System
PORT=3000
DEMO_MODE=true # Uses mock payments
EMERGENCY_STOP=false # Blocks all payments
# Allowlist
PROVIDER_ALLOWLIST=0x742d...,0x8626...# Ports
GEMINI_PORT=4001
CLAUDE_PORT=4002
# API Keys (optional - uses mocks if not provided)
GEMINI_API_KEY=your_key_here
ANTHROPIC_API_KEY=your_key_here
# Models
GEMINI_MODEL=gemini-1.5-flash
CLAUDE_MODEL=claude-3-haiku-20240307
# Demo Mode
DEMO_MODE=trueWhen DEMO_MODE=true:
- β Payments are simulated (mock tx hashes generated)
- β LLM responses are templated (no API keys needed)
- β Full routing logic still executes
- β Perfect for demos and testing
Set DEMO_MODE=false for real blockchain transactions.
-
Per-Request Cap
- Default: $0.02 USDC
- Prevents single expensive requests
-
Daily Spending Cap
- Default: $1.00 USDC
- Resets at midnight
- Blocks requests if exceeded
-
Provider Allowlist
- Only pre-approved addresses can receive funds
- Prevents malicious provider injection
-
Emergency Stop
- Set
EMERGENCY_STOP=trueto block all payments immediately - Does not require service restart
- Set
-
Nonce-Based Replay Protection
- Each payment has unique UUID nonce
- Providers track used nonces
- Prevents double-spending attacks
-
Onchain Verification
- Providers verify tx hash on Arc blockchain
- Checks: amount, recipient, block confirmation
- Rejects invalid/insufficient payments
-
Treasury Isolation
- Private key only held by router service
- Never exposed to frontend or logs
- Use hardware wallet for production
- Append-only logs in
router-backend/data/audit.jsonl - Contains: timestamp, request_id, provider, payment tx, cost, verification
- Immutable record for accounting
| Threat | Mitigation |
|---|---|
| Excessive Spending | Per-request and daily caps enforced |
| Replay Attacks | Unique nonces, providers track usage |
| Malicious Providers | Allowlist enforcement |
| Payment Fraud | Onchain verification of tx hash |
| Provider Collusion | Multiple provider options, cost optimization |
| Treasury Compromise | Demo keys only, production uses HSM/MPC |
| Data Tampering | Append-only audit logs |
- Private keys in
.env(use vault/HSM for prod) - No rate limiting on API endpoints
- No user authentication/authorization
- Limited error handling for network failures
- No retry logic for failed transactions
- Demo mode uses mock transactions
- Real mode needs Arc testnet USDC
- No MEV protection
- No gas price optimization
cd router-backend
npm test- β Classifier: prompt categorization
- β Selector: provider selection logic
- β Verifier: completion quality checks
- π§ Integration tests: end-to-end flow (stub provided)
Test Scenario 1: Simple Math
Prompt: "Calculate 2 + 2"
Expected: Selects Gemini (cheap), verifies successfully
Test Scenario 2: Complex Reasoning
Prompt: "Explain the theory of relativity and its implications"
Expected: Selects Claude (premium) or escalates to it
Test Scenario 3: Budget Constraint
Prompt: Long coding task
Max Cost: $0.001 USDC (very low)
Expected: Rejects or selects cheapest provider
Test Scenario 4: Escalation
Prompt: "What is foobar?" (nonsense)
Expected: First provider may fail verification, escalates to premium
- Replace test private keys with secure key management (HSM/MPC/Vault)
- Set
DEMO_MODE=falseand configure real Arc RPC endpoint - Fund treasury wallet with real USDC on Arc mainnet
- Add real LLM API keys (Gemini, Anthropic)
- Implement rate limiting (e.g., express-rate-limit)
- Add authentication (JWT/OAuth) for API access
- Set up monitoring (Datadog, New Relic, etc.)
- Configure alerting for:
- Low treasury balance
- Failed payments
- Unusual spending patterns
- Deploy behind load balancer with HTTPS
- Enable CORS only for trusted domains
- Set up database for audit logs (PostgreSQL recommended)
- Implement retry logic with exponential backoff
- Add circuit breakers for provider failures
Infrastructure:
- Docker + Kubernetes for orchestration
- PostgreSQL for persistent storage
- Redis for caching quotes
- Nginx as reverse proxy
Monitoring:
- Prometheus + Grafana for metrics
- ELK stack for log aggregation
- Sentry for error tracking
Security:
- AWS KMS / GCP Secret Manager for keys
- Cloudflare for DDoS protection
- Regular security audits
| Task Type | Provider | Cost per Request | Typical Savings |
|---|---|---|---|
| Simple Math | Gemini | $0.0001 | 95% vs Claude |
| Short QA | Gemini | $0.0003 | 90% vs Claude |
| Code Generation | Claude | $0.0025 | N/A (quality needed) |
| Long Writing | Claude | $0.0045 | N/A (quality needed) |
Monthly Projection (1000 requests):
- All Gemini: ~$0.30
- All Claude: ~$3.50
- Smart Routing: ~$0.80 (77% savings)
This is a hackathon MVP. Contributions welcome!
Priority Improvements:
- Real LLM API integrations (Gemini, Claude)
- Additional providers (OpenAI, Mistral, etc.)
- Advanced verification (semantic similarity, fact-checking)
- UI improvements (charts, history, export)
- Database backend (replace file-based storage)
MIT License - See LICENSE file
Built for hackathon demo by Claude Code.
Technologies Used:
- Node.js + TypeScript
- Express.js
- Next.js
- ethers.js
- Arc Network (EVM L1)
- Arc Network Explorer: https://explorer.arc.xyz
- Arc RPC Docs: https://docs.arc.xyz
- USDC on Arc: Check Arc docs for token contract address
- Gemini API: https://ai.google.dev/
- Anthropic API: https://www.anthropic.com/api
For issues or questions:
- Check the Issues tab
- Review environment variable configuration
- Ensure all services are running (
npm run dev) - Check logs in each service's terminal
Common Issues:
"No providers available" β Ensure provider-wrappers are running on ports 4001, 4002
"Payment verification failed" β Check DEMO_MODE is true, or treasury has USDC balance
"Daily spend cap exceeded"
β Reset: delete router-backend/data/usage-stats.json
Ready to route some prompts? π
npm run dev
# Open http://localhost:5173