Settings & Usage

Context Window Meter

Token usage by segment — system, history, files, tools — with a near-limit warning and a compact action.

4 variantsMIT

Preview

Installation

Copies the component into components/ui and installs its dependencies and theme tokens.

pnpm dlx shadcn@latest add https://uilab.tmih06.com/r/context-window-meter.json

Usage

Pass your own data and wire the callbacks to your agent runtime.

import { useState } from "react";
import { ContextWindowMeter } from "@/components/ui/context-window-meter";

export function ContextUsage({ usage }: { usage: Usage }) {
  const [compacting, setCompacting] = useState(false);

  return (
    <ContextWindowMeter
      data={{
        limit: 200_000,
        model: "opus-5.5",
        segments: [
          { id: "history", label: "Conversation", tokens: usage.history },
          { id: "files", label: "Workspace files", tokens: usage.files },
          { id: "tools", label: "MCP tools", tokens: usage.tools, pinned: true },
          { id: "system", label: "Instructions", tokens: usage.system, pinned: true },
        ],
      }}
      warnAt={0.8}
      compacting={compacting}
      onCompact={async () => {
        setCompacting(true);
        await agent.compact(); // summarize older turns
        setCompacting(false);
      }}
    />
  );
}

// Pass null while the usage request is in flight; keep stale data and set
// loading when refetching.
export function ContextUsageState({ query }: { query: UsageQuery }) {
  return (
    <ContextWindowMeter
      data={query.data ? toMeterData(query.data) : null}
      loading={query.isRefetching}
      skeleton={{ count: 4 }}
    />
  );
}

Props

PropTypeDefaultDescription
data*{ limit: number; segments: ContextSegment[]; model?: string } | null—The model's context limit, token counts per segment in fill order, and an optional model name shown as a tag. Pass null while the counts load.
loadingbooleanfalseForce the layout-preserving skeleton while refetching, even when stale data is available.
skeleton{ count?: number }—Shape hints for the loading state; count is the number of legend row placeholders (card variant).
warnAtnumber0.8Share of the limit where the tinted warning zone starts. Past it the count turns orange and the inline row shows Compact.
onCompact() => void—Shows a Compact button (always in the card, past `warnAt` inline). Summarize or drop context here.
compactingbooleanfalsePuts the Compact button in its loading state.
variant"card" | "inline""card"A panel with a 100-cell grid (1% per cell) and a legend, or a 20-cell row for a composer footer.
localestring"en-US"BCP 47 locale for token counts and percentages.
labelsPartial<ContextWindowMeterLabels>—Your own UI strings (loading, button, captions, screen-reader text), for translating. Right-to-left works under `dir="rtl"`; the cells fill from the right.
classNamestring—Extra classes for the root element.
type ContextSegment = {
  id: string;
  label: string;
  tokens: number;
  detail?: string; // secondary text, e.g. "4 open"
  pinned?: boolean; // shows a lock next to the label
  color?: string; // any CSS color; defaults to the built-in palette
};