Skip to main content

What is Vercel AI SDK?

The Vercel AI SDK is a powerful TypeScript toolkit for building AI-powered applications with React, Next.js, Vue, Svelte, and more. It provides a unified interface for working with different AI providers, streaming responses, and building interactive AI experiences. Learn more at sdk.vercel.ai. By integrating Cerebras with the Vercel AI SDK, you can leverage ultra-fast inference speeds while using familiar AI SDK patterns and components.

Prerequisites

Before you begin, ensure you have:
  • Cerebras API Key - Get a free API key here
  • Node.js 20 or higher - Download from nodejs.org
  • npm, yarn, or pnpm - Package manager for installing dependencies

Configure Vercel AI SDK with Cerebras

1

Install required dependencies

Install the Vercel AI SDK and the OpenAI-compatible provider package:
npm install ai @ai-sdk/openai-compatible zod
2

Configure environment variables

Create a .env.local file in your project root to store your Cerebras API key securely:
CEREBRAS_API_KEY=your-cerebras-api-key-here
If you’re using Next.js, avoid prefixing with NEXT_PUBLIC_ to keep your API key private and prevent browser exposure.
3

Create a Cerebras provider instance

Set up an OpenAI-compatible provider configured to use Cerebras’s API endpoint. This routes all requests through Cerebras’s infrastructure:
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';

const cerebras = createOpenAICompatible({
  name: 'cerebras',
  apiKey: process.env.CEREBRAS_API_KEY,
  baseURL: 'https://api.cerebras.ai/v1'
});
The baseURL parameter points to Cerebras’s API endpoint, while the custom header helps us track integration usage and provide better support.
4

Generate streaming text responses

Use the streamText function to generate streaming text responses. This is perfect for chat interfaces where you want to show responses as they’re generated:
import { streamText } from 'ai';
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';

const cerebras = createOpenAICompatible({
  name: 'cerebras',
  apiKey: process.env.CEREBRAS_API_KEY,
  baseURL: 'https://api.cerebras.ai/v1',
  headers: {
    'X-Cerebras-3rd-Party-Integration': 'Vercel AI SDK'
  }
});

const result = await streamText({
  model: cerebras('llama-3.3-70b'),
  prompt: 'Explain the concept of quantum computing in simple terms.',
});

// Stream the response
for await (const textPart of result.textStream) {
  process.stdout.write(textPart);
}
The streamText function returns an async iterable that yields text chunks as they’re generated, providing a smooth user experience with Cerebras’s ultra-fast inference.
5

Generate text responses

Use the generateText function for non-streaming text generation:
import { generateText } from 'ai';
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';

const cerebras = createOpenAICompatible({
  name: 'cerebras',
  apiKey: process.env.CEREBRAS_API_KEY,
  baseURL: 'https://api.cerebras.ai/v1',
  headers: {
    'X-Cerebras-3rd-Party-Integration': 'Vercel AI SDK'
  }
});

const result = await generateText({
  model: cerebras('llama-3.3-70b'),
  prompt: 'Write a haiku about programming.',
});

console.log(result.text);
The generateText function returns the complete response at once, which is useful for batch processing or when you don’t need streaming.

Available Models

You can use any of the following Cerebras models with the Vercel AI SDK:
ModelDescriptionBest For
llama-3.3-70bMeta’s latest Llama modelBest for complex reasoning, long-form content, and tasks requiring deep understanding
qwen-3-32bQwen’s powerful modelBalanced performance for general-purpose applications
llama3.1-8bSmaller, faster modelFastest option for simple tasks and high-throughput scenarios
gpt-oss-120bLarge open-source modelLargest model for the most demanding tasks
zai-glm-4.6Advanced 357B parameter modelAdvanced 357B parameter model with strong reasoning capabilities
Example usage:
const model = cerebras('llama-3.3-70b');

Advanced Features

Tool Calling

The Vercel AI SDK supports tool calling (function calling) with Cerebras models. This allows the model to call external functions and use their results:
import { generateText, tool } from 'ai';
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { z } from 'zod';

const cerebras = createOpenAICompatible({
  name: 'cerebras',
  apiKey: process.env.CEREBRAS_API_KEY,
  baseURL: 'https://api.cerebras.ai/v1'
});

const result = await generateText({
  model: cerebras('llama-3.3-70b'),
  tools: {
    weather: tool({
      description: 'Get the weather in a location',
      parameters: z.object({
        location: z.string().describe('The location to get the weather for'),
      }),
      execute: async ({ location }) => ({
        location,
        temperature: 72 + Math.floor(Math.random() * 21) - 10,
      }),
    }),
  },
  prompt: 'What is the weather in San Francisco?',
});

console.log(result.text);

Troubleshooting

If you see an error about missing API keys:
  • Verify your .env.local file contains CEREBRAS_API_KEY
  • Restart your development server after adding environment variables
  • In production, ensure environment variables are set in your deployment platform (Vercel, AWS, etc.)
  • Check that you’re not accidentally using NEXT_PUBLIC_ prefix
If you receive a “model not found” error:
  • Check that you’re using a valid Cerebras model name: llama-3.3-70b, qwen-3-32b, llama3.1-8b, or gpt-oss-120b
  • Ensure you’re using the correct format: cerebras('model-name')
  • Verify your API key has access to the requested model
  • Try a different model to isolate the issue
If streaming responses aren’t displaying:
  • Ensure you’re using streamText instead of generateText for streaming
  • Check that your API route returns result.toDataStreamResponse()
  • Verify your client component uses the useChat or useCompletion hook correctly
  • Check browser console for network errors
  • Ensure your hosting platform supports streaming responses
If you encounter TypeScript errors:
  • Make sure you have @types/node installed: npm install -D @types/node
  • Verify your tsconfig.json includes the necessary compiler options
  • Check that all AI SDK packages are up to date: npm update ai @ai-sdk/openai-compatible
  • Ensure you’re using TypeScript 4.5 or higher
If you experience rate limiting or timeouts:
  • Check your Cerebras API key quota and usage limits
  • Implement retry logic with exponential backoff
  • Consider using smaller models like llama3.1-8b for high-volume applications
  • Monitor your request patterns and optimize batch processing

Next Steps