Skip to content

Architecture

AgentWatch is built on Cloudflare's global edge infrastructure to deliver sub-10ms budget enforcement with zero impact on your production traffic.

System Overview

Client Application Architecture

System Overview (Detailed)

┌─────────────────────────────────────────────────────────────┐
│                        Your Application                     │
│  ┌─────────────────────────────────────────────────────┐    │
│  │  Standard SDK (OpenAI / Anthropic / any provider)   │    │
│  │  ├── HTTP Request formatting                        │    │
│  │  └── Base URL pointing to AgentWatch                │    │
│  └─────────────────────────────────────────────────────┘    │
└───────────────────────────┬─────────────────────────────────┘
                            │ HTTPS

┌─────────────────────────────────────────────────────────────┐
│                  Cloudflare Edge Network                    │
│  ┌─────────────────────────────────────────────────────┐    │
│  │  AgentWatch Worker                                  │    │
│  │  ├── Authentication (Bearer token + BYOK)           │    │
│  │  ├── Rate limiting (per-tenant, native binding)     │    │
│  │  ├── Budget check (KV lookup + Durable Object)      │    │
│  │  ├── Rule evaluation (custom anomaly rules)         │    │
│  │  ├── Data residency routing (EU/US/APAC)            │    │
│  │  ├── Proxy routing (10 providers)                   │    │
│  │  ├── Stream budget enforcement (mid-generation)     │    │
│  │  └── Telemetry ingestion → Queue → Supabase         │    │
│  └─────────────────────────────────────────────────────┘    │
│                                                             │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐       │
│  │  Cloudflare  │  │  Durable     │  │  Cloudflare  │       │
│  │  KV          │  │  Objects     │  │  Queues      │       │
│  │  (session    │  │  (atomic     │  │  (telemetry  │       │
│  │   state,     │  │   counters,  │  │   buffer)    │       │
│  │   caching)   │  │   budget)    │  │              │       │
│  └──────────────┘  └──────────────┘  └──────────────┘       │
└─────────────────────────────────────────────────────────────┘


┌─────────────────────────────────────────────────────────────┐
│                     Supabase (Postgres)                     │
│  ├── llm_request_logs (telemetry)                           │
│  ├── developer_keys (API key management)                    │
│  ├── audit_logs (security audit trail)                      │
│  ├── sla_records (SLA monitoring)                           │
│  ├── tenant_rules (custom rules engine)                     │
│  └── tenant_settings (per-tenant configuration)             │
└─────────────────────────────────────────────────────────────┘

Request Flow

1. API Call Initiated

Your application initiates a standard completion request via any standard provider SDK (e.g., openai.chat.completions.create()). The SDK constructs the HTTP request and sends it to the AgentWatch proxy URL.

2. Edge Proxy Processing

The Worker receives the request and executes:

  1. Authentication — Validate Bearer token via KV lookup (primary) or static map fallback
  2. Rate limiting — Per-tenant rate limit check using native Cloudflare binding
  3. Rule evaluation — Custom anomaly rules from tenant configuration
  4. Data residency — Check x-agentwatch-residency header and route to EU endpoints if needed
  5. Proxy routing — Route to the correct upstream provider
  6. Response forwarding — Stream response back to SDK
  7. Stream monitoring — If streaming, monitor token usage and terminate if budget exceeded

3. Asynchronous Telemetry

After the response is returned:

  1. Queue dispatch — Log record sent to Cloudflare Queue
  2. KV update — Session token count incremented
  3. Durable Object update — Atomic budget counter updated
  4. Anomaly check — Rolling window growth ratio analysis
  5. Supabase write — Queue consumer writes to llm_request_logs

Supported Providers

ProviderBase URLAuth Method
OpenAIapi.openai.comBearer token
Anthropicapi.anthropic.comx-api-key
Groqapi.groq.com/openaiBearer token
xAIapi.x.aiBearer token
Geminigenerativelanguage.googleapis.comBearer token
Azure OpenAI{resource}.openai.azure.comapi-key
AWS Bedrockbedrock-runtime.{region}.amazonaws.comAWS SigV4
Xiaomi MiMoapi.xiaomimimo.comBearer token
Mistralapi.mistral.aiBearer token
Cohereapi.cohere.comBearer token

Key Design Decisions

BYOK (Bring Your Own Key)

AgentWatch never stores user API keys. The user combines their AgentWatch token with their provider key:

aw_live_token:sk-proj-real-openai-key

AgentWatch extracts the token for identification and forwards the provider key to the upstream API. This means:

  • Zero liability for key storage
  • Users pay their own provider bills
  • AgentWatch only needs to know the token-to-tenant mapping

Tenant Isolation

All KV keys are namespaced by tenant ID: tenant:token:{token}, tenant:plan:{tenantId}, etc. Cross-tenant data access is impossible through the KV layer.

Timing-Safe Authentication

Token comparison uses crypto.subtle.timingSafeEqual to prevent timing attacks.

Fail-Open vs Fail-Closed

AgentWatch supports both policies:

  • Fail-open (default): If AgentWatch infrastructure fails, requests proceed to the provider. Your production stays online.
  • Fail-closed: If AgentWatch fails, requests are blocked. Budget is guaranteed, but availability may be impacted.

Enterprise customers can configure this per-tenant.

Telemetry & Reporting

Asynchronous Ingestion

Payload logging and risk scanning are offloaded to ctx.waitUntil() in the Worker. The client receives the provider's response immediately with zero added latency.

Data Collected

  • Request metadata (provider, model, latency, status)
  • Token counts (prompt, completion)
  • Cost estimation
  • PII risk tags (email, SSN, credit card, API keys detected)
  • Anomaly flags

Data NOT Collected

  • Prompt content
  • Completion content
  • User messages

Performance Characteristics

OperationLatency
Authentication (KV read)5-50ms
Budget check (KV read)5-50ms
Rate limiting (native binding)<1ms
PII detection (100KB)<50ms
Rule evaluation (50 rules)<10ms
Stream budget monitoringNon-blocking
Telemetry dispatchNon-blocking (async)
Total proxy overhead<100ms (excluding upstream)

Security

  • HSTS on all responses
  • X-Content-Type-Options: nosniff on all responses
  • X-Frame-Options: DENY on all responses
  • Timing-safe token comparison prevents timing attacks
  • Header stripping — dangerous headers (host, cookie, x-forwarded-for) are stripped before forwarding to upstream
  • CORS configured for agent-watch.dev origin only