Cloudflare Workers are perfect for AI endpoints — zero cold starts, global distribution, and you pay only for what you use. But there is an awkward gap in the story: your Worker starts in under a millisecond, routes the request from the nearest PoP, does all the edge logic beautifully... and then makes a single hardcoded API call to one LLM provider in one region. All that edge optimization leads to one bottleneck. If that provider is slow or down, your globally-distributed Worker returns a 500 from the edge. Fast failure is still failure.
Antbase fills that gap. Your Worker makes one call to Antbase, and Antbase handles the model selection, provider routing, and failover. Simple requests get fast free models for minimal latency. Complex requests get premium models. And if any provider is having issues, Antbase routes around it — your Worker never sees the failure. The edge stays fast and your AI stays reliable.
For serverless economics, this pairing is especially compelling. Cloudflare charges by requests and CPU time, and AI calls are I/O wait — they do not count against your CPU budget. Antbase routes simple requests to free models, so many of your AI calls cost nothing on both the compute side (Cloudflare free tier) and the AI side (Antbase free models). You can build genuinely useful AI endpoints that cost almost nothing to run at global scale.
Cloudflare Workers run at the edge in 300+ locations worldwide. Combining them with Antbase gives you globally distributed AI endpoints with sub-millisecond Worker startup times. The Worker handles request logic at the edge, and Antbase routes the LLM call to the optimal model.
Create a Worker
npm create cloudflare@latest my-ai-worker
cd my-ai-workerWorker Code
// src/index.ts
export interface Env {
ANTBASE_API_KEY: string;
}
export default {
async fetch(request: Request, env: Env): Promise<Response> {
if (request.method !== 'POST') {
return new Response('Send a POST with { "message": "..." }', { status: 405 });
}
const { message } = await request.json<{ message: string }>();
const aiResponse = await fetch('https://antbase.ai/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${env.ANTBASE_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'auto',
messages: [
{ role: 'system', content: 'You are a helpful assistant. Be concise.' },
{ role: 'user', content: message },
],
}),
});
const data = await aiResponse.json<any>();
return Response.json({
reply: data.choices[0].message.content,
});
},
};Deploy
# Set the secret
npx wrangler secret put ANTBASE_API_KEY
# Deploy
npx wrangler deployTips
- •Workers have a 30-second CPU time limit on the free plan (unbounded on paid). AI calls are I/O wait, not CPU, so they do not count toward this limit.
- •Use streaming for long responses. Pass the Antbase response body directly through: return new Response(aiResponse.body, { headers }).
- •Store the API key as a Wrangler secret, not in wrangler.toml. Secrets are encrypted at rest.
- •For high traffic, Antbase handles rate limiting and load balancing across providers. Your Worker just forwards requests.



