Skip to main content

Cerebras

The Cerebras provider offers access to powerful language models through the Cerebras API, including their high-speed inference capabilities powered by Wafer-Scale Engines and CS-3 systems. Start with AI Gateway for models in its catalog (recommended), or use @ai-sdk/cerebras to connect directly to Cerebras.

import { generateText } from 'ai';

const { text } = await generateText({
  model: 'anthropic/claude-sonnet-5.5',
  prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});

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:

pnpm add ai
npm install ai
yarn add ai
bun add ai

Set an AI Gateway API key in your environment:

export AI_GATEWAY_API_KEY=your_api_key_here

Vercel deployments can also use OIDC authentication. See the AI Gateway provider documentation for configuration and the model catalog for available models.

The example sets providerOptions.gateway.order to ['cerebras']. This sends the request to Cerebras first, then falls back to other available providers of the same model if Cerebras is unavailable or the request fails. order sets a preference; it does not restrict fallback to Cerebras. See provider ordering.

The dropdown lists models served by Cerebras through Gateway with a corresponding direct-provider example. Provider availability depends on the model and your team's Provider Allowlist.

Using @ai-sdk/cerebras

For direct access to Cerebras, install ai and @ai-sdk/cerebras:

pnpm add ai @ai-sdk/cerebras
npm install ai @ai-sdk/cerebras
yarn add ai @ai-sdk/cerebras
bun add ai @ai-sdk/cerebras

Set your Cerebras API credential in the CEREBRAS_API_KEY environment variable:

export CEREBRAS_API_KEY=your_api_key_here

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

Note

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

API keys can be obtained from the Cerebras Platform.

Provider Instance

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

import { cerebras } from '@ai-sdk/cerebras';

For custom configuration, you can import createCerebras and create a provider instance with your settings:

import { createCerebras } from '@ai-sdk/cerebras';

const cerebras = createCerebras({
  apiKey: process.env.CEREBRAS_API_KEY ?? '',
});

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

  • baseURL string

    Use a different URL prefix for API calls. The default prefix is https://api.cerebras.ai/v1.

  • apiKey string

    API key that is being sent using the Authorization header. It defaults to the CEREBRAS_API_KEY environment variable.

  • headers Record<string,string>

    Custom headers to include in the requests.

  • fetch (input: RequestInfo, init?: RequestInit) => Promise<Response>

    Custom fetch implementation.

Language Models

You can create language models using a provider instance:

import { cerebras } from '@ai-sdk/cerebras';
import { generateText } from 'ai';

const { text } = await generateText({
  model: cerebras('gpt-oss-120b'),
  prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});

Cerebras language models can be used in the streamText function (see AI SDK Core).

You can create Cerebras language models using a provider instance. The first argument is the model ID, e.g. gpt-oss-120b:

const model = cerebras('gpt-oss-120b');

You can also use the .languageModel() and .chat() methods:

const languageModel = cerebras.languageModel('gpt-oss-120b');
const chatModel = cerebras.chat('gpt-oss-120b');

Reasoning Models

Cerebras offers reasoning models including gpt-oss-120b and gemma-4-31b that generate intermediate thinking tokens before their final response. The reasoning output is streamed through the standard AI SDK reasoning parts.

For gpt-oss-120b, you can control the reasoning depth using the reasoningEffort provider option:

import { cerebras } from '@ai-sdk/cerebras';
import { streamText } from 'ai';

const result = streamText({
  model: cerebras('gpt-oss-120b'),
  providerOptions: {
    cerebras: {
      reasoningEffort: 'medium',
    },
  },
  prompt: 'How many "r"s are in the word "strawberry"?',
});

for await (const part of result.stream) {
  if (part.type === 'reasoning') {
    console.log('Reasoning:', part.text);
  } else if (part.type === 'text-delta') {
    process.stdout.write(part.textDelta);
  }
}

See AI SDK UI: Chatbot for more details on how to integrate reasoning into your chatbot.

Provider Options

The following optional provider options are available for Cerebras language models:

  • parallelToolCalls boolean

    Whether to enable parallel function calling during tool use. Defaults to true.

  • logprobs boolean

    Whether to return log probabilities for generated tokens. Defaults to false.

  • topLogprobs number

    Number of most likely tokens to return at each token position. Accepts values from 0 through 20. Requires logprobs to be true.

  • logitBias Record<string, number>

    Maps token IDs to bias values from -100 through 100.

  • serviceTier 'auto' | 'default' | 'flex' | 'priority'

    Controls request priority. Availability depends on your account and endpoint.

  • reasoningEffort 'none' | 'low' | 'medium' | 'high'

    Controls the amount of reasoning performed by supported models. Supported values and defaults depend on the model.

  • reasoningFormat 'none' | 'parsed' | 'text_parsed' | 'raw' | 'hidden'

    Controls how reasoning content appears in the response. Format support depends on the model.

  • prediction { type: 'content'; content: string | Array<{ type: 'text'; text: string }> }

    Supplies predicted output that can accelerate requests where most of the response is already known.

  • promptCacheKey string

    Routes related requests to the same prompt cache. The value can contain up to 1024 characters and requires account-level enablement.

  • user string

    A unique identifier representing your end-user, which can help with monitoring and abuse detection.

  • strictJsonSchema boolean

    Whether to use strict JSON schema validation. When true, the model uses constrained decoding to guarantee schema compliance. Defaults to true.

For example:

import {
  cerebras,
  type CerebrasLanguageModelChatOptions,
} from '@ai-sdk/cerebras';
import { generateText } from 'ai';

const result = await generateText({
  model: cerebras('gpt-oss-120b'),
  prompt: 'Explain why the sky is blue.',
  providerOptions: {
    cerebras: {
      reasoningEffort: 'low',
      reasoningFormat: 'parsed',
      promptCacheKey: 'conversation-123',
    } satisfies CerebrasLanguageModelChatOptions,
  },
});

Model Capabilities

ModelImage InputObject GenerationTool UsageTool StreamingReasoning
gpt-oss-120b✗✓✓✓✓
gemma-4-31b✓✓✓✓✓