Epicenter
Local-first, open source apps
Context shows how much of the model's context window the conversation has used. It is a Hover Card with a Progress bar.
Installation
Context is built with the Hover Card and Progress components.
Usage
<HoverCard.Root openDelay={10} closeDelay={100}>
<HoverCard.Trigger>
{#snippet child({ props })}
<Button {...props} variant="outline" size="sm">
{percent}% context
</Button>
{/snippet}
</HoverCard.Trigger>
<HoverCard.Content class="flex w-64 flex-col gap-3">
<div class="flex items-center justify-between gap-3 text-sm">
<span class="font-medium">Context</span>
<span class="text-muted-foreground">
{used} / {limit}
</span>
</div>
<Progress value={percent} aria-label="Context used" />
</HoverCard.Content>
</HoverCard.Root> AI SDK
Read usage from the metadata on the agent's message. messageMetadata runs when the stream finishes.
return result.toUIMessageStreamResponse({
messageMetadata: ({ part }) => {
if (part.type === "finish") {
return { usage: part.totalUsage };
}
},
}); const usage = $derived(message.metadata?.usage);
const used = $derived((usage?.inputTokens ?? 0) + (usage?.outputTokens ?? 0));
const percent = $derived(Math.round((used / limit) * 100)); limit is the model's context window. The AI SDK does not send it, so set it from the model you use.