Context Meter

A ring that shows how full an AI chat's context window is.

Preview

"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>
  );
}

Install

Add it with the shadcn CLI, or copy the source manually.

$ bunx --bun shadcn add @beui/context-meter

API Reference

segments
ContextSegment[]

What fills the context window, in display order. The meter shows their sum.

limit
number

Size of the context window in tokens.

warnAt?
number

Share of the window (0 to 1) where the meter turns to the warning color. Default: 0.8

Default0.8
dangerAt?
number

Share of the window (0 to 1) where the meter turns to the danger color. Default: 0.95

Default0.95
onCompact?
(() => void)

Shows a Compact button in the panel and calls this when it is pressed. Shrink the segments yourself.

compacting?
boolean

Shows the Compact button as busy and disables it while your compaction runs. Default: false

Defaultfalse
formatTokens?
((tokens: number) => string)

Formats a token count for display. Default: compact notation, e.g. 42.1k

side?
"top" | "bottom"

Which way the pill grows into the panel. Default: "top"

Defaulttop
align?
"end" | "start"

The pill's edge the panel stays lined up with as it grows. Default: "start"

Defaultstart
labels?
Partial<{ trigger: string; collapse: string; tokens: string; free: string; warning: string; danger: string; compact: string; compacting: string; warningReached: string; }>

Override any built-in string, e.g. for translation.

className?
string

Classes merged onto the root element.

Updated