> ## Documentation Index
> Fetch the complete documentation index at: https://inference-docs.cerebras.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Get Started with Vercel AI SDK

> Learn how to integrate Cerebras's ultra-fast inference with the Vercel AI SDK for building AI-powered applications with streaming responses and structured outputs.

## 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](https://sdk.vercel.ai/docs?utm_source=3pi_vercel-ai-sdk\&utm_campaign=partner_doc).

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](https://cloud.cerebras.ai/?utm_source=3pi_vercel-ai-sdk\&utm_campaign=partner_doc)
* **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

<Steps>
  <Step title="Install required dependencies">
    Install the Vercel AI SDK and its native Cerebras provider:

    <CodeGroup>
      ```bash npm theme={null}
      npm install ai @ai-sdk/cerebras zod
      ```

      ```bash yarn theme={null}
      yarn add ai @ai-sdk/cerebras zod
      ```

      ```bash pnpm theme={null}
      pnpm add ai @ai-sdk/cerebras zod
      ```
    </CodeGroup>
  </Step>

  <Step title="Configure environment variables">
    Create a `.env.local` file in your project root to store your Cerebras API key securely:

    ```bash theme={null}
    CEREBRAS_API_KEY=your-cerebras-api-key-here
    ```

    <Note>
      If you're using Next.js, avoid prefixing with `NEXT_PUBLIC_` to keep your API key private and prevent browser exposure.
    </Note>
  </Step>

  <Step title="Create a Cerebras provider instance">
    Create a provider instance with your Cerebras API key:

    ```typescript theme={null}
    import { createCerebras } from '@ai-sdk/cerebras';

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

    You can also import the default `cerebras` provider directly when `CEREBRAS_API_KEY` is available in the environment.
  </Step>

  <Step title="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:

    ```typescript theme={null}
    import { streamText } from 'ai';
    import { createCerebras } from '@ai-sdk/cerebras';

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

    const result = await streamText({
      model: cerebras('gpt-oss-120b'),
      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.
  </Step>

  <Step title="Generate text responses">
    Use the `generateText` function for non-streaming text generation:

    ```typescript theme={null}
    import { generateText } from 'ai';
    import { createCerebras } from '@ai-sdk/cerebras';

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

    const result = await generateText({
      model: cerebras('gpt-oss-120b'),
      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.
  </Step>
</Steps>

## Available Models

You can use any of the following Cerebras models with the Vercel AI SDK:

| Model          | Description          | Best For                           |
| -------------- | -------------------- | ---------------------------------- |
| `gpt-oss-120b` | 120B model           | General text, coding, and tool use |
| `gemma-4-31b`  | 31B multimodal model | Text and image input               |

Example usage:

```typescript theme={null}
import { createCerebras } from '@ai-sdk/cerebras';

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

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

## 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:

```typescript theme={null}
import { generateText, tool } from 'ai';
import { createCerebras } from '@ai-sdk/cerebras';
import { z } from 'zod';

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

const result = await generateText({
  model: cerebras('gpt-oss-120b'),
  tools: {
    weather: tool({
      description: 'Get the weather in a location',
      inputSchema: 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

<AccordionGroup>
  <Accordion title="API Key Not Found">
    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
  </Accordion>

  <Accordion title="Model Not Found">
    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
  </Accordion>

  <Accordion title="Streaming Not Working">
    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
  </Accordion>

  <Accordion title="TypeScript Errors">
    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
  </Accordion>

  <Accordion title="Rate Limiting or Timeout Errors">
    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
  </Accordion>
</AccordionGroup>

## Next Steps

* Explore the [Vercel AI SDK documentation](https://sdk.vercel.ai/docs?utm_source=3pi_vercel-ai-sdk\&utm_campaign=partner_doc) for more features and examples
* Try different [Cerebras models](/models/overview) to find the best fit for your use case
* Check out [example applications](https://sdk.vercel.ai/examples?utm_source=3pi_vercel-ai-sdk\&utm_campaign=partner_doc) built with the AI SDK
