shadcn.io is an independent project built on shadcn/ui
On this page11 sections
Context is a small button near the message field that says how much of the model's context window the conversation has filled, such as 8% context. Hovering or focusing it opens a card with the tokens used against the limit, a bar, and the input, output and reasoning counts.
It is a pattern built from Button, Hover Card and Progress. Show it in chats long enough to fill the window, so people see the limit coming before a request fails.
Installation
Context is a pattern. The command adds the parts it's built from, Hover Card, Progress and Button, and the code to copy is the demo's.
Anatomy

Usage
import { Button } from "@/components/ui/button";
import {
HoverCard, HoverCardContent, HoverCardTrigger,
} from "@/components/ui/hover-card";
import { Progress } from "@/components/ui/progress";export function Context(props: { limit: number; used: number }) {
const percent = Math.round((props.used / props.limit) * 100);
return (
<HoverCard closeDelay={100} openDelay={10}>
<HoverCardTrigger asChild>
<Button size="sm" variant="outline">
{percent}% context
</Button>
</HoverCardTrigger>
<HoverCardContent className="flex w-64 flex-col gap-3">
<div className="flex items-center justify-between gap-3 text-sm">
<span className="font-medium">Context</span>
<span className="text-muted-foreground">
{props.used} / {props.limit}
</span>
</div>
<Progress aria-label="Context used" value={percent} />
</HoverCardContent>
</HoverCard>
);
}Both props are token counts: used is what the conversation holds now, and limit is the model's context window.
Accessibility
The trigger is a <button>, and its text, such as 8% context, is the name a screen reader announces. Focusing it from the keyboard opens the card as hovering does. Ark UI doesn't tie the card to the button, renders it at the end of the page and closes it once focus moves on, so the breakdown reaches sighted users only. Keep what everyone needs in the label, including a warning in words when the window is nearly full.
- Tab
- Moves focus to the button, which opens the card. Moving focus on closes it.
- Shift+Tab
- Moves focus to the previous focusable element, and the card closes.
- EscEscape
- Closes the card while focus stays on the button.
AI SDK
The AI SDK reports usage at the end of each step of a model call. Attach the latest step's usage to the agent's message from messageMetadata, which the SDK calls for every part of the stream.
return result.toUIMessageStreamResponse({
messageMetadata: ({ part }) => {
if (part.type === "finish-step") {
return { usage: part.usage };
}
},
});const usage = message.metadata?.usage;
// outputTokens already includes outputTokenDetails.reasoningTokens.
const used = (usage?.inputTokens ?? 0) + (usage?.outputTokens ?? 0);
const percent = Math.round((used / limit) * 100);Prefer the step's usage to the totalUsage of the finish part. In a run with tool calls, totalUsage adds up every step, and each step resends the whole thread, so it overstates how full the window is. The SDK doesn't report the model's context window, so set limit yourself for the model in use.
Demos
Nearly full
The same meter at 92%, past the 80% threshold the demo keeps in WARN_AT. The button switches to the destructive variant with a warning icon, and its label reads 92% context, nearly full, which is what a screen reader announces and what a phone shows without any hover. The card adds a line that suggests a new chat. Use it once a conversation can outgrow the window.
Migrating from AI SDK Elements
If you added Context from AI SDK Elements (opens in a new tab), your project has a components/ai-elements/context.tsx wrapper. Paste this prompt into your coding agent to rebuild it on Hover Card and Progress. Every export keeps its name and props, so the code that uses it keeps working.
Migrate components/ai-elements/context.tsx from AI SDK Elements to the shadcn.io Hover Card and Progress components.
Keep every export, with the same name and props, so the code that imports it keeps working. Only change how each part renders.
1. Read https://www.shadcn.io/ui/ark/context.md and build Context the same way. If the project hasn't got Hover Card, Progress and Button, run the command in its Installation section.
2. Rebuild each part:
- Context renders a HoverCard with openDelay and closeDelay. Keep usedTokens, maxTokens, usage and modelId in its context.
- ContextTrigger renders a HoverCardTrigger with asChild and a Button with variant="outline" and size="sm", showing the percent used.
- ContextContent renders a HoverCardContent.
- ContextContentHeader renders the used and max tokens, and a Progress with aria-label="Context used".
- Keep ContextContentBody, ContextContentFooter and the usage parts as they are.
3. Remove the imports the file no longer uses.
4. Run the type checker and fix any errors.FAQ
Where does the Context component get its token counts?
From the model's own usage report. With the AI SDK, the route attaches each step's usage to the agent's message as metadata, and the client reads inputTokens and outputTokens from the latest message that has them, as the AI SDK section shows.
The limit is yours to supply, because the SDK doesn't report a model's context window. Keep it next to each model you offer, from the provider's documentation, and pass the one for the model in use.
Should I add up the token usage of every message for Context?
No. Each request sends the whole thread, so the input tokens of the latest step already count every message before it. Add that step's output tokens and you have what the next request starts from.
Summing messages counts the early ones again on every turn, and the meter fills far faster than the window. Leave reasoning out of the sum as well, since the AI SDK's outputTokens already includes it. The demo counts the same way: its Reasoning row sits under Output as a part of it, and the total adds input and output alone.
Why does the Context percentage only change after the reply finishes?
Because the model reports its token counts only once a step is done. While a reply streams there is no new number, and the agent's new message carries no usage in its metadata until its first step ends.
Read the usage from the latest message that has one, so the button keeps the previous turn's value instead of dropping to 0% while the reply streams. With the route in the AI SDK section, an agent that calls tools then moves the meter after every step, since each step is a request of its own.
Can people see the Context token breakdown on a phone?
The percentage, always, since it is the button's label. The card is another matter: Ark UI ignores touch when it tracks hover, so on a phone the card opens only when a tap gives the button focus, which depends on the browser.
If the breakdown matters on a phone, show the same content in a Popover that opens on press instead.
What should Context show when the context window is nearly full?
Make the warning hard to miss, since a bar changing length is easy to overlook. Past a threshold, such as 80%, change the button's text and its variant, as the Nearly full demo does, and add a line to the thread that suggests starting a new chat or summarizing this one.
At 100% the next request doesn't fit, and the provider rejects it. Decide before then whether your route drops the oldest messages or ends the conversation with a clear message.
How does a screen reader announce the Context meter?
It reads the button's text, such as 8% context, as its name, which is why the percentage belongs in the label rather than only in the card.
Ark UI doesn't connect the card to the button, and the card closes when focus moves on, so a screen reader user hears the label and not the breakdown. Put anything they must know, such as a near-full warning, in the label too.
Is this the same as AI SDK Elements' Context?
It does the same job without a wrapper. In AI SDK Elements, Context was a Hover Card with usage parts and a cost estimate, and here it is a Hover Card with a Progress, on a Button.
The Migrating from AI SDK Elements section has a prompt that rebuilds the old file on these parts and keeps its exports, so the code that imports it doesn't change.
API Reference
Context has no props of its own. The card is a Hover Card on a Button with variant outline and size sm, and the bar is a Progress with an aria-label.











