---
title: "AI gateway"
description: "Choose the AI gateway the AI features call models through, the Convex AI Gateway on a paid plan or a Vercel AI Gateway key, or both in order, then set up its spend cap, EU inference, and what development costs."
canonical_url: "https://nuxt-backend.dev/docs/services/ai-gateway"
---
# AI gateway

> Choose the AI gateway the AI features call models through, the Convex AI Gateway on a paid plan or a Vercel AI Gateway key, or both in order, then set up its spend cap, EU inference, and what development costs.

Version: nuxt-backend 0.6.x · Tab: Services · Kind: how-to · Status: stable

Every page: [llms.txt](https://nuxt-backend.dev/llms.txt) · Skill for coding agents: [nuxt-backend](https://nuxt-backend.dev/.well-known/skills/nuxt-backend/SKILL.md)

Commands use npm; use your project's package manager.

An AI gateway reaches every model provider through one account and reports what each call cost. The AI features call models by `provider/model` id through one of two gateways, or both in the order you choose. Set a spend cap before anything else.

<table>
<thead>
  <tr>
    <th>
      You need
    </th>
    
    <th>
      Time
    </th>
    
    <th>
      Cost to start
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      A paid Convex plan, or a Vercel account for a gateway key
    </td>
    
    <td>
      5 minutes
    </td>
    
    <td>
      Each call at the provider's price. No free tier on the Convex route
    </td>
  </tr>
</tbody>
</table>

## What it powers

<table>
<thead>
  <tr>
    <th>
      Capability
    </th>
    
    <th>
      What the gateway gives
    </th>
    
    <th>
      Build pages
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        ai
      </code>
    </td>
    
    <td>
      Model calls and streams by <code>
        provider/model
      </code>
      
       id, with the cost of each
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/build/ai/models">
        Choose a model
      </a>
      
      , <a href="https://nuxt-backend.dev/docs/build/ai/streaming">
        Stream a response
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        ai
      </code>
      
      , <code>
        billing
      </code>
    </td>
    
    <td>
      The cost each metered action settles in credits
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/build/ai/metered-actions">
        Meter an AI action
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        jobs
      </code>
    </td>
    
    <td>
      The calls each generation job makes
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/build/ai/jobs">
        Run generation jobs
      </a>
    </td>
  </tr>
</tbody>
</table>

## Choose a gateway

`setupAi({ gateway })` names the gateways a model id goes through, in order. Without it, the order is the Convex AI Gateway, then the Vercel AI Gateway:

1. **The Convex AI Gateway**, on Convex Cloud with a paid plan and `@convex-dev/ai-sdk-provider` installed. Convex holds the provider credentials: there is no key to create, store or rotate.
2. **The Vercel AI Gateway**, with the deployment's `AI_GATEWAY_API_KEY`. It answers where the Convex one cannot: a free plan, an account-free local deployment, a self-hosted backend.
3. **A refusal**, `unavailable` with reason `not_configured`, when neither answers.

Name one gateway to use it alone, or a list for another order:

```ts [backend/ai.ts]
export const ai = setupAi(components, {
  billing,
  model: 'openai/gpt-5-mini',
  gateway: 'convex', // or 'vercel', or ['vercel', 'convex']
})
```

<table>
<thead>
  <tr>
    <th>
      <code>
        gateway
      </code>
    </th>
    
    <th>
      What it does
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      Omitted
    </td>
    
    <td>
      The Convex AI Gateway where the deployment has it, else the Vercel AI Gateway with the key
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        'convex'
      </code>
    </td>
    
    <td>
      The Convex AI Gateway alone. The key is never read, so a call never lands on a second invoice
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        'vercel'
      </code>
    </td>
    
    <td>
      The Vercel AI Gateway alone, even on a paid Convex plan
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        ['vercel', 'convex']
      </code>
    </td>
    
    <td>
      The Vercel AI Gateway where the key is set, else the Convex AI Gateway
    </td>
  </tr>
</tbody>
</table>

The next gateway answers only when one cannot serve the deployment. A spent limit, a budget's `402` or a provider's error refuses the call, whatever comes next: a limit is never routed around. [Where a model id goes](https://nuxt-backend.dev/docs/build/ai/models#where-a-model-id-goes) has the detail.

Content safety follows the gateway that answers, unless you name a classifier: Llama Guard 4 through the Convex AI Gateway, which reads images; `openai/gpt-oss-safeguard-20b` through the Vercel AI Gateway, which serves no Llama Guard and reads text only, so an image check there is refused as unchecked ([Content safety](https://nuxt-backend.dev/docs/build/ai/content-safety)).

`setupAi({ routes })` gives some models an order of their own, over `gateway`: a provider the Vercel AI Gateway serves and the Convex one does not, say, or one image model. Each key is a model id or a pattern ending in `*`, and each value is a gateway or an order, in the same terms as `gateway`. [Route some models elsewhere](https://nuxt-backend.dev/docs/build/ai/models#route-some-models-elsewhere) has the rules.

### How the two differ

Checked on 2026-11-08:

<table>
<thead>
  <tr>
    <th>
      
    </th>
    
    <th>
      Convex AI Gateway
    </th>
    
    <th>
      Vercel AI Gateway
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      Sign-in from a Convex action
    </td>
    
    <td>
      A token the runtime mints for the deployment (<code>
        getServiceToken('ai-gateway')
      </code>
      
      ). No key to keep
    </td>
    
    <td>
      <code>
        AI_GATEWAY_API_KEY
      </code>
      
      . Vercel's keyless OIDC sign-in works only on Vercel, never in a Convex action
    </td>
  </tr>
  
  <tr>
    <td>
      Who bills
    </td>
    
    <td>
      Your Convex invoice. Paid plans only
    </td>
    
    <td>
      Vercel credits you buy, at the provider's list price. A monthly free credit covers some models
    </td>
  </tr>
  
  <tr>
    <td>
      Spend cap
    </td>
    
    <td>
      The team's spending limit, and a usage limit per deployment
    </td>
    
    <td>
      Budgets per team, key or member, reset daily, weekly, monthly or never
    </td>
  </tr>
  
  <tr>
    <td>
      Region
    </td>
    
    <td>
      None documented
    </td>
    
    <td>
      Pinned per call to a region, the US or the EU among them; <code>
        setupAi
      </code>
      
       pins the EU
    </td>
  </tr>
  
  <tr>
    <td>
      Routing
    </td>
    
    <td>
      Convex's. Provider order and model fallbacks are refused with <code>
        400
      </code>
      
      . Zero data retention and <code>
        max_price
      </code>
      
       work per call
    </td>
    
    <td>
      Provider order and filters, model fallbacks, your own provider keys (BYOK), caching, user tags
    </td>
  </tr>
  
  <tr>
    <td>
      Models
    </td>
    
    <td>
      Text and embeddings. Image, video, speech and transcription in alpha
    </td>
    
    <td>
      Text, embeddings, image, video and rerank. Speech in beta
    </td>
  </tr>
  
  <tr>
    <td>
      Where you see spend
    </td>
    
    <td>
      The Convex dashboard, per project and per function
    </td>
    
    <td>
      The AI Gateway dashboard, per key, with request logs
    </td>
  </tr>
  
  <tr>
    <td>
      Where it works
    </td>
    
    <td>
      Convex Cloud, and a local deployment linked to a project
    </td>
    
    <td>
      Any deployment with the key, self-hosted included
    </td>
  </tr>
</tbody>
</table>

Sources: [Convex AI Gateway](https://docs.convex.dev/ai-gateway/overview), [its API](https://docs.convex.dev/ai-gateway/api), [Vercel AI Gateway](https://vercel.com/docs/ai-gateway), [provider options](https://vercel.com/docs/ai-gateway/models-and-providers/provider-filtering-and-ordering).

**Pick the Convex AI Gateway** for one invoice, no key to rotate, and spend per function next to the rest of your Convex usage.

**Pick the Vercel AI Gateway** for:

- Inference pinned to a region ([`inferenceRegion`](https://nuxt-backend.dev/docs/build/ai/models#pin-inference-to-the-eu)).
- A budget per key, so development and production each have their own.
- Vercel's routing options: provider order, model fallbacks, your own provider keys, caching. They ride in `providerOptions.gateway` and do nothing on the Convex route.
- A free Convex plan, or a self-hosted or account-free local deployment.

**Pick ['vercel', 'convex']** to develop on the Vercel free credit and run production on the Convex invoice. Push the key to development only.

A model object from any AI SDK provider package, such as OpenRouter's, works anywhere an id does, with that provider's own key. It reports no gateway cost, so price it by tokens ([Token pricing](https://nuxt-backend.dev/docs/build/ai/metered-actions#token-pricing)).

## Create the account

Set up the gateways your order names.

**The Convex AI Gateway:**

<steps level="3">

### Move the Convex team to a paid plan

In the [Convex dashboard](https://dashboard.convex.dev), open the team's billing page and choose Starter (pay as you go) or Professional ([Convex](https://nuxt-backend.dev/docs/services/convex#costs-and-limits)). The free plan answers `AiGatewayDisabled`.

### Install the providers

```bash
npm install ai @convex-dev/ai-sdk-provider
```

### Set the team's spending limit

On the same billing page, set a spending limit. At the limit, calls are refused until it resets or rises, and the key route is never tried instead.

</steps>

**The Vercel AI Gateway:**

<steps level="3">

### Sign up for Vercel

At [vercel.com/signup](https://vercel.com/signup). The gateway works with any host: the key is all it needs.

### Create a key

Open **AI Gateway**, **API Keys**, **Create key**. A key never expires, but Vercel deactivates it when the member who created it leaves the team.

### Set a budget on the key

Open **AI Gateway**, **Budgets**, and set a monthly budget for the key. The CLI does the same:

```bash
vercel ai-gateway budgets set api-key <key-name> --limit 50 --refresh-period monthly
```

</steps>

**Who should own it.** The person who answers for spend owns the Convex team's billing, or the Vercel team that holds the key. Create a production key under a shared account rather than one person's, so it survives a departure.

## Sandbox and test mode

Neither gateway has a test mode: development calls the same models as production, at their real cost. What differs is who pays:

<table>
<thead>
  <tr>
    <th>
      <code>
        BACKEND_ENVIRONMENT
      </code>
    </th>
    
    <th>
      Credits the call spends
    </th>
    
    <th>
      What the gateway bills
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        development
      </code>
      
      , <code>
        preview
      </code>
      
      , <code>
        sandbox
      </code>
    </td>
    
    <td>
      Credits from Polar's sandbox
    </td>
    
    <td>
      Real money, to your Convex or Vercel team
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        production
      </code>
    </td>
    
    <td>
      Your customers' credits
    </td>
    
    <td>
      Real money, to your Convex or Vercel team
    </td>
  </tr>
</tbody>
</table>

Point development at a small model, such as a `-mini` one, and set its own budget. Tests never call a model: `nuxt-backend/test` runs your functions on `convex-test` ([Test backend functions](https://nuxt-backend.dev/docs/develop/testing/functions)).

## Keys and environment

<table>
<thead>
  <tr>
    <th>
      Variable
    </th>
    
    <th>
      Where to find it
    </th>
    
    <th>
      Dev
    </th>
    
    <th>
      Prod
    </th>
    
    <th>
      Secret
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        AI_GATEWAY_API_KEY
      </code>
    </td>
    
    <td>
      Vercel: <strong>
        AI Gateway
      </strong>
      
      , <strong>
        API Keys
      </strong>
    </td>
    
    <td>
      Where the order names <code>
        'vercel'
      </code>
      
      : first in a Vercel-first order, else once the Convex AI Gateway cannot serve the deployment. Production takes a key of its own: <code>
        env push --prod
      </code>
      
       never sends <code>
        .env.local
      </code>
      
      's
    </td>
    
    <td>
      The same
    </td>
    
    <td>
      Yes
    </td>
  </tr>
</tbody>
</table>

The Convex AI Gateway needs no variable, and `gateway: 'convex'` never reads the key. `AI_GATEWAY_API_KEY` is the gateway's own name, read as it is, and lives on the Convex deployment. Vercel's keyless sign-in through `VERCEL_OIDC_TOKEN` never reaches the backend: model calls run in Convex actions, on Convex's servers.

## Link it

<steps level="3">

### Name a model

```ts [backend/ai.ts]
import { setupAi } from 'nuxt-backend/ai'
import { components } from './_generated/api'
import { billing } from './billing'
import { rateLimiter } from './rateLimiter'

export const ai = setupAi(components, { billing, rateLimiter, model: 'openai/gpt-5-mini' })
```

### Push the key, if you use one

```bash [.env.local]
AI_GATEWAY_API_KEY=your_ai_gateway_api_key
```

```bash
npx nuxt-backend env push
```

</steps>

## Check it

```bash
npx nuxt-backend doctor --only ai
```

<table>
<thead>
  <tr>
    <th>
      Finding
    </th>
    
    <th>
      What it checks
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <a href="https://nuxt-backend.dev/docs/reference/cli/diagnostics#deployment-ai-gateway-api-key">
        <code>
          deployment-ai-gateway-api-key
        </code>
      </a>
    </td>
    
    <td>
      Every <code>
        provider/model
      </code>
      
       id your backend names has a route on this deployment, through the <code>
        gateway
      </code>
      
       order. It warns when none does, and fails under <code>
        --prod
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <a href="https://nuxt-backend.dev/docs/reference/cli/diagnostics#ai-stream-route">
        <code>
          ai-stream-route
        </code>
      </a>
    </td>
    
    <td>
      The deployment serves <code>
        /ai/stream
      </code>
      
       once <code>
        setupAi
      </code>
      
       is deployed
    </td>
  </tr>
</tbody>
</table>

## Go to production

<steps level="3">

### Cap the deployment's spend

On the Convex route, add a warning on production's own spend:

```bash
npx convex deployment usage-limits set --prod --metric aiGatewayCostDollars --window day --type warning --limit 50
```

A `--type disable` limit pauses the whole deployment, every function included. To stop AI alone, use the [`pause_ai` kill switch](https://nuxt-backend.dev/docs/ship/operate/flags).

### Set production's key, if you use one

```bash
npx convex env set --prod AI_GATEWAY_API_KEY
```

Give production a key and a budget of its own, never the development key.

### Check it

```bash
npx nuxt-backend doctor --prod
```

</steps>

## Costs and limits

Checked on 2026-10-25, the spend caps again on 2026-10-27, and both again on 2026-11-08:

<table>
<thead>
  <tr>
    <th>
      
    </th>
    
    <th>
      Convex AI Gateway
    </th>
    
    <th>
      Vercel AI Gateway
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      Price
    </td>
    
    <td>
      The same rates as OpenRouter, on your Convex invoice (<a href="https://docs.convex.dev/ai-gateway/usage-and-billing" rel="nofollow">
        Usage and billing
      </a>
      
      )
    </td>
    
    <td>
      The provider's list price, with no markup (<a href="https://vercel.com/docs/ai-gateway/pricing" rel="nofollow">
        Pricing
      </a>
      
      )
    </td>
  </tr>
  
  <tr>
    <td>
      Free use
    </td>
    
    <td>
      None: paid Convex plans only
    </td>
    
    <td>
      A monthly free credit on some models, with a card on file, until you buy credits
    </td>
  </tr>
  
  <tr>
    <td>
      Spend cap
    </td>
    
    <td>
      The team's spending limit, and a per-deployment usage limit
    </td>
    
    <td>
      Budgets per team, key or member, reset daily, weekly or monthly at 00:00 UTC, or never. Past one, calls get <code>
        402
      </code>
      
      . Budgets are soft caps. A project budget counts only OIDC traffic, which a Convex action never sends
    </td>
  </tr>
  
  <tr>
    <td>
      Rate limits
    </td>
    
    <td>
      The providers' own
    </td>
    
    <td>
      None of its own on paid credits; lower limits on the free credit
    </td>
  </tr>
  
  <tr>
    <td>
      Modalities
    </td>
    
    <td>
      Text and embeddings; image, video, speech and transcription in alpha
    </td>
    
    <td>
      Text, embeddings, image, video and rerank; speech in beta (<a href="https://vercel.com/ai-gateway/models" rel="nofollow">
        model list
      </a>
      
      )
    </td>
  </tr>
</tbody>
</table>

- **Budgets cap the team, not your users.** Neither gateway limits what one person of yours spends. The package does that with credits, rate limits and, for queued jobs, a per-tier budget ([Budgets and costs](https://nuxt-backend.dev/docs/build/ai/models#budgets-and-costs), [Cap what an owner spends per day](https://nuxt-backend.dev/docs/build/ai/jobs/priority#cap-what-an-owner-spends-per-day)).
- **A failed run** releases its credit reservation, even when the gateway billed the calls it made before failing.
- **Vercel credits** you buy expire after a year. Auto top-up is off until you turn it on.

### Per-user limits and gateway-wide stops

Checked on 2026-10-27. These facts decide what the [job queue](https://nuxt-backend.dev/docs/build/ai/jobs/priority#why-the-package-does-this-not-the-gateway) does itself:

- **No limit per app user or workspace.** Convex documents daily and monthly dollar limits per deployment, and a monthly spending limit per team ([Usage and billing](https://docs.convex.dev/ai-gateway/usage-and-billing)). Vercel's budgets scope to a team, a project, an API key or a team member, and its `quotaEntityId` tag has no documented budget ([Budgets](https://vercel.com/docs/ai-gateway/observability-and-spend/budgets)).
- **A limit stops everyone in its scope.** A Convex usage limit at its disable threshold disables the whole deployment for the rest of its window, sign-in and billing webhooks included. It comes back when the window resets, daily ones at 00:00 UTC, or when the limit is raised ([Usage limits](https://docs.convex.dev/production/usage-limits)). A Vercel budget answers `402`, type `quota_for_entity_exceeded`, to every request in its scope until it resets or is raised. The AI SDK can surface that as `GatewayInternalServerError`.
- **Caps are soft and late.** A Vercel budget is checked at the start of each request, so the request that crosses it still completes, and spend shows about 20 seconds later.
- **Dollars arrive after the call.** Convex chat completions return tokens and no dollar cost. Vercel reports a call's cost once it ran.
- **No capacity to divide.** The Convex gateway documents no rate or concurrency limit. Vercel sets none of its own on paid credits, and lower per-model limits on the free credit, without numbers. Convex's Workpool advises at most about 20 jobs running across all pools on the free plan, and 100 on Pro.
- **Retry-After is not always there.** Convex documents forwarding it on chat completions ([API](https://docs.convex.dev/ai-gateway/api)), and the Vercel AI Gateway sends `retry-after` on some `429`s.
- **Canceling does not always stop the cost.** Convex says canceling a video request does not cancel its generation or cost, and that a lost answer to an async video job may still be charged.

## Data and compliance

- **What it holds:** each prompt and response passes through the gateway to the model provider. The package stores neither, unless your code does.
- **Convex route:** Convex processes the calls under its [DPA](https://www.convex.dev/legal/dpa), and lists OpenRouter among its [subprocessors](https://www.convex.dev/legal/subprocessors) for routing. Region pinning is not documented as of 2026-11-08; zero data retention can be asked for per call.
- **Vercel route:** Vercel processes the calls under its [DPA](https://vercel.com/legal/dpa). It can pin inference per call to a region, the US or the EU among them; `setupAi({ inferenceRegion: 'eu' })` pins every call to the EU, the one region it takes. A model with no provider in the region fails with `400` rather than leave it, and the regional rate is the provider's own, often a little higher ([Regional inference](https://vercel.com/docs/ai-gateway/security-and-compliance/regional-inference)).
- **Providers:** each model provider has its own retention and training terms. Read them before you send personal data, and name the providers you use in your privacy policy.

Generated content may need a visible label in the EU ([Disclose AI-generated content](https://nuxt-backend.dev/docs/ship/compliance/ai-transparency)). This is not legal advice.

## Related

- [Choose a model](https://nuxt-backend.dev/docs/build/ai/models): ids, routes, caps and costs.
- [Meter an AI action](https://nuxt-backend.dev/docs/build/ai/metered-actions): charge credits for what a call cost.
- [Convex](https://nuxt-backend.dev/docs/services/convex): plans, and the team's billing page.
- [Roll out features and pause services](https://nuxt-backend.dev/docs/ship/operate/flags): the `pause_ai` kill switch.
- [Service map](https://nuxt-backend.dev/docs/services): every service, and the order to set them up in.


## Sitemap

See the full [sitemap](https://nuxt-backend.dev/sitemap.md) for all pages.
