# Getting Started

This quickstart will walk you through making an AI
model request with Vercel's [AI Gateway](https://vercel.com/ai-gateway).
While this guide uses the [AI SDK](https://ai-sdk.dev),
you can also integrate with the [OpenAI SDK](/docs/ai-gateway/sdks-and-apis/openai-compat),
[Anthropic SDK](/docs/ai-gateway/sdks-and-apis/anthropic-compat),
[OpenResponses API](/docs/ai-gateway/sdks-and-apis/openresponses),
or other [community frameworks](/docs/ai-gateway/ecosystem/framework-integrations).

- ### Set up your application
  Start by creating a new directory using the `mkdir` command.
  Change into your new directory and then run the `pnpm init`
  command, which will create a `package.json`.
  ```bash filename="Terminal"
  mkdir demo
  cd demo
  pnpm init
  ```

- ### Install dependencies
  Install the AI SDK package, `ai`, along with other necessary dependencies.
  #### npm
  ```bash filename="Terminal"
  npm install ai dotenv @types/node tsx typescript
  ```
  #### yarn
  ```bash filename="Terminal"
  yarn add ai dotenv @types/node tsx typescript
  ```
  #### pnpm
  ```bash filename="Terminal"
  pnpm add ai dotenv @types/node tsx typescript
  ```
  #### bun
  ```bash filename="Terminal"
  bun add ai dotenv @types/node tsx typescript
  ```
  `dotenv` is used to access environment variables
  (your AI Gateway API key) within your application. The `tsx` package is a TypeScript runner
  that allows you to run your TypeScript code. The `typescript` package is the TypeScript compiler.
  The `@types/node` package is the TypeScript definitions for the Node.js API.

- ### Set up your API key
  Go to the [AI Gateway API Keys page](https://vercel.com/d?to=%2F%5Bteam%5D%2F%7E%2Fai-gateway%2Fapi-keys\&title=AI+Gateway+API+Keys) in your Vercel dashboard and click **Create key** to generate a new API key.

  Once you have the API key, create a `.env.local` file and save your API key:
  ```bash filename=".env.local"
  AI_GATEWAY_API_KEY=your_ai_gateway_api_key
  ```
  > **💡 Note:** Instead of using an API key, you can use [OIDC
  > tokens](/docs/ai-gateway/authentication#oidc-token-authentication) to
  > authenticate your requests.
  The AI Gateway provider will default to using the `AI_GATEWAY_API_KEY` environment variable.

- ### Create and run your script
  Create an `index.ts` file in the root of your project and add the following code:
  ```typescript filename="index.ts" {6}
  import { streamText } from 'ai';
  import 'dotenv/config';

  async function main() {
    const result = streamText({
      model: 'openai/gpt-5.2',
      prompt: 'Invent a new holiday and describe its traditions.',
    });

    for await (const textPart of result.textStream) {
      process.stdout.write(textPart);
    }

    console.log();
    console.log('Token usage:', await result.usage);
    console.log('Finish reason:', await result.finishReason);
  }

  main().catch(console.error);
  ```
  Now, run your script:
  ```bash filename="Terminal"
  pnpm tsx index.ts
  ```
  You should see the AI model's response to your prompt.

- ### Next steps
  Continue with the [AI SDK documentation](https://ai-sdk.dev/getting-started) to learn about configuration options, [provider and model routing with fallbacks](/docs/ai-gateway/models-and-providers/provider-options), and integration examples.

## Using OpenAI SDK

The AI Gateway provides OpenAI-compatible API endpoints that allow you to use existing OpenAI client libraries and tools with the AI Gateway.

The OpenAI-compatible API includes:

- **Model Management**: List and retrieve the available models
- **Chat Completions**: Create chat completions that support streaming, images, and file attachments
- **Tool Calls**: Call functions with automatic or explicit tool selection
- **Existing Tool Integration**: Use your existing OpenAI client libraries and tools without needing modifications
- **Multiple Languages**: Use the OpenAI SDK in TypeScript and Python, or any language via the REST API

#### TypeScript

```typescript filename="index.ts"
import OpenAI from 'openai';
import 'dotenv/config';

const client = new OpenAI({
  apiKey: process.env.AI_GATEWAY_API_KEY,
  baseURL: 'https://ai-gateway.vercel.sh/v1',
});

async function main() {
  const response = await client.chat.completions.create({
    model: 'anthropic/claude-sonnet-4.5',
    messages: [
      {
        role: 'user',
        content: 'Invent a new holiday and describe its traditions.',
      },
    ],
  });

  console.log(response.choices[0].message.content);
}

main().catch(console.error);
```

#### Python

```python filename="main.py"
import os
from openai import OpenAI
from dotenv import load_dotenv

load_dotenv()

client = OpenAI(
    api_key=os.getenv('AI_GATEWAY_API_KEY'),
    base_url='https://ai-gateway.vercel.sh/v1',
)

response = client.chat.completions.create(
    model='anthropic/claude-sonnet-4.5',
    messages=[
        {
            'role': 'user',
            'content': 'Invent a new holiday and describe its traditions.',
        },
    ],
)

print(response.choices[0].message.content)
```

Learn more about using the OpenAI SDK with the AI Gateway in the [OpenAI-Compatible API page](/docs/ai-gateway/sdks-and-apis/openai-compat).

## Using Anthropic SDK

The AI Gateway provides Anthropic-compatible API endpoints that allow you to use the Anthropic SDK and tools like Claude Code with the AI Gateway.

The Anthropic-compatible API includes:

- **Messages API**: Create messages with support for streaming and multi-turn conversations
- **Tool Calls**: Call functions with automatic or explicit tool selection
- **Extended Thinking**: Enable extended thinking for complex reasoning tasks
- **File Attachments**: Attach files and images to your messages
- **Multiple Languages**: Use the Anthropic SDK in TypeScript and Python, or any language via the REST API

#### TypeScript

```typescript filename="index.ts"
import Anthropic from '@anthropic-ai/sdk';
import 'dotenv/config';

const client = new Anthropic({
  apiKey: process.env.AI_GATEWAY_API_KEY,
  baseURL: 'https://ai-gateway.vercel.sh',
});

async function main() {
  const message = await client.messages.create({
    model: 'anthropic/claude-sonnet-4.5',
    max_tokens: 1024,
    messages: [
      {
        role: 'user',
        content: 'Invent a new holiday and describe its traditions.',
      },
    ],
  });

  console.log(message.content[0].text);
}

main().catch(console.error);
```

#### Python

```python filename="main.py"
import os
import anthropic
from dotenv import load_dotenv

load_dotenv()

client = anthropic.Anthropic(
    api_key=os.getenv('AI_GATEWAY_API_KEY'),
    base_url='https://ai-gateway.vercel.sh',
)

message = client.messages.create(
    model='anthropic/claude-sonnet-4.5',
    max_tokens=1024,
    messages=[
        {
            'role': 'user',
            'content': 'Invent a new holiday and describe its traditions.',
        },
    ],
)

print(message.content[0].text)
```

Learn more about using the Anthropic SDK with the AI Gateway in the [Anthropic-Compatible API page](/docs/ai-gateway/sdks-and-apis/anthropic-compat).

## Using OpenResponses API

The [OpenResponses API](https://openresponses.org) is an open standard for AI model interactions that provides a unified, provider-agnostic interface with built-in support for streaming, tool calling, and reasoning.

The OpenResponses API includes:

- **Text Generation**: Generate text responses from prompts
- **Streaming**: Stream tokens as they're generated
- **Tool Calling**: Define tools the model can call
- **Reasoning**: Enable extended thinking for complex tasks
- **Provider Options**: Configure model fallbacks and provider-specific settings

#### TypeScript

```typescript filename="index.ts"
import 'dotenv/config';

async function main() {
  const response = await fetch('https://ai-gateway.vercel.sh/v1/responses', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${process.env.AI_GATEWAY_API_KEY}`,
    },
    body: JSON.stringify({
      model: 'anthropic/claude-sonnet-4.5',
      input: [
        {
          type: 'message',
          role: 'user',
          content: 'Invent a new holiday and describe its traditions.',
        },
      ],
    }),
  });

  const result = await response.json();
  console.log(result.output[0].content[0].text);
}

main().catch(console.error);
```

#### Python

```python filename="main.py"
import os
import requests
from dotenv import load_dotenv

load_dotenv()

response = requests.post(
    'https://ai-gateway.vercel.sh/v1/responses',
    headers={
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {os.getenv("AI_GATEWAY_API_KEY")}',
    },
    json={
        'model': 'anthropic/claude-sonnet-4.5',
        'input': [
            {
                'type': 'message',
                'role': 'user',
                'content': 'Invent a new holiday and describe its traditions.',
            },
        ],
    },
)

result = response.json()
print(result['output'][0]['content'][0]['text'])
```

#### cURL

```bash filename="Terminal"
curl -X POST "https://ai-gateway.vercel.sh/v1/responses" \
  -H "Authorization: Bearer $AI_GATEWAY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "anthropic/claude-sonnet-4.5",
    "input": [
      {
        "type": "message",
        "role": "user",
        "content": "Invent a new holiday and describe its traditions."
      }
    ]
  }'
```

Learn more about the OpenResponses API in the [OpenResponses API documentation](/docs/ai-gateway/sdks-and-apis/openresponses).

## Using other community frameworks

AI Gateway works with any framework that supports the OpenAI API or AI SDK v5/v6, and also supports tools like [Claude Code](/docs/ai-gateway/coding-agents/claude-code).

See the [framework integrations](/docs/ai-gateway/ecosystem/framework-integrations) section to learn more about using AI Gateway with community frameworks.


--------------------------------------------------------------------------------
title: "Model Fallbacks"
description: "Configure model-level failover to try backup models when the primary model is unavailable"
last_updated: "2026-02-03T02:58:35.780Z"
source: "https://vercel.com/docs/ai-gateway/models-and-providers/model-fallbacks"
--------------------------------------------------------------------------------