---
title: "nuxt-backend/ai"
description: "Every export of nuxt-backend/ai, with its signature and where it is defined."
canonical_url: "https://nuxt-backend.dev/docs/reference/api/ai"
---
# nuxt-backend/ai

**Experimental**

The rails for selling metered AI features — `nuxt-backend/ai`: wrap any
Convex action so it is rate-limited, prepaid-credit-metered (reserve → run →
settle — a failed run consumes nothing), and optionally token-streamed to
the browser with the text persisted server-side (reload mid-stream and it
continues).

Composes the billing spend reservation (`setupBilling`), the rate limiter,
and the upstream persistent-text-streaming component — usage recording IS
the provider event, so per-customer usage shows up in the provider's own
portal and dashboard with no extra tables here.

Experimental: the metering model (reserve/settle over provider meter
events) is the newest part of the package and may change shape in a minor
release; `useAiStream` on the Vue side is experimental with it.

## Interfaces

### AiRateLimiter

Defined in: [src/convex/integrations/ai/config.ts](https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts)

**Experimental**

The rate limiter shape `setupAi` consumes — satisfied by a
`setupRateLimiter(...)` instance, which seeds the `ai` and `aiBudget` limits
by default. Typed against the literal default names (the limiter's
conditional rest-tuple signature only resolves for known names — the same
pattern as `BillingRateLimiter`); custom `limit:` names pass through a cast
at the call site.

#### Methods

##### limit()

```ts
limit(
   ctx, 
   name, 
   options?
): Promise<{
  ok: boolean;
  retryAfter?: number;
}>;
```

Defined in: [src/convex/integrations/ai/config.ts](https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts)

**Experimental**

###### Parameters

<table>
<thead>
  <tr>
    <th>
      Parameter
    </th>
    
    <th>
      Type
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        ctx
      </code>
    </td>
    
    <td>
      <code>
        RunWriteCtx
      </code>
      
       & { <code>
        auth?
      </code>
      
      : <code>
        Auth
      </code>
      
      ; }
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        name
      </code>
    </td>
    
    <td>
      <code>
        "ai"
      </code>
      
       | <code>
        "aiBudget"
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        options?
      </code>
    </td>
    
    <td>
      { <code>
        key?
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        throws?
      </code>
      
      : <code>
        boolean
      </code>
      
      ; <code>
        count?
      </code>
      
      : <code>
        number
      </code>
      
      ; <code>
        config?
      </code>
      
      : <a href="https://nuxt-backend.dev/docs/reference/api/rate-limit#ratelimitconfig">
        <code>
          RateLimitConfig
        </code>
      </a>
      
      ; }
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        options.key?
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        options.throws?
      </code>
    </td>
    
    <td>
      <code>
        boolean
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        options.count?
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        options.config?
      </code>
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/reference/api/rate-limit#ratelimitconfig">
        <code>
          RateLimitConfig
        </code>
      </a>
    </td>
  </tr>
</tbody>
</table>

###### Returns

`Promise`<{
`ok`: `boolean`;
`retryAfter?`: `number`;
}>

---

### AiComponents

Defined in: [src/convex/integrations/ai/config.ts](https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts)

**Experimental**

The component handles `setupAi` reads from your generated `components` object.

#### Properties

<table>
<thead>
  <tr>
    <th>
      Property
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Description
    </th>
    
    <th>
      Defined in
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <a id="backend">
        
      </a>
      
       <code>
        backend
      </code>
    </td>
    
    <td>
      { <code>
        ai
      </code>
      
      : { <code>
        createRequest
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        name
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        entityId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        userId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        args
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        meterId?
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        cost
      </code>
      
      : <code>
        number
      </code>
      
      ; <code>
        externalId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        null
      </code>
      
      >; <code>
        getByStream
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "query"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, | { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        name
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        entityId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        userId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        args
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        meterId?
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        cost
      </code>
      
      : <code>
        number
      </code>
      
      ; <code>
        externalId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        status
      </code>
      
      : <code>
        "reserved"
      </code>
      
       | <code>
        "settled"
      </code>
      
       | <code>
        "released"
      </code>
      
      ; <code>
        createdAt
      </code>
      
      : <code>
        number
      </code>
      
      ; } | <code>
        null
      </code>
      
      >; <code>
        markSettled
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        null
      </code>
      
      >; <code>
        markReleased
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        null
      </code>
      
      >; }; <code>
        billing
      </code>
      
      : { <code>
        finalize
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        userId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        externalId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        finalAmount?
      </code>
      
      : <code>
        number
      </code>
      
      ; }, { <code>
        settled
      </code>
      
      : <code>
        boolean
      </code>
      
      ; <code>
        released
      </code>
      
      : <code>
        number
      </code>
      
      ; <code>
        balance
      </code>
      
      : <code>
        number
      </code>
      
      ; <code>
        balanceBefore
      </code>
      
      : <code>
        number
      </code>
      
      ; <code>
        releaseJobId?
      </code>
      
      : <code>
        string
      </code>
      
      ; }>; <code>
        release
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        userId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        externalId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        null
      </code>
      
      >; <code>
        attachReleaseJob
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        userId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        externalId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        jobId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        null
      </code>
      
      >; }; }
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        backend.ai
      </code>
    </td>
    
    <td>
      { <code>
        createRequest
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        name
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        entityId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        userId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        args
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        meterId?
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        cost
      </code>
      
      : <code>
        number
      </code>
      
      ; <code>
        externalId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        null
      </code>
      
      >; <code>
        getByStream
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "query"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, | { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        name
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        entityId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        userId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        args
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        meterId?
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        cost
      </code>
      
      : <code>
        number
      </code>
      
      ; <code>
        externalId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        status
      </code>
      
      : <code>
        "reserved"
      </code>
      
       | <code>
        "settled"
      </code>
      
       | <code>
        "released"
      </code>
      
      ; <code>
        createdAt
      </code>
      
      : <code>
        number
      </code>
      
      ; } | <code>
        null
      </code>
      
      >; <code>
        markSettled
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        null
      </code>
      
      >; <code>
        markReleased
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        null
      </code>
      
      >; }
    </td>
    
    <td>
      -
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        backend.ai.createRequest
      </code>
    </td>
    
    <td>
      <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        name
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        entityId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        userId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        args
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        meterId?
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        cost
      </code>
      
      : <code>
        number
      </code>
      
      ; <code>
        externalId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        null
      </code>
      
      >
    </td>
    
    <td>
      -
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        backend.ai.getByStream
      </code>
    </td>
    
    <td>
      <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "query"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, | { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        name
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        entityId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        userId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        args
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        meterId?
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        cost
      </code>
      
      : <code>
        number
      </code>
      
      ; <code>
        externalId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        status
      </code>
      
      : <code>
        "reserved"
      </code>
      
       | <code>
        "settled"
      </code>
      
       | <code>
        "released"
      </code>
      
      ; <code>
        createdAt
      </code>
      
      : <code>
        number
      </code>
      
      ; } | <code>
        null
      </code>
      
      >
    </td>
    
    <td>
      -
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        backend.ai.markSettled
      </code>
    </td>
    
    <td>
      <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        null
      </code>
      
      >
    </td>
    
    <td>
      -
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        backend.ai.markReleased
      </code>
    </td>
    
    <td>
      <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        null
      </code>
      
      >
    </td>
    
    <td>
      -
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        backend.billing
      </code>
    </td>
    
    <td>
      { <code>
        finalize
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        userId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        externalId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        finalAmount?
      </code>
      
      : <code>
        number
      </code>
      
      ; }, { <code>
        settled
      </code>
      
      : <code>
        boolean
      </code>
      
      ; <code>
        released
      </code>
      
      : <code>
        number
      </code>
      
      ; <code>
        balance
      </code>
      
      : <code>
        number
      </code>
      
      ; <code>
        balanceBefore
      </code>
      
      : <code>
        number
      </code>
      
      ; <code>
        releaseJobId?
      </code>
      
      : <code>
        string
      </code>
      
      ; }>; <code>
        release
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        userId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        externalId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        null
      </code>
      
      >; <code>
        attachReleaseJob
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        userId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        externalId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        jobId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        null
      </code>
      
      >; }
    </td>
    
    <td>
      The entitlement cache's reservation protocol. <code>
        setupAi
      </code>
      
       closes its own spends here rather than through <code>
        settleSpend
      </code>
      
      : only <code>
        finalize
      </code>
      
       can drop a reservation <em>
        and
      </em>
      
       hand back the unspent remainder atomically, and it reports the balance the low-balance hooks read.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        backend.billing.finalize
      </code>
    </td>
    
    <td>
      <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        userId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        externalId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        finalAmount?
      </code>
      
      : <code>
        number
      </code>
      
      ; }, { <code>
        settled
      </code>
      
      : <code>
        boolean
      </code>
      
      ; <code>
        released
      </code>
      
      : <code>
        number
      </code>
      
      ; <code>
        balance
      </code>
      
      : <code>
        number
      </code>
      
      ; <code>
        balanceBefore
      </code>
      
      : <code>
        number
      </code>
      
      ; <code>
        releaseJobId?
      </code>
      
      : <code>
        string
      </code>
      
      ; }>
    </td>
    
    <td>
      -
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        backend.billing.release
      </code>
    </td>
    
    <td>
      <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        userId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        externalId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        null
      </code>
      
      >
    </td>
    
    <td>
      -
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        backend.billing.attachReleaseJob
      </code>
    </td>
    
    <td>
      <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        userId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        externalId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        jobId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        null
      </code>
      
      >
    </td>
    
    <td>
      -
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="persistenttextstreaming">
        
      </a>
      
       <code>
        persistentTextStreaming
      </code>
    </td>
    
    <td>
      { <code>
        lib
      </code>
      
      : { <code>
        createStream
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { }, <code>
        string
      </code>
      
      , <code>
        string
      </code>
      
       | <code>
        undefined
      </code>
      
      >; <code>
        addChunk
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        text
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        final
      </code>
      
      : <code>
        boolean
      </code>
      
      ; }, <code>
        null
      </code>
      
      , <code>
        string
      </code>
      
       | <code>
        undefined
      </code>
      
      >; <code>
        setStreamStatus
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        status
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        null
      </code>
      
      , <code>
        string
      </code>
      
       | <code>
        undefined
      </code>
      
      >; <code>
        getStreamStatus
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "query"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        string
      </code>
      
      , <code>
        string
      </code>
      
       | <code>
        undefined
      </code>
      
      >; <code>
        getStreamText
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "query"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, { <code>
        text
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        status
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        string
      </code>
      
       | <code>
        undefined
      </code>
      
      >; <code>
        deleteStream
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        null
      </code>
      
      , <code>
        string
      </code>
      
       | <code>
        undefined
      </code>
      
      >; }; }
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        persistentTextStreaming.lib
      </code>
    </td>
    
    <td>
      { <code>
        createStream
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { }, <code>
        string
      </code>
      
      , <code>
        string
      </code>
      
       | <code>
        undefined
      </code>
      
      >; <code>
        addChunk
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        text
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        final
      </code>
      
      : <code>
        boolean
      </code>
      
      ; }, <code>
        null
      </code>
      
      , <code>
        string
      </code>
      
       | <code>
        undefined
      </code>
      
      >; <code>
        setStreamStatus
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        status
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        null
      </code>
      
      , <code>
        string
      </code>
      
       | <code>
        undefined
      </code>
      
      >; <code>
        getStreamStatus
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "query"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        string
      </code>
      
      , <code>
        string
      </code>
      
       | <code>
        undefined
      </code>
      
      >; <code>
        getStreamText
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "query"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, { <code>
        text
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        status
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        string
      </code>
      
       | <code>
        undefined
      </code>
      
      >; <code>
        deleteStream
      </code>
      
      : <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        null
      </code>
      
      , <code>
        string
      </code>
      
       | <code>
        undefined
      </code>
      
      >; }
    </td>
    
    <td>
      -
    </td>
    
    <td>
      
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        persistentTextStreaming.lib.createStream
      </code>
    </td>
    
    <td>
      <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { }, <code>
        string
      </code>
      
      , <code>
        string
      </code>
      
       | <code>
        undefined
      </code>
      
      >
    </td>
    
    <td>
      -
    </td>
    
    <td>
      node_modules/@convex-dev/persistent-text-streaming/dist/component/lib.d.ts:1
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        persistentTextStreaming.lib.addChunk
      </code>
    </td>
    
    <td>
      <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        text
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        final
      </code>
      
      : <code>
        boolean
      </code>
      
      ; }, <code>
        null
      </code>
      
      , <code>
        string
      </code>
      
       | <code>
        undefined
      </code>
      
      >
    </td>
    
    <td>
      -
    </td>
    
    <td>
      node_modules/@convex-dev/persistent-text-streaming/dist/component/lib.d.ts:2
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        persistentTextStreaming.lib.setStreamStatus
      </code>
    </td>
    
    <td>
      <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        status
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        null
      </code>
      
      , <code>
        string
      </code>
      
       | <code>
        undefined
      </code>
      
      >
    </td>
    
    <td>
      -
    </td>
    
    <td>
      node_modules/@convex-dev/persistent-text-streaming/dist/component/lib.d.ts:7
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        persistentTextStreaming.lib.getStreamStatus
      </code>
    </td>
    
    <td>
      <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "query"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        string
      </code>
      
      , <code>
        string
      </code>
      
       | <code>
        undefined
      </code>
      
      >
    </td>
    
    <td>
      -
    </td>
    
    <td>
      node_modules/@convex-dev/persistent-text-streaming/dist/component/lib.d.ts:11
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        persistentTextStreaming.lib.getStreamText
      </code>
    </td>
    
    <td>
      <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "query"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, { <code>
        text
      </code>
      
      : <code>
        string
      </code>
      
      ; <code>
        status
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        string
      </code>
      
       | <code>
        undefined
      </code>
      
      >
    </td>
    
    <td>
      -
    </td>
    
    <td>
      node_modules/@convex-dev/persistent-text-streaming/dist/component/lib.d.ts:14
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        persistentTextStreaming.lib.deleteStream
      </code>
    </td>
    
    <td>
      <a href="https://docs.convex.dev/api/modules/server#functionreference" rel="nofollow">
        <code>
          FunctionReference
        </code>
      </a>
      
      <<code>
        "mutation"
      </code>
      
      , <code>
        "internal"
      </code>
      
      , { <code>
        streamId
      </code>
      
      : <code>
        string
      </code>
      
      ; }, <code>
        null
      </code>
      
      , <code>
        string
      </code>
      
       | <code>
        undefined
      </code>
      
      >
    </td>
    
    <td>
      -
    </td>
    
    <td>
      node_modules/@convex-dev/persistent-text-streaming/dist/component/lib.d.ts:20
    </td>
  </tr>
</tbody>
</table>

---

### AiBudget

Defined in: [src/convex/integrations/ai/config.ts](https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts)

**Experimental**

A per-entity spending cap, enforced through the rate limiter as a fixed
window over spend *value*: each metered call consumes its cost in tokens, so
`{ units: 500, period: DAY }` is "500 credits a day per workspace", not "500
calls a day".

A cap, not a ledger — the window is the limiter's own state, there is no new
table, and (like every rate limit) consumption is never given back: a run
that fails, or finalizes under its estimate, still spent the estimate
against the window. Size the budget with that slack in mind.

#### Properties

<table>
<thead>
  <tr>
    <th>
      Property
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Description
    </th>
    
    <th>
      Defined in
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <a id="units">
        
      </a>
      
       <code>
        units
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Credits an entity may spend per window.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="period">
        
      </a>
      
       <code>
        period
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Window length in ms — <code>
        HOUR
      </code>
      
       / <code>
        DAY
      </code>
      
       from <code>
        nuxt-backend/rate-limit
      </code>
      
      .
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="limit-1">
        
      </a>
      
       <code>
        limit?
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       The named limit the window is recorded under. Default <code>
        'aiBudget'
      </code>
      
       (see <code>
        DEFAULT_LIMITS
      </code>
      
      ); name your own to keep separate budgets per feature.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
</tbody>
</table>

---

### CreditsBalanceEvent

Defined in: [src/convex/integrations/ai/config.ts](https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts)

**Experimental**

What crossed a balance threshold, handed to the low-balance hooks.

#### Properties

<table>
<thead>
  <tr>
    <th>
      Property
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Description
    </th>
    
    <th>
      Defined in
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <a id="entityid">
        
      </a>
      
       <code>
        entityId
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       The billing entity (workspace/user) charged.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="userid">
        
      </a>
      
       <code>
        userId
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       The member whose call spent the credits.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="meter">
        
      </a>
      
       <code>
        meter?
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       The configured meter name, when the spend named one.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="meterid">
        
      </a>
      
       <code>
        meterId?
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       The provider meter id.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="balance">
        
      </a>
      
       <code>
        balance
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Balance after the spend settled (negative when overage is allowed).
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="previousbalance">
        
      </a>
      
       <code>
        previousBalance
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Balance as of before the spend reserved.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="spent">
        
      </a>
      
       <code>
        spent
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Credits this spend actually consumed.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="threshold">
        
      </a>
      
       <code>
        threshold?
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       The configured threshold, for <code>
        onCreditsLow
      </code>
      
      .
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
</tbody>
</table>

---

### AiCreditsConfig

Defined in: [src/convex/integrations/ai/config.ts](https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts)

**Experimental**

Credit-side reactions to a settled spend.

The threshold lives here — on `setupAi` — rather than in
`appConfig.backend.*`: that is the client-side content layer (labels, plan
copy), and these hooks fire inside a Convex action where no Nuxt app config
exists. Keep them thin: send nothing from here (the lifecycle emails own
that), just record or hand off.

#### Properties

<table>
<thead>
  <tr>
    <th>
      Property
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Description
    </th>
    
    <th>
      Defined in
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <a id="lowbalancethreshold">
        
      </a>
      
       <code>
        lowBalanceThreshold?
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Fire <a href="#oncreditslow">
        onCreditsLow
      </a>
      
       when a settle takes the balance from above this to at or below it. Omit to never fire it.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="oncreditslow">
        
      </a>
      
       <code>
        onCreditsLow?
      </code>
    </td>
    
    <td>
      (<code>
        ctx
      </code>
      
      , <code>
        event
      </code>
      
      ) => <code>
        void
      </code>
      
       | <code>
        Promise
      </code>
      
      <<code>
        void
      </code>
      
      >
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       The balance just crossed <a href="#lowbalancethreshold">
        lowBalanceThreshold
      </a>
      
       downwards. Fires on the crossing only — a second spend that stays below it is silent, so this is safe to wire straight to a notification.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="oncreditsexhausted">
        
      </a>
      
       <code>
        onCreditsExhausted?
      </code>
    </td>
    
    <td>
      (<code>
        ctx
      </code>
      
      , <code>
        event
      </code>
      
      ) => <code>
        void
      </code>
      
       | <code>
        Promise
      </code>
      
      <<code>
        void
      </code>
      
      >
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       The balance just went from positive to zero-or-below. Same crossing rule.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
</tbody>
</table>

---

### SetupAiConfig

Defined in: [src/convex/integrations/ai/config.ts](https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts)

**Experimental**

#### Properties

<table>
<thead>
  <tr>
    <th>
      Property
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Description
    </th>
    
    <th>
      Defined in
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <a id="billing">
        
      </a>
      
       <code>
        billing
      </code>
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/reference/api/billing#billing">
        <code>
          Billing
        </code>
      </a>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       The <code>
        setupBilling(...)
      </code>
      
       instance — supplies entity resolution + the credit spend.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="ratelimiter">
        
      </a>
      
       <code>
        rateLimiter?
      </code>
    </td>
    
    <td>
      <a href="#airatelimiter">
        <code>
          AiRateLimiter
        </code>
      </a>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Your <code>
        setupRateLimiter(...)
      </code>
      
       instance. Metered actions/streams check their named limit (default <code>
        'ai'
      </code>
      
      ) per billing entity before reserving. Omit to leave them unthrottled.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="cors">
        
      </a>
      
       <code>
        cors?
      </code>
    </td>
    
    <td>
      { <code>
        origin?
      </code>
      
      : <code>
        string
      </code>
      
      ; }
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       CORS origin for the stream endpoint. Defaults to the <code>
        SITE_URL
      </code>
      
       env var.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        cors.origin?
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      -
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="budget">
        
      </a>
      
       <code>
        budget?
      </code>
    </td>
    
    <td>
      <a href="#aibudget">
        <code>
          AiBudget
        </code>
      </a>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       A per-entity credit budget on top of the call-rate limit. Requires <code>
        rateLimiter
      </code>
      
       (it is enforced through it).
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="credits">
        
      </a>
      
       <code>
        credits?
      </code>
    </td>
    
    <td>
      <a href="#aicreditsconfig">
        <code>
          AiCreditsConfig
        </code>
      </a>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Low-balance reactions fired after a spend settles.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="streamtimeout">
        
      </a>
      
       <code>
        streamTimeout?
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
      
       | <code>
        false
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       How long a started stream may hold its reservation before it is released automatically (ms). Default 5 minutes; <code>
        false
      </code>
      
       disables the timer.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
</tbody>
</table>

---

### MeteredUsage

Defined in: [src/convex/integrations/ai/config.ts](https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts)

**Experimental**

Usage details handed to metered handlers (`ctx.usage`).

#### Properties

<table>
<thead>
  <tr>
    <th>
      Property
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Description
    </th>
    
    <th>
      Defined in
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <a id="entityid-1">
        
      </a>
      
       <code>
        entityId
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       The billing entity (workspace/user) charged.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="userid-1">
        
      </a>
      
       <code>
        userId
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       The signed-in caller.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="meter-1">
        
      </a>
      
       <code>
        meter?
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Configured meter name, when metered.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="cost">
        
      </a>
      
       <code>
        cost
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Credits this call costs — the estimate, for a finalized cost.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
</tbody>
</table>

---

### FinalizedCost

Defined in: [src/convex/integrations/ai/config.ts](https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts)

**Experimental**

A cost that is only known once the work is done: reserve `estimate`, run,
then settle what `finalize` returns and hand the remainder back.

The estimate is the *ceiling* — a final amount above it is clamped down (the
reservation is what made the spend safe against a concurrent one, so
settling beyond it would be a second, unguarded debit). Estimate high.

Requires a sum meter (one with a `property`): the finalized amount rides in
the ingested event's metadata, and a count meter can only ever count 1 per
event.

#### Example

```ts
cost: {
  estimate: 500,
  finalize: result => priceTokens({ model: result.model, ...result.usage }, prices),
}
```

#### Type Parameters

<table>
<thead>
  <tr>
    <th>
      Type Parameter
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <a id="type-parameter-args">
        
      </a>
      
       <code>
        Args
      </code>
      
       <em>
        extends
      </em>
      
       <code>
        PropertyValidators
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="type-parameter-result">
        
      </a>
      
       <code>
        Result
      </code>
    </td>
  </tr>
</tbody>
</table>

#### Properties

<table>
<thead>
  <tr>
    <th>
      Property
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Description
    </th>
    
    <th>
      Defined in
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <a id="estimate">
        
      </a>
      
       <code>
        estimate
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
      
       | ((<code>
        args
      </code>
      
      ) => <code>
        number
      </code>
      
      )
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Credits to reserve up front — a number or derived from the args.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="finalize">
        
      </a>
      
       <code>
        finalize
      </code>
    </td>
    
    <td>
      (<code>
        result
      </code>
      
      , <code>
        usage
      </code>
      
      ) => <code>
        number
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       The credits actually consumed, from what the handler returned.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
</tbody>
</table>

---

### MeteredActionConfig

Defined in: [src/convex/integrations/ai/config.ts](https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts)

**Experimental**

#### Type Parameters

<table>
<thead>
  <tr>
    <th>
      Type Parameter
    </th>
    
    <th>
      Default type
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <a id="type-parameter-args-2">
        
      </a>
      
       <code>
        Args
      </code>
      
       <em>
        extends
      </em>
      
       <code>
        PropertyValidators
      </code>
    </td>
    
    <td>
      -
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="type-parameter-result-2">
        
      </a>
      
       <code>
        Result
      </code>
    </td>
    
    <td>
      <code>
        unknown
      </code>
    </td>
  </tr>
</tbody>
</table>

#### Properties

<table>
<thead>
  <tr>
    <th>
      Property
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Description
    </th>
    
    <th>
      Defined in
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <a id="meter-2">
        
      </a>
      
       <code>
        meter
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
      
       | <code>
        false
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       The configured credit meter to charge (<code>
        setupBilling({ credits })
      </code>
      
       / catalog key) — or <code>
        false
      </code>
      
       for a rate-limit-only action.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="cost-1">
        
      </a>
      
       <code>
        cost?
      </code>
    </td>
    
    <td>
      <a href="#meteredcost">
        <code>
          MeteredCost
        </code>
      </a>
      
      <<code>
        Args
      </code>
      
      , <code>
        Result
      </code>
      
      >
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Credits per call. Default <code>
        1
      </code>
      
      . See <a href="#meteredcost">
        MeteredCost
      </a>
      
      .
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="limit-2">
        
      </a>
      
       <code>
        limit?
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
      
       | <code>
        false
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Named rate limit (checked per billing entity via the configured <code>
        rateLimiter
      </code>
      
      ). Default <code>
        'ai'
      </code>
      
      ; <code>
        false
      </code>
      
       disables the check.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="allowoverage">
        
      </a>
      
       <code>
        allowOverage?
      </code>
    </td>
    
    <td>
      <code>
        boolean
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Let this spend take the balance negative instead of refusing it — for a pay-as-you-go meter with no credit benefit behind it, where every unit is overage the provider invoices at the end of the cycle.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="charge">
        
      </a>
      
       <code>
        charge?
      </code>
    </td>
    
    <td>
      <code>
        "success"
      </code>
      
       | <code>
        "start"
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       When to charge: <code>
        'success'
      </code>
      
       (default) settles after the handler returns — a failed run consumes nothing; <code>
        'start'
      </code>
      
       settles before running, for work that is irrevocably consumed upstream the moment it starts. A finalized cost needs the result, so it only applies to <code>
        'success'
      </code>
      
      .
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="args">
        
      </a>
      
       <code>
        args
      </code>
    </td>
    
    <td>
      <code>
        Args
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="handler">
        
      </a>
      
       <code>
        handler
      </code>
    </td>
    
    <td>
      (<code>
        ctx
      </code>
      
      , <code>
        args
      </code>
      
      ) => <code>
        Promise
      </code>
      
      <<code>
        Result
      </code>
      
      >
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
</tbody>
</table>

---

### MeteredStreamConfig

Defined in: [src/convex/integrations/ai/config.ts](https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts)

**Experimental**

#### Type Parameters

<table>
<thead>
  <tr>
    <th>
      Type Parameter
    </th>
    
    <th>
      Default type
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <a id="type-parameter-args-3">
        
      </a>
      
       <code>
        Args
      </code>
      
       <em>
        extends
      </em>
      
       <code>
        PropertyValidators
      </code>
    </td>
    
    <td>
      -
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="type-parameter-result-3">
        
      </a>
      
       <code>
        Result
      </code>
    </td>
    
    <td>
      <code>
        unknown
      </code>
    </td>
  </tr>
</tbody>
</table>

#### Properties

<table>
<thead>
  <tr>
    <th>
      Property
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Description
    </th>
    
    <th>
      Defined in
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <a id="name">
        
      </a>
      
       <code>
        name
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Registry key — unique per app; the HTTP dispatcher routes by it.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="meter-3">
        
      </a>
      
       <code>
        meter
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
      
       | <code>
        false
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       The configured credit meter to charge, or <code>
        false
      </code>
      
       for limit-only.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="cost-2">
        
      </a>
      
       <code>
        cost?
      </code>
    </td>
    
    <td>
      <a href="#meteredcost">
        <code>
          MeteredCost
        </code>
      </a>
      
      <<code>
        Args
      </code>
      
      , <code>
        Result
      </code>
      
      >
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Credits per stream. Default <code>
        1
      </code>
      
      . A <a href="#finalizedcost">
        FinalizedCost
      </a>
      
       prices what the handler <em>
        returns
      </em>
      
       — return the model's token usage from it.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="limit-3">
        
      </a>
      
       <code>
        limit?
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
      
       | <code>
        false
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Named rate limit. Default <code>
        'ai'
      </code>
      
      ; <code>
        false
      </code>
      
       disables.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="allowoverage-1">
        
      </a>
      
       <code>
        allowOverage?
      </code>
    </td>
    
    <td>
      <code>
        boolean
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Let this spend take the balance negative (pay-as-you-go meters).
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="args-1">
        
      </a>
      
       <code>
        args
      </code>
    </td>
    
    <td>
      <code>
        Args
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="handler-1">
        
      </a>
      
       <code>
        handler
      </code>
    </td>
    
    <td>
      (<code>
        ctx
      </code>
      
      , <code>
        args
      </code>
      
      , <code>
        stream
      </code>
      
      ) => <code>
        Promise
      </code>
      
      <<code>
        Result
      </code>
      
      >
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Produce the stream: <code>
        append(text)
      </code>
      
       pushes a chunk to the client AND persists it. Credits settle when the handler finishes; an error or disconnect releases them — an interrupted stream never charges.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
</tbody>
</table>

---

### Ai

Defined in: [src/convex/integrations/ai/config.ts](https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts)

**Experimental**

#### Properties

<table>
<thead>
  <tr>
    <th>
      Property
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Description
    </th>
    
    <th>
      Defined in
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <a id="meteredaction">
        
      </a>
      
       <code>
        meteredAction
      </code>
    </td>
    
    <td>
      <<code>
        Args
      </code>
      
      , <code>
        Result
      </code>
      
      >(<code>
        config
      </code>
      
      ) => <code>
        RegisteredAction
      </code>
      
      <<code>
        "public"
      </code>
      
      >
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       A drop-in replacement for <code>
        action
      </code>
      
       that rate-limits, reserves prepaid credits, runs your handler, then settles the spend (usage lands in the billing provider as an ingested event). Failed runs release the reservation — nothing charged. <strong>
        Examples
      </strong>
      
       <code>
        export const generate = ai.meteredAction({ meter: 'credits', cost: args => args.long ? 5 : 1, args: { prompt: v.string(), long: v.optional(v.boolean()) }, handler: async (ctx, { prompt }) => runModel(prompt), })
      </code>
      
       <strong>
        Token pricing — reserve an estimate, settle the actual usage
      </strong>
      
       <code>
        export const generate = ai.meteredAction({ meter: 'credits', cost: { estimate: 200, finalize: result => priceTokens(result.usage, prices), }, args: { prompt: v.string() }, handler: async (ctx, { prompt }) => runModel(prompt), })
      </code>
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="stream">
        
      </a>
      
       <code>
        stream
      </code>
    </td>
    
    <td>
      <<code>
        Args
      </code>
      
      , <code>
        Result
      </code>
      
      >(<code>
        config
      </code>
      
      ) => { <code>
        start
      </code>
      
      : <code>
        RegisteredAction
      </code>
      
      <<code>
        "public"
      </code>
      
      >; <code>
        body
      </code>
      
      : <code>
        RegisteredQuery
      </code>
      
      <<code>
        "public"
      </code>
      
      >; }
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       A metered, persisted token stream. Returns <code>
        { start, body }
      </code>
      
       to re-export from your module — <code>
        useAiStream
      </code>
      
       drives them: <code>
        start
      </code>
      
       reserves credits and creates the stream, the HTTP endpoint (<code>
        registerBackendRoutes({ ai })
      </code>
      
      ) runs your handler and streams chunks, <code>
        body
      </code>
      
       serves the persisted text reactively (reload mid-stream and it continues). A started stream that is never delivered (the browser closed before the HTTP call, or it died mid-flight) holds its reservation until the scheduled auto-release hands the credits back — <code>
        streamTimeout
      </code>
      
      .
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="httphandler">
        
      </a>
      
       <code>
        httpHandler
      </code>
    </td>
    
    <td>
      <code>
        PublicHttpAction
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       The stream endpoint for <code>
        registerBackendRoutes({ ai })
      </code>
      
       (POST + CORS preflight).
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="corsorigin">
        
      </a>
      
       <code>
        corsOrigin
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       CORS headers for the stream route (used by <code>
        registerBackendRoutes
      </code>
      
      ).
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts" rel="nofollow">
        src/convex/integrations/ai/config.ts
      </a>
    </td>
  </tr>
</tbody>
</table>

---

### ModelPrice

Defined in: [src/convex/integrations/ai/pricing.ts](https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/pricing.ts)

**Experimental**

What one model charges, in credits per [ModelPrice.per](#per) tokens.

#### Properties

<table>
<thead>
  <tr>
    <th>
      Property
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Description
    </th>
    
    <th>
      Defined in
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <a id="input">
        
      </a>
      
       <code>
        input
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Credits per block of input (prompt) tokens.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/pricing.ts" rel="nofollow">
        src/convex/integrations/ai/pricing.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="output">
        
      </a>
      
       <code>
        output
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Credits per block of output (completion) tokens.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/pricing.ts" rel="nofollow">
        src/convex/integrations/ai/pricing.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="cachedinput">
        
      </a>
      
       <code>
        cachedInput?
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Credits per block of cache-read input tokens, when the model prices them separately. Omit and cached tokens are billed as ordinary input.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/pricing.ts" rel="nofollow">
        src/convex/integrations/ai/pricing.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="per">
        
      </a>
      
       <code>
        per?
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Tokens each rate covers. Default 1; use <code>
        1_000_000
      </code>
      
       for per-million rates.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/pricing.ts" rel="nofollow">
        src/convex/integrations/ai/pricing.ts
      </a>
    </td>
  </tr>
</tbody>
</table>

---

### TokenUsage

Defined in: [src/convex/integrations/ai/pricing.ts](https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/pricing.ts)

**Experimental**

A model call's token counts, as the SDKs report them.

#### Properties

<table>
<thead>
  <tr>
    <th>
      Property
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Description
    </th>
    
    <th>
      Defined in
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <a id="model">
        
      </a>
      
       <code>
        model
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       The model id — the key looked up in the price table.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/pricing.ts" rel="nofollow">
        src/convex/integrations/ai/pricing.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="input-1">
        
      </a>
      
       <code>
        input
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Input (prompt) tokens billed at the full rate.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/pricing.ts" rel="nofollow">
        src/convex/integrations/ai/pricing.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="output-1">
        
      </a>
      
       <code>
        output
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Output (completion) tokens.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/pricing.ts" rel="nofollow">
        src/convex/integrations/ai/pricing.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="cachedinput-1">
        
      </a>
      
       <code>
        cachedInput?
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <strong>
        <code>
          Experimental
        </code>
      </strong>
      
       Cache-read input tokens, when the model reports them separately.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/pricing.ts" rel="nofollow">
        src/convex/integrations/ai/pricing.ts
      </a>
    </td>
  </tr>
</tbody>
</table>

## Type Aliases

### MeteredCost

```ts
type MeteredCost<Args, Result> = 
  | number
  | ((args) => number)
| FinalizedCost<Args, Result>;
```

Defined in: [src/convex/integrations/ai/config.ts](https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/config.ts)

**Experimental**

Credits per call: fixed, derived from the args, or estimated then finalized.

#### Type Parameters

<table>
<thead>
  <tr>
    <th>
      Type Parameter
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <a id="type-parameter-args-1">
        
      </a>
      
       <code>
        Args
      </code>
      
       <em>
        extends
      </em>
      
       <code>
        PropertyValidators
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="type-parameter-result-1">
        
      </a>
      
       <code>
        Result
      </code>
    </td>
  </tr>
</tbody>
</table>

---

### TokenPriceTable

```ts
type TokenPriceTable = Record<string, ModelPrice>;
```

Defined in: [src/convex/integrations/ai/pricing.ts](https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/pricing.ts)

**Experimental**

The app's price list: model id → [ModelPrice](#modelprice).

## Functions

### priceTokens()

```ts
function priceTokens(
   usage, 
   table, 
   options?
): number;
```

Defined in: [src/convex/integrations/ai/pricing.ts](https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/pricing.ts)

**Experimental**

Price a model call in credits from its token usage and a price table the app
owns. No provider knows what a model costs you — rates change weekly and
only your margin decides the credit price — so the table is yours; this is
the arithmetic, pure and testable.

Rates are credits per `per` tokens (default 1). Express per-million pricing
as `per: 1_000_000` on the entry or as a table-wide `options.per`.

#### Parameters

<table>
<thead>
  <tr>
    <th>
      Parameter
    </th>
    
    <th>
      Type
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        usage
      </code>
    </td>
    
    <td>
      <a href="#tokenusage">
        <code>
          TokenUsage
        </code>
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        table
      </code>
    </td>
    
    <td>
      <a href="#tokenpricetable">
        <code>
          TokenPriceTable
        </code>
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        options?
      </code>
    </td>
    
    <td>
      { <code>
        per?
      </code>
      
      : <code>
        number
      </code>
      
      ; <code>
        round?
      </code>
      
      : <code>
        "up"
      </code>
      
       | <code>
        "nearest"
      </code>
      
       | <code>
        "none"
      </code>
      
      ; <code>
        minimum?
      </code>
      
      : <code>
        number
      </code>
      
      ; }
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        options.per?
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        options.round?
      </code>
    </td>
    
    <td>
      <code>
        "up"
      </code>
      
       | <code>
        "nearest"
      </code>
      
       | <code>
        "none"
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        options.minimum?
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
  </tr>
</tbody>
</table>

#### Returns

`number`

#### Example

```ts
const prices = {
  'fast-1': { input: 3, output: 15, per: 1_000_000 },
  'deep-1': { input: 15, output: 75, cachedInput: 1.5, per: 1_000_000 },
}
priceTokens({ model: 'fast-1', input: 12_000, output: 800 }, prices) // → 0.048
```

#### Throws

when the model is missing from the table — a silent 0 would be a
revenue leak, and an unpriced model is a deployment mistake worth surfacing.

---

### setupAi()

```ts
function setupAi(components, config): Ai;
```

Defined in: [src/convex/integrations/ai/setup.ts](https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/ai/setup.ts)

**Experimental**

Configure the AI rails over billing + rate limiting + streaming.

#### Parameters

<table>
<thead>
  <tr>
    <th>
      Parameter
    </th>
    
    <th>
      Type
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        components
      </code>
    </td>
    
    <td>
      <a href="#aicomponents">
        <code>
          AiComponents
        </code>
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        config
      </code>
    </td>
    
    <td>
      <a href="#setupaiconfig">
        <code>
          SetupAiConfig
        </code>
      </a>
    </td>
  </tr>
</tbody>
</table>

#### Returns

[`Ai`](#ai)

#### Example

```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 })
export const generate = ai.meteredAction({ ... })
export const { start: startEcho, body: echoBody } = ai.stream({ name: 'echo', ... })
```


## Sitemap

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