Context
shadcn/uiDisplays token context window utilization with a circular gauge and breakdown hover card.
Context
Hover the ring gauge to inspect the token breakdown.
import { useState } from "react";import { Context, ContextContent, ContextContentBody, ContextContentFooter, ContextContentHeader, ContextTrigger, ContextUsageRow, type ContextUsage,} from "@/components/optics/ai-elements/context";import { Button } from "@/components/optics/button";type PresetId = "24" | "68" | "94";interface ContextPreset { id: PresetId; label: string; usedTokens: number; usage: ContextUsage; costs: { input: string; cache: string; reasoning: string; output: string; };}const MAX_TOKENS = 200_000;const PRESETS: ContextPreset[] = [ { id: "24", label: "24%", usedTokens: 48_000, usage: { inputTokens: 32_400, cachedInputTokens: 8_200, reasoningTokens: 3_800, outputTokens: 3_600, totalTokens: 48_000, }, costs: { input: "$0.10", cache: "$0.01", reasoning: "$0.06", output: "$0.05" }, }, { id: "68", label: "68%", usedTokens: 136_000, usage: { inputTokens: 84_500, cachedInputTokens: 28_000, reasoningTokens: 11_500, outputTokens: 12_000, totalTokens: 136_000, }, costs: { input: "$0.25", cache: "$0.02", reasoning: "$0.17", output: "$0.18" }, }, { id: "94", label: "94%", usedTokens: 188_000, usage: { inputTokens: 112_000, cachedInputTokens: 42_000, reasoningTokens: 18_000, outputTokens: 16_000, totalTokens: 188_000, }, costs: { input: "$0.34", cache: "$0.03", reasoning: "$0.27", output: "$0.24" }, },];function ContextDemo() { const [selectedId, setSelectedId] = useState<PresetId>("24"); const activePreset = PRESETS.find((preset) => preset.id === selectedId) ?? PRESETS[0]; return ( <div className="flex flex-col items-center gap-4"> <Context usedTokens={activePreset.usedTokens} maxTokens={MAX_TOKENS} usage={activePreset.usage} modelId="claude-sonnet-4-5" > <ContextTrigger /> <ContextContent> <ContextContentHeader /> <ContextContentBody> <ContextUsageRow kind="input" costText={activePreset.costs.input} /> <ContextUsageRow kind="cache" costText={activePreset.costs.cache} /> <ContextUsageRow kind="reasoning" costText={activePreset.costs.reasoning} /> <ContextUsageRow kind="output" costText={activePreset.costs.output} /> </ContextContentBody> <ContextContentFooter /> </ContextContent> </Context> <div className="flex items-center gap-2"> {PRESETS.map((preset) => ( <Button key={preset.id} type="button" size="sm" variant={selectedId === preset.id ? "default" : "outline"} onClick={() => setSelectedId(preset.id)} > {preset.label} </Button> ))} </div> <p className="text-xs text-muted-foreground"> Hover the ring gauge to inspect the token breakdown. </p> </div> );}<ContextDemo />import { useState } from "react";import { Context, ContextContent, ContextContentBody, ContextContentFooter, ContextContentHeader, ContextTrigger, ContextUsageRow, type ContextUsage,} from "@/components/optics/ai-elements/context";import { Button } from "@/components/optics/button";type PresetId = "24" | "68" | "94";interface ContextPreset { id: PresetId; label: string; usedTokens: number; usage: ContextUsage; costs: { input: string; cache: string; reasoning: string; output: string; };}const MAX_TOKENS = 200_000;const PRESETS: ContextPreset[] = [ { id: "24", label: "24%", usedTokens: 48_000, usage: { inputTokens: 32_400, cachedInputTokens: 8_200, reasoningTokens: 3_800, outputTokens: 3_600, totalTokens: 48_000, }, costs: { input: "$0.10", cache: "$0.01", reasoning: "$0.06", output: "$0.05" }, }, { id: "68", label: "68%", usedTokens: 136_000, usage: { inputTokens: 84_500, cachedInputTokens: 28_000, reasoningTokens: 11_500, outputTokens: 12_000, totalTokens: 136_000, }, costs: { input: "$0.25", cache: "$0.02", reasoning: "$0.17", output: "$0.18" }, }, { id: "94", label: "94%", usedTokens: 188_000, usage: { inputTokens: 112_000, cachedInputTokens: 42_000, reasoningTokens: 18_000, outputTokens: 16_000, totalTokens: 188_000, }, costs: { input: "$0.34", cache: "$0.03", reasoning: "$0.27", output: "$0.24" }, },];function ContextDemo() { const [selectedId, setSelectedId] = useState<PresetId>("24"); const activePreset = PRESETS.find((preset) => preset.id === selectedId) ?? PRESETS[0]; return ( <div className="flex flex-col items-center gap-4"> <Context usedTokens={activePreset.usedTokens} maxTokens={MAX_TOKENS} usage={activePreset.usage} modelId="claude-sonnet-4-5" > <ContextTrigger /> <ContextContent> <ContextContentHeader /> <ContextContentBody> <ContextUsageRow kind="input" costText={activePreset.costs.input} /> <ContextUsageRow kind="cache" costText={activePreset.costs.cache} /> <ContextUsageRow kind="reasoning" costText={activePreset.costs.reasoning} /> <ContextUsageRow kind="output" costText={activePreset.costs.output} /> </ContextContentBody> <ContextContentFooter /> </ContextContent> </Context> <div className="flex items-center gap-2"> {PRESETS.map((preset) => ( <Button key={preset.id} type="button" size="sm" variant={selectedId === preset.id ? "default" : "outline"} onClick={() => setSelectedId(preset.id)} > {preset.label} </Button> ))} </div> <p className="text-xs text-muted-foreground"> Hover the ring gauge to inspect the token breakdown. </p> </div> );}<ContextDemo />Installation
pnpm dlx shadcn@latest add @optics/ai-context
Props
<Context />
Name | Type | |
|---|---|---|
usedTokens | number | |
maxTokens | number | |
modelId | string | |
open | boolean | |
defaultOpen | boolean | |
onOpenChangeComplete | ((open: boolean) => void) | |
triggerId | string | |
defaultTriggerId | string | |
onOpenChange | (...args) => void | |
delay | number |
<ContextTrigger />
Name | Type | |
|---|---|---|
variant | "link" | "outline" | "default"(default: outline) | |
delay | number(default: 10) | |
closeDelay | number(default: 100) | |
render | React.ReactElement |
<ContextContent />
Name | Type | |
|---|---|---|
render | React.ReactElement | |
sideOffset | number | |
alignOffset | number |
<ContextUsageRow />
Name | Type | |
|---|---|---|
label | React.ReactNode | |
tokens | number | |
costText | React.ReactNode |