# Call Claude and Gemini from the OpenAI Node.js SDK

> Point the OpenAI Node.js SDK to thelastapi's gateway to call Claude and Gemini using standard chat completions, streaming, and tool calls.

**You can call Anthropic's Claude and Google's Gemini directly from the official OpenAI Node.js SDK by setting `baseURL` to thelastapi's AI gateway and using your project key.** Because the endpoint matches the OpenAI chat completions specification, you do not need to install separate client libraries, manage multiple vendor tokens, or rewrite prompt handling across providers. You point the standard `openai` package at `https://thelastapi.dev/v1/ai` and pass either a role-based model alias or an exact lab model ID.

This guide walks through configuring the client in TypeScript or JavaScript, streaming responses, inspecting per-call usage costs, and handling provider outages cleanly.

## How to point the OpenAI Node.js SDK at Claude and Gemini

When you add AI to a project using the `vibiz` CLI, your local environment receives the connection strings automatically:

```bash
vibiz init "Support Router" --resources ai
vibiz dev
```

Running `vibiz dev` populates `.env.development.local` with `VIBIZ_AI_BASE_URL` and `VIBIZ_AI_API_KEY`, alongside standard aliases `OPENAI_BASE_URL` and `OPENAI_API_KEY`. If your application reads environment variables conventionally, initializing `new OpenAI()` works without manually specifying credentials.

To configure the client explicitly in Node.js:

```ts
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 our unified application SDK, you can also pass configuration directly using [@vibiz/app](https://thelastapi.dev/docs/app-sdk):

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

// Reads baseURL and apiKey from your unified app key
const openai = new OpenAI(vibiz.ai);
```

For more details on key scoping across environments, see [keys and environments](https://thelastapi.dev/docs/keys). Once initialized, the client accepts standard requests for OpenAI models, Anthropic's Claude models, and Google's Gemini models through the same `openai.chat.completions.create` method.

## Choose between tier names and exact model IDs

When dispatching requests through the gateway, you specify the target model in the `model` parameter. You have two options: alias tiers or lab-specific model identifiers.

Alias tiers represent stable functional roles. Each alias maps to an underlying model provided by an upstream lab. The platform never routes requests to an unexpected model behind the scenes; the mapped model is documented and returned in the `served_model` field of every response.

| thelastapi model | Runs on | Best suited for | List price (in / out per M tokens) |
| --- | --- | --- | --- |
| `vibiz/fast` | GPT-6 Luna (OpenAI) | Chat, structured JSON, tool calls | $0.10 / $0.50 |
| `vibiz/smart-mini` | Claude Haiku 5.5 (Anthropic) | Fast, cost-conscious writing and coding | $0.10 / $0.50 |
| `vibiz/balanced` | Gemini 3.8 Flash (Google) | Balanced quality and cost, 1M context | $0.825 / $4.125 |
| `vibiz/smart` | Claude Sonnet 5.5 (Anthropic) | Complex coding, long reasoning, agents | $2.00 / $10.00 |
| `vibiz/pro` | Claude Opus 5.5 (Anthropic) | Deep reasoning, high-capability agent loops | $4.00 / $20.00 |
| `vibiz/lite` | DeepSeek V4.1 Flash (DeepSeek) | Low-cost conversational chat, 1M context | $0.14 / $0.42 |

Alternatively, you can call any model directly by its lab identifier, such as `anthropic/claude-opus-5.5` or `google/gemini-omni-1.1-flash`. Over 400 language models from OpenAI, Anthropic, Google, DeepSeek, xAI, Moonshot AI, and Meta are available. You can discover available IDs through the CLI:

```bash
vibiz ai models claude
vibiz ai models gemini
```

If you submit a model ID that does not exist in the catalogue, the gateway returns a `400 invalid_request` error naming `model` as the invalid parameter rather than silently substituting a fallback. To understand more about how our routing structure works across different providers, read about [how a unified AI API works](https://thelastapi.dev/blog/one-api-key-gpt-claude-gemini) and our guide on how an [OpenAI-compatible API for Claude and Gemini](https://thelastapi.dev/blog/openai-compatible-api-claude-gemini) operates under the hood.

## Code: Streaming responses and tool calls

Because the gateway supports the OpenAI chat completions schema, features such as Server-Sent Events (SSE) streaming and tool calling function as expected when targeting Claude or Gemini.

### Streaming Claude Sonnet

Here is how to stream tokens from Claude Sonnet 5.5 using the standard async iterable interface:

```ts
import OpenAI from 'openai';

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

async function streamClaude() {
  const stream = await openai.chat.completions.create({
    model: 'vibiz/smart',
    messages: [
      { role: 'system', content: 'You are an expert technical editor.' },
      { role: 'user', content: 'Draft three guidelines for resilient microservice design.' },
    ],
    stream: true,
  });

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

await streamClaude();
```

### Tool calling with Gemini 3.8 Flash

You define tools using the OpenAI JSON schema. The gateway translates the definitions into Google's native function declarations, routes the response, and formats tool calls back into OpenAI-compatible tool call objects:

```ts
import OpenAI from 'openai';

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

const tools: OpenAI.ChatCompletionTool[] = [
  {
    type: 'function',
    function: {
      name: 'lookup_order',
      description: 'Fetch the delivery status of an order.',
      parameters: {
        type: 'object',
        properties: {
          order_id: { type: 'string', description: 'The order identifier' },
        },
        required: ['order_id'],
      },
    },
  },
];

const response = await openai.chat.completions.create({
  model: 'vibiz/balanced',
  messages: [{ role: 'user', content: 'Where is order ORD-9921?' }],
  tools,
});

const toolCall = response.choices[0]?.message?.tool_calls?.[0];
if (toolCall) {
  console.log('Gemini called function:', toolCall.function.name);
  console.log('Arguments:', toolCall.function.arguments);
}
```

If you prefer to see how to update older codebases by modifying minimal configuration, check our walkthrough on how to [change the base URL to call other AI models](https://thelastapi.dev/blog/change-base-url-other-ai-models).

## How to check per-call costs and stay inside budgets

Every non-streaming completion JSON response contains standard usage token counts plus metadata reporting the exact model that ran (`served_model`) and the real cost of the request in millionths of a dollar (`cost_micro_usd`):

```json
{
  "id": "chatcmpl_abc123",
  "object": "chat.completion",
  "model": "vibiz/smart",
  "served_model": "anthropic/claude-sonnet-5.5",
  "cost_micro_usd": 14200,
  "choices": [...]
}
```

Accounts are prepaid and draw from credit balances upon recording. Reviewing your spend from your terminal or scripts requires running:

```bash
vibiz usage --by model --period 7d
```

You can guard against runaway loops or unexpected traffic spikes through built-in budget thresholds:

1. **Daily Safety Cap**: The account has a default $5 daily spend cap across all AI calls and actions, adjustable in console billing settings or with `PATCH /v1/billing {"daily_spend_limit_usd": 50}`.
2. **Project AI Budget**: Projects carry a default daily limit of $1, adjustable using the CLI:
   ```bash
   vibiz ai budget 10 --project prj_123
   ```
3. **End-User Tracking**: Pass a pseudonymous identifier in the `user` property of your request. Spending is isolated per end user, allowing you to enforce per-user spending controls using `end_user_daily_budget_usd`.

For comprehensive details on budget controls and alerts, consult the documentation on [usage and limits](https://thelastapi.dev/docs/usage) and [billing and credits](https://thelastapi.dev/docs/billing).

## What happens when a model is unavailable or rate limited?

When issues arise, the gateway returns standard HTTP status codes structured in the OpenAI error shape `{ "error": { "code", "message", "type", "param", "details" } }`.

- **Outages**: If Anthropic or Google suffers downtime, the gateway returns a `503` with code `model_unavailable` or `provider_unavailable`. It will never secretly downgrade your request to an unintended fallback model.
- **Budget Exceeded**: When a request exceeds your account ceiling or project budget, you receive a `429` status code with `details.reason` set to `"project_budget"` or `"daily_spend"`, along with a `raise_url` linking directly to the management screen.
- **Credit Exhaustion**: If your prepaid balance cannot cover the worst-case token allocation of an execution, the gateway answers `402 payment_required`.
- **Streaming Failures**: Mid-stream disconnections or errors emit a final SSE event containing `data: {"error": {...}}` rather than dropping the socket silently.

## Using your own Google key alongside other models

If you already maintain Google Cloud credits, you can link your own Gemini API key while continuing to route Anthropic and OpenAI calls through thelastapi:

```bash
GEMINI_API_KEY=your_key_here vibiz ai keys set google
```

When configured, any request specifying `vibiz/balanced`, Google image models, or direct `google/...` identifiers runs against your private key. The response marks `"paid_by": "your_key"` and bills $0 on thelastapi. If Google rejects your credential or suffers downtime, the request fails with `provider_failed` instead of falling back to our account. All other models, including Claude and GPT, continue to run and bill normally.

To explore all supported parameters, context thresholds, and media capabilities across providers, visit the complete [AI documentation](https://thelastapi.dev/docs/ai).

## Frequently asked questions

### Do I need separate client libraries installed for Claude and Gemini?

No. By pointing the standard OpenAI Node.js SDK at the gateway URL, you can query Claude, Gemini, and OpenAI models using the exact same methods and interfaces.

### How does the gateway handle tool calls for models from different labs?

You supply tool definitions in standard OpenAI JSON schema, and the gateway handles translation to the model provider's native format and returns standard tool call objects.

### Will the gateway route to a different model if Claude or Gemini is down?

No. The gateway will return a 503 error such as model_unavailable rather than quietly routing your query to a different model you did not request.

### How do I track costs for individual users calling these models?

Pass your application's user identifier in the `user` property of your chat completion request, which records spend per user and allows you to enforce per-user daily budgets.

---

Published 2026-11-15 · https://thelastapi.dev/blog/claude-gemini-openai-node-sdk
