---
title: "Context"
description: "How much of the context window the conversation has used."
---

```tsx
import { For } from "solid-js"

import { Button } from "~/components/ui/button"
import {
  HoverCard,
  HoverCardContent,
  HoverCardTrigger
} from "~/components/ui/hover-card"
import { Progress } from "~/components/ui/progress"

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 }
]

export default function ContextDemo() {
  return (
    <div class="flex justify-center py-12">
      <HoverCard delay={10} closeDelay={100}>
        <HoverCardTrigger
          render={(triggerProps) => (
            <Button {...triggerProps} variant="outline" size="sm">
              {percent}% context
            </Button>
          )}
        />
        <HoverCardContent 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">
              {formatTokens(used)} / {formatTokens(usage.limit)}
            </span>
          </div>
          <Progress value={percent} aria-label="Context used" />
          <div class="flex flex-col gap-1 text-sm">
            {
              <For each={rows}>
                {(row) => (
                  <div class="flex justify-between gap-3">
                    <span class="text-muted-foreground">{row.label}</span>
                    <span>{formatTokens(row.value)}</span>
                  </div>
                )}
              </For>
            }
          </div>
        </HoverCardContent>
      </HoverCard>
    </div>
  )
}
```

Context shows how much of the model's context window the conversation has used. It is a [Hover Card](/docs/components/hover-card/) with a [Progress](/docs/components/progress/) bar.

## Installation

Context is built with the [Hover Card](/docs/components/hover-card/) and [Progress](/docs/components/progress/) components.

<CodeTabs>

<TabsList>
  <TabsTrigger value="cli">Command</TabsTrigger>
  <TabsTrigger value="manual">Manual</TabsTrigger>
</TabsList>
<TabsContent value="cli">

```bash
npx solidui-cli@latest add hover-card progress
```

</TabsContent>

<TabsContent value="manual">

<Steps class="mb-0 pt-2">

<Step>Install the required Solid UI dependencies:</Step>

```bash
npx solidui-cli@latest add hover-card progress
```

<Step>Copy and paste the following code into your project.</Step>

```tsx title="components/ui/hover-card.tsx"
type HoverCardProps = ComponentProps<typeof SdkHoverCard>
import { type ComponentProps } from "@solidjs/web"
import {
  HoverCard as SdkHoverCard,
  HoverCardTrigger as SdkHoverCardTrigger,
  HoverCardContent as SdkHoverCardContent
} from "@ui-sdk/solid/hover-card"
import { omit } from "solid-js"

import { cn } from "~/lib/utils"
const HoverCard = (props: HoverCardProps) => <SdkHoverCard {...props} />
type HoverCardTriggerProps = Omit<
  ComponentProps<typeof SdkHoverCardTrigger>,
  "class"
> & {
  class?: string
}
const HoverCardTrigger = (props: HoverCardTriggerProps) => (
  <SdkHoverCardTrigger {...props} />
)
type HoverCardContentProps = Omit<
  ComponentProps<typeof SdkHoverCardContent>,
  "class"
> & {
  class?: string
}
const HoverCardContent = (props: HoverCardContentProps) => {
  const others = omit(props, "class", "children")
  return (
    <>
      <SdkHoverCardContent
        class={cn(
          "w-64 rounded-lg bg-popover p-2.5 text-sm text-popover-foreground shadow-md ring-1 ring-foreground/10 duration-100 data-ending-style:animate-out data-ending-style:fade-out-0 data-ending-style:zoom-out-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 z-50 origin-(--transform-origin) outline-hidden",
          props.class
        )}
        {...others}
      >
        {props.children}
      </SdkHoverCardContent>
    </>
  )
}
export { HoverCard, HoverCardContent, HoverCardTrigger }
```

<Step>Update the import paths to match your project setup.</Step>

</Steps>

</TabsContent>

</CodeTabs>

## Usage

```tsx showLineNumbers
<HoverCard openDelay={10} closeDelay={100}>
  <HoverCardTrigger
    render={(triggerProps) => (
      <Button {...triggerProps} variant="outline" size="sm">
        {percent}% context
      </Button>
    )}
  />
  <HoverCardContent 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" />
  </HoverCardContent>
</HoverCard>
```

## AI SDK

Read usage from the metadata on the agent's message. `messageMetadata` runs when the stream finishes.

```ts showLineNumbers title="app/api/chat/route.ts"
return result.toUIMessageStreamResponse({
  messageMetadata: ({ part }) => {
    if (part.type === "finish") {
      return { usage: part.totalUsage }
    }
  }
})
```

```tsx showLineNumbers
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.

## Migrating from AI SDK Elements

If you added Context from [AI SDK Elements](https://elements.ai-sdk.dev), 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.

```txt title="Prompt" prompt
Migrate components/ai-elements/context.tsx from AI SDK Elements to the Solid UI 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. Install the components: npx solidui-cli@latest add hover-card progress button
2. Read https://www.solid-ui.com/docs/ai-elements/context.md and build Context the same way.
3. 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 a Button passed through render 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.
4. Remove the imports the file no longer uses.
5. Run the type checker and fix any errors.
```
