# Usage meter

> Credits, messages or context left: quiet when there’s plenty, clear before it runs out.

Running out mid-task is the worst way to learn about a limit. The meter stays quiet while there’s plenty, with numbers that roll as they’re spent; it turns amber and opens the way to upgrade once a fifth is left; and when you’re out the numbers fold away as the words glide into "No credits left" and "Resets Oct 12" morphs into "You’re out until Oct 12." Use the bar in settings and menus, or the ring, small enough for a composer toolbar, for how full the context window is.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/usage-meter.json
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): `number-roll`, `text-morph`
- Docs and live preview: https://hairlineui.com/ui/usage-meter
- Registry item (source): https://hairlineui.com/r/usage-meter.json

## Usage

```tsx
import { UsageMeter } from "@/components/usage-meter";

<UsageMeter used={760} limit={2000} unit="credits" resetAt={resetAt} onUpgrade={openPricing} />
<UsageMeter variant="ring" label="Context" unit="tokens" used={54_000} limit={128_000} />
```

## With the AI SDK

```tsx
// app/api/chat/route.ts: attach token usage to each answer
return result.toUIMessageStreamResponse({
  messageMetadata: ({ part }) =>
    part.type === "finish" ? { totalTokens: part.totalUsage.totalTokens } : undefined,
});

// The composer toolbar: how full the context window is
import { useChat } from "@ai-sdk/react";
import type { UIMessage } from "ai";
import { UsageMeter } from "@/components/usage-meter";

type Message = UIMessage<{ totalTokens?: number }>;

function ContextMeter() {
  const { messages } = useChat<Message>();
  // The last answer's total covers everything sent with it plus what it wrote.
  const used = messages.findLast((m) => m.role === "assistant")?.metadata?.totalTokens ?? 0;
  return <UsageMeter variant="ring" label="Context" unit="tokens" used={used} limit={128_000} />;
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `used / limit` | `number` | – | How much is used, out of how much. |
| `unit / label` | `string` | `"credits" / "Usage"` | What’s counted, and the meter’s name. |
| `resetAt` | `number \| Date` | – | When it resets; shown under the bar and in the out state. |
| `variant` | `"bar" \| "ring"` | `"bar"` | A full-width bar, or a 16px ring with a percentage. |
| `lowAt` | `number` | `0.2` | Share left at which it turns amber. |
| `onUpgrade / upgradeLabel` | `() => void / string` | `"Upgrade"` | Shown when low or out. |

## States

- **normal**: "1,240 of 2,000 credits left" over a neutral bar, and when it resets. Spending rolls the numbers (700ms) and eases the bar.
- **low**: The bar turns amber once a fifth (lowAt) is left and the Upgrade link opens in beside the reset date.
- **out**: Red. The numbers fold away while "credits left" morphs into "No credits left"; the footer morphs to "You’re out until Oct 12."
- **ring**: A ring that fills and a percent that rolls; amber when low, red when full.

## What it handles

Running out mid-task is the worst way to learn about a limit. This stays quiet while there is plenty and becomes clearer as the limit gets close.

- **Quiet when there is plenty**: The numbers roll as credits are spent and the bar eases, with nothing shouting.
- **Amber before it runs out**: Once a fifth is left (you can change that) the bar turns amber and an Upgrade link opens in.
- **Clear when it is out**: The numbers fold away, the words become “No credits left”, and the footer reads “You’re out until Oct 12.”
- **A bar or a ring**: Use the bar in settings and menus, or a small ring that fits a composer toolbar.
- **Read as words**: The meter reports “1,240 of 2,000 credits left” to screen readers, not just a percentage.
- **Any unit**: Count credits, messages, tokens or anything else, and name the meter.

## Replaces

a progress bar, a low-credit warning, an out-of-credits message, a reset date line, an upgrade prompt, a context ring

## Where it fits

- **AI apps with credits**: Show credits or messages left and when they reset.
- **Settings and billing menus**: A bar in the account menu that shows how much is used.
- **Composer toolbars**: A small ring for how full the context window is.
- **API products**: A quota meter that nudges people toward upgrading before they hit it.

## Notes

- role="meter" with aria-valuetext like "1,240 of 2,000 credits left", so the number is read, not a percentage.
- Colour is never the only signal: the text changes too, and the ring has a title with the full summary.
- Numbers are mono and tabular, so they don’t jitter as they roll. The rolling text is decorative; the meter’s aria-valuetext carries the real figures.
- Installs Number roll and Text morph. With reduced motion the bar, ring and numbers change in place.

License: MIT (https://hairlineui.com/license#components)
