---
title: "Context Meter"
description: "A pill with a ring that shows how much of a model's context window a chat uses. The pill itself morphs into a breakdown by segment with what is left, turns to warning and danger colors near the limit, and offers a Compact action. The app counts the tokens."
category: "AI Agents"
publishedAt: "2026-11-29"
updatedAt: "2026-11-29"
documentation: "https://beui.dev/components/agents/context-meter"
markdown: "https://beui.dev/components/agents/context-meter.md"
license: "MIT"
---

# Context Meter

> A pill with a ring that shows how much of a model's context window a chat uses. The pill itself morphs into a breakdown by segment with what is left, turns to warning and danger colors near the limit, and offers a Compact action. The app counts the tokens.

## Install

```bash
npx shadcn@latest add @beui/context-meter
```

## Dependencies

- `@floating-ui/dom`
- `clsx`
- `lucide-react`
- `motion`
- `react`
- `react-dom`
- `tailwind-merge`

## Usage

```tsx
"use client";

import { Plus } from "lucide-react";
import { useEffect, useRef, useState } from "react";
import { type ContextSegment, ContextMeter } from "@/components/agents/context-meter";
import { Button } from "@/components/motion/button";

const LIMIT = 200_000;
const FIXED = { system: 6_200, tools: 14_800, files: 38_500 };
const ROOM = LIMIT - FIXED.system - FIXED.tools - FIXED.files;
// Each demo message adds roughly one long exchange.
const MESSAGE = 18_000;

export function ContextMeterPreview() {
  const [messages, setMessages] = useState(84_000);
  const [compacting, setCompacting] = useState(false);
  const timer = useRef<ReturnType<typeof setTimeout>>(undefined);
  useEffect(() => () => clearTimeout(timer.current), []);

  const segments: ContextSegment[] = [
    { key: "system", label: "System prompt", tokens: FIXED.system },
    { key: "tools", label: "Tools", tokens: FIXED.tools },
    { key: "files", label: "Files", tokens: FIXED.files },
    { key: "messages", label: "Messages", tokens: messages },
  ];

  const compact = () => {
    setCompacting(true);
    clearTimeout(timer.current);
    // Stands in for the model summarizing the conversation.
    timer.current = setTimeout(() => {
      setMessages(9_000);
      setCompacting(false);
    }, 900);
  };

  return (
    <div className="flex min-h-72 items-end gap-2">
      <ContextMeter segments={segments} limit={LIMIT} onCompact={compact} compacting={compacting} />
      <Button
        variant="ghost"
        size="sm"
        disabled={messages >= ROOM}
        onClick={() => setMessages((value) => Math.min(value + MESSAGE, ROOM))}
      >
        <Plus className="size-3.5" aria-hidden="true" />
        Add message
      </Button>
    </div>
  );
}
```

## API Reference

### ContextMeter

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `segments` | `ContextSegment[]` |  | Yes | What fills the context window, in display order. The meter shows their sum. |
| `limit` | `number` |  | Yes | Size of the context window in tokens. |
| `warnAt` | `number` | `0.8` | No | Share of the window (0 to 1) where the meter turns to the warning color. Default: 0.8 |
| `dangerAt` | `number` | `0.95` | No | Share of the window (0 to 1) where the meter turns to the danger color. Default: 0.95 |
| `onCompact` | `(() => void)` |  | No | Shows a Compact button in the panel and calls this when it is pressed. Shrink the segments yourself. |
| `compacting` | `boolean` | `false` | No | Shows the Compact button as busy and disables it while your compaction runs. Default: false |
| `formatTokens` | `((tokens: number) => string)` |  | No | Formats a token count for display. Default: compact notation, e.g. 42.1k |
| `side` | `"top" \| "bottom"` | `top` | No | Which way the pill grows into the panel. Default: "top" |
| `align` | `"start" \| "end"` | `start` | No | The pill's edge the panel stays lined up with as it grows. Default: "start" |
| `labels` | `Partial<{ trigger: string; collapse: string; tokens: string; free: string; warning: string; danger: string; compact: string; compacting: string; warningReached: string; }>` |  | No | Override any built-in string, e.g. for translation. |
| `className` | `string` |  | No | Classes merged onto the root element. |

## Types

### ContextSegment

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `key` | `string` | Yes | Stable id for the segment, used as its React key. |
| `label` | `string` | Yes | Name shown in the breakdown, e.g. "System prompt" or "Messages". |
| `tokens` | `number` | Yes | Tokens this segment takes up. |

## About Context Meter

Context Meter is a React context window indicator for AI chat and agent apps, built with Framer Motion. A small pill with a ring and percentage sits in the composer, and the ring fills as the conversation grows. Press it and the pill itself grows into the panel, no separate dropdown: a token total, a segmented bar and a row per segment (system prompt, tools, files, messages) with its share and what is still free. It folds back into the pill on close. Near the limit the ring turns to the warning color, then danger, and an optional Compact button lets people free up space. The counting is yours: pass the segments and the window size, and shrink the segments when you compact. The panel renders in place over the page, so give it room in any parent that clips overflow.

### When to use it

- An AI chat or coding agent should show how close a conversation is to the model's context limit.
- People need to see what is taking up the context, such as attached files or tool definitions.
- An app compacts or summarizes long conversations and wants a clear place to offer it.

### Accessibility

The pill is a real button whose name gives the percentage and the token counts, with aria-expanded for the panel it controls. Opened from the keyboard, the panel appears at once and focus moves to its header, which closes it again; Escape and an outside press close it too, and focus returns to the pill. Crossing into warning or danger is announced once in a polite live region, not on every change. The ring and bar are hidden from assistive technology because the rows carry the same numbers as text. With reduced motion the pill opens without the morph and the ring and bar jump to their new values.

### FAQ

**Does it count tokens?**

No. Count tokens with your model provider's tokenizer or usage data, and pass each part as a segment with its token count. The meter adds them up against limit.

**How does Compact work?**

Pass onCompact to show the button. Run your summary or trimming there, set compacting while it runs, and pass the smaller segments when it finishes. The ring and bar drain to the new size.

**Can I change when it warns?**

Yes. Set warnAt and dangerAt as shares of the window, for example 0.7 and 0.9.

## Source

- Registry detail: https://beui.dev/r/context-meter
- Raw source: https://beui.dev/r/context-meter/raw
- GitHub: https://github.com/starc007/ui-components
