# Spend limits

Caps per member and per agent: spend against each, the day it gets there, and what happens at the limit.

> 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: Spend limits. Four members and three agents mid month: one near, one past, one with no limit. Click an amount to edit it. Preview: https://chatcn.dev/preview/spend-limits-demo

## Installation

```bash
npx shadcn@latest add @chatcn-pro/spend-limits
```

## Usage

```tsx
import { SpendLimits } from "@/components/ui/chatcn/pro/spend-limits"

<SpendLimits
  limits={[
    { id: "priya", kind: "member", name: "Priya Raman", detail: "priya@northwind.dev", spent: 264, cap: 300, atCap: "approve" },
    { id: "evals", kind: "agent", name: "Nightly evals", detail: "Runs at 02:00", spent: 188, cap: 200, atCap: "pause" },
  ]}
  period={{ start, end }}
  budget={1500}
  onCapChange={(id, cap) => saveCap(id, cap)}       // cap is null for no limit
  onAtCapChange={(id, mode) => saveMode(id, mode)}   // "pause" | "alert" | "approve"
/>
```

Members and agents sit on two tabs. Each row shows what was spent against its limit with a bar that turns amber near it and red past it, and, at the pace so far this period, the day it gets there. What happens at the limit is set per row: pause new requests, keep running and alert admins, or make each request wait for an admin; a row past its limit says which one happened. Click the amount to edit the limit in place (Enter saves, Escape cancels, No limit removes it). With `budget`, limits that add up past the workspace budget are pointed out once. Pair it with `budget-guard` for the workspace cap itself.

## Examples

### States

Example: States. Limits that add up past the workspace budget, and a read only narrow frame. Preview: https://chatcn.dev/preview/spend-limits-states

## Props

| Prop | Type | Notes |
| --- | --- | --- |
| `limits` | `SpendLimit[]` | `{ id, name, kind, detail?, spent, cap, atCap? }` |
| `period` | `{ start, end }` | For the forecast |
| `budget` | `number` | The workspace budget, to check the sum against |
| `onCapChange` | `(id, cap | null) => void` | Makes amounts editable |
| `onAtCapChange` | `(id, mode) => void` | Shows the at the limit menu |
| `currency`, `locale`, `now` |  | Formatting and the forecast |
| `labels` | `Partial<SpendLimitsLabels>` | Every string, for translation |

## Keyboard

| Key | Action |
| --- | --- |
| Enter | Save a limit |
| Escape | Cancel editing |

## Accessibility

- Amounts are buttons named with the person and the current limit.
- The at the limit menu is a radio group with a line on what each choice does.
- A reached limit is a status message, in words as well as color.

## Motion

None.

Source: https://chatcn.dev/docs/components/spend-limits