thelastapi
Get started free

The simplest way to switch between LLM providers in code

Switch between LLM providers in code by pointing the OpenAI SDK to an OpenAI-compatible gateway and changing only the model string.

On this page 7 sections

The simplest way to switch between LLM providers in code is to point the OpenAI SDK to an OpenAI-compatible gateway and change only the model parameter string. Instead of installing separate client libraries and managing different API keys for OpenAI, Anthropic, Google, and DeepSeek, a single base URL and key lets you swap models on demand. Your request format, streaming logic, tool calls, and error handling stay identical across every provider.

This guide walks through configuring your client, routing requests across different models in TypeScript, choosing between stable aliases and lab model IDs, and keeping costs predictable.

Why switching LLM providers usually breaks code

Moving an application from one model provider to another usually requires a painful refactor:

  • SDK sprawl. Each provider publishes its own SDK with unique initialization arguments, authentication patterns, and method names. Supporting three providers means maintaining three client wrappers in your application.
  • Divergent payload shapes. Message structures, tool calling schemas, JSON mode flags, and streaming chunk formats vary across labs. A parser written for one provider rarely works with another out of the box.
  • Scattered credentials and billing. Every new lab requires setting up a new account, entering credit card details, tracking credits across separate dashboards, and distributing another secret key across your environments.
  • Incompatible error responses. Mid-stream connection drops, context length errors, and rate limits arrive with different status codes and JSON payloads, complicating error recovery.

Using an OpenAI-compatible gateway removes these problems. Because the OpenAI chat completions shape is supported across developer tools and agent frameworks, you can treat model providers as swappable strings rather than separate architectures. For a broader look at this setup, see one API key for GPT, Claude and Gemini: how a unified AI API works.

Route all models through an OpenAI-compatible gateway

To switch providers without changing your codebase, you configure the standard OpenAI SDK to point to the thelastapi gateway at /v1/ai. You only need one API key.

When you initialize a project using the CLI:

Terminal
vibiz init "Acme Agent" --resources ai
vibiz dev

The CLI writes your development environment variables to .env.development.local. These include VIBIZ_AI_BASE_URL and VIBIZ_AI_API_KEY, alongside the standard OPENAI_BASE_URL and OPENAI_API_KEY aliases.

If you are using the official OpenAI Node SDK, pass the environment variables directly:

TypeScript
import OpenAI from 'openai';

const openai = new OpenAI({
  baseURL: process.env.VIBIZ_AI_BASE_URL,
  apiKey: process.env.VIBIZ_AI_API_KEY,
});

If your application uses the @vibiz/app package with VIBIZ_APP_KEY, you can pass the configuration object directly without managing individual AI environment variables:

TypeScript
import OpenAI from 'openai';
import { vibiz } from '@vibiz/app';

const openai = new OpenAI(vibiz.ai);

For more details on setting up environment variables across staging and local builds, review Keys and environments and change the base URL to call other AI models from OpenAI code.

How to switch models dynamically in code

Once your client points to the gateway, switching between providers happens entirely through the model property. You can route simple queries to smaller models and complex reasoning tasks to larger models using the same client instance.

Here is an example that routes prompts to different providers dynamically based on a task configuration:

TypeScript
import OpenAI from 'openai';

const openai = new OpenAI({
  baseURL: process.env.VIBIZ_AI_BASE_URL,
  apiKey: process.env.VIBIZ_AI_API_KEY,
});

type TaskType = 'classification' | 'reasoning' | 'creative';

const MODEL_MAP: Record<TaskType, string> = {
  classification: 'vibiz/lite',      // Runs DeepSeek V4.1 Flash
  reasoning: 'vibiz/reasoning',       // Runs GPT-6.1 Terra
  creative: 'vibiz/smart',           // Runs Claude Sonnet 5.5
};

async function executeTask(task: TaskType, prompt: string) {
  const model = MODEL_MAP[task];

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

  console.log(`Executed with ${response.model}:`);
  console.log(response.choices[0]?.message.content);
}

// Switch between three distinct AI labs using identical code:
await executeTask('classification', 'Categorize this ticket: "Cannot reset password"');
await executeTask('reasoning', 'Verify the logic in this database migration script.');
await executeTask('creative', 'Draft a launch announcement for our new feature.');

The gateway handles translating your request into the upstream provider's native format and returns a standard OpenAI chat completion response. Streaming works the same way: enable stream: true, and you receive standard server-sent events regardless of which lab generates the tokens.

You can also run requests over standard HTTP without an SDK:

Terminal
curl https://thelastapi.dev/v1/ai/chat/completions \
  -H "Authorization: Bearer $VIBIZ_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "vibiz/smart",
    "messages": [{"role": "user", "content": "Explain vector databases in one sentence."}]
  }'

Choosing between stable tier aliases and direct model IDs

When switching models, the thelastapi gateway gives you two choices for the model parameter: stable thelastapi tier aliases or direct lab model IDs.

1. Stable tier aliases

Tier aliases let you define what type of model your code requires without pinning your implementation to a specific model version. As labs release improvements, tiers move to newer models.

Tier aliasUnderlying modelPrimary use casePrice per million tokens (in / out)
vibiz/fastGPT-6 Luna (OpenAI)Fast chat, tool calls, JSON responses$0.10 / $0.50
vibiz/liteDeepSeek V4.1 FlashCost-efficient chat, 1M context$0.14 / $0.42
vibiz/smart-miniClaude Haiku 5.5 (Anthropic)Quick, strong writing and coding$0.10 / $0.50
vibiz/balancedGemini 3.8 Flash (Google)Balanced quality and cost, 1M context$0.825 / $4.125
vibiz/reasoningGPT-6.1 Terra (OpenAI)Math, logic, complex planning$1 / $5
vibiz/smartClaude Sonnet 5.5 (Anthropic)Writing, deep coding, autonomous agents$2 / $10
vibiz/proClaude Opus 5.5 (Anthropic)Long agent runs, deepest capabilities$4 / $20

2. Direct lab model IDs

If you need a specific version, over 400 language models from OpenAI, Anthropic, Google, DeepSeek, xAI, Moonshot AI, Qwen, Mistral, and Meta can be called directly by their identifier, such as anthropic/claude-opus-5.5 or moonshotai/kimi-k3.1-pro.

You can search the model catalogue from your terminal:

Terminal
vibiz ai models kimi

Or list all models via the API:

Terminal
curl "https://thelastapi.dev/v1/ai/models?all=1&q=claude" \
  -H "Authorization: Bearer $VIBIZ_API_KEY"

The full specification of available endpoints is in the AI docs.

Handling failures without silent fallbacks

When switching providers programmatically, a common concern is whether the gateway will quietly substitute a different model if the requested provider is experiencing an outage.

thelastapi never swaps models behind your back. If you call vibiz/smart and Claude Sonnet 5.5 is unavailable, the request fails with a 503 status and model_unavailable or provider_unavailable. This ensures your application's output characteristics and costs remain strictly under your control.

Every JSON completion response includes two tracking fields:

  • served_model: The exact model that processed the request.
  • cost_micro_usd: The exact cost of the call in micro-USD.

If an error occurs mid-stream, the connection does not abruptly terminate without context. Instead, the final server-sent event delivers the error payload formatted as data: {"error": {...}} without the trailing [DONE] token, allowing your client code to catch and log the issue cleanly.

Controlling cost when routing between models

Different models carry vastly different price tags. Calling vibiz/pro costs twenty times more per token than vibiz/fast. When switching between models dynamically in code, budget enforcement is critical.

thelastapi enforces guardrails at both the account and project levels:

  • Daily spend limit. Every account starts with a default safety ceiling of $5 per UTC day across AI calls and actions. Account owners can raise this ceiling up to $1,000 in the console or via the API (PATCH /v1/billing).
  • Project daily budget. Each project defaults to a $1 daily AI budget. When hit, calls return HTTP 429 with details.reason: "project_budget".
  • Prepaid credits. Accounts start with $2 of trial credit. Calls are checked before execution against your current balance to ensure the worst-case token cost does not exceed your funds.

To view your current spend broken down by model:

Terminal
vibiz usage --by model --period 7d

You can learn more about configuring automated alerts and setting up user-level caps in how to set spending limits across AI providers and the Usage and limits guide.

To get started, explore the available endpoints in the AI docs, or review prepaid account options in Billing and credits.

Frequently asked questions

What happens if I make a typo in the model name?

If you request a model ID that does not exist in the catalogue, the gateway returns a `400 invalid_request` error with `param: "model"`. It will never default to another model or run an unintended tier.

Do I need separate API keys for each AI provider?

No. A single thelastapi key (`vz_sk_...` or `vz_ai_...`) authenticates requests for all supported providers, including OpenAI, Anthropic, Google, and DeepSeek.

Can I use my own Google key to avoid usage billing on Gemini models?

Yes. You can configure your own Gemini API key using `GEMINI_API_KEY=... vibiz ai keys set google`, and requests for Google models will run against your Google account at zero cost on thelastapi.

What are the default request rate limits for AI calls?

Account keys permit up to 300 requests and 400,000 tokens per minute with 50 concurrent requests in flight; project keys permit 120 requests and 200,000 tokens per minute with 25 concurrent requests.