---
title: Baseten
description: Learn how to use Baseten with AI Gateway and the direct provider.
url: "https://ai-sdk.dev/providers/ai-sdk-providers/baseten"
docs_index: /llms.txt
---

> For an index of all documentation, see [/llms.txt](/llms.txt).

[Baseten](https://baseten.co/) is an inference platform for serving frontier, enterprise-grade opensource AI models via their [API](https://docs.baseten.co/overview).
Start with [AI Gateway](/providers/ai-sdk-providers/ai-gateway) for models in its catalog (recommended), or use `@ai-sdk/baseten` to connect directly to Baseten.

```ts provider="baseten" model="openai/gpt-oss-120b"
import { generateText } from 'ai';

const { text } = await generateText({
  model: "openai/gpt-oss-120b",
  prompt: 'Write a vegetarian lasagna recipe for 4 people.',
  providerOptions: {
    gateway: {
      order: ['baseten'],
    },
  },
});

console.log(text);
```

## Using AI Gateway

The AI SDK uses AI Gateway by default for model strings. Gateway provides access to models across providers through a single API key, with usage and cost tracking. Install `ai`:

```bash
pnpm add ai
```

Set an [AI Gateway API key](https://vercel.com/docs/ai-gateway/getting-started) in your environment:

```bash
export AI_GATEWAY_API_KEY=your_api_key_here
```

Vercel deployments can also use [OIDC authentication](/providers/ai-sdk-providers/ai-gateway#oidc-authentication-vercel-deployments).
See the [AI Gateway provider documentation](/providers/ai-sdk-providers/ai-gateway) for configuration and the [model catalog](https://vercel.com/ai-gateway/models) for available models.

The example sets `providerOptions.gateway.order` to `['baseten']`. This sends the request to Baseten first, then falls back to other available providers of the same model if Baseten is unavailable or the request fails. `order` sets a preference; it does not restrict fallback to Baseten. See [provider ordering](https://vercel.com/docs/ai-gateway/models-and-providers/provider-filtering-and-ordering).

The dropdown lists models served by Baseten through Gateway with a corresponding direct-provider example. Provider availability depends on the model and your team's [Provider Allowlist](https://vercel.com/docs/ai-gateway/models-and-providers/provider-filtering-and-ordering).

## Using `@ai-sdk/baseten`

For direct access to Baseten, install `ai` and `@ai-sdk/baseten`:

```bash
pnpm add ai @ai-sdk/baseten
```

Set your Baseten API credential in the `BASETEN_API_KEY` environment variable:

```bash
export BASETEN_API_KEY=your_api_key_here
```

AI Gateway does not expose every API in `@ai-sdk/baseten`. Use the direct provider for provider-specific APIs or custom connection settings.

The following sections document the direct `@ai-sdk/baseten` provider,
including its model factories and settings. Gateway model and feature
availability can differ.

## Provider Instance

You can import the default provider instance `baseten` from `@ai-sdk/baseten`:

```ts
import { baseten } from '@ai-sdk/baseten';
```

If you need a customized setup, you can import `createBaseten` from `@ai-sdk/baseten`
and create a provider instance with your settings:

```ts
import { createBaseten } from '@ai-sdk/baseten';

const baseten = createBaseten({
  apiKey: process.env.BASETEN_API_KEY ?? '',
});
```

You can use the following optional settings to customize the Baseten provider instance:

- **baseURL** *string*

  Use a different URL prefix for API calls, e.g. to use proxy servers.
  The default prefix is `https://inference.baseten.co/v1`.

- **apiKey** *string | PromiseLike\<string> | (() => string | PromiseLike\<string>)*

  API key that is being sent using the `Authorization` header. It defaults to
  the `BASETEN_API_KEY` environment variable. It is recommended you set the environment variable using `export` so you do not need to include the field every time.
  You can grab your Baseten API Key [here](https://app.baseten.co/settings/api_keys)

  A function is called before each request and may return a promise. Use it to
  supply refreshed credentials; a promise value resolves to the same key each time.

- **modelURL** *string*

  Custom model URL for specific models (chat or embeddings). If not provided,
  the default Model APIs will be used.

- **headers** *Record\<string,string>*

  Custom headers to include in the requests.

- **performanceClient** *PerformanceClient constructor*

  Opt in to Baseten's native performance client for embeddings, for client-side
  batching and request hedging. Pass the `PerformanceClient` constructor from
  `@basetenlabs/performance-client`, which you install yourself. When omitted,
  embeddings use plain HTTP. See
  [Native performance client](#native-performance-client-optional).

- **fetch** *(input: RequestInfo, init?: RequestInit) => Promise\<Response>*

  Custom [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) implementation.

## Model APIs

You can select [Baseten models](https://www.baseten.co/products/model-apis/) using a provider instance.
The first argument is the model id, e.g. `'moonshotai/Kimi-K2-Instruct-0905'`: The complete supported models under Model APIs can be found [here](https://docs.baseten.co/development/model-apis/overview#supported-models).

```ts
const model = baseten('moonshotai/Kimi-K2-Instruct-0905');
```

### Example

You can use Baseten language models to generate text with the `generateText` function:

```ts
import { baseten } from '@ai-sdk/baseten';
import { generateText } from 'ai';

const { text } = await generateText({
  model: baseten('moonshotai/Kimi-K2-Instruct-0905'),
  prompt: 'What is the meaning of life? Answer in one sentence.',
});
```

Baseten language models can also be used in the `streamText` function
(see [AI SDK Core](/docs/ai-sdk-core)).

On Baseten Model APIs, `deepseek-ai/DeepSeek-V4-Pro-0813` can return an empty
answer when JSON mode and reasoning are combined. When reasoning is enabled
(the default), the provider puts JSON instructions and your schema in a system
message and omits `response_format`. It emits a compatibility warning because
the server does not constrain the output; `Output.object` still validates the
result. Set `reasoning: 'none'` to use native JSON Schema enforcement instead.

## Dedicated Models

Baseten supports dedicated model URLs for both chat and embedding models. You have to specify a `modelURL` when creating the provider:

### OpenAI-Compatible Endpoints (`/sync/v1`)

For models deployed with Baseten's OpenAI-compatible endpoints:

```ts
import { createBaseten } from '@ai-sdk/baseten';

const baseten = createBaseten({
  modelURL: 'https://model-{MODEL_ID}.api.baseten.co/sync/v1',
});
// No modelId is needed because we specified modelURL
const model = baseten();
const { text } = await generateText({
  model: model,
  prompt: 'Say hello from a Baseten chat model!',
});
```

### `/predict` Endpoints

`/predict` endpoints are currently NOT supported for chat models. You must use `/sync/v1` endpoints for chat functionality.

## Embedding Models

You can create models that call the Baseten embeddings API using the `.embeddingModel()` factory method. Baseten Embeddings Inference deployments are OpenAI-compatible, so embeddings use plain HTTP by default with no extra dependencies.

**Important:** Embedding models require a dedicated deployment with a custom
`modelURL`. Unlike chat models, embeddings cannot use Baseten's default Model
APIs and must specify a dedicated model endpoint.

```ts
import { createBaseten } from '@ai-sdk/baseten';
import { embed, embedMany } from 'ai';

const baseten = createBaseten({
  modelURL: 'https://model-{MODEL_ID}.api.baseten.co/sync',
});

const embeddingModel = baseten.embeddingModel();

// Single embedding
const { embedding } = await embed({
  model: embeddingModel,
  value: 'sunny day at the beach',
});

// Batch embeddings
const { embeddings } = await embedMany({
  model: embeddingModel,
  values: [
    'sunny day at the beach',
    'rainy afternoon in the city',
    'snowy mountain peak',
  ],
});
```

Each request sends at most 128 values. `embedMany` splits larger inputs into
chunks of that size and runs them in parallel, so you can pass as many values as
you like.

### Endpoint Support for Embeddings

**Supported:**

- `/sync` endpoints (`/v1/embeddings` is appended for you)
- `/sync/v1` endpoints

**Not Supported:**

- `/predict` endpoints

### Native performance client (optional)

Baseten also publishes `@basetenlabs/performance-client`, a native client that
adds client-side batching and request hedging on top of the server-side dynamic
batching your deployment already does. It is **not** installed by default: it is
a native addon, so it cannot load in edge runtimes and bundlers cannot resolve
its platform binaries.

To use it, install it yourself and pass the constructor:

```bash
npm i @basetenlabs/performance-client
```

```ts
import { createBaseten } from '@ai-sdk/baseten';
import { PerformanceClient } from '@basetenlabs/performance-client';

const baseten = createBaseten({
  modelURL:
    'https://model-{MODEL_ID}.api.baseten.co/environments/production/sync',
  performanceClient: PerformanceClient,
});
```

When you opt in, the client handles batching itself, so values are sent in a
single call rather than being split at 128.

## Decision Models

Use `baseten.decisionModel()` with
[`experimental_decide`](/docs/reference/ai-sdk-core/decide) to call Baseten's
[decision models](https://docs.baseten.co/inference/model-apis/decision-models)
through the Model APIs
[System One endpoint](https://docs.baseten.co/reference/inference-api/system-one).
This API is experimental and may change in patch releases.

```ts
import { baseten } from '@ai-sdk/baseten';
import { experimental_decide as decide } from 'ai';

const result = await decide({
  model: baseten.decisionModel('decision-models/baseten-bison-latest'),
  state:
    'I was charged twice for my subscription. Please refund the duplicate.',
  questions: {
    team: {
      type: 'choice',
      instructions: 'Which team should handle this ticket?',
      criteria: {
        billing: 'Charges, refunds, and payment methods',
        engineering: 'Bugs and outages',
      },
    },
    refund: {
      type: 'boolean',
      instructions: 'Is the customer asking for a refund?',
    },
    urgency: {
      type: 'score',
      instructions: 'How urgent is this ticket?',
      criteria: ['Can wait', 'Handle today', 'Blocking revenue'],
    },
  },
});

result.answers.refund.probability; // P(true), from 0 to 1
result.answers.team.choice; // 'billing' | 'engineering'
result.answers.urgency.score; // Expected zero-based level, from 0 to 2
```

The provider maps SDK `boolean` questions to Baseten's native `noul` type and
returns Choice and Score probability distributions without rounding. Decision
models always use the Model APIs endpoint and ignore `modelURL`.

### Images

Add image file parts to `state`. When the state contains an image, the provider
sends it as a conversation (`{ messages: [...] }`) with a single user message
whose content holds the text, JSON, and image parts in request order. JSON parts
are sent as text.

```ts
import { readFileSync } from 'node:fs';

const result = await decide({
  model: baseten.decisionModel('decision-models/baseten-groundhog-latest'),
  state: [
    { type: 'text', text: 'A screenshot from a customer report.' },
    {
      type: 'file',
      mediaType: 'image/png',
      data: readFileSync('screenshot.png'),
    },
  ],
  questions: {
    error: {
      type: 'boolean',
      instructions: 'Is an error message visible?',
    },
  },
});
```

Images must be JPEG, PNG, or WebP and are sent as base64 data URLs. For URL file
state, `experimental_decide` requires `experimental_download` and downloads the
image before calling the provider, so it is also sent inline. URL file data
passed to the model directly is forwarded as is; Baseten accepts only HTTPS URLs
on Amazon S3, Google Cloud Storage, and Azure Blob Storage and rejects others.

A single text part is sent as a string and a single JSON part as structured
JSON, including a conversation object you build yourself. Other combinations of
text and JSON parts are joined with newlines.

### Confidence, legends, and usage

Baseten's confidence values and Score legends are available in
`providerMetadata.baseten`, keyed by question ID. Confidence is distinct from the
selected option's probability.

```ts
const metadata = result.providerMetadata?.baseten;
console.log(metadata?.confidence); // Choice and Score confidence by question ID
console.log(metadata?.legend); // Score index-to-description maps by question ID
console.log(result.usage.inputTokens); // Includes image tokens
console.log(result.usage.outputTokens); // One token per question
```

### Decision limits

The provider validates these limits before sending the decision request:

| Input          | Limit                                                                                                           |
| -------------- | --------------------------------------------------------------------------------------------------------------- |
| Choice options | 1–255                                                                                                           |
| Score levels   | 1–10                                                                                                            |
| Options        | At most 512 across all questions: each Choice option and Score level counts as one, each yes/no question as two |
| Images         | At most 16                                                                                                      |

Box questions, audio, and video are not supported. Each model's context limit is
enforced by the API, which rejects prompts that exceed it instead of truncating
them. Baseten decision models do not accept provider-specific options,
`maxOutputTokens`, or `reasoning`; these produce unsupported warnings and are not
sent to the API.

### Decision Model Capabilities

| Model                                        | Boolean | Choice | Score | Box | Image Input | Reasoning | Max Output Tokens |
| -------------------------------------------- | ------- | ------ | ----- | --- | ----------- | --------- | ----------------- |
| `decision-models/baseten-bison-latest`       | ✓       | ✓      | ✓     | ✗   | ✓           | ✗         | ✗                 |
| `decision-models/baseten-groundhog-latest`   | ✓       | ✓      | ✓     | ✗   | ✓           | ✗         | ✗                 |
| `decision-models/baseten-mammoth-27b-latest` | ✓       | ✓      | ✓     | ✗   | ✓           | ✗         | ✗                 |
| `decision-models/baseten-elephant-9b-latest` | ✓       | ✓      | ✓     | ✗   | ✓           | ✗         | ✗                 |
| `decision-models/baseten-joey-2b-latest`     | ✓       | ✓      | ✓     | ✗   | ✓           | ✗         | ✗                 |

## Error Handling

The Baseten provider includes built-in error handling for common API errors:

```ts
import { baseten } from '@ai-sdk/baseten';
import { generateText } from 'ai';

try {
  const { text } = await generateText({
    model: baseten('moonshotai/Kimi-K2-Instruct-0905'),
    prompt: 'Hello, world!',
  });
} catch (error) {
  console.error('Baseten API error:', error.message);
}
```

### Common Error Scenarios

```ts
// Embeddings require a modelURL
try {
  baseten.embeddingModel();
} catch (error) {
  // Error: "No model URL provided for embeddings. Please set modelURL option for embeddings."
}

// /predict endpoints are not supported for chat models
try {
  const baseten = createBaseten({
    modelURL:
      'https://model-{MODEL_ID}.api.baseten.co/environments/production/predict',
  });
  baseten(); // This will throw an error
} catch (error) {
  // Error: "Not supported. You must use a /sync/v1 endpoint for chat models."
}

// /sync/v1 endpoints are now supported for embeddings
const baseten = createBaseten({
  modelURL:
    'https://model-{MODEL_ID}.api.baseten.co/environments/production/sync/v1',
});
const embeddingModel = baseten.embeddingModel(); // This works fine!

// /predict endpoints are not supported for embeddings
try {
  const baseten = createBaseten({
    modelURL:
      'https://model-{MODEL_ID}.api.baseten.co/environments/production/predict',
  });
  baseten.embeddingModel(); // This will throw an error
} catch (error) {
  // Error: "Not supported. You must use a /sync or /sync/v1 endpoint for embeddings."
}

// Image models are not supported
try {
  baseten.imageModel('test-model');
} catch (error) {
  // Error: NoSuchModelError for imageModel
}
```

For more information about Baseten models and deployment options, see the
[Baseten documentation](https://docs.baseten.co/).

## Model Capabilities

Use `basetenModelCapabilities(modelId)` from `@ai-sdk/baseten` to look up a model's
declared capabilities and provider options, grouped by endpoint.

## Provider Option Reference

The following option definitions are generated from the provider catalog.

| Operation | Option             | Description                                                                                                                                                                                                    |
| --------- | ------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| language  | `user`             | A unique identifier representing your end-user, which can help providers to monitor and detect abuse.                                                                                                          |
| language  | `reasoningEffort`  | Reasoning effort for reasoning models. Defaults to `medium`. Set to `null` to send `"reasoning_effort": null`, for providers that treat an explicit `null` differently from an omitted field.                  |
| language  | `textVerbosity`    | Controls the verbosity of the generated text. Defaults to `medium`.                                                                                                                                            |
| language  | `strictJsonSchema` | Whether to use strict JSON schema validation. When true, the model uses constrained decoding to guarantee schema compliance. Only used when the provider supports structured outputs and a schema is provided. |
| language  | `echo`             | Echo back the prompt in addition to the completion.                                                                                                                                                            |
| language  | `logitBias`        | Modify the likelihood of specified tokens appearing in the completion. Accepts a JSON object that maps tokens (specified by their token ID in the GPT tokenizer) to an associated bias value from -100 to 100. |
| language  | `suffix`           | The suffix that comes after a completion of inserted text.                                                                                                                                                     |
| embedding | `dimensions`       | The number of dimensions the resulting output embeddings should have. Only supported in text-embedding-3 and later models.                                                                                     |
| embedding | `user`             | A unique identifier representing your end-user, which can help providers to monitor and detect abuse.                                                                                                          |

---

For a semantic overview of all documentation, see [/sitemap.md](/sitemap.md)

For an index of all available documentation, see [/llms.txt](/llms.txt)

For agent-facing discovery, including API and MCP surfaces, see [/agents.md](/agents.md)