# Token estimate

A live token and cost estimate under the composer that never jitters, knows the model's context window, and says how much room is left before it runs out.

> chatcn Pro: needs a Pro license (https://chatcn.dev/pro). Install from the `@chatcn-pro` registry with your license token; setup at https://chatcn.dev/docs/chatcn-pro.

Example: Token estimate. A contracts lead types a question, then pastes a clause again and again. The count glides, the row never shifts, and it warns near the limit. Preview: https://chatcn.dev/preview/token-estimate-demo

## Installation

```bash
npx shadcn@latest add @chatcn-pro/token-estimate
```

## Usage

```tsx
import { TokenEstimate } from "@/components/ui/chatcn/pro/token-estimate"

<PromptInput value={draft} onValueChange={setDraft}>…</PromptInput>
<TokenEstimate
  text={draft}
  model={{ name: "Claude Opus 5.5", contextWindow: 128_000, inputPrice: 5 }}
  context={[{ key: "messages", label: "Conversation", tokens: 71_940 }]}
  reserve={8_000}
  onStatusChange={(s) => setBlocked(s === "over")}
/>
```

The row says what this message costs: its tokens, the input cost of sending it with everything already in the context, and how full the window is. Each number sits in a slot reserved for its widest value and aligned to the end, so it grows into empty space and nothing beside it moves; between values it glides for 160 ms, so a paste reads as one change instead of a flicker of digits. Near the limit the start of the row turns amber and says how much room is left; over it, red, with by how much and what to do, and `onStatusChange` lets you hold Send. The ring is the free `ContextMeter`, with this message and the reply reserve as their own segments in its breakdown. The default `estimateTokenCount` is a fast heuristic within about 10% on English prose and code; pass your tokenizer as `estimate` for exact counts.

## Examples

### States

Example: States. Nothing typed, a short question, a pasted clause with a reply reserve, near the limit, over it, no price, and attached files. Preview: https://chatcn.dev/preview/token-estimate-states

## Props

| Prop | Type | Notes |
| --- | --- | --- |
| `text` | `string` | The draft. Estimated on every change |
| `model` | `{ name, contextWindow, inputPrice? }` | Price is US dollars per million input tokens. No price hides the cost |
| `context` | `ContextSegment[]` | What is already in the window: system, conversation, files |
| `attached` | `number` | Tokens that go with this message, such as files |
| `reserve` | `number` | Tokens kept free for the reply, counted against the limit |
| `estimate` | `(text) => number` | Your tokenizer. Default `estimateTokenCount` |
| `nearAt` | `number` | Fraction of the window that warns. Default 0.85 |
| `onStatusChange` | `("empty" | "ok" | "near" | "over") => void` | For example to disable Send when over |
| `labels` | `Partial<TokenEstimateLabels>` | Every string, for translation |

## Accessibility

- Crossing into near or over is announced once in a polite live region, never per keystroke.
- Warnings carry an icon and words, not color alone.
- The ring is a button that opens the breakdown, named with the share of the window in use.
- The cost has a title that names the price it was computed at.

## Motion

Numbers glide to their new value in 160 ms on an ease out cubic, in place, inside reserved tabular slots. Reduced motion shows the value directly. Nothing else animates.

Source: https://chatcn.dev/docs/components/token-estimate