Antbase/Documentation

Cloudflare Workers Integration

Deploy AI-powered edge functions on Cloudflare Workers with Antbase. Process requests at the edge with intelligent model routing and sub-second cold starts.

What this enables

  • AI functions at the edge with global low latency
  • Streaming responses from Cloudflare's edge network
  • Zero cold start for AI-powered APIs
  • Works with Hono, itty-router, or raw fetch handler

Quick Start

1. Create a Worker project

npm create cloudflare@latest my-ai-worker
cd my-ai-worker
npm install openai

2. Add your API key as a secret

npx wrangler secret put ANTBASE_API_KEY

3. Create the Worker

// src/index.ts
import OpenAI from 'openai';

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() as { message: string };

    const openai = new OpenAI({
      apiKey: env.ANTBASE_API_KEY,
      baseURL: 'https://antbase.ai/v1',
    });

    const response = await openai.chat.completions.create({
      model: 'ant:fast',
      messages: [{ role: 'user', content: message }],
      max_tokens: 1024,
    });

    return Response.json({
      reply: response.choices[0].message.content,
    });
  },
};

4. Deploy

npx wrangler deploy

Configuration

# wrangler.toml
name = "my-ai-worker"
main = "src/index.ts"
compatibility_date = "2024-01-01"

[vars]
ANTBASE_BASE_URL = "https://antbase.ai/v1"

Usage Examples

Streaming response

export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    const { message } = await request.json() as { message: string };

    const openai = new OpenAI({
      apiKey: env.ANTBASE_API_KEY,
      baseURL: 'https://antbase.ai/v1',
    });

    const stream = await openai.chat.completions.create({
      model: 'ant:auto',
      messages: [{ role: 'user', content: message }],
      stream: true,
    });

    const encoder = new TextEncoder();
    const readable = new ReadableStream({
      async start(controller) {
        for await (const chunk of stream) {
          const text = chunk.choices[0]?.delta?.content || '';
          controller.enqueue(encoder.encode(`data: ${JSON.stringify({ text })}\n\n`));
        }
        controller.enqueue(encoder.encode('data: [DONE]\n\n'));
        controller.close();
      },
    });

    return new Response(readable, {
      headers: {
        'Content-Type': 'text/event-stream',
        'Cache-Control': 'no-cache',
      },
    });
  },
};

With Hono framework

import { Hono } from 'hono';
import OpenAI from 'openai';

const app = new Hono<{ Bindings: { ANTBASE_API_KEY: string } }>();

app.post('/chat', async (c) => {
  const { message } = await c.req.json();
  const openai = new OpenAI({
    apiKey: c.env.ANTBASE_API_KEY,
    baseURL: 'https://antbase.ai/v1',
  });

  const response = await openai.chat.completions.create({
    model: 'ant:auto',
    messages: [{ role: 'user', content: message }],
  });

  return c.json({ reply: response.choices[0].message.content });
});

export default app;

Virtual Models

ModelBest forNotes
ant:autoGeneral usePicks optimal model per request
ant:fastEdge APIsLowest latency for edge functions
ant:bestComplex tasksFrontier models for quality output
ant:freeDevelopmentZero cost while building
ant:reasoningAnalysisChain-of-thought reasoning
ant:coderCode tasksCode-optimized models

See Virtual Models for the full list.

Troubleshooting

Worker timeout

Free Workers have a 10ms CPU time limit (but network I/O does not count). Paid Workers get 30 seconds of wall-clock time. Use ant:fast to minimize response time.

OpenAI SDK bundle size

The OpenAI SDK adds to your Worker bundle. If you hit size limits, use fetch() directly instead of the SDK for a smaller bundle.