Context window
Inspect prompt capacity, context categories, and removable files using authoritative token counts.
import { ContextWindow } from '@uthereal/ui/context-window'Context window playground
Context window
- 8.2K
- 14.4K
- 48.5K
- 104.5K
Overview
ContextWindow displays a model's prompt token capacity as a 216-cell field and an interactive category ledger. Users inspect itemized files, request item removal, compact a conversation category, or start a new chat. All counts come directly from the host; the component never estimates tokens from text length or invents a compaction ratio.
Use ContextWindow for prompt token budgets inside an agent interface. For billing allowances and daily plan usage, use Usage card. For span-level timing and cost breakdown, use Trace waterfall.
Usage
'use client'
import { ContextWindow } from '@uthereal/ui/context-window'
export function PromptBudget({
onContextAction,
}: {
onContextAction: (action: string, itemId: string) => void
}) {
return (
<ContextWindow
title="Context window"
modelLabel="Claude Sonnet"
limitTokens={200000}
parts={[
{
id: 'system',
label: 'System instructions',
tokens: 8200,
locked: true,
},
{
id: 'files',
label: 'Files',
items: [
{ id: 'billing', label: 'billing.ts', tokens: 21300 },
{ id: 'schema', label: 'schema.sql', tokens: 18700 },
],
},
{
id: 'conversation',
label: 'Conversation',
tokens: 104500,
compact: true,
},
]}
removeAction="remove_context_item"
compactAction="compact_conversation"
newChatAction="start_new_chat"
onAction={onContextAction}
/>
)
}Each action button calls onAction(action, targetId) with the host-registered action name. Item removal passes the item ID, compaction passes the category ID, and starting a new chat passes 'context-window'. Your application performs the operation and supplies the next snapshot.
Ownership and lifecycle
Supply either tokens or items for each category in parts, never both. Itemized categories sum their supplied item counts automatically. Item IDs must be unique across all categories because removeAction identifies targets by item ID.
Set locked: true on a category or removable: false on an item to disable its removal button. Locking controls interface availability only; the host still authorizes every action.
Mark a category with compact: true and provide compactAction to show the compact button beside that row. Report operation progress through status, statusMessage, and savedTokens:
| Status | Behavior |
|---|---|
idle | Shows remaining token count or hovered category share. Highlights warning state at or above warnAt. |
pending | Disables compact and remove buttons and animates the compacting category's cells. |
complete | Shows completion confirmation and savedTokens for 1.6 seconds. The compact button returns if it grows. |
error | Displays statusMessage or labels.compactionFailed while preserving the current context snapshot. |
Hovering or focusing a category row or its cells highlights that category in the 216-cell field and rolls the headline readout to its count. Below 310px, the grid uses fewer columns and the compact control collapses to its icon.
API reference
These serializable arguments and defaults come directly from the component schema.
| Argument | Type / default | Description |
|---|---|---|
labels | ContextWindowLabels— | Translated transport, grouping, control and feedback text; unspecified labels retain defaults. |
title | stringDefault: "Context window" | Visible heading and accessible name of this inspector. |
modelLabel | string— | Optional model name displayed above the budget. |
limitTokensRequired | integer— | Authoritative token capacity; must be a positive integer. |
partsRequired | ContextPart[]— | Context categories. Supply tokens or itemized counts per category, never both. Set compact on a category to expose the registered compact action beside it. |
warnAt | numberDefault: 0.8 | Capacity fraction at which to show the near-limit warning. |
defaultExpanded | string[]Default: [] | Initially expanded item IDs; edits preserve existing host disclosure state. |
removeAction | string— | Registered host action requested with the item ID; removal happens only after a host update. |
compactAction | string— | Registered host action requested with the compact category ID; the host supplies pending state and revised counts. |
newChatAction | string— | Registered host action to start a new conversation. |
status | idle | pending | complete | errorDefault: "idle" | Authoritative lifecycle or outcome supplied by the host; edits never execute work. |
statusMessage | string— | Accessible host-owned pending, success or error feedback. |
savedTokens | integer— | Authoritative number of tokens saved by the completed compaction. |
recipe | studio | softDefault: "studio" | Visual treatment. Both recipes preserve interaction behavior. |
theme | ContextWindowTheme— | Color mode, accent and typed semantic token overrides. |
motion | none | subtle | expressiveDefault: "subtle" | Animation preset. System reduced-motion preferences take precedence. |
motionOptions | DisclosureMotionOptions— | Disclosure transition overrides; durations are milliseconds. |
dir | ltr | rtl— | Reading direction; inherited from the page when omitted. |
ContextPart accepts { id, label, tokens?, items?, compact?, locked? }, where each item is { id, label, tokens, removable? }. Translate all interface strings together through labels.
React integration
| Prop | Purpose |
|---|---|
expanded | Controlled list of expanded itemized category IDs. |
onExpandedChange(ids) | Called when itemized categories expand or collapse, in both modes. |
onAction(action, itemId) | Called for item removal, category compaction, and new-chat requests. |
preset | Application defaults, overridden by explicit instance arguments. |
tokens | CSS variables applied after the typed theme. |
ref, className, style | Applied to the root <section> element. |
| Native attributes | id, aria-*, data-*, and remaining <section> props forwarded to the root. |
Keyboard & accessibility
| Key | Interaction |
|---|---|
Tab | Move through itemized category disclosures, compact, remove, and new-chat controls. |
Enter, Space | Toggle an itemized category disclosure or activate the focused action button. |
The 216-cell field is decorative (aria-hidden="true") inside a role="meter" region that exposes aria-valuemin, aria-valuemax, aria-valuenow, and exact aria-valuetext. Focusing a category row highlights its cells and updates the live summary. Footer feedback renders inside a role="status" live region. Set dir to override inherited RTL layout. Under forced-colors, controls preserve visible boundaries and focus rings.
Motion and theming
Itemized file groups expand and collapse on the shared disclosure spring (motionOptions). Token totals rise smoothly on change, cell updates sweep across the grid in order, and removed items fold away. motion="none" and prefers-reduced-motion: reduce remove spatial movement and settle updates immediately.
theme accepts a mode, an accent, and panel tokens for colors, status tones, typography, surface radius, and category series (series1 through series4). An application-wide UtherealProvider supplies defaults.
Composition
In an agent composition, ContextWindow is a leaf. The host stores expanded category IDs at /components/instance-<id>/expanded, seeded from defaultExpanded. Edits preserve existing expanded IDs and drop IDs removed from parts. removeAction, compactAction, and newChatAction must name registered host actions; configuration edits never execute host work. See Building with agents.
{
"type": "ContextWindow",
"props": {
"title": "Context window",
"limitTokens": 200000,
"parts": [
{
"id": "system",
"label": "System instructions",
"tokens": 8200,
"locked": true
},
{
"id": "conversation",
"label": "Conversation",
"tokens": 104500,
"compact": true
}
],
"compactAction": "compact_conversation"
},
"children": []
}