Context meter
A ring for context window use, with a breakdown.
Installation
Run the CLI
Run
pnpm dlx asta-ui@latest initonce, then this. The file lands incomponents/astabyte/, installsreact-domand the colour tokens go into your CSS.$ pnpm dlx asta-ui@latest add context-meterOr install the package
Don’t want the file? Import from
asta-ui/context-meterand update withpnpm update asta-ui. Add the stylesheet once. More.$ pnpm add asta-uiimport { ContextMeter } from "asta-ui/context-meter";Or copy it by hand
Install
react-dom, then copy these files. Keep the paths, or fix the@/imports. Then add the tokens fromglobals.css."use client"; import { useEffect, useRef, useState } from "react"; import { Popover, type PopoverAlign, type PopoverSide } from "@/components/kit/popover"; const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" "); const CURVE = "cubic-bezier(.16,1,.3,1)"; const KEYFRAMES = `@keyframes asta-ctx-in { from { opacity: 0; transform: translateY(3px) } } @keyframes asta-ctx-spin { to { transform: rotate(360deg) } } @media (prefers-reduced-motion: reduce) { .asta-ctx-a, .asta-ctx-a * { animation: none !important; transition-duration: 0s !important } }`; // Segment fills, strongest first: brand, softer brand, then greys. Same order as Meter. const FILLS = [ "var(--color-brand)", "color-mix(in srgb, var(--color-brand) 55%, var(--color-paper))", "color-mix(in srgb, var(--color-ink) 62%, var(--color-paper))", "color-mix(in srgb, var(--color-ink) 28%, var(--color-paper))", "color-mix(in srgb, var(--color-ink) 14%, var(--color-paper))", ]; export interface ContextSegment { label: string; tokens: number; } export interface ContextBreakdownProps { /** What fills the window, e.g. system prompt, tools, files, conversation. */ segments: ContextSegment[]; /** Size of the context window in tokens. */ max: number; /** Model name shown in the header. */ model?: string; /** From this share of `max` (0–1) the ring and bar turn ink and a note shows. Default 0.8. */ warnAt?: number; /** Shows "Compact conversation". Return a promise and the button shows progress until it settles. */ onCompact?: () => void | Promise<void>; className?: string; } export interface ContextMeterProps extends ContextBreakdownProps { /** `sm` shows the ring and percentage only. */ size?: "sm" | "md"; side?: PopoverSide; align?: PopoverAlign; } /** 182600 → "182.6k", 200000 → "200k". */ export const formatTokens = (n: number) => { if (n < 1000) return String(Math.round(n)); const k = n / 1000; return `${k >= 100 || Number.isInteger(k) ? Math.round(k) : k.toFixed(1)}k`; }; /** Glides a number to its new value over 700ms. One short rAF run per change. */ function useTween(value: number) { const [shown, setShown] = useState(value); const from = useRef(value); useEffect(() => { const start = from.current; if (start === value) return; if (matchMedia("(prefers-reduced-motion: reduce)").matches) { from.current = value; const id = requestAnimationFrame(() => setShown(value)); return () => cancelAnimationFrame(id); } const t0 = performance.now(); let id = 0; const step = (t: number) => { const p = Math.min(1, (t - t0) / 700); const e = 1 - Math.pow(1 - p, 4); const v = start + (value - start) * e; from.current = v; setShown(v); if (p < 1) id = requestAnimationFrame(step); }; id = requestAnimationFrame(step); return () => cancelAnimationFrame(id); }, [value]); return shown; } function Ring({ share, warn, className }: { share: number; warn: boolean; className?: string }) { return ( <svg viewBox="0 0 16 16" className={cx("-rotate-90 shrink-0", className)} aria-hidden> <circle cx="8" cy="8" r="6.25" fill="none" strokeWidth="2.5" style={{ stroke: "color-mix(in srgb, var(--color-ink) 11%, transparent)" }} /> <circle cx="8" cy="8" r="6.25" fill="none" strokeWidth="2.5" strokeLinecap="round" pathLength={100} strokeDasharray="100 100" strokeDashoffset={100 - Math.max(share * 100, share > 0 ? 3 : 0)} className={cx("transition-[stroke-dashoffset,stroke] duration-700 motion-reduce:transition-none", warn ? "stroke-ink" : "stroke-brand")} style={{ transitionTimingFunction: CURVE }} /> </svg> ); } function WarnIcon() { return ( <svg viewBox="0 0 24 24" className="mt-px h-3.5 w-3.5 shrink-0" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round" aria-hidden> <path d="M10.3 4.6 3 17.5A2 2 0 0 0 4.7 20.5h14.6a2 2 0 0 0 1.7-3L13.7 4.6a2 2 0 0 0-3.4 0Z" /> <path d="M12 9.5v4" /> <path d="M12 17h.01" /> </svg> ); } const sum = (s: ContextSegment[]) => s.reduce((a, p) => a + Math.max(0, p.tokens), 0); /** * The full breakdown of a context window: total used, a segmented bar, a legend per part and a Compact action. * When usage drops, the bar glides down and the total counts down to the new value. ContextMeter shows it in a * popover; place it on its own in a side panel. */ export function ContextBreakdown({ segments, max, model, warnAt = 0.8, onCompact, className }: ContextBreakdownProps) { const used = sum(segments); const share = max > 0 ? Math.min(1, used / max) : 0; const warn = share >= warnAt; const shown = useTween(used); const [busy, setBusy] = useState(false); const [freed, setFreed] = useState(0); const [last, setLast] = useState(used); const alive = useRef(true); useEffect(() => { alive.current = true; return () => { alive.current = false; }; }, []); // Say how much a drop freed. Growing again clears the note. if (last !== used) { setLast(used); setFreed(used < last ? last - used : 0); } const compact = async () => { if (!onCompact || busy) return; setBusy(true); try { await onCompact(); } finally { if (alive.current) setBusy(false); } }; // Where each part starts, in tokens. const starts = segments.map((_, i) => sum(segments.slice(0, i))); return ( <div className={cx("asta-ctx-a flex w-full flex-col gap-3 text-ink", className)}> <style href="asta-context-meter" precedence="asta">{KEYFRAMES}</style> <div className="flex items-baseline justify-between gap-3"> <p className="text-[13.5px] font-medium">Context window</p> {model && <p className="truncate text-[12px] text-mute">{model}</p>} </div> <div className="flex items-baseline justify-between gap-3"> <p className="tabular-nums"> <span className="text-[22px] font-medium tracking-[-0.03em]">{formatTokens(shown)}</span> <span className="text-[12.5px] text-mute"> of {formatTokens(max)} tokens</span> </p> <p className={cx("text-[12.5px] tabular-nums", warn ? "font-medium text-ink" : "text-mute")}>{Math.round((shown / (max || 1)) * 100)}% used</p> </div> <div role="meter" aria-label="Context used" aria-valuemin={0} aria-valuemax={max} aria-valuenow={Math.min(used, max)} aria-valuetext={`${formatTokens(used)} of ${formatTokens(max)} tokens${warn ? ", near the limit" : ""}`} className="relative h-2 w-full overflow-hidden rounded-full bg-paper-2 shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_6%,transparent)]" > {segments.map((p, i) => { const x = starts[i] / (max || 1); const w = Math.max(0, p.tokens) / (max || 1); return ( <span key={p.label} className="absolute inset-y-0 left-0 w-full origin-left transition-transform duration-700 motion-reduce:transition-none" style={{ transform: `translateX(${Math.min(1, x) * 100}%) scaleX(${Math.min(w, Math.max(0, 1 - x))})`, background: FILLS[i % FILLS.length], transitionTimingFunction: CURVE, boxShadow: "inset -1px 0 0 var(--color-paper)", }} /> ); })} </div> <ul className="flex flex-col gap-1.5 text-[12.5px]"> {segments.map((p, i) => ( <li key={p.label} className="flex items-center gap-2"> <span aria-hidden className="h-2 w-2 shrink-0 rounded-full" style={{ background: FILLS[i % FILLS.length] }} /> <span className="min-w-0 flex-1 truncate text-ink-2">{p.label}</span> <span className="tabular-nums text-ink">{formatTokens(p.tokens)}</span> <span className="w-9 text-right tabular-nums text-mute">{Math.round((p.tokens / (max || 1)) * 100)}%</span> </li> ))} <li className="flex items-center gap-2"> <span aria-hidden className="h-2 w-2 shrink-0 rounded-full shadow-[inset_0_0_0_1.5px_color-mix(in_srgb,var(--color-ink)_22%,transparent)]" /> <span className="min-w-0 flex-1 truncate text-mute">Free</span> <span className="tabular-nums text-ink">{formatTokens(Math.max(0, max - used))}</span> <span className="w-9 text-right tabular-nums text-mute">{Math.max(0, 100 - Math.round(share * 100))}%</span> </li> </ul> <p className="sr-only" aria-live="polite"> {freed > 0 ? `Compacted. Freed ${formatTokens(freed)} tokens.` : busy ? "Compacting conversation." : ""} </p> {warn ? ( <p key="warn" className="asta-ctx-a flex gap-1.5 rounded-lg bg-paper-2 px-2.5 py-2 text-[12.5px] leading-snug text-ink animate-[asta-ctx-in_.35s_cubic-bezier(.16,1,.3,1)_both]"> <WarnIcon /> <span> <span className="font-medium">Near the limit.</span> Older messages get summarised soon. Compact now to choose when. </span> </p> ) : freed > 0 ? ( <p key={`freed-${freed}`} className="asta-ctx-a text-[12.5px] text-mute animate-[asta-ctx-in_.35s_cubic-bezier(.16,1,.3,1)_both]"> Freed <span className="font-medium tabular-nums text-ink">{formatTokens(freed)}</span> tokens. The summary keeps decisions and open tasks. </p> ) : null} {onCompact && ( <button type="button" onClick={compact} aria-busy={busy || undefined} disabled={busy || used === 0} className="flex h-8 w-full items-center justify-center gap-2 rounded-md bg-paper-2 text-[13px] font-medium text-ink outline-none transition-[background-color,opacity,transform] duration-200 hover:bg-[color-mix(in_srgb,var(--color-ink)_8%,var(--color-paper-2))] active:scale-[0.98] focus-visible:ring-2 focus-visible:ring-brand/50 disabled:pointer-events-none disabled:opacity-60" > {busy ? ( <svg viewBox="0 0 16 16" className="h-3.5 w-3.5 animate-[asta-ctx-spin_.8s_linear_infinite]" fill="none" aria-hidden> <circle cx="8" cy="8" r="6" stroke="currentColor" strokeOpacity=".2" strokeWidth="2" /> <path d="M14 8a6 6 0 0 0-6-6" stroke="currentColor" strokeWidth="2" strokeLinecap="round" /> </svg> ) : ( <svg viewBox="0 0 24 24" className="h-3.5 w-3.5" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round" aria-hidden> <path d="M14 5v5h5" /> <path d="m20 4-6 6" /> <path d="M10 19v-5H5" /> <path d="m4 20 6-6" /> </svg> )} {busy ? "Compacting…" : "Compact conversation"} </button> )} </div> ); } /** * How much of the model's context window a conversation uses. A compact ring and "62% of 200k" open a breakdown * by part with a Compact action. Near the limit the ring turns ink and the label says so in words. */ export function ContextMeter({ size = "md", side = "top", align = "end", ...props }: ContextMeterProps) { const { segments, max, warnAt = 0.8 } = props; const used = sum(segments); const share = max > 0 ? Math.min(1, used / max) : 0; const warn = share >= warnAt; const pct = Math.round(share * 100); return ( <Popover side={side} align={align} label="Context window" className="w-[300px] p-4" trigger={ <button type="button" aria-label={`Context window: ${pct}% used, ${formatTokens(used)} of ${formatTokens(max)} tokens${warn ? ", near the limit" : ""}`} className={cx( "asta-ctx-a inline-flex shrink-0 items-center gap-1.5 rounded-md text-[12.5px] tabular-nums outline-none transition-colors duration-200 hover:bg-paper-2 focus-visible:ring-2 focus-visible:ring-brand/50 data-[state=open]:bg-paper-2", size === "sm" ? "h-7 px-1.5" : "h-8 px-2", warn ? "text-ink" : "text-mute hover:text-ink", )} > <Ring share={share} warn={warn} className="h-4 w-4" /> <span className={cx(warn && "font-medium")}>{pct}%</span> {size === "md" && (warn ? <span className="font-medium">· Near limit</span> : <span>of {formatTokens(max)}</span>)} </button> } > <ContextBreakdown {...props} /> </Popover> ); } export default ContextMeter;Or ask your AI agent
Paste this into Claude Code, Cursor, Codex, v0 or Lovable. Fill in the last line. More in Prompts.
Add the Astabyte UI Context meter component to my project. A ring for context window use, with a breakdown. It includes: ContextMeter, ContextBreakdown, ring, segments, warn, compact. Install it: ```sh pnpm dlx asta-ui@latest add context-meter --radix ``` Or with the shadcn CLI: ```sh pnpm dlx shadcn@latest add https://ui.astabyte.com/r/context-meter.json ``` If the project isn't set up yet, run `pnpm dlx asta-ui@latest init --radix` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4). Import it like this: ```tsx import { ContextBreakdown, ContextMeter, type ContextSegment } from "@/components/astabyte/context-meter"; ``` Usage example and props: https://ui.astabyte.com/context-meter Full catalogue for context: https://ui.astabyte.com/llms.txt Rules: - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS. - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback. - Keep keyboard, focus and ARIA behaviour. - Use fake data and fake brand names in examples. Use it to: [describe where it goes and what it should do]Or use plain HTML + CSS
Not on React? Use it from Django, Rails, Laravel, Go
html/template, htmx or a static page. Loadasta.cssandasta.jsonce (thehead.htmltab), then paste the markup. Theui.astabyte.com/cdnlink is temporary: pin a version from jsDelivr for production. More in HTML + CSS.<!-- The ring button opens the breakdown with the native popovertarget attribute; asta.js positions it. Set the ring's stroke-dashoffset to 100 minus the percentage, each segment's --start and --value (0 to 1), and data-warn on the button and breakdown near the limit. Compact posts to your server, e.g. hx-post. --> <div style="display:flex;flex-wrap:wrap;align-items:center;gap:12px"> <button type="button" class="asta-context-meter" popovertarget="ex-ctx" aria-haspopup="dialog" aria-label="Context window: 91% used, 183k of 200k tokens, near the limit" data-warn> <svg class="asta-context-meter-ring" viewBox="0 0 16 16" aria-hidden="true"><circle class="asta-context-meter-ring-track" cx="8" cy="8" r="6.25"/><circle class="asta-context-meter-ring-bar" cx="8" cy="8" r="6.25" pathLength="100" stroke-dasharray="100 100" stroke-dashoffset="9"/></svg> <span>91%</span><span class="asta-context-meter-of">of 200k</span><span class="asta-context-meter-warn">· Near limit</span> </button> <div id="ex-ctx" popover="auto" role="dialog" tabindex="-1" data-asta-popover data-side="bottom" data-align="start" aria-label="Context window" class="asta-float asta-popover asta-context-meter-panel"> <div class="asta-context-meter-breakdown" data-warn> <div class="asta-context-meter-head"><span>Context window</span><span>Astra 2 Pro</span></div> <div class="asta-context-meter-total"><span><b>183k</b> of 200k tokens</span><span>91% used</span></div> <div class="asta-context-meter-bar" role="meter" aria-label="Context used" aria-valuemin="0" aria-valuemax="200000" aria-valuenow="182600"> <span class="asta-context-meter-seg" style="--start:0;--value:.65"></span><span class="asta-context-meter-seg" style="--start:.65;--value:.206"></span><span class="asta-context-meter-seg" style="--start:.856;--value:.042"></span><span class="asta-context-meter-seg" style="--start:.898;--value:.016"></span> </div> <ul class="asta-context-meter-legend"> <li><span class="asta-context-meter-dot"></span><span>Conversation</span><span>130k</span><span>65%</span></li> <li><span class="asta-context-meter-dot"></span><span>Files</span><span>41.2k</span><span>21%</span></li> <li><span class="asta-context-meter-dot"></span><span>Tools</span><span>8.4k</span><span>4%</span></li> <li><span class="asta-context-meter-dot"></span><span>System prompt</span><span>3.1k</span><span>2%</span></li> <li data-free><span class="asta-context-meter-dot"></span><span>Free</span><span>17.4k</span><span>9%</span></li> </ul> <p class="asta-context-meter-note"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M8 2.5 14 13H2zM8 6.5v3M8 11.2h.01"/></svg><span><b>Near the limit.</b> Older messages get summarised soon. Compact now to choose when.</span></p> <button type="button" class="asta-context-meter-compact"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 6h10M3 10h10M8 1.5V4.5L6.5 3M8 4.5 9.5 3M8 14.5v-3L6.5 13M8 11.5l1.5 1.5"/></svg>Compact conversation</button> </div> </div> <button type="button" class="asta-context-meter asta-context-meter-sm" aria-label="Context window: 17% used"> <svg class="asta-context-meter-ring" viewBox="0 0 16 16" aria-hidden="true"><circle class="asta-context-meter-ring-track" cx="8" cy="8" r="6.25"/><circle class="asta-context-meter-ring-bar" cx="8" cy="8" r="6.25" pathLength="100" stroke-dasharray="100 100" stroke-dashoffset="83"/></svg> <span>17%</span> </button> </div>
Usage
The demo above, as code.
"use client";
import { useState } from "react";
import { ContextBreakdown, ContextMeter, type ContextSegment } from "@/components/astabyte/context-meter";
const FULL: ContextSegment[] = [
{ label: "Conversation", tokens: 129_900 },
{ label: "Files", tokens: 41_200 },
{ label: "Tools", tokens: 8_400 },
{ label: "System prompt", tokens: 3_100 },
];
const wait = (ms: number) => new Promise((r) => setTimeout(r, ms));
export default function ContextMeterDemo() {
const [segments, setSegments] = useState(FULL);
const compacted = segments[0].tokens < FULL[0].tokens;
// Summarise the conversation: keep decisions and open tasks, drop the back-and-forth.
const compact = async () => {
await wait(1100);
setSegments((s) => s.map((p) => (p.label === "Conversation" ? { ...p, tokens: 12_800 } : p)));
};
return (
<div className="flex min-h-[480px] w-full items-center justify-center p-4 sm:p-10">
<div className="grid w-full max-w-[820px] items-start gap-6 md:grid-cols-[1fr_300px]">
<div className="flex flex-col gap-3">
<div className="rounded-[14px] bg-paper p-1.5 shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_12%,transparent),0_1px_2px_rgb(0_0_0/0.04)]">
<label htmlFor="ctx-demo-prompt" className="sr-only">
Message
</label>
<textarea
id="ctx-demo-prompt"
name="prompt"
rows={3}
placeholder="e.g. Move the invoice emails to the new template"
className="block w-full resize-none bg-transparent px-3 py-2.5 text-[16px] leading-relaxed text-ink outline-none placeholder:text-mute sm:text-[14px]"
/>
<div className="flex items-center gap-2 px-1.5 pb-0.5">
<span className="shrink-0 whitespace-nowrap rounded-md bg-paper-2 px-2 py-1 text-[12px] text-ink-2">Astra 2 Pro</span>
<span className="hidden truncate text-[12px] text-mute sm:block">14 files</span>
<span className="ml-auto" />
<ContextMeter segments={segments} max={200_000} model="Astra 2 Pro" onCompact={compact} />
<button
type="button"
aria-label="Send message"
className="grid h-8 w-8 shrink-0 place-items-center rounded-lg bg-brand text-on-brand outline-none transition-transform duration-150 active:scale-95 focus-visible:ring-2 focus-visible:ring-brand/50 focus-visible:ring-offset-2 focus-visible:ring-offset-paper"
>
<svg viewBox="0 0 24 24" className="h-4 w-4" fill="none" stroke="currentColor" strokeWidth="2.55" strokeLinecap="round" strokeLinejoin="round" aria-hidden>
<path d="M12 20V5" />
<path d="m6 11 6-6 6 6" />
</svg>
</button>
</div>
</div>
<div className="flex items-center justify-between gap-3 px-1 text-[12.5px] text-mute">
<span>Open the ring above the composer, or use the panel.</span>
<button
type="button"
onClick={() => setSegments(FULL)}
disabled={!compacted}
className="shrink-0 rounded-md px-2 py-1 font-medium text-brand outline-none transition-[background-color,opacity] duration-200 hover:bg-[color-mix(in_srgb,var(--color-brand)_8%,transparent)] focus-visible:ring-2 focus-visible:ring-brand/50 disabled:opacity-40"
>
Refill context
</button>
</div>
<div className="flex items-center gap-4 px-1 text-[12.5px] text-mute">
Small
<ContextMeter size="sm" segments={segments} max={200_000} />
<ContextMeter size="sm" segments={[{ label: "Conversation", tokens: 21_400 }]} max={128_000} />
</div>
</div>
<ContextBreakdown
segments={segments}
max={200_000}
model="Astra 2 Pro"
onCompact={compact}
className="rounded-[14px] bg-paper p-4 shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_9%,transparent),0_1px_2px_rgb(0_0_0/0.04)]"
/>
</div>
</div>
);
}