AI Context Meter
How full the context window is: a tank of liquid that rises toward the limit, a hundred blocks filled by category, and a rim meter that squeezes back when you compact.
"use client"
import * as React from "react"
import { ArrowUp, Minimize2, Plus } from "lucide-react"
import {
AiKitStyles,
AiStyle,
cx,
Pill,
Roll,
toneColor,
type AiStatus,
type AiTone,
} from "@/lib/ai-kit"
const CSS = `
.aik-mt-tank{--aik-r-shell:28px;--aik-rim:6px;width:min(100%,540px);container-type:inline-size}
.aik-mt-tcard{display:grid;grid-template-columns:148px minmax(0,1fr);gap:28px;align-items:center;padding:22px 26px 22px 22px}
.aik-mt-vessel{--c:var(--aik-blue);position:relative;height:268px;border-radius:30px;background:var(--aik-sunk);overflow:hidden;isolation:isolate}
.aik-mt-vessel[data-level="near"]{--c:var(--aik-amber-fill)}
.aik-mt-vessel[data-level="full"]{--c:var(--aik-red)}
.aik-mt-liquid{position:absolute;inset:0;transform:translateY(calc((1 - var(--p))*100%));transition:transform 620ms var(--aik-ease-move)}
.aik-mt-slosh{position:absolute;inset:0}
.aik-mt-vessel[data-dip] .aik-mt-slosh{animation:aik-mt-dip 760ms var(--aik-ease-move)}
.aik-mt-body{position:absolute;inset:11px 0 0;background:var(--c);transition:background 420ms var(--aik-ease-out)}
.aik-mt-wave{position:absolute;left:0;top:-8px;width:200%;height:20px;fill:var(--c);transition:fill 420ms var(--aik-ease-out);animation:aik-mt-drift 4.4s linear infinite}
.aik-mt-wave[data-back]{top:-13px;opacity:.4;animation-duration:7s;animation-direction:reverse}
.aik-mt-glass{position:absolute;inset:0;border-radius:inherit;pointer-events:none;box-shadow:inset 0 0 0 1px var(--aik-line-2),inset 10px 0 18px -10px rgb(255 255 255 / 30%);z-index:2}
.aik-mt-tick{position:absolute;right:0;width:12px;height:1.5px;border-radius:1px;background:var(--aik-line-3);z-index:1}
.aik-mt-tidy{position:absolute;left:0;right:0;height:0;border-top:1.5px dashed var(--aik-text-3);opacity:.55;z-index:1}
.aik-mt-tidy span{position:absolute;right:8px;bottom:5px;font:10.5px var(--aik-mono);color:var(--aik-text-3);letter-spacing:.04em;text-transform:uppercase}
@keyframes aik-mt-drift{to{transform:translateX(-50%)}}
@keyframes aik-mt-dip{0%{transform:none}38%{transform:translateY(16px) scaleY(.96)}100%{transform:none}}
.aik-mt-info{display:grid;gap:10px;min-width:0;justify-items:start}
.aik-mt-num{display:flex;align-items:baseline;gap:2px;font-size:68px;line-height:68px;font-weight:600;letter-spacing:-.045em;font-variant-numeric:tabular-nums}
.aik-mt-num small{font-size:24px;font-weight:500;letter-spacing:0;color:var(--aik-text-3)}
.aik-mt-of{font:13px var(--aik-mono);color:var(--aik-text-2);font-variant-numeric:tabular-nums}
.aik-mt-acts{display:flex;flex-wrap:wrap;gap:8px;padding-top:8px}
.aik-mt-tank .aik-pill{height:26px;font-size:12.5px}
@container (max-width:430px){.aik-mt-tcard{grid-template-columns:96px minmax(0,1fr);gap:18px;padding:16px}.aik-mt-vessel{height:212px;border-radius:24px}.aik-mt-num{font-size:46px;line-height:48px}.aik-mt-tidy span{display:none}}
@media (prefers-reduced-motion: reduce){.aik-mt-wave{animation:none}.aik-mt-liquid{transition:none}.aik-mt-vessel[data-dip] .aik-mt-slosh{animation:none}}
.aik-mt-blocks{--aik-r-shell:28px;--aik-rim:6px;width:min(100%,560px);container-type:inline-size}
.aik-mt-bcard{display:grid;grid-template-columns:auto minmax(0,1fr);gap:26px;align-items:center;padding:22px}
.aik-mt-grid{display:grid;grid-template-columns:repeat(10,20px);grid-auto-rows:20px;gap:4px;transform:scaleY(-1)}
.aik-mt-cell{border-radius:6px;background:var(--aik-raised);position:relative}
.aik-mt-cell::after{content:"";position:absolute;inset:0;border-radius:inherit;background:var(--c,transparent);transform:scale(0);opacity:0;transition:transform 420ms var(--aik-ease-move),opacity 260ms var(--aik-ease-out),background 260ms;transition-delay:calc(var(--d)*1ms)}
.aik-mt-cell[data-on]::after{transform:none;opacity:1}
.aik-mt-leg{display:grid;gap:4px;min-width:0}
.aik-mt-lhead{display:flex;align-items:baseline;justify-content:space-between;gap:10px;padding-bottom:10px}
.aik-mt-lhead b{font-size:16px;font-weight:600;letter-spacing:-.01em}
.aik-mt-lhead span{font:13px var(--aik-mono);color:var(--aik-text-2);font-variant-numeric:tabular-nums;white-space:nowrap}
.aik-mt-row{display:grid;grid-template-columns:10px minmax(0,1fr) auto 38px;gap:10px;align-items:center;height:32px;font-size:14px;border-top:1px solid var(--aik-line)}
.aik-mt-row i{width:10px;height:10px;border-radius:3px;background:var(--c)}
.aik-mt-row span{min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
.aik-mt-row b,.aik-mt-row em{font:12.5px var(--aik-mono);font-weight:400;font-style:normal;font-variant-numeric:tabular-nums;text-align:right}
.aik-mt-row em{color:var(--aik-text-3)}
.aik-mt-row[data-free]{color:var(--aik-text-3)}
.aik-mt-row[data-free] i{background:var(--aik-raised);box-shadow:inset 0 0 0 1px var(--aik-line-2)}
.aik-mt-bfoot{display:flex;align-items:center;justify-content:space-between;gap:10px;padding-top:12px}
.aik-mt-bfoot .aik-roll{font-size:13px;color:var(--aik-text-2);line-height:18px}
@container (max-width:500px){.aik-mt-bcard{grid-template-columns:minmax(0,1fr);justify-items:center;gap:18px;padding:16px}.aik-mt-leg{width:100%}.aik-mt-grid{grid-template-columns:repeat(10,18px);grid-auto-rows:18px}}
@media (prefers-reduced-motion: reduce){.aik-mt-cell::after{transition:none}}
.aik-mt-rim{--aik-r-shell:26px;--aik-rim:6px;display:grid;gap:14px;width:min(100%,540px);container-type:inline-size}
.aik-mt-above{display:grid;gap:14px;padding:0 8px;min-height:120px;align-content:end}
.aik-mt-field{display:flex;align-items:flex-end;gap:10px;min-height:88px;padding:16px 12px 12px 18px;font-size:15px;line-height:22px;color:var(--aik-text-3)}
.aik-mt-field span{flex:1;align-self:flex-start}
.aik-mt-send{display:grid;place-items:center;width:36px;height:36px;border-radius:11px;background:color-mix(in oklab,var(--aik-raised) 78%,var(--aik-text) 22%);color:var(--aik-pri-ink)}
.aik-mt-rfoot{--c:var(--aik-text-2);display:flex;align-items:center;gap:12px;height:40px;padding:0 6px 0 14px;font-size:12.5px;color:var(--aik-text-3)}
.aik-mt-rfoot[data-level="near"],.aik-mt-rfoot[data-level="full"]{--c:var(--aik-amber)}
.aik-mt-rfoot[data-level="full"]{--c:var(--aik-red)}
.aik-mt-bar{position:relative;flex:1;max-width:190px;height:6px;border-radius:3px;background:var(--aik-line-2)}
.aik-mt-bar i{position:absolute;inset:0;border-radius:inherit;background:var(--c);transform-origin:0 50%;transform:scaleX(var(--p));transition:transform 560ms var(--aik-ease-move),background var(--aik-d-panel)}
.aik-mt-bar em{position:absolute;top:-4px;bottom:-4px;width:1.5px;border-radius:1px;background:var(--aik-text-3)}
.aik-mt-rfoot b{font:12.5px var(--aik-mono);font-weight:400;color:var(--c);font-variant-numeric:tabular-nums;transition:color var(--aik-d-panel)}
.aik-mt-rfoot .aik-roll{line-height:18px;min-width:0}
.aik-mt-grow{flex:1}
.aik-mt-cbtn{display:inline-flex;align-items:center;gap:6px;height:28px;padding:0 10px;border-radius:9px;font-size:12.5px;font-weight:600;color:var(--aik-on-fill);background:var(--aik-amber-fill);animation:aik-pop var(--aik-d-enter) var(--aik-ease-out) both;transition:transform var(--aik-d-press) var(--aik-ease-out)}
.dark .aik-mt-cbtn{color:#101013}
.dark .aik-light .aik-mt-cbtn{color:var(--aik-on-fill)}
.aik-mt-cbtn:active{transform:scale(.95)}
.aik-mt-marker{display:flex;align-items:center;gap:12px;color:var(--aik-text-3);font-size:12.5px;white-space:nowrap;animation:aik-mt-drop 520ms var(--aik-ease-move) both}
.aik-mt-marker::before,.aik-mt-marker::after{content:"";flex:1;height:1px;background:var(--aik-line-2)}
.aik-mt-marker span{display:inline-flex;align-items:center;gap:6px}
@keyframes aik-mt-drop{from{opacity:0;transform:translateY(-14px) scaleX(.6)}}
@container (max-width:400px){.aik-mt-rfoot .aik-roll{display:none}}
@media (prefers-reduced-motion: reduce){.aik-mt-bar i{transition:none}.aik-mt-marker{animation:none}}
`
/** 104000 becomes "104k"; values under a thousand stay whole. */
export function formatTokens(tokens: number) {
if (tokens < 1000) return String(Math.round(tokens))
const k = tokens / 1000
return `${k >= 100 || Number.isInteger(k) ? Math.round(k) : k.toFixed(1)}k`
}
type Level = "ok" | "near" | "full"
function levelOf(ratio: number, tidyAt: number): Level {
if (ratio >= 0.92) return "full"
if (ratio >= tidyAt) return "near"
return "ok"
}
const clamp01 = (value: number) => Math.min(1, Math.max(0, value))
const LEVEL_PILL: Record<Level, [AiStatus | undefined, string]> = {
ok: ["run", "Room to work"],
near: ["ask", "Getting full"],
full: ["fail", "Almost out"],
}
interface MeterBase extends React.HTMLAttributes<HTMLDivElement> {
/** Tokens in the window right now. */
used: number
/** The window's size in tokens. */
limit: number
/** Share of the window, 0 to 1, past which the meter warns. */
tidyAt?: number
/** Called by the Compact action. */
onCompact?: () => void
}
export interface AiContextTankProps extends MeterBase {
/** A short line under the figure. Rolls when it changes. */
note?: string
/** Called by the Add context action. Hidden when omitted. */
onAdd?: () => void
}
/**
* The context window as a vessel. Liquid rises to the share in use behind a
* drifting wave, turns amber then red near the top, and dips when it drains.
*/
export function AiContextTank({
used,
limit,
tidyAt = 0.75,
note,
onCompact,
onAdd,
className,
...rest
}: AiContextTankProps) {
const ratio = limit > 0 ? used / limit : 0
const level = levelOf(ratio, tidyAt)
const [status, label] = LEVEL_PILL[level]
const previous = React.useRef(used)
const [dip, setDip] = React.useState(false)
React.useEffect(() => {
const dropped = used < previous.current - limit * 0.08
previous.current = used
if (!dropped) return
setDip(true)
const timer = window.setTimeout(() => setDip(false), 780)
return () => window.clearTimeout(timer)
}, [used, limit])
return (
<div {...rest} className={cx("aik aik-mt-tank", className)}>
<AiKitStyles />
<AiStyle id="ai-meter" css={CSS} />
<div className="aik-shell">
<div className="aik-surface aik-mt-tcard">
<div
className="aik-mt-vessel"
data-level={level}
data-dip={dip ? "" : undefined}
role="meter"
aria-label="Context window"
aria-valuemin={0}
aria-valuemax={limit}
aria-valuenow={Math.round(used)}
aria-valuetext={`${formatTokens(used)} of ${formatTokens(limit)} tokens`}
style={{ "--p": clamp01(ratio) } as React.CSSProperties}
>
<div className="aik-mt-liquid">
<div className="aik-mt-slosh">
{[true, false].map((back) => (
<svg
key={String(back)}
className="aik-mt-wave"
data-back={back ? "" : undefined}
viewBox="0 0 296 20"
preserveAspectRatio="none"
aria-hidden="true"
>
<path d="M0 10 Q37 0 74 10 T148 10 T222 10 T296 10 V20 H0Z" />
</svg>
))}
<div className="aik-mt-body" />
</div>
</div>
{[0.25, 0.5].map((at) => (
<i
key={at}
className="aik-mt-tick"
style={{ bottom: `${at * 100}%` }}
/>
))}
<div className="aik-mt-tidy" style={{ bottom: `${tidyAt * 100}%` }}>
<span>tidy</span>
</div>
<div className="aik-mt-glass" />
</div>
<div className="aik-mt-info">
<span className="aik-label">Context window</span>
<div className="aik-mt-num">
<Roll>{String(Math.round(ratio * 100))}</Roll>
<small>%</small>
</div>
<span className="aik-mt-of">
{formatTokens(used)} of {formatTokens(limit)} tokens
</span>
<Pill status={status}>{label}</Pill>
{note ? <Roll className="aik-mt-of">{note}</Roll> : null}
<div className="aik-mt-acts">
{onAdd ? (
<button
type="button"
className="aik-btn sm line"
onClick={onAdd}
>
<Plus size={14} />
Add context
</button>
) : null}
<button
type="button"
className={cx("aik-btn sm", level === "ok" ? "line" : "pri")}
onClick={onCompact}
>
<Minimize2 size={14} />
Compact
</button>
</div>
</div>
</div>
</div>
</div>
)
}
export interface AiContextSegment {
id: string
label: string
tokens: number
/** A named tone or any CSS colour. */
tone: AiTone | (string & {})
}
export interface AiContextBlocksProps extends Omit<
MeterBase,
"used" | "tidyAt"
> {
/** What fills the window, in the order the cells fill. */
segments: AiContextSegment[]
/** A line beside the Compact action. Rolls when it changes. */
note?: string
}
/**
* The window as a hundred cells, one per percent. Cells fill in each part's
* colour, and compacting dissolves them while the rest repack.
*/
export function AiContextBlocks({
segments,
limit,
note,
onCompact,
className,
...rest
}: AiContextBlocksProps) {
const used = segments.reduce((sum, segment) => sum + segment.tokens, 0)
const owners: Array<string | null> = []
for (const segment of segments) {
const cells = limit > 0 ? Math.round((segment.tokens / limit) * 100) : 0
for (let at = 0; at < cells && owners.length < 100; at++) {
owners.push(toneColor(segment.tone))
}
}
const filled = owners.length
while (owners.length < 100) owners.push(null)
const percent = (tokens: number) =>
limit > 0 ? `${Math.round((tokens / limit) * 100)}%` : "0%"
return (
<div {...rest} className={cx("aik aik-mt-blocks", className)}>
<AiKitStyles />
<AiStyle id="ai-meter" css={CSS} />
<div className="aik-shell">
<div className="aik-surface aik-mt-bcard">
<div
className="aik-mt-grid"
role="img"
aria-label={`${formatTokens(used)} of ${formatTokens(limit)} tokens used`}
>
{owners.map((color, at) => (
<i
key={at}
className="aik-mt-cell"
data-on={color ? "" : undefined}
style={
{
"--c": color ?? undefined,
// Filling runs up from the first cell; dissolving runs back from the last.
"--d": color ? at * 7 : Math.max(0, filled + 24 - at) * 7,
} as React.CSSProperties
}
/>
))}
</div>
<div className="aik-mt-leg">
<div className="aik-mt-lhead">
<b>Context window</b>
<span>
{formatTokens(used)} / {formatTokens(limit)}
</span>
</div>
{segments.map((segment) => (
<div
key={segment.id}
className="aik-mt-row"
style={
{ "--c": toneColor(segment.tone) } as React.CSSProperties
}
>
<i />
<span>{segment.label}</span>
<b>{formatTokens(segment.tokens)}</b>
<em>{percent(segment.tokens)}</em>
</div>
))}
<div className="aik-mt-row" data-free="">
<i />
<span>Free</span>
<b>{formatTokens(Math.max(0, limit - used))}</b>
<em>{percent(Math.max(0, limit - used))}</em>
</div>
<div className="aik-mt-bfoot">
<Roll>{note ?? "One cell is one percent"}</Roll>
<button
type="button"
className={cx(
"aik-btn sm",
limit > 0 && used / limit >= 0.75 ? "pri" : "line"
)}
onClick={onCompact}
>
<Minimize2 size={14} />
Compact
</button>
</div>
</div>
</div>
</div>
</div>
)
}
export interface AiContextRimProps extends MeterBase {
/** The composer field's placeholder. */
placeholder?: string
/** A line in the rim. Rolls when it changes. */
note?: string
/** The thread above the composer; put `AiCompactMarker` here after a compaction. */
children?: React.ReactNode
}
/**
* The meter where it lives: in the rim under a composer. A thin bar with a
* tick at the tidy point; past it the rim turns amber and offers Compact.
*/
export function AiContextRim({
used,
limit,
tidyAt = 0.8,
placeholder = "Ask a follow-up",
note,
onCompact,
children,
className,
...rest
}: AiContextRimProps) {
const ratio = limit > 0 ? used / limit : 0
const level = levelOf(ratio, tidyAt)
return (
<div {...rest} className={cx("aik aik-mt-rim", className)}>
<AiKitStyles />
<AiStyle id="ai-meter" css={CSS} />
<div className="aik-mt-above">{children}</div>
<div className="aik-shell">
<div className="aik-surface aik-mt-field">
<span>{placeholder}</span>
<i className="aik-mt-send" aria-hidden="true">
<ArrowUp size={18} />
</i>
</div>
<div className="aik-mt-rfoot" data-level={level}>
<div
className="aik-mt-bar"
role="meter"
aria-label="Context window"
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={Math.round(ratio * 100)}
style={{ "--p": clamp01(ratio) } as React.CSSProperties}
>
<i />
<em style={{ left: `${tidyAt * 100}%` }} />
</div>
<b>{Math.round(ratio * 100)}%</b>
<Roll>
{note ??
(level === "ok"
? `${formatTokens(used)} of ${formatTokens(limit)}`
: "Older messages can be folded")}
</Roll>
<span className="aik-mt-grow" />
{level !== "ok" ? (
<button type="button" className="aik-mt-cbtn" onClick={onCompact}>
<Minimize2 size={13} />
Compact
</button>
) : null}
</div>
</div>
</div>
)
}
/** The rule left in a thread where older messages were folded. */
export function AiCompactMarker({
tokens,
className,
}: {
/** How many tokens the compaction freed. */
tokens: number
className?: string
}) {
return (
<div className={cx("aik-mt-marker", className)} role="separator">
<span>
<Minimize2 size={13} />
Compacted {formatTokens(tokens)} tokens
</span>
</div>
)
}
Installation
One command adds ai-meter and the shared AI Kit layer it is built on (tokens, faces, marks and brand icons). No animation library, light and dark built in.
$npx shadcn@latest add https://tweenui.dev/r/ai-meter.json
ai-meter-demo
"use client"
import * as React from "react"
import { AiDemo } from "@/lib/ai-demo"
import { AiStyle, Face, useAiLoop, useIdleHands } from "@/lib/ai-kit"
import {
AiCompactMarker,
AiContextBlocks,
AiContextRim,
AiContextTank,
type AiContextSegment,
} from "@/registry/tween/ai-meter"
const CSS = `
.aik-mtd-turn{display:grid;grid-template-columns:28px minmax(0,1fr);gap:10px;font-size:15px;line-height:24px}
.aik-mtd-turn p{padding-top:2px;color:var(--aik-text)}
.aik-mtd-user{justify-self:end;max-width:78%;padding:10px 16px;border-radius:18px;background:var(--aik-raised);font-size:15px;line-height:22px}
`
const LIMIT = 200_000
const TANK_STEPS: Array<[number, string]> = [
[68_000, "Loaded 6 files"],
[104_000, "Three tool results"],
[142_000, "Long conversation"],
[166_000, "Past the tidy point"],
[188_000, "Time to compact"],
]
const WRAP: React.CSSProperties = { display: "grid", width: "min(100%, 560px)" }
function Tank() {
const root = React.useRef<HTMLDivElement>(null)
const [hands, touch] = useIdleHands()
const [used, setUsed] = React.useState(104_000)
const [note, setNote] = React.useState("Tools returned 3 results")
useAiLoop(
root,
async ({ sleep }) => {
setUsed(36_000)
setNote("System and rules")
await sleep(1100)
for (const [value, line] of TANK_STEPS) {
setUsed(value)
setNote(line)
await sleep(1500)
}
await sleep(500)
setUsed(72_000)
setNote("Compacted 116k tokens")
await sleep(2400)
},
{ gap: 500, enabled: !hands, still: () => setUsed(166_000) }
)
return (
<div ref={root} style={WRAP} onPointerDown={touch} onKeyDown={touch}>
<AiContextTank
used={used}
limit={LIMIT}
note={note}
onAdd={() => {
setUsed((value) => Math.min(LIMIT, value + 24_000))
setNote("Added 24k of files")
}}
onCompact={() => {
setUsed((value) => Math.max(30_000, Math.round(value * 0.4)))
setNote("Compacted older messages")
}}
/>
</div>
)
}
const seg = (
system: number,
files: number,
chat: number,
tools: number
): AiContextSegment[] => [
{ id: "system", label: "System and rules", tokens: system, tone: "ink" },
{ id: "files", label: "Files", tokens: files, tone: "blue" },
{ id: "chat", label: "Conversation", tokens: chat, tone: "violet" },
{ id: "tools", label: "Tool results", tokens: tools, tone: "teal" },
]
const BLOCK_STEPS: Array<[AiContextSegment[], string]> = [
[seg(12_000, 0, 4_000, 0), "A fresh session"],
[seg(12_000, 38_000, 10_000, 0), "Loaded 6 files"],
[seg(12_000, 52_000, 22_000, 18_000), "Tools returned 18k"],
[seg(12_000, 60_000, 46_000, 44_000), "Getting full"],
]
function Blocks() {
const root = React.useRef<HTMLDivElement>(null)
const [hands, touch] = useIdleHands()
const [segments, setSegments] = React.useState(BLOCK_STEPS[2]![0])
const [note, setNote] = React.useState("Tools returned 18k")
useAiLoop(
root,
async ({ sleep }) => {
for (const [value, line] of BLOCK_STEPS) {
setSegments(value)
setNote(line)
await sleep(1900)
}
setSegments(seg(12_000, 60_000, 14_000, 6_000))
setNote("Compacted 70k tokens")
await sleep(2600)
},
{ gap: 400, enabled: !hands, still: () => setSegments(BLOCK_STEPS[3]![0]) }
)
return (
<div ref={root} style={WRAP} onPointerDown={touch} onKeyDown={touch}>
<AiContextBlocks
segments={segments}
limit={LIMIT}
note={note}
onCompact={() => {
setSegments((list) =>
list.map((item) =>
item.id === "chat" || item.id === "tools"
? { ...item, tokens: Math.round(item.tokens * 0.25) }
: item
)
)
setNote("Folded older messages and results")
}}
/>
</div>
)
}
function Rim() {
const root = React.useRef<HTMLDivElement>(null)
const [hands, touch] = useIdleHands()
const [used, setUsed] = React.useState(96_000)
const [freed, setFreed] = React.useState(0)
const compact = () => {
const next = Math.round(used * 0.36)
setFreed(used - next)
setUsed(next)
}
useAiLoop(
root,
async ({ sleep }) => {
setFreed(0)
setUsed(96_000)
await sleep(1300)
setUsed(138_000)
await sleep(1300)
setUsed(172_000)
await sleep(2200)
setFreed(110_000)
setUsed(62_000)
await sleep(3000)
},
{ gap: 500, enabled: !hands, still: () => setUsed(172_000) }
)
return (
<div ref={root} style={WRAP} onPointerDown={touch} onKeyDown={touch}>
<AiContextRim
used={used}
limit={LIMIT}
placeholder="Ask Mira to keep going"
onCompact={compact}
>
<div className="aik-mtd-user">Now add the yearly toggle.</div>
{freed > 0 ? <AiCompactMarker tokens={freed} /> : null}
<div className="aik-mtd-turn">
<Face size={28} shape="orb" mood={freed > 0 ? "happy" : "talk"} />
<p>
The toggle is in. Prices switch with a 180ms state change and the
saving badge rolls over.
</p>
</div>
</AiContextRim>
</div>
)
}
export function AiMeterDemo({ only }: { only?: string }) {
return (
<>
<AiStyle id="ai-meter-demo" css={CSS} />
<AiDemo
only={only}
tabs={[
{ name: "Tank", node: <Tank /> },
{ name: "Blocks", node: <Blocks /> },
{ name: "Rim meter", node: <Rim /> },
]}
/>
</>
)
}
Installation
Usage
import {
AiCompactMarker,
AiContextBlocks,
AiContextRim,
AiContextTank,
} from "@/components/ui/ai-meter"<AiContextTank
used={142_000}
limit={200_000}
note="Long conversation"
onCompact={compact}
/>
<AiContextBlocks
limit={200_000}
segments={[
{ id: "system", label: "System and rules", tokens: 12_000, tone: "ink" },
{ id: "files", label: "Files", tokens: 52_000, tone: "blue" },
{ id: "chat", label: "Conversation", tokens: 22_000, tone: "violet" },
]}
onCompact={compact}
/>
<AiContextRim used={172_000} limit={200_000} onCompact={compact}>
<AiCompactMarker tokens={110_000} />
</AiContextRim>Versions
AiContextTankis the window as a vessel. Liquid rises to the share in use behind a drifting wave, turns amber past the tidy point and red near the top, and dips when a compaction drains it.AiContextBlocksis a hundred cells, one per percent. Cells fill in each part's colour with a stagger, so you can see which part is growing. Compacting dissolves cells and the rest repack.AiContextRimis the meter where it lives, in the rim under a composer. A thin bar carries a tick at the tidy point; past it the rim turns amber and offers Compact. PutAiCompactMarkerin the thread to show where older messages were folded.
All three take plain numbers and work at any value, including zero and over the limit. formatTokens is exported for your own labels.
Props
AiContextTank
| Prop | Type | Default | Description |
|---|---|---|---|
used | number | Tokens in the window now. | |
limit | number | The window's size in tokens. | |
tidyAt | number | 0.75 | Share of the window past which it warns. |
note | string | A line under the figure. Rolls when it changes. | |
onCompact | () => void | Called by the Compact action. | |
onAdd | () => void | Called by Add context. The button hides when omitted. |
AiContextBlocks
| Prop | Type | Default | Description |
|---|---|---|---|
segments | { id, label, tokens, tone }[] | What fills the window, in fill order. | |
limit | number | The window's size in tokens. | |
note | string | A line beside the Compact action. | |
onCompact | () => void | Called by the Compact action. |
AiContextRim
| Prop | Type | Default | Description |
|---|---|---|---|
used | number | Tokens in the window now. | |
limit | number | The window's size in tokens. | |
tidyAt | number | 0.8 | Where the tick sits and the warning starts. |
placeholder | string | "Ask a follow-up" | The mock field's text. |
onCompact | () => void | Called by the Compact button in the rim. | |
children | ReactNode | The thread above, including AiCompactMarker. |