Thinking UI

Context Meter

A ring showing how much of the model's context window is used.

Code

components/thinking-ui/context-meter.tsx
"use client";

import { motion } from "motion/react";
import { AnimatedNumber } from "@/components/thinking-ui/animated-number";
import { useMotion } from "@/lib/motion";
import { cn } from "@/lib/utils";

export type ContextMeterProps = {
  /** Tokens used so far. */
  used: number;
  /** Context window size. */
  total: number;
  label?: string;
  /** Ratio at which the meter turns to warning (default 0.7) and danger (0.9). */
  thresholds?: [warning: number, danger: number];
  className?: string;
};

const compact = (n: number) =>
  n >= 1000 ? `${Math.round(n / 1000)}k` : String(n);

export function ContextMeter({ used, total, label = "Context", thresholds = [0.7, 0.9], className }: ContextMeterProps) {
  const m = useMotion();
  const ratio = Math.min(1, Math.max(0, used / total));
  const tone = ratio >= thresholds[1] ? "danger" : ratio >= thresholds[0] ? "warning" : "ok";

  return (
    <div
      role="meter"
      aria-label={label}
      aria-valuemin={0}
      aria-valuemax={total}
      aria-valuenow={used}
      className={cn("inline-flex items-center gap-3 rounded-card border border-card-line bg-card py-2.5 pl-2.5 pr-4 shadow-card", className)}
    >
      <svg viewBox="0 0 40 40" className="size-10 -rotate-90">
        <circle cx="20" cy="20" r="16" fill="none" stroke="var(--meter-track)" strokeWidth="4" />
        <motion.circle
          cx="20"
          cy="20"
          r="16"
          fill="none"
          strokeWidth="4"
          strokeLinecap="round"
          className={cn(
            "[transition:stroke_var(--motion-duration-slow)_var(--motion-ease-out)]",
            tone === "danger" ? "stroke-danger" : tone === "warning" ? "stroke-warning" : "stroke-meter-fill",
          )}
          initial={{ pathLength: 0 }}
          animate={{ pathLength: Math.max(ratio, 0.001) }}
          transition={m.spring("layout")}
        />
      </svg>
      <div className="text-sm">
        <div className="flex items-baseline gap-1 font-medium text-fg">
          <span className="inline-flex items-baseline">
            <AnimatedNumber value={Math.round(ratio * 100)} />
            <span className="text-fg-muted">%</span>
          </span>
          <span className="ml-1 font-normal text-fg-muted">{label}</span>
        </div>
        <div className="mt-0.5 font-mono text-xs text-fg-muted">
          {compact(used)} / {compact(total)} tokens
        </div>
      </div>
    </div>
  );
}

Motion notes

  • The ring fills on the layout spring and the percentage rolls with Animated Number.
  • Color moves from the meter token to warning at 70% and danger at 90%, cross-fading via CSS motion tokens.
  • Exposed as an ARIA meter with real min, max and current values.

Installation

  1. 1

    Install dependencies

    Terminal
    npm install motion clsx tailwind-merge
  2. 2

    Add the design tokens and motion system (once per project)

    Copy styles/tokens/*, lib/motion.tsx and lib/utils.ts from Get started.

  3. 3

    Add the components it uses

    Animated Number

  4. 4

    Paste the source into components/thinking-ui/context-meter.tsx

    Copy it from the Code tab, or use Copy prompt to let your coding agent do all of this.

Usage

Example
<ContextMeter used={usage.input} total={200_000} />

Props

PropTypeDefaultDescription
usednumber—Tokens used.
totalnumber—Context window size.
labelstring"Context"Label next to the percentage.
thresholds[number, number][0.7, 0.9]Warning and danger ratios.