Skip to main content
Docs · Integrations

Vercel AI SDK

Use Inferbase from the Vercel AI SDK with the OpenAI-compatible provider: text, streaming, tools, structured output, reasoning, embeddings and the routing decision.

Browse docs · Integrations › Vercel AI SDK
On this page
  1. Install
  2. Create the provider
  3. Choose a model
  4. Generate and stream text
  5. Tools
  6. Structured output
  7. Reasoning
  8. Embeddings
  9. Read the routing decision

The AI SDK reaches Inferbase through its OpenAI-compatible provider. No Inferbase package is needed: createOpenAICompatible with the Inferbase base URL is the whole setup. Every example on this page was run against https://api.inferbase.ai/v1 with ai 7.0.185 and @ai-sdk/openai-compatible 3.0.97.

Install

bash
npm install ai @ai-sdk/openai-compatible zod

Create a key at Gateway › API keys and set it as INFERBASE_API_KEY.

Create the provider

ts
import { createOpenAICompatible } from '@ai-sdk/openai-compatible'

export const inferbase = createOpenAICompatible({
  name: 'inferbase',
  baseURL: 'https://api.inferbase.ai/v1',
  apiKey: process.env.INFERBASE_API_KEY,
  includeUsage: true,
  supportsStructuredOutputs: true,
})

includeUsage asks for token usage at the end of a stream. supportsStructuredOutputs sends a JSON schema as response_format: json_schema, which Inferbase forwards to the serving model.

Choose a model

Model idWhat it does
autoInferbase classifies each request and picks the model, on the objective of the key's routing policy.
A catalog id, such as deepseek-v4-flashPins that model. GET /v1/models lists the ids the key can use, with auto first.
config:<slug>Runs the request with a configuration: its routing policy, guardrails and request settings.

Generate and stream text

ts
import { generateText, streamText } from 'ai'
import { inferbase } from './inferbase'

const { text } = await generateText({
  model: inferbase('auto'),
  prompt: 'Say hello in one short sentence.',
})

const result = streamText({
  model: inferbase('auto'),
  prompt: 'Count from 1 to 5, comma separated.',
})
for await (const delta of result.textStream) process.stdout.write(delta)

The response's model names the model that served the request, never auto.

Tools

Tool calling and multi-step tool use work on auto and on any model that supports tools. A request that carries tools is routed only to models that support them.

ts
import { generateText, tool, stepCountIs } from 'ai'
import { z } from 'zod'

const { text } = await generateText({
  model: inferbase('auto'),
  tools: {
    weather: tool({
      description: 'Current temperature in a city, in Celsius',
      inputSchema: z.object({ city: z.string() }),
      execute: async ({ city }) => ({ city, celsius: 21 }),
    }),
  },
  stopWhen: stepCountIs(3),
  prompt: 'Use the weather tool to get the temperature in Paris, then answer in one sentence.',
})

Structured output

ts
import { generateText, Output } from 'ai'
import { z } from 'zod'

const { output } = await generateText({
  model: inferbase('auto'),
  output: Output.object({ schema: z.object({ city: z.string(), country: z.string() }) }),
  prompt: 'Name the capital of Japan and its country.',
})

Reasoning

A reasoning model's thinking arrives as reasoning parts, and its tokens are counted in usage.outputTokenDetails.reasoningTokens. reasoningEffort is read as a thinking switch: low and above ask for thinking where the model has it, none and minimal suppress it.

ts
const { text, reasoningText } = await generateText({
  model: inferbase('deepseek-v4-flash'),
  prompt: 'What is 17 * 23? Answer with the number.',
  providerOptions: { inferbase: { reasoningEffort: 'low' } },
})

On auto, Inferbase decides whether the chosen model thinks; see Chat completions.

Embeddings

ts
import { embed } from 'ai'

const { embedding } = await embed({
  model: inferbase.embeddingModel('auto'),
  value: 'routing a request to the right model',
})

auto on embeddings takes the first embedding model in the routing policy's pool. Name a model to keep vectors comparable across an index; see Embeddings and rerank.

Read the routing decision

A non-streamed response carries the decision as a routing field, and a stream sends it as the first event. A metadata extractor puts it on providerMetadata:

ts
import type { MetadataExtractor } from '@ai-sdk/openai-compatible'

const routingExtractor: MetadataExtractor = {
  extractMetadata: async ({ parsedBody }) => ({
    inferbase: { routing: parsedBody.routing ?? null },
  }),
  createStreamExtractor: () => {
    let routing: unknown = null
    return {
      processChunk: (chunk) => {
        if (chunk?.object === 'routing') routing = chunk
      },
      buildMetadata: () => ({ inferbase: { routing } }),
    }
  },
}

const inferbase = createOpenAICompatible({
  name: 'inferbase',
  baseURL: 'https://api.inferbase.ai/v1',
  apiKey: process.env.INFERBASE_API_KEY,
  includeUsage: true,
  metadataExtractor: routingExtractor,
})

const { providerMetadata } = await generateText({ model: inferbase('auto'), prompt: 'Hello' })
console.log(providerMetadata?.inferbase?.routing)

The decision names the task, the chosen model and the reasons. The response id reads the full record back from Routing decisions.

Last updated December 1, 2026.