Skip to content

Backend Architecture for CR8 Agent Platform

Overview

Hybrid Architecture:

  • On-Chain: Ownership, tokenization, registry (metadata), staking, fees
  • Off-Chain Backend: Agent execution, GPU compute, KYC, developer tools, analytics

Backend Services

1. API Gateway / Main Backend

Tech: Node.js (Express/Fastify) or Python (FastAPI) Purpose: Main API, routing, authentication

Endpoints:

POST   /api/agents/register          # Register new agent
GET    /api/agents                   # List agents
GET    /api/agents/:id               # Agent details
POST   /api/agents/:id/execute       # Execute agent
GET    /api/agents/:id/status        # Agent status
POST   /api/agents/:id/configure     # Configure agent
GET    /api/users/me                 # User profile
POST   /api/kyc/verify               # KYC verification
GET    /api/analytics/agents/:id     # Agent analytics

2. Agent Execution Service

Tech: Python (FastAPI) or Node.js Purpose: Execute agent tasks, manage GPU resources

Features:

  • Task queue management
  • GPU allocation
  • Agent container orchestration
  • Result caching
  • Error handling & retries

3. GPU Compute Manager

Tech: Kubernetes, Docker Purpose: Manage GPU resources from partners

Features:

  • GPU node management
  • Load balancing
  • Resource allocation
  • Partner API integration
  • Cost tracking

4. KYC/Identity Service

Tech: Node.js + Third-party APIs Purpose: User verification, compliance

Integrations:

  • KYC providers (Sumsub, Onfido, etc.)
  • Identity verification
  • Compliance tracking
  • Privacy protection

5. Developer Portal API

Tech: Node.js/Python Purpose: Developer tools and management

Features:

  • API key management
  • Usage quotas
  • Billing integration
  • Analytics dashboard
  • Documentation

6. Analytics Service

Tech: Time-series DB (TimescaleDB/InfluxDB) Purpose: Metrics and analytics

Tracks:

  • Agent executions
  • Usage metrics
  • Revenue
  • User behavior
  • Performance metrics

Database Schema

PostgreSQL (Main Database)

sql
-- Users
CREATE TABLE users (
  id UUID PRIMARY KEY,
  wallet_address VARCHAR(42) UNIQUE,
  email VARCHAR(255),
  kyc_status VARCHAR(50),
  created_at TIMESTAMP,
  updated_at TIMESTAMP
);

-- Agents (Off-Chain)
CREATE TABLE agents (
  id UUID PRIMARY KEY,
  on_chain_id INTEGER UNIQUE, -- Links to on-chain registry
  creator_id UUID REFERENCES users(id),
  name VARCHAR(255),
  description TEXT,
  metadata_ipfs_hash VARCHAR(255),
  execution_config JSONB,
  api_keys JSONB, -- Encrypted
  status VARCHAR(50),
  created_at TIMESTAMP,
  updated_at TIMESTAMP
);

-- Agent Executions
CREATE TABLE agent_executions (
  id UUID PRIMARY KEY,
  agent_id UUID REFERENCES agents(id),
  user_id UUID REFERENCES users(id),
  status VARCHAR(50),
  input_data JSONB,
  output_data JSONB,
  gpu_used BOOLEAN,
  execution_time_ms INTEGER,
  cost_usd DECIMAL,
  created_at TIMESTAMP,
  completed_at TIMESTAMP
);

-- Usage & Billing
CREATE TABLE usage_records (
  id UUID PRIMARY KEY,
  agent_id UUID REFERENCES agents(id),
  user_id UUID REFERENCES users(id),
  execution_id UUID REFERENCES agent_executions(id),
  resource_type VARCHAR(50), -- 'gpu', 'api', 'storage'
  quantity DECIMAL,
  cost_usd DECIMAL,
  created_at TIMESTAMP
);

-- API Keys (Developers)
CREATE TABLE api_keys (
  id UUID PRIMARY KEY,
  user_id UUID REFERENCES users(id),
  key_hash VARCHAR(255) UNIQUE,
  name VARCHAR(255),
  permissions JSONB,
  rate_limit INTEGER,
  usage_count INTEGER,
  created_at TIMESTAMP,
  expires_at TIMESTAMP
);

Redis (Cache & Queue)

redis
# Agent execution queue
agent:execute:queue

# Cached agent data
agent:{id}:cache

# Rate limiting
ratelimit:{user_id}:{endpoint}

# Session data
session:{session_id}

Data Flow: Agent Registration

1. Developer calls Backend API
   POST /api/agents/register
   {
     name: "My Agent",
     description: "...",
     executionConfig: {...}
   }

2. Backend validates & creates record
   - Check KYC status
   - Validate config
   - Create database record

3. Backend calls On-Chain Contract
   AgentRegistry.registerAgent(metadataHash)

4. On-Chain returns agentId

5. Backend updates record with onChainId

6. Backend returns agent info
   {
     id: "uuid",
     onChainId: 123,
     ...
   }

Data Flow: Agent Execution

1. User requests execution (Frontend)
   POST /api/agents/{id}/execute
   { input: "..." }

2. Backend validates
   - Check ownership/access
   - Verify quotas
   - Check rate limits

3. Backend queues task
   - Add to execution queue
   - Allocate GPU (if needed)

4. Agent Execution Service processes
   - Load agent config
   - Execute on GPU (if needed)
   - Call APIs (OpenAI, etc.)
   - Generate result

5. Backend records execution
   - Save to database
   - Calculate fees
   - Update analytics

6. Backend triggers on-chain distribution
   - Calculate owner shares
   - Calculate staker shares
   - Call FeeDistributor contract

7. Return result to user

Chain Strategy

Option A: Optimism

  • Low fees (~$0.01-0.1 per tx)
  • EVM compatible
  • Fast finality
  • Good for MVP

Option B: Arbitrum

  • Very low fees
  • High throughput
  • Mature ecosystem
  • Good for scale

Option C: Base

  • Coinbase-backed
  • Growing ecosystem
  • Low fees
  • Good for user acquisition

Deploy:

  • AgentRegistry.sol
  • AgentToken.sol (ERC-20 per agent)
  • CR8Token.sol
  • CR8Staking.sol
  • FeeDistributor.sol

Phase 2: Build Custom Chain (When Needed)

When to Build:

  • High transaction volume
  • Need custom gas token (CR8)
  • Want to subsidize fees
  • Need custom features

Options:

A. OP Stack (Recommended)

  • Optimism's framework
  • EVM compatible
  • Customizable
  • Good documentation

B. Arbitrum Orbit

  • Arbitrum's framework
  • Low fees
  • Customizable
  • Mature

C. Polygon CDK

  • Polygon's framework
  • Good for NFTs
  • Customizable

Implementation:

  1. Deploy OP Stack chain
  2. Use CR8 as gas token
  3. Migrate contracts
  4. Bridge tokens (if needed)

Backend Service Architecture

packages/
├── contracts/          # Smart contracts (existing)
├── dapp/              # Frontend (existing)
├── website/           # Landing (existing)
└── backend/           # NEW - Backend services
    ├── api/           # Main API service
    ├── agent-executor/# Agent execution service
    ├── gpu-manager/   # GPU compute manager
    ├── kyc-service/   # KYC/Identity service
    ├── analytics/     # Analytics service
    └── shared/        # Shared utilities

API Service (packages/backend/api)

typescript
// Structure
packages/backend/api/
├── src/
│   ├── routes/
│   │   ├── agents.ts
│   │   ├── users.ts
│   │   ├── kyc.ts
│   │   └── analytics.ts
│   ├── services/
│   │   ├── agentService.ts
│   │   ├── blockchainService.ts
│   │   └── kycService.ts
│   ├── middleware/
│   │   ├── auth.ts
│   │   └── rateLimit.ts
│   └── config/
│       └── database.ts
├── package.json
└── tsconfig.json

Agent Executor Service (packages/backend/agent-executor)

python
# Structure
packages/backend/agent-executor/
├── src/
│   ├── executors/
│   │   ├── base.py
│   │   ├── crewai_executor.py
│   │   └── langchain_executor.py
│   ├── gpu/
│   │   ├── manager.py
│   │   └── allocator.py
│   ├── queue/
│   │   └── task_queue.py
│   └── main.py
├── requirements.txt
└── Dockerfile

Integration Points

On-Chain ↔ Off-Chain

Event Listening:

typescript
// Backend listens to on-chain events
contract.on('AgentRegistered', (agentId, metadataHash) => {
  // Update backend database
  // Index agent metadata
});

contract.on('FeeDistributed', (agentId, amount) => {
  // Update analytics
  // Trigger notifications
});

Contract Calls from Backend:

typescript
// Backend calls contracts
async function registerAgentOnChain(metadataHash: string) {
  const tx = await agentRegistry.registerAgent(metadataHash);
  return tx.wait();
}

async function distributeFees(agentId: number, amount: bigint) {
  const tx = await feeDistributor.distributeFees(agentId, amount);
  return tx.wait();
}

KYC Integration

Flow

1. User connects wallet (Frontend)

2. Frontend calls Backend
   POST /api/kyc/initiate

3. Backend creates KYC session
   - Call KYC provider API
   - Get verification link

4. User completes KYC (External)
   - Submit documents
   - Verification process

5. KYC provider webhook
   POST /api/kyc/webhook

6. Backend updates status
   - Mark user as verified
   - Update on-chain (if needed)

7. User can now register agents

GPU Partner Integration

Architecture

Agent Execution Request

Backend API

GPU Manager Service

GPU Partner API / Kubernetes

GPU Cluster

Agent Execution

Results

Partner Integration

Option 1: API-Based Partners

  • RunPod API
  • Vast.ai API
  • Lambda Labs API
  • Other GPU providers

Option 2: Kubernetes Cluster

  • Self-hosted GPU nodes
  • Partner-managed clusters
  • Hybrid approach

Developer Experience

Agent Registration API

typescript
// Developer registers agent
POST /api/v1/agents/register
Headers: { Authorization: 'Bearer {api_key}' }
Body: {
  name: "My AI Agent",
  description: "Agent that does X",
  executionConfig: {
    framework: "crewai",
    gpuRequired: true,
    modelType: "llama2",
    maxConcurrent: 10
  },
  pricing: {
    perExecution: 0.01,
    gpuMultiplier: 2.0
  }
}

Response: {
  agentId: "uuid",
  onChainId: 123,
  apiKey: "agent_xxx",
  status: "pending"
}

Agent Execution API

typescript
// Execute agent
POST /api/v1/agents/{agentId}/execute
Headers: { Authorization: 'Bearer {api_key}' }
Body: {
  input: "Generate content about...",
  options: {
    useGPU: true,
    timeout: 30000
  }
}

Response: {
  executionId: "uuid",
  status: "queued",
  estimatedTime: 30
}

// Poll for result
GET /api/v1/executions/{executionId}

Response: {
  status: "completed",
  result: "...",
  cost: 0.05,
  executionTime: 25000
}

Phase 1: Backend Foundation (Months 1-2)

On-Chain:

  • Deploy to Optimism/Arbitrum
  • AgentRegistry contract
  • AgentToken contract (ERC-20 per agent)
  • Fee distribution

Backend:

  • Basic API service
  • Agent registration endpoint
  • Database setup
  • Simple execution service

Frontend:

  • Update DApp for hybrid model
  • Developer registration UI

Phase 2: Full Backend (Months 3-4)

Backend:

  • Agent execution service
  • GPU integration
  • KYC service
  • Analytics service
  • Developer portal

On-Chain:

  • Optimize contracts
  • Add features

Phase 3: Scale & Optimize (Months 5-6)

Backend:

  • Performance optimization
  • Caching layer
  • Load balancing
  • Monitoring

Consider:

  • Custom chain (if volume justifies)

Technology Recommendations

Backend Stack

API Service:

  • Node.js + TypeScript
  • Express or Fastify
  • PostgreSQL
  • Redis

Agent Executor:

  • Python (better for AI/ML)
  • FastAPI
  • Celery (task queue)
  • Docker containers

GPU Manager:

  • Kubernetes
  • GPU operator
  • Partner APIs

Database:

  • PostgreSQL (main data)
  • Redis (cache/queue)
  • TimescaleDB (analytics)

Infrastructure

Hosting:

  • Backend: AWS/GCP/Azure
  • GPU: Partner providers
  • Database: Managed PostgreSQL
  • Cache: Redis Cloud

Monitoring:

  • Prometheus + Grafana
  • Sentry (errors)
  • Logging: ELK stack

Cost Estimates

On-Chain (L2)

  • Agent registration: ~$0.10
  • Token transfers: ~$0.01
  • Fee distribution: ~$0.05

Off-Chain

  • Backend hosting: $100-500/month
  • GPU compute: Variable ($0.10-1/hour)
  • Database: $50-200/month
  • KYC: $1-5 per verification

Security Considerations

Backend Security

  • API authentication (JWT/API keys)
  • Rate limiting
  • Input validation
  • Secure API key storage
  • Encrypted database
  • HTTPS only

On-Chain Security

  • Smart contract audits
  • Access controls
  • Upgrade mechanisms
  • Emergency pauses

Next Steps

  1. Decide on L2: Optimism vs Arbitrum vs Base
  2. Design backend API: Define endpoints
  3. Set up infrastructure: Database, hosting
  4. Build MVP backend: Basic agent registration
  5. Integrate GPU partners: Start with one partner
  6. Add KYC: Integrate provider
  7. Build developer portal: Registration & management

CR8 Platform Documentation