---
title: "Charge for usage beyond the plan"
description: "A plan that includes a monthly allowance of credits and bills what goes past it at the end of the cycle, with a cap: the catalog shape, spends that may go past the balance only on that plan, and the overage shown to the customer."
canonical_url: "https://nuxt-backend.dev/docs/build/billing/subscriptions/overage"
---
# Charge for usage beyond the plan

> A plan that includes a monthly allowance of credits and bills what goes past it at the end of the cycle, with a cap: the catalog shape, spends that may go past the balance only on that plan, and the overage shown to the customer.

Version: nuxt-backend 0.6.x · Tab: Build · Kind: recipe · Status: stable

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

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

- [API reference](https://nuxt-backend.dev/docs/reference/api/use-credits)
- [Source](https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/billing/credits.ts)

Some work can't stop at zero: a strip halfway through its panels, a render someone's deadline depends on. Pay-as-you-go past an allowance fits it. The plan includes 500 credits a month, and every credit past them is billed at the end of the cycle, at a price per credit and up to a cap. Polar meters the usage, prices it and invoices it. The package lets a spend go past the balance only where the plan says so, and shows the customer where they stand.

## Ingredients

- A plan with `credits` and a `usage` price on the same meter: [Choose a pricing model](https://nuxt-backend.dev/docs/build/billing/pricing-models#usage-beyond-the-plan).
- A feature that marks the plans allowed past their balance: [Gate features by plan](https://nuxt-backend.dev/docs/build/billing/subscriptions/feature-gating).
- `reserveCredits` with `allowOverage`, then `settleSpend` or `releaseSpend`: [Spend credits in your functions](https://nuxt-backend.dev/docs/build/billing/credits/spending#reserve-run-settle).
- `useCredits().overage` and `<UsageHistory>`: [Sell credits and top-ups](https://nuxt-backend.dev/docs/build/billing/credits).

## Steps

<steps level="3">

### Price the usage

Give the plan its allowance and a metered price on the same meter. `unitAmount` is what one credit past the allowance costs, in cents (a string for less than a cent), and `cap` the most one cycle may bill. Mark the plan with a feature, `overage`, so your functions can tell which plans may go past their balance. Run `npx nuxt-backend billing sync`: it creates the metered price beside the plan's fixed one.

**Checkpoint:** `/pricing` shows Studio with its 500 credits a month, and `npx nuxt-backend doctor --only billing` passes `billing-meter-usage`.

### Let the spend go past the balance

In the function that does the work, ask whether the plan has `overage`, and pass the answer as `allowOverage`. On Studio the reservation goes through past zero, and the balance turns negative. On any other plan an empty balance refuses the call as `insufficient_credits`, and nothing is charged. A run that fails releases its reservation either way.

**Checkpoint:** on Studio with the allowance used up, `generateChapter` runs and the balance reads below zero. On Free it is refused with `need` and `balance`.

### Show the customer where they stand

`useCredits().overage` is what the meter ran up past what was credited this cycle: the units Polar will bill. Show it beside the balance, and list the spends with `<UsageHistory>`. On the plans that stop at zero, the low-balance notice of `<BackendNotices>` warns before the balance runs out.

**Checkpoint:** after the run above, the page says how many credits went past the allowance, and the usage history lists the generation.

</steps>

## The code

<code-tree default-value="backend/billing.catalog.ts">

```ts [backend/billing.catalog.ts]
import { defineBillingCatalog } from 'nuxt-backend/billing'

export default defineBillingCatalog({
  meters: { credits: {} },
  plans: {
    free: { name: 'Free', prices: { month: 0 }, credits: { meter: 'credits', units: 20 } },
    studio: {
      name: 'Studio',
      prices: { month: 2900 },
      credits: { meter: 'credits', units: 500 },               // the allowance, every month
      usage: [{ meter: 'credits', unitAmount: 5, cap: 5000 }], // then 5 cents a credit, at most $50
      features: ['overage'],
    },
  },
  features: {
    overage: { description: 'Usage beyond the plan' },
  },
})
```

```ts [backend/chapters.ts]
import { v } from 'convex/values'
import { billing } from './billing'
import { workspace } from './functions'

const CREDITS_PER_CHAPTER = 10

export const generateChapter = workspace.action({
  args: { storyId: v.id('stories') },
  handler: async (ctx, { storyId }) => {
    // Only a plan with a usage price may go past its balance.
    const allowOverage = await billing.hasFeature(ctx, 'overage')
    const reservation = await billing.reserveCredits(ctx, {
      meter: 'credits',
      value: CREDITS_PER_CHAPTER,
      allowOverage,
    })
    try {
      const chapter = await writeChapter(ctx, storyId)
      await billing.settleSpend(ctx, reservation)
      return chapter
    }
    catch (error) {
      await billing.releaseSpend(ctx, reservation)
      throw error
    }
  },
})
```

```vue [app/pages/usage.vue]
<script setup lang="ts">
const credits = useCredits({ meter: 'credits' })
</script>

<template>
  <p>{{ credits.balance.value ?? '…' }} credits left this cycle</p>
  <p v-if="credits.overage.value">
    {{ credits.overage.value }} credits past your allowance, billed at the end of the cycle
  </p>
  <UsageHistory meter="credits" />
</template>
```

</code-tree>

`writeChapter` is your own code.

## Ship it

`billing sync` creates the metered price, so sync the catalog to production with `--env production` like the rest ([Go live](https://nuxt-backend.dev/docs/build/billing/setup#go-live)). Each settled spend is a usage event on the entity's Polar customer. Polar's dashboard shows it per customer, and its invoice at the end of the cycle bills what went past the allowance.

## Variations

- **Metered AI calls:** `ai.meteredAction` and `ai.stream` take `allowOverage` too, fixed per action. Declare one action for the plans with overage and one for the rest, or spend from your own action as above ([Meter an AI action](https://nuxt-backend.dev/docs/build/ai/metered-actions)).
- **Pure pay-as-you-go:** a plan with a usage price and no `credits` has nothing prepaid, so every unit is billed, and every spend on it passes `allowOverage: true`.
- **Packs on top:** a pack tops up the same meter and its credits roll over, so a customer who would rather prepay can.
- **Yearly plans, monthly overage:** `meterCycle: 'month'` on a yearly plan makes Polar invoice the overage and grant the allowance every month, while the base price stays yearly ([Grant credits every month on a yearly plan](https://nuxt-backend.dev/docs/build/billing/credits#grant-credits-every-month-on-a-yearly-plan)).

The cap limits what Polar bills in a cycle, not what your functions let through: past it, usage is free. To stop a customer at a limit of yours, refuse in your own function before reserving, once the meter read from `billing.getCustomerState` is past it. Don't pass `allowOverage` for a plan without a usage price: its balance would go negative and nobody would be billed for it.

## Related

- [Polar](https://nuxt-backend.dev/docs/services/polar): meters, metered prices and the sandbox.
- [Choose a pricing model](https://nuxt-backend.dev/docs/build/billing/pricing-models#usage-beyond-the-plan): where usage pricing fits among the models.
- [Spend credits in your functions](https://nuxt-backend.dev/docs/build/billing/credits/spending#reserve-run-settle): reserve, run and settle.
- [Sell credits and top-ups](https://nuxt-backend.dev/docs/build/billing/credits): `useCredits().overage` and the meter cycle.
- [Show orders, invoices and usage](https://nuxt-backend.dev/docs/build/billing/subscriptions/orders): the usage history and the invoices.


## Sitemap

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