> ## 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 20 or higher** - Download from [nodejs.org](https://nodejs.org/)
* **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 the OpenAI-compatible provider package:

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

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

      ```bash pnpm theme={null}
      pnpm add ai @ai-sdk/openai-compatible 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">
    Set up an OpenAI-compatible provider configured to use Cerebras's API endpoint. This routes all requests through Cerebras's infrastructure:

    ```typescript theme={null}
    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'
      }
    });
    ```

    The `baseURL` parameter points to Cerebras's API endpoint, while the custom header helps us track integration usage and provide better support.
  </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 { 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('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 { 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('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` | Large open-source model       | Largest model for the most demanding tasks                       |
| `zai-glm-4.7`  | Advanced 357B parameter model | Advanced 357B parameter model with strong reasoning capabilities |

Example usage:

```typescript theme={null}
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';

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

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 { 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',
  headers: {
    'X-Cerebras-3rd-Party-Integration': 'vercel-ai-sdk'
  }
});

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

<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 valid Cerebras model name: `gpt-oss-120b`, `gpt-oss-120b`, or `zai-glm-4.7`
    * 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/openai-compatible`
    * Ensure you're using TypeScript 4.5 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
    * Consider using smaller models like `gpt-oss-120b` for high-volume applications
    * 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](https://inference-docs.cerebras.ai/introduction?utm_source=3pi_vercel-ai-sdk\&utm_campaign=partner_doc) 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
* Switch to the latest model by following our [GLM4.7 migration guide](https://inference-docs.cerebras.ai/resources/glm-47-migration?utm_source=3pi_vercel-ai-sdk\&utm_campaign=partner_doc)
