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 22 or higher - Required by the current AI SDK packages
  • 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 its native Cerebras provider:
2

Configure environment variables

Create a .env.local file in your project root to store your Cerebras API key securely:
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

Create a provider instance with your Cerebras API key:
You can also import the default cerebras provider directly when CEREBRAS_API_KEY is available in the environment.
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:
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:
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: Example usage:

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:

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 current Cerebras model name such as gpt-oss-120b or gemma-4-31b
  • 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/cerebras
  • Ensure you’re using Node.js 22 or higher
If you experience rate limiting or timeouts:
  • Check your Cerebras API key quota and usage limits
  • Implement retry logic with exponential backoff
  • Reduce unnecessary output tokens and retry only transient failures
  • Monitor your request patterns and optimize batch processing

Next Steps