"use client"; import { useState } from "react"; import "./context-meter.css"; /** * Context Meter * How full the model's context window is, and with what. One bar split into what's using it — * instructions, files, conversation, tool results — that grows as things are added, warns as it * nears the limit, and squeezes the conversation into a short summary when you compact it. */ export type Segment = { id: string; label: string; tokens: number; colour: string }; type Props = { limit?: number; initial?: Segment[]; theme?: "paper" | "night"; className?: string }; const DEFAULT: Segment[] = [ { id: "system", label: "Instructions", tokens: 2100, colour: "#8b93a3" }, { id: "files", label: "Files", tokens: 38400, colour: "#2a78d6" }, { id: "chat", label: "Conversation", tokens: 61200, colour: "#eb6834" }, { id: "tools", label: "Tool results", tokens: 9800, colour: "#1baf7a" }, ]; const fmt = (n: number) => (n >= 1000 ? `${(n / 1000).toFixed(n >= 100000 ? 0 : 1)}k` : String(n)); export function ContextMeter({ limit = 200000, initial = DEFAULT, theme = "paper", className = "" }: Props) { const [segs, setSegs] = useState(initial); const [hover, setHover] = useState(null); const [compacting, setCompacting] = useState(false); const used = segs.reduce((a, s) => a + s.tokens, 0); const pct = used / limit; const level = pct > 0.9 ? "full" : pct > 0.75 ? "warn" : "ok"; const add = (id: string, n: number) => setSegs((x) => x.map((s) => (s.id === id ? { ...s, tokens: s.tokens + n } : s))); const compact = () => { setCompacting(true); setTimeout(() => { setSegs((x) => x.map((s) => (s.id === "chat" ? { ...s, label: "Summary", tokens: Math.round(s.tokens * 0.08) } : s.id === "tools" ? { ...s, tokens: Math.round(s.tokens * 0.2) } : s))); setCompacting(false); }, 650); }; const focusSeg = segs.find((s) => s.id === hover); return (

Context window

{fmt(used)} / {fmt(limit)} tokens

`${s.label} ${fmt(s.tokens)}`).join(", ")}`} data-compacting={compacting ? "" : undefined} > {segs.map((s) => ( setHover(s.id)} onPointerLeave={() => setHover(null)} /> ))}

{focusSeg ? `${focusSeg.label}: ${fmt(focusSeg.tokens)} tokens (${Math.round((focusSeg.tokens / used) * 100)}% of what's used)` : level === "full" ? "Almost full — older turns will be dropped soon. Compact to keep going." : level === "warn" ? "Getting full. Compacting will summarise the conversation." : "Plenty of room."}