---
title: "AI Gateway Overview"
description: "Learn how to use the AI Gateway to build with AI."
---

> For the complete documentation index for AI agents, see [llms.txt](https://docs.netlify.com/llms.txt). Markdown versions of any documentation page are available by appending `.md` to its docs.netlify.com URL.

export const aiProvidersResponse = await fetch('https://api.netlify.com/api/v1/ai-gateway/providers/detailed');
export const aiProvidersData = await aiProvidersResponse.json();
export const providers = aiProvidersData.providers;
export const providerDisplayNames = { openai: 'OpenAI', anthropic: 'Anthropic', gemini: 'Gemini' };
export const displayProviderName = (providerName) =>
  providerDisplayNames[providerName] ?? providerName.charAt(0).toUpperCase() + providerName.slice(1);
export const aiGatewayTable = marked.parse(`
| AI Provider | Model |
| --- | --- |
` + Object.entries(providers).flatMap(([providerName, providerData]) =>
  Object.keys(providerData.models).map(modelName =>
    `| ${displayProviderName(providerName)} | ${modelName} |`
  )
).join('\n'))

Use popular AI models in your code, without needing to manage API keys or external accounts.

> **Pricing Information:** This feature is available on Credit-based plans only, including the [Free, Personal, and Pro](https://www.netlify.com/pricing/) plans. If you are on an Enterprise plan and you're interested, reach out to your Account Manager.

## Overview

The AI Gateway service simplifies technical and operational concerns when using AI inference in your code,
by removing the need to:
* Open an account with each provider you want to use.
* Maintain a separate credit balance with each provider.
* Copy the API key from each provider to your projects on Netlify.

### Promoted Content

**Title - Explore an AI Gateway example**

**description**
Learn how AI Gateway works in this [TanStack Start](/build/frameworks/framework-setup-guides/tanstack-start) chat app example, which automatically proxies your requests to a supported AI provider with built-in security, usage analytics, and rate limiting.

## AI Gateway examples

For a video overview of how the AI Gateway works with a fun demo project, check out our AI Gateway gameshow demo.

> **Video**: [Watch video](https://www.youtube.com/embed/9CqxH7IFbds)

Check out more examples of working projects that are powered with AI models in our [AI Gateway examples docs](/build/ai-gateway/examples).

### How it works

By default, Netlify automatically sets the [appropriate environment variables](#managing-environment-variables) that AI client libraries typically use for configuration, in all Netlify compute contexts (e.g., Netlify Functions, Edge Functions, Preview Server, etc.).

These variables include:
* **API keys** for OpenAI, Anthropic, Google Gemini, and OpenRouter.
* **A custom base URL** for each provider, to route requests via the AI Gateway service.

These variables are picked up by the official client libraries of these providers, so no extra configuration is necessary - with the exception of the OpenRouter SDK, which needs the base URL passed explicitly. Alternatively, if you make AI calls via a provider's REST API, these values are easy to incorporate in your code.

When receiving a request from a client, the AI Gateway makes the call to the AI provider on your behalf. Then, it bills your Netlify account by converting the actual token usage in the request into credits, using your existing credit quota.

### Tip - Local development with the AI Gateway

The AI Gateway has full support with the [Netlify CLI](/api-and-cli-guides/cli-guides/get-started-with-cli/). For Vite-based projects, you can also use the [Netlify Vite plugin](/build/frameworks/framework-setup-guides/vite/#vite-plugin) to access AI Gateway locally without running `netlify dev`. Check our [Quickstart](/build/ai-gateway/quickstart-for-ai-gateway/) for a hands-on guided example project using the AI Gateway.<br/><br/>**Note:** A project must have a [production deploy](/deploy/deploy-types/production-deploy/) for the AI Gateway to activate, so if you're creating a new project locally, deploy to production at least once to enable it.

The AI Gateway does not store your prompts or model outputs. Learn more about [Security and Privacy for AI features](/build/build-with-ai/security-and-privacy-for-ai-features).
To opt out, [check your opt-out options](/build/build-with-ai/manage-ai-for-your-team/manage-ai-features/#disable-ai-features).

### Support in web frameworks

When you develop server-side code with any web framework [supported by Netlify](/build/frameworks/overview) (e.g., Astro; Tanstack Start; Next.js, Gatsby, Nuxt, etc.), your code is packaged in Netlify Functions and Edge Functions under the hood, as part of the build process.

Therefore, the above environment variables are available as when explicitly using Netlify compute primitives, without any further settings required.

## Using the AI Gateway

For a quickstart, check out our [Quickstart for AI Gateway](/build/ai-gateway/quickstart-for-ai-gateway).

The AI Gateway is available by default in all **credit-based plans**, unless:

1. You have [disabled Netlify AI Features](/build/build-with-ai/manage-ai-for-your-team/manage-ai-features/#disable-ai-features) for your team, or:
1. You have set your own API keys for AI providers via environment variables. Netlify does not override these keys. You can add or remove your own keys at any point.

For full information on which environment variables are automatically set, and how to control this behavior, [see here](#managing-environment-variables).

### Using official client libraries

If you're using any of the following libraries, little to no configuration is required. The AI Gateway automatically provides the necessary environment variables that these libraries use:

### Tabs Component:

<TabItem label="Anthropic Claude">

```js
import Anthropic from '@anthropic-ai/sdk';

const anthropic = new Anthropic();
// No API key or base URL configuration needed - automatically uses:
// process.env.ANTHROPIC_API_KEY and process.env.ANTHROPIC_BASE_URL

async function callAnthropic() {
  const message = await anthropic.messages.create({
    model: 'claude-sonnet-4-5-20250929',
    max_tokens: 1024,
    messages: [{ role: 'user', content: 'Hello!' }]
  });
  return message;
}
```

Library: [Anthropic TypeScript API Library](https://www.npmjs.com/package/@anthropic-ai/sdk)

</TabItem>

<TabItem label="OpenAI">

```js
import OpenAI from 'openai';

const openai = new OpenAI();
// No API key or base URL configuration needed - automatically uses:
// process.env.OPENAI_API_KEY and process.env.OPENAI_BASE_URL

async function callOpenAI() {
  const completion = await openai.chat.completions.create({
    // Models available through OpenRouter can also be used here, with no extra
    // configuration - just pass a model name in the OpenRouter notation,
    // e.g. 'deepseek/deepseek-v4-flash-0731'
    model: 'gpt-5',
    messages: [{ role: 'user', content: 'Hello!' }]
  });
  return completion;
}
```

Library: [OpenAI TypeScript and JavaScript API Library](https://www.npmjs.com/package/openai)

</TabItem>

<TabItem label="Google Gemini">

```js
import { GoogleGenAI } from '@google/genai';

const genAI = new GoogleGenAI({});
// No API key or base URL configuration needed - automatically uses:
// process.env.GEMINI_API_KEY and process.env.GOOGLE_GEMINI_BASE_URL

async function callGemini() {
  const result = await genAI.models.generateContent({
    model: 'gemini-2.5-pro',
    contents: 'Hello!'
  });
  return result;
}
```

Library: [Google Gen AI SDK for TypeScript and JavaScript](https://www.npmjs.com/package/@google/genai)

</TabItem>

<TabItem label="OpenRouter">

```js
import { OpenRouter } from '@openrouter/sdk';

// The API key is picked up automatically from process.env.OPENROUTER_API_KEY,
// but the base URL must be passed explicitly, so that requests are routed
// through the AI Gateway:
const openRouter = new OpenRouter({
  serverURL: process.env.OPENROUTER_BASE_URL
});

async function callOpenRouter() {
  const result = await openRouter.chat.send({
    chatRequest: {
      model: 'x-ai/grok-4.5',
      messages: [{ role: 'user', content: 'Hello!' }]
    }
  });
  return result;
}
```

Library: [OpenRouter TypeScript SDK](https://www.npmjs.com/package/@openrouter/sdk)

</TabItem>

Note that models available through OpenRouter can be used with either the OpenRouter SDK or the OpenAI SDK.

### Using official REST APIs

### Tabs Component:

<TabItem label="Anthropic Claude">

```js
async function callAnthropic() {
  const ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY;
  const ANTHROPIC_BASE_URL = process.env.ANTHROPIC_BASE_URL;

  const response = await fetch(`${ANTHROPIC_BASE_URL}/v1/messages`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'x-api-key': ANTHROPIC_API_KEY,
      'anthropic-version': '2023-06-01'
    },
    body: JSON.stringify({
      model: 'claude-sonnet-4-5-20250929',
      max_tokens: 1024,
      messages: [{ role: 'user', content: 'Hello!' }]
    })
  });
  return await response.json();
}
```
</TabItem>

<TabItem label="OpenAI">

```js
async function callOpenAI() {
  const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
  const OPENAI_BASE_URL = process.env.OPENAI_BASE_URL;

  const response = await fetch(`${OPENAI_BASE_URL}/v1/chat/completions`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${OPENAI_API_KEY}`
    },
    body: JSON.stringify({
      model: 'gpt-5',
      messages: [{ role: 'user', content: 'Hello!' }]
    })
  });
  return await response.json();
}
```

</TabItem>

<TabItem label="Google Gemini">

```js
async function callGemini() {
  const GEMINI_API_KEY = process.env.GEMINI_API_KEY;
  const GEMINI_BASE_URL = process.env.GOOGLE_GEMINI_BASE_URL;

  const response = await fetch(
    `${GEMINI_BASE_URL}/v1beta/models/gemini-2.5-pro:generateContent`,
    {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'x-goog-api-key': GEMINI_API_KEY
      },
      body: JSON.stringify({
        contents: [{
          parts: [{ text: 'Hello!' }]
        }]
      })
    }
  );
  return await response.json();
}
```

</TabItem>

<TabItem label="OpenRouter">

```js
async function callOpenRouter() {
  const OPENROUTER_API_KEY = process.env.OPENROUTER_API_KEY;
  const OPENROUTER_BASE_URL = process.env.OPENROUTER_BASE_URL;

  const response = await fetch(`${OPENROUTER_BASE_URL}/chat/completions`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${OPENROUTER_API_KEY}`
    },
    body: JSON.stringify({
      model: 'x-ai/grok-4.5',
      messages: [{ role: 'user', content: 'Hello!' }]
    })
  });
  return await response.json();
}
```

</TabItem>

### Using third-party client libraries

If you are using a client library that does not work out-of-the-box with the environment variables set for the AI Gateway, you need to manually pass the API key and base URL as arguments to the library.

This is similar to manually reading & passing variable values when using a provider's REST API. See [Using official REST APIs](#using-official-rest-apis) above for the relevant variable names.

### Managing environment variables

If you have already set an API key or base URL at the project or team level, Netlify will never override it.

When a Netlify Function or Edge Function is initialized, the following environment variables are set to the appropriate values for the AI Gateway:

1. `OPENAI_API_KEY` and `OPENAI_BASE_URL` - unless any of these is already set by you at the project or team level.
1. `ANTHROPIC_API_KEY` and `ANTHROPIC_BASE_URL` - unless any of these is already set by you.
1. `GEMINI_API_KEY` and `GOOGLE_GEMINI_BASE_URL`-  unless any of these is already set by you, or if either `GOOGLE_API_KEY` or `GOOGLE_VERTEX_BASE_URL` are set.
1. `OPENROUTER_API_KEY` and `OPENROUTER_BASE_URL` - unless any of these is already set by you.

`NETLIFY_AI_GATEWAY_KEY` and `NETLIFY_AI_GATEWAY_BASE_URL` environment variables are always injected into the AI Gateway-supported runtimes. If you want to mix different setups with your own keys and Netlify's or you want to be explicit about using AI Gateway keys in your calls, use these env variables as they will never collide with other environment variables values.

To prevent any variables from being automatically set, you can [disable AI Features.](/build/build-with-ai/manage-ai-for-your-team/manage-ai-features/#disable-ai-features)

## Model availability

Models are available through the AI Gateway in two ways.

### Models served directly

Models from Anthropic, OpenAI, and Google Gemini are served directly by the AI Gateway, using each provider's own API. These are the models listed below, and they are **not** routed through OpenRouter.

### Models served via OpenRouter

Models from other model creators, such as xAI, DeepSeek, Meta, Mistral, and Qwen, are available in partnership with [OpenRouter](https://openrouter.ai/), which routes many AI models. To browse the full catalog and find the model IDs to use, see the [OpenRouter models directory](https://openrouter.ai/models?zdr=true).

To call these models, pass a model ID in the OpenRouter notation - for example, `deepseek/deepseek-v4-flash-0731` - to either the OpenRouter SDK or the OpenAI SDK, as shown in [Using official client libraries](#using-official-client-libraries) above. You can also call them via REST, as shown in [Using official REST APIs](#using-official-rest-apis).

### Note - Zero Data Retention only

The same model on OpenRouter can be hosted by multiple providers (i.e., vendors that offer that model). Netlify only routes your requests to providers that have a [Zero Data Retention (ZDR)](https://openrouter.ai/docs/guides/features/zdr#zero-data-retention) policy, meaning they do not store your prompts or model outputs.

As a result, if none of the providers hosting a given model offers a ZDR guarantee, that model is not served by the AI Gateway - even though it is listed in the OpenRouter models directory.

## Pricing

To understand pricing for AI Gateway, check out our [Pricing for AI features](/manage/accounts-and-billing/billing/billing-for-credit-based-plans/pricing-for-ai-features) docs.

## Rate limits

Netlify applies rate limits for your team's usage of the AI Gateway, across all of your team's projects. The rate limit is per minute and differs by plan, with higher plans having a higher limit.

Tokens consumed by each request to the AI Gateway are converted to USD (U.S. dollars) based on the costs published by the providers we support, and then to Netlify credits. $1 USD of AI model usage equates to 180 credits. Learn more in our [Pricing for AI features](/manage/accounts-and-billing/billing/billing-for-credit-based-plans/pricing-for-ai-features/#ai-inference-as-a-usage-meter).

The limit depends on your plan:

| Plan | Limit per minute (credits) |
| --- | --- |
| Free | 90 |
| Personal | 450 |
| Pro | 1,800 |
| Enterprise | 9,000 |

Thus, when adding AI Gateway-based features to your site, it's always advised to track your credit usage - and ensure you have either enabled [auto recharge](/manage/accounts-and-billing/billing/billing-for-credit-based-plans/configure-auto-recharge/) or purchased [credit packs](/manage/accounts-and-billing/billing/billing-for-credit-based-plans/buy-credit-packs/) to meet the demand you expect without exhausting your credits.

### Tip - Configuring advanced rate limiting

We recommend that you [set up rate limiting rules](/manage/security/secure-access-to-sites/rate-limiting/) for Netlify functions or edge fuctions that use the AI gateway.

This lets you limit any given visitor from abusing calls to the AI Gateway, thus avoiding high costs over time or hitting your account-wide AI gateway limits.

## Limitations

The AI Gateway has the following limitations at this time:

1. Using the AI Gateway requires that the site has had at least one production deployment in the past.
1. The context window (input prompt) is limited to 200k tokens.
1. Prompt caching:
    - Anthropic Claude: only the default 5-minute ephemeral cache duration is supported for Claude.
    - OpenAI: the AI Gateway sets a per-account [prompt_cache_key](https://platform.openai.com/docs/api-reference/responses/create#responses_create-prompt_cache_key).
    - Google Gemini: explicit context caching is not supported.
1. The AI Gateway does not pass through any request headers (and thus you cannot enable proprietary experimental features via headers).
1. Batch inference is not supported.
1. Priority processing (an OpenAI feature) is not supported.

## Monitor AI Gateway usage 

To help you monitor AI Gateway usage, check out our docs on [monitoring AI feature usage](/manage/accounts-and-billing/billing/billing-for-credit-based-plans/monitor-usage-for-credit-based-plans).
