Architecture
How the V5 Gateway works under the hood.
Overview
AI Agent / App
│
▼
┌─────────────────────────────────┐
│ Cloudflare Worker (Hono.js) │
│ │
│ Auth → Token → Route → Execute │
│ 5ms 2ms 3ms ≤30s │
└──────────┬──────────────────────┘
│
┌─────┼─────┐
▼ ▼ ▼
KV DO D1
(hot) (OAuth) (cold)
Request flow (≤10ms gateway overhead)
- Auth (~5ms) — Hash bearer token, look up
tenant_auth:{hash}in KV - Token (~2ms) — Read
tokens:{tenant}:{provider}:{account_id}from KV - Route (~3ms) — Read
api_config:{provider}from KV, build URL + headers - Execute (≤30s) — Proxy request to upstream API with AbortController timeout
- Return — Structured JSON response with success/error status
Data stores
KV (hot path — sub-ms reads)
tenant_auth:{hash}— bearer token → tenant mappingtokens:{tenant}:{provider}:{account_id}— cached OAuth/API tokensapi_config:{provider}— base URLs, versions, auth types, endpointstenant_config:{tenant}— tenant metadata, connected providers, billing
Durable Objects (background only)
- TokenManager — one DO per
{tenant}:{provider}:{account_id}triplet - Alarm fires 10 minutes before token expiry
- Refreshes via provider's token endpoint
- Writes new token to KV
- Request path NEVER contacts a DO
D1 (cold path only)
error_ledger— failed request log for debuggingkv_audit— every admin KV write (key, action, old/new hashes, source, timestamp)decision_log— self-improvement substrate (Phase 0)
Design principles
- Fail-fast — No retries in the gateway. Caller handles retries.
- KV-only hot path — No D1 reads during request processing.
- Proactive token refresh — DOs refresh before expiry, not on-demand.
- Multi-tenant isolation — Every KV key is tenant-scoped.
- One Worker — No Service Bindings, no multi-Worker complexity.
Cron schedules
| Schedule | Job | Description |
|---|---|---|
*/5 * * * * | System health | Writes health snapshot to KV |
*/15 * * * * | Token health | Checks for expiring/expired tokens |
*/30 * * * * | Provider health | Canary checks against registered APIs |
0 4 * * * | Error cleanup | Prunes old error_ledger entries |
0 6 * * * | Autoresearch | Daily briefing with suggestions |