Getting Started with Antbase in TypeScript
ยท5 min read
๐Ÿ“˜

Getting Started with Antbase in TypeScript

Use the standard OpenAI TypeScript SDK with Antbase. Covers setup, chat completions, streaming, and tool calling.

typescriptsdknodejs

Every TypeScript project that touches LLMs ends up building the same ad-hoc infrastructure: retry logic for when OpenAI is down, model selection logic for different use cases, cost tracking to figure out where the budget is going. It is undifferentiated plumbing that has nothing to do with your actual product, and it is surprisingly hard to get right โ€” especially the fallback logic when a provider returns a 500 at 2 AM.

Antbase replaces all of that with a single base URL change in your OpenAI client. You get intelligent model routing, automatic failover across providers, and cost optimization โ€” all without adding a dependency, writing a wrapper, or maintaining provider-specific code. The official openai npm package works perfectly because Antbase is fully OpenAI-compatible. Your types stay correct, your streaming works, your tool calling works.

For TypeScript specifically, this matters because type safety is the whole point. You do not want to adopt a custom SDK with questionable types or a wrapper library that breaks on the next OpenAI SDK update. With Antbase, you use the official SDK, you get full type inference on every response, and the only difference is where your requests go. Two lines of config, zero new types to learn.

Antbase is OpenAI-compatible, so you use the official openai npm package. Point it at Antbase and every request gets intelligent model routing โ€” no custom SDK, no wrapper libraries.

Installation

bash
npm install openai

Basic Chat Completion

typescript
import OpenAI from 'openai';

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

const response = await client.chat.completions.create({
  model: 'auto',
  messages: [
    { role: 'system', content: 'You are a helpful assistant.' },
    { role: 'user', content: 'Explain TypeScript generics briefly.' },
  ],
});

console.log(response.choices[0].message.content);

Streaming

typescript
const stream = await client.chat.completions.create({
  model: 'auto',
  messages: [{ role: 'user', content: 'Write a limerick about Node.js.' }],
  stream: true,
});

for await (const chunk of stream) {
  const content = chunk.choices[0]?.delta?.content;
  if (content) process.stdout.write(content);
}

Tool Calling

typescript
const response = await client.chat.completions.create({
  model: 'auto',
  messages: [{ role: 'user', content: 'What is the weather in London?' }],
  tools: [
    {
      type: 'function',
      function: {
        name: 'get_weather',
        description: 'Get current weather for a city',
        parameters: {
          type: 'object',
          properties: {
            city: { type: 'string', description: 'City name' },
          },
          required: ['city'],
        },
      },
    },
  ],
});

// Antbase routes tool-calling requests to models that support it
const toolCall = response.choices[0].message.tool_calls?.[0];
console.log(toolCall?.function.name, toolCall?.function.arguments);

Tips

  • โ€ขThe openai package is fully typed โ€” you get autocomplete and type checking for all request/response fields.
  • โ€ขUse model: 'auto' for routing or any specific model name (e.g., 'gpt-4o', 'claude-sonnet') to pin.
  • โ€ขFor serverless (Vercel, Cloudflare), the client is lightweight and stateless. Create it per-request or at module scope.
  • โ€ขAll OpenAI parameters work: temperature, max_tokens, response_format, seed, etc.

Try ANT routing today

Drop-in OpenAI-compatible API. Change your base URL and every request gets intelligent routing across 30+ providers.