Skip to content
WebJs
Menu

How much of the context window the conversation has used.

import { html } from '@webjsdev/core';
import { buttonClass } from '#components/ui/button.ts';
import { progressClass } from '#components/ui/progress.ts';

const usage = {
  input: 8420,
  output: 1260,
  reasoning: 980,
  limit: 128000,
};

const used = usage.input + usage.output + usage.reasoning;
const percent = Math.round((used / usage.limit) * 100);

function formatTokens(value: number) {
  if (value < 1000) return String(value);
  return `${(value / 1000).toFixed(1).replace(/\.0$/, '')}k`;
}

const rows = [
  { label: 'Input', value: usage.input },
  { label: 'Output', value: usage.output },
  { label: 'Reasoning', value: usage.reasoning },
];

// The class on <ui-hover-card-content> lands on the card panel, as shadcn's
// className on HoverCardContent does.
export default function ContextDemo() {
  return html`
    <div class="flex justify-center py-12">
      <ui-hover-card open-delay="10" close-delay="100">
        <ui-hover-card-trigger>
          <button type="button" data-slot="button" data-variant="outline" data-size="sm" class=${buttonClass({ variant: 'outline', size: 'sm' })}>
            ${percent}% context
          </button>
        </ui-hover-card-trigger>
        <ui-hover-card-content class="flex w-64 flex-col gap-3">
          <div class="flex items-center justify-between gap-3 text-sm">
            <span data-slot="hover-card-title" class="font-medium">Context</span>
            <span class="text-muted-foreground">${formatTokens(used)} / ${formatTokens(usage.limit)}</span>
          </div>
          <progress data-slot="progress" value=${percent} max="100" aria-label="Context used" class=${progressClass()}></progress>
          <div class="flex flex-col gap-1 text-sm">
            ${rows.map((row) => html`
              <div class="flex justify-between gap-3">
                <span class="text-muted-foreground">${row.label}</span>
                <span>${formatTokens(row.value)}</span>
              </div>
            `)}
          </div>
        </ui-hover-card-content>
      </ui-hover-card>
    </div>
  `;
}
View Code

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.

pnpm dlx @webjsdev/ui add hover-card progress

Usage

<ui-hover-card open-delay="10" close-delay="100">
  <ui-hover-card-trigger>
    <button type="button" data-slot="button" data-variant="outline" data-size="sm" class=${buttonClass({ variant: 'outline', size: 'sm' })}>
      ${percent}% context
    </button>
  </ui-hover-card-trigger>
  <ui-hover-card-content class="flex w-64 flex-col gap-3">
    <div class="flex items-center justify-between gap-3 text-sm">
      <span data-slot="hover-card-title" class="font-medium">Context</span>
      <span class="text-muted-foreground">${used} / ${limit}</span>
    </div>
    <progress data-slot="progress" value=${percent} max="100" aria-label="Context used" class=${progressClass()}></progress>
  </ui-hover-card-content>
</ui-hover-card>

The class on <ui-hover-card-content> lands on the card panel.

AI SDK

Read usage from the metadata on the agent's message. In the 'use server' action that streams the reply, messageMetadata runs when the stream finishes.

modules/chat/actions/chat.server.ts
'use server';
import { streamText } from 'ai';

export async function* chat(messages: ModelMessage[]) {
  const result = streamText({ model, messages });
  yield* result.toUIMessageStream({
    messageMetadata: ({ part }) => {
      if (part.type === 'finish') return { usage: part.totalUsage };
    },
  });
}
const usage = message.metadata?.usage;
const used = (usage?.inputTokens ?? 0) + (usage?.outputTokens ?? 0);
const percent = 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. See AI SDK for streaming the parts to a component.

Migrating from AI SDK Elements

If you used Context from AI SDK Elements, its wrapper lives in components/ai-elements/context.tsx. In a WebJs app it becomes this pattern, built from WebJs UI components. Each export of the wrapper maps onto a part:

AI SDK ElementsWebJs UI
ContextA <ui-hover-card open-delay close-delay>. Keep the used and max tokens, the usage and the model id in your component.
ContextTriggerA <ui-hover-card-trigger> holding a button with buttonClass({ variant: 'outline', size: 'sm' }), showing the percent used.
ContextContentA <ui-hover-card-content>.
ContextContentHeaderThe used and max tokens, and a <progress> with progressClass() and aria-label="Context used".
ContextContentBody, ContextContentFooter and the usage partsPlain rows inside the card, as the demo's Input, Output and Reasoning rows.