Skip to main content

Edge Runtime

AgentDeploy edge agents run on Cloudflare Workers with Durable Objects — a serverless execution environment deployed to 300+ cities worldwide.

How SDK-based agents are deployed​

When a buyer deploys an SDK-based agent, the platform:

  1. Receives the bundled ESM module from ad deploy (already bundled by esbuild)
  2. Analyzes the script with analyzeRenderedScript() to detect:
    • Durable Object classes (extends Agent, AIChatAgent)
    • Agents SDK imports (agents, @cloudflare/ai-chat, @agentdeploy/edge-sdk)
    • Workers AI binding usage (env.AI)
    • Agent routing patterns (routeAgentRequest)
  3. Generates deployment metadata including:
    • DO namespace bindings for each detected class
    • new_sqlite_classes migration tag for SQLite-backed DOs
    • nodejs_compat compatibility flag
    • Workers AI binding (if env.AI detected)
  4. Wraps with AD_ constants* — Injects AD_DEPLOYMENT_ID, AD_MODEL, AD_GATEWAY_BASE_URL, etc. as ambient globals
  5. Deploys to Cloudflare via the Workers API with full metadata
  6. Binds buyer secrets via the Cloudflare Secrets API
  7. Returns a global URL that routes to the worker

The rendered worker​

// AgentDeploy Edge Worker — v2.0.0-edge
// Generated from template 42 version 1.0.0

// Platform-injected constants
const AD_DEPLOYMENT_ID = "uuid-here";
const AD_USER_ID = "buyer-uuid";
const AD_TEMPLATE_ID = 42;
const AD_MODEL = "openai/gpt-4o-mini";
const AD_REGION = "auto";
const AD_GATEWAY_BASE_URL = "https://gateway.agentdeploy.io";

// ─── Seller's bundled code (single ESM module) ───
// This is the output of: ad deploy (esbuild)
// Contains: @agentdeploy/edge-sdk + agents + @cloudflare/ai-chat + seller code
export class MyAgent extends AgentDeployChatAgent { ... }
export default createHandler(MyAgent);

Durable Object auto-detection​

The platform's analyzeRenderedScript() uses regex patterns to detect DO classes:

// Detection patterns:
/export\s+class\s+(\w+)\s+extends\s+(?:Agent|AIChatAgent|AgentDeployAgent|AgentDeployChatAgent)\b/g
/export\s+(?:const|let)\s+(\w+)\s*=\s*(?:createAgent|createChatAgent)\s*\(/g

Each detected class results in:

  1. A durable_object_namespace binding in the Worker metadata
  2. A new_sqlite_classes entry in the migration tag (for SQLite-backed DOs)

No manual configuration needed — the platform infers everything from the bundled code.

Worker naming convention​

edge-{template_id}-{user_id_8chars}-{timestamp}

Example: edge-42-abc12345-1721640000000

Lowercase, sanitized, max 52 characters (Cloudflare limit).

Execution limits​

ResourceLimit
CPU time50ms default (configurable up to 30,000ms)
Memory128 MB
Script size (raw)256 KB
Script size (SDK-bundled)3 MB
Request body100 MB
Subrequests50 per request (1,000 with nodejs_compat)
Concurrent connectionsNo limit

Compatibility flags​

SDK-based agents automatically get nodejs_compat injected when Agents SDK imports are detected. For raw workers, specify manually:

{
"compatibility_date": "2025-01-01",
"compatibility_flags": ["nodejs_compat"]
}

Bindings​

The platform auto-generates these binding types based on script analysis:

Binding typeTriggerUse case
durable_object_namespaceDO class detectedStateful agent storage
aienv.AI reference detectedWorkers AI models
secret_textBuyer-configured secretsAPI keys, tokens
plain_textAD_* constantsPlatform metadata

For KV namespaces, R2 buckets, D1 databases, and queues — declare them in your wrangler.jsonc for local dev, and the platform passes them through if included in the edge version metadata.

Gateway integration​

All LLM calls route through the AgentDeploy gateway:

Agent code → useGateway() → AD_GATEWAY_BASE_URL/v1/chat/completions → Gateway
│
┌────────────────────┤
│ │
Token metering Provider routing
│ │
Billing system OpenAI / Anthropic
/ Google / Groq

The gateway:

  • Uses the X-AD-Deployment header for authentication (no API key needed in agent code)
  • Tracks token usage per deployment
  • Enforces buyer plan limits (overage protection)
  • Writes revshare ledger entries for token markup

Local development​

ad dev

This wraps wrangler dev with your project's wrangler.jsonc. The wrangler config includes:

  • DO bindings for local testing
  • SQLite migrations
  • nodejs_compat flag
  • Local AD_* variable stubs

The local gateway stubs route to the platform's gateway URL, so billing and metering still work in dev mode.