---
title: "AI features"
description: "Sell AI features paid for with prepaid credits: every model call reserves its credits, runs any provider's model through the Convex AI Gateway, then settles at what it cost, and a failed run costs nothing. Plus generation jobs, chat, content safety and provenance labels."
canonical_url: "https://nuxt-backend.dev/docs/build/ai"
---
# AI features

> Sell AI features paid for with prepaid credits: every model call reserves its credits, runs any provider's model through the Convex AI Gateway, then settles at what it cost, and a failed run costs nothing. Plus generation jobs, chat, content safety and provenance labels.

Version: nuxt-backend 0.6.x · Tab: Build · Kind: landing · Status: experimental

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.

- [Try it live in the playground](https://nuxt-backend.dev/playground/strips)
- [API reference](https://nuxt-backend.dev/docs/reference/api/ai)
- [Source](https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai.ts)

- **You get:** AI features your users pay for in credits, with any provider's model, streamed answers, long generations in a fair queue, chat, content checks and an AI-generated label.
- **Powered by:** the AI SDK for model calls, the [Convex AI Gateway](https://nuxt-backend.dev/docs/services/ai-gateway) for every provider's models with no key (or the Vercel AI Gateway with one), Convex's persistent text streaming, Workflow, rate limiter, Agent and RAG components, and Polar's credit meters.
- **The package adds:** reserve, run, settle around every call, so a failed run costs nothing; rate limits, budgets and a kill switch; the job queue with priority tiers and adaptive model limits; ownership and workspace checks; and provenance.

The `ai` capability lets your app sell AI features, paid for with prepaid credits from your billing catalog. Before a model call runs, its credits are reserved. After it runs, it settles at what the model cost, and a run that fails costs nothing. Any provider's model works by its `provider/model` id. Answers stream to the page, and long generations wait their turn in a fair job queue. Rate limits, a credit budget and a kill switch cap the spend.

<note>

The `ai` capability is in the [experimental tier](https://nuxt-backend.dev/docs/ship/releases/stability): its metering model, reserve, run, then settle against the billing provider's meters, may change shape in a minor release.

</note>

## What it gives your app

<table>
<thead>
  <tr>
    <th>
      Feature
    </th>
    
    <th>
      What you write
    </th>
    
    <th>
      Page
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      Metered actions
    </td>
    
    <td>
      A Convex action wrapped by <code>
        ai.meteredAction
      </code>
      
      , with a meter and a cost
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/build/ai/metered-actions">
        Meter an AI action
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      Streamed answers
    </td>
    
    <td>
      <code>
        ai.stream
      </code>
      
       on the server, <code>
        useAiStream
      </code>
      
       in a page; the text persists and resumes
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/build/ai/streaming">
        Stream a response
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      Any provider's model
    </td>
    
    <td>
      A <code>
        provider/model
      </code>
      
       id, called through <code>
        ctx.model()
      </code>
      
       with the AI SDK
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/build/ai/models">
        Choose a model
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      Limits, budgets and alerts
    </td>
    
    <td>
      The <code>
        ai
      </code>
      
       rate limit, a credit <code>
        budget
      </code>
      
      , low-balance hooks, the <code>
        pause_ai
      </code>
      
       kill switch
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/build/ai/models#budgets-and-costs">
        Budgets and costs
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      Generation jobs
    </td>
    
    <td>
      A job queue and jobs of durable steps; a composable that shows a job's place in line and its progress
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/build/ai/jobs">
        Run generation jobs
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      Chat
    </td>
    
    <td>
      A chat with your instructions, and a composable that drives the thread
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/build/ai/chat">
        Add chat
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      Knowledge search
    </td>
    
    <td>
      A namespace per workspace, and replies grounded in it
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/build/ai/chat/knowledge-search">
        Add knowledge search
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      Content safety
    </td>
    
    <td>
      A check on prompts and uploads, prompt-injection guidance, and a report queue for administrators
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/build/ai/content-safety">
        Keep content safe
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      Provenance
    </td>
    
    <td>
      Content credentials in generated images, and the visible AI-generated label
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/build/ai/provenance">
        Label AI-generated content
      </a>
    </td>
  </tr>
</tbody>
</table>

Every row is experimental, like the rest of this capability. The [Feature map](https://nuxt-backend.dev/docs/start/feature-map#ai) lists every AI feature with its status. New to it? Start with the [credit-metered feature recipe](https://nuxt-backend.dev/docs/build/ai/metered-actions#a-complete-feature): one meter, one pack, one streamed answer and the page that runs it.

## How a metered call works

Every metered call takes the same path, whether it is an action, a stream, a chat reply or a job:

1. **Check the caller.** The caller must be signed in, a member of the workspace that pays, and acting as themselves: an administrator impersonating them is refused.
2. **Check the switches and limits.** The `pause_ai` kill switch, the model being reachable, the `ai` rate limit and the credit budget are checked, in that order, before anything is spent.
3. **Reserve.** The call's cost, or an estimate of it, is taken from the cached balance at once, so two calls at the same moment can never spend the same credits. Too few credits refuses the call with `insufficient_credits`.
4. **Run.** Your handler runs, and calls its model through `ctx.model()`.
5. **Settle or release.** A run that succeeds settles at its real cost: a fixed price, its token usage priced from your table, or the cost the gateway reports. The rest of the estimate goes back to the balance. A run that throws releases the whole reservation.

The spend lands with the billing provider as a usage event on the credit meter, with the member who made the call. The balance in the page updates live. [Meter an AI action](https://nuxt-backend.dev/docs/build/ai/metered-actions#what-happens-on-each-call) walks through each step, and [Spend credits in your functions](https://nuxt-backend.dev/docs/build/billing/credits/spending#reserve-run-settle) explains the reservation underneath. A job holds its credits for as long as it waits instead, and charges them when it starts ([Charge for a queued job](https://nuxt-backend.dev/docs/build/ai/jobs/credits)).

### Who pays

The billing entity pays: the active workspace by default, so its members share one balance, or the person, when `setupBilling` is set to `billTo: 'user'`. The spend is always the member's own, checked fresh on every call, so someone removed from the workspace is refused on their next call.

### Why no budget service

A budget is a cap per workspace and window, such as 500 credits a day. It is recorded as a rate-limit window, so there is no separate budget store to keep in step with the credits: the credits are the ledger, and the provider keeps it. See [Budgets and costs](https://nuxt-backend.dev/docs/build/ai/models#budgets-and-costs).

## What powers it

- **The AI SDK** (`ai`, an optional peer) runs the model calls and streams. An app that never names a model never installs it.
- **The Convex AI Gateway** answers every provider's models on a paid Convex plan, with no key: Convex bills your team and reports each call's cost. Its AI SDK provider, `@convex-dev/ai-sdk-provider`, is an optional peer too.
- **The Vercel AI Gateway** answers where the Convex one does not, such as a free plan or a self-hosted backend, with one key, `AI_GATEWAY_API_KEY`. It is also the route for pinning inference to the EU ([Choose a model](https://nuxt-backend.dev/docs/build/ai/models)).
- **Convex persistent text streaming** keeps a streamed answer on the server, so a reload or a second device reads it.
- **The billing capability**, powered by Polar, holds the credit meters, the plans and packs that grant credits, and the usage events.
- **Convex's Workflow and rate limiter components** run the job queue: each job's durable steps, and a calls-per-minute limit for each model that halves on a `429`.
- **Convex's Agent and RAG components**, optional peers, hold chat threads and the documents knowledge search answers from.
- **A safety classifier through the gateway** checks prompts and uploads, and, once `setupAi({ provenance })` turns them on, **C2PA content credentials** in every image stored through `ctx.storeImage` say a model made it.

[Capabilities](https://nuxt-backend.dev/docs/start/concepts/capabilities) shows how the `ai` capability sits beside the others, and how `npx nuxt-backend status` reads its state. The [AI gateway](https://nuxt-backend.dev/docs/services/ai-gateway) service page has the plans, keys and spending caps.

## In development

Development runs against real models from the first call, through the same route as production:

- **On a paid Convex plan**, your dev deployment reaches the Convex AI Gateway with no key. A local deployment does once it is linked to a project with `npx convex login`.
- **Otherwise**, add `AI_GATEWAY_API_KEY` to `.env.local` and run `npx nuxt-backend env push`.
- **Credits** come from your billing sandbox: buy a pack with the test card, then spend it at the model's real cost ([Develop on real sandboxes](https://nuxt-backend.dev/docs/develop/sandboxes#ai-through-the-convex-ai-gateway)).
- **Tests** under Vitest answer every model id from a mock, so a test of a metered action needs no key and makes no network call.

There is no stand-in model: a deployment with no route to one refuses the call with `unavailable` (reason `not_configured`), and a test answers from its mock ([Choose a model](https://nuxt-backend.dev/docs/build/ai/models#develop-and-test)).

The [demo](https://nuxt-backend.dev/docs/start/playground) draws strips with a generation job: a [strip](https://nuxt-backend.dev/playground/strips) costs 30 credits, and a run that fails refunds them. On the demo, AI stops for the rest of a UTC day once that day's model calls reach `spendLimit`.

## In production

1. **Sync the catalog to production.** The credit meter, and the plans and packs that grant its credits: `npx nuxt-backend billing sync --env production`. See [Define and sync your catalog](https://nuxt-backend.dev/docs/build/billing/catalog).
2. **Pick the model route.** On a paid Convex plan, install `@convex-dev/ai-sdk-provider` and the Convex AI Gateway answers, with no key. Otherwise create a Vercel AI Gateway key for production, with a budget of its own, and set it with `npx convex env set --prod AI_GATEWAY_API_KEY`: `env push --prod` never sends `.env.local`'s development key. See [Where a model id goes](https://nuxt-backend.dev/docs/build/ai/models#where-a-model-id-goes).
3. **Cap the gateway's spend.** Set a spending limit for your Convex team, and a warning on the deployment's AI Gateway usage, or a budget on the Vercel AI Gateway. Either way a mistake in your prices can never cost more than you chose. See [Cap what the gateway spends](https://nuxt-backend.dev/docs/build/ai/models#cap-what-the-gateway-spends).
4. **Run npx nuxt-backend doctor --prod.** It reports the route `deployment-ai-gateway-api-key` finds for your models, fails when none is ready, and probes the stream route (`ai-stream-route`). See [Production checklist](https://nuxt-backend.dev/docs/ship/checklist).
5. **Label what a model made.** Under the EU AI Act, generated images and text people may take for real need a label from 2026-08-02. See [Disclose AI-generated content](https://nuxt-backend.dev/docs/ship/compliance/ai-transparency); that page is not legal advice.

To stop every metered call at once, with no deploy, turn on the `pause_ai` kill switch in the admin console: see [Pause AI](https://nuxt-backend.dev/docs/build/ai/models#pause-ai).

## What you get

What the package ships for this part of the app, generated from the code: each name links to its
reference entry.

- Pages: [`/report`](https://nuxt-backend.dev/docs/reference/backend/http-routes#in-the-nuxt-app)
- Components: [`<AiLabel>`](https://nuxt-backend.dev/docs/reference/components/shell#ailabel)
- Composables: [`useAiStream`](https://nuxt-backend.dev/docs/reference/api/use-ai-stream), [`useJob`](https://nuxt-backend.dev/docs/reference/api/use-job), [`useAiLabel`](https://nuxt-backend.dev/docs/reference/nuxt/composables#at-a-glance), [`useChat`](https://nuxt-backend.dev/docs/reference/nuxt/composables#at-a-glance)
- Routes: [`/ai/stream`](https://nuxt-backend.dev/docs/reference/backend/http-routes)
- Setup helpers: [`defineJobQueue`](https://nuxt-backend.dev/docs/reference/api/ai), [`setupAi`](https://nuxt-backend.dev/docs/reference/api/ai)
- Findings: [`ai-stream-route`](https://nuxt-backend.dev/docs/reference/cli/diagnostics#ai-stream-route), [`ai-components`](https://nuxt-backend.dev/docs/reference/cli/diagnostics#ai-components), [`ai-spend-limit`](https://nuxt-backend.dev/docs/reference/cli/diagnostics#ai-spend-limit), [`ai-gateway-budget`](https://nuxt-backend.dev/docs/reference/cli/diagnostics#ai-gateway-budget), [`ai-moderation-images`](https://nuxt-backend.dev/docs/reference/cli/diagnostics#ai-moderation-images), [`ai-moderation-providers`](https://nuxt-backend.dev/docs/reference/cli/diagnostics#ai-moderation-providers), [`deployment-content-safety-endpoint`](https://nuxt-backend.dev/docs/reference/cli/diagnostics#deployment-content-safety-endpoint), [`deployment-content-safety-key`](https://nuxt-backend.dev/docs/reference/cli/diagnostics#deployment-content-safety-key), [`deployment-second-opinion-key`](https://nuxt-backend.dev/docs/reference/cli/diagnostics#deployment-second-opinion-key), [`deployment-known-abuse-key`](https://nuxt-backend.dev/docs/reference/cli/diagnostics#deployment-known-abuse-key), [`deployment-ai-gateway-api-key`](https://nuxt-backend.dev/docs/reference/cli/diagnostics#deployment-ai-gateway-api-key), [`deployment-provenance-private-key`](https://nuxt-backend.dev/docs/reference/cli/diagnostics#deployment-provenance-private-key), [`deployment-provenance-certificate`](https://nuxt-backend.dev/docs/reference/cli/diagnostics#deployment-provenance-certificate)

## In this section

<card-group>
<card icon="i-lucide-boxes" title="Choose a model" to="https://nuxt-backend.dev/docs/build/ai/models">

Call any provider's model by its `provider/model` id, pin inference to the EU, cap what the gateway spends, set budgets, and charge the cost each call reports.

</card>

<card icon="i-lucide-sparkles" title="Meter an AI action" to="https://nuxt-backend.dev/docs/build/ai/metered-actions">

Wrap an action so it is rate-limited and paid for in credits, set a flat or token-priced cost, and ship a complete feature.

</card>

<card icon="i-lucide-audio-lines" title="Stream a response" to="https://nuxt-backend.dev/docs/build/ai/streaming">

Stream a metered answer that persists on the server, drive it with `useAiStream`, and resume it after a reload.

</card>

<card icon="i-lucide-list-ordered" title="Run generation jobs" to="https://nuxt-backend.dev/docs/build/ai/jobs">

Long generations in a fair queue, a head start for paid plans, model limits that adapt, credits held until a job starts, one guest try, and dead letters.

</card>

<card icon="i-lucide-messages-square" title="Add chat" to="https://nuxt-backend.dev/docs/build/ai/chat">

Threads each person keeps, every reply streamed, persisted and paid for in credits, and answers grounded in each workspace's documents.

</card>

<card icon="i-lucide-shield-alert" title="Keep content safe" to="https://nuxt-backend.dev/docs/build/ai/content-safety">

Check prompts and uploads with a safety model, guard against prompt injection, let people report content, and review reports.

</card>

<card icon="i-lucide-badge-check" title="Label AI-generated content" to="https://nuxt-backend.dev/docs/build/ai/provenance">

C2PA content credentials and the IPTC source type in generated images, a provenance record on every result, and a visible label.

</card>
</card-group>

## Not covered here

- **The model's work.** The package routes and meters the call. The prompt and what you do with the answer are yours, written with the AI SDK.
- **Your price.** The gateway reports what a call cost you. What you charge for it, `creditsPerUsd` or a token price table, is yours to set.
- **Agents that use your app.** An MCP endpoint your users' agents sign in to waits for a later release: see the [Roadmap](https://nuxt-backend.dev/docs/start/roadmap#service-app-features). Agents that build your app are on [Build with a coding agent](https://nuxt-backend.dev/docs/develop/agents).
- **The docs site's Ask AI.** That is the docs site's own assistant for these docs, a separate thing from the `ai` capability: it shares no key, model or credits with your app.


## Sitemap

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