Architecture
Edge deployment uses a two-layer pattern:- Edge Host (JavaScript/TypeScript): Handles HTTP, secrets, API calls, webhook parsing
- WASM Core (Zig): Contains agent decision logic, policy selection, feature extraction
Cloudflare Worker Example
Theexamples/edge/cloudflare-worker/ directory demonstrates a complete edge deployment.
What It Does
1
Receive webhook
Cloudflare Worker receives Telegram webhook update
2
Extract features
JavaScript host extracts text features (length, question marks, keywords)
3
Call WASM policy
WASM
choose_policy() returns response policy (concise/detailed/urgent)4
Build prompt
Host builds system prompt based on selected policy
5
Call LLM
Send request to OpenAI Chat Completions API
6
Send response
Reply sent back to Telegram chat
WASM Core
The agent logic is a tiny Zig module compiled to WASM:agent_core.zig
Build WASM
Compile the Zig module to WASM:Worker Implementation
The edge host loads the WASM module and calls it for each request:worker.mjs
Wrangler Configuration
wrangler.toml
Prerequisites
- Cloudflare account
- Wrangler CLI
- Zig 0.15.2
- Telegram bot token (or other webhook provider)
- OpenAI API key
Deployment
1
Build WASM core
2
Configure secrets
3
Create KV namespace (optional dedup)
wrangler.toml:4
Deploy
5
Set webhook
Why WASM?
Separation of Concerns
- Host owns secrets: API keys, tokens, credentials stay in edge environment variables
- Host owns network: HTTP calls, webhooks, rate limiting handled by Worker
- WASM owns logic: Decision trees, policy selection, feature extraction
Portability
The same WASM module can run on:- Cloudflare Workers
- Deno Deploy
- AWS Lambda@Edge
- Fastly Compute@Edge
- Any WASI-compatible runtime
Security
WASM provides sandboxing:- No filesystem access
- No network access
- No system calls
- Pure function execution
Size
Zig’s ReleaseSmall optimization produces tiny WASM modules:- agent_core.wasm: < 10 KB
- Faster cold starts on edge platforms
- Fits in Cloudflare’s 1 MB Worker script limit with room to spare
Feature Extraction
The host extracts simple features from text:Policy-Based Prompts
The worker selects system prompts based on WASM policy:Deduplication with KV
Telegram webhooks retry on failure. Use Cloudflare KV to deduplicate:Monitoring
Cloudflare Workers provide built-in analytics:- Request rate
- Error rate
- CPU time
- Edge location distribution
Evolution Strategy
To update agent behavior:- Edit
agent_core.zig - Run
./build_wasm.sh - Run
wrangler deploy