āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā š shadcn/directory/ai-elements/context ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā
A compound component system for displaying AI model context window usage, token consumption, and cost estimation.
The Context component provides a comprehensive view of AI model usage through a compound component system. It displays context window utilization, token consumption breakdown (input, output, reasoning, cache), and cost estimation in an interactive hover card interface.
npx ai-elements@latest add context
import {
Context,
ContextTrigger,
ContextContent,
ContextContentHeader,
ContextContentBody,
ContextContentFooter,
ContextInputUsage,
ContextOutputUsage,
ContextReasoningUsage,
ContextCacheUsage,
} from '@/components/ai-elements/context';
<Context maxTokens={128000} usedTokens={40000} modelId="openai:gpt-4">
<ContextTrigger />
<ContextContent>
<ContextContentHeader />
<ContextContentBody>
<ContextInputUsage />
<ContextOutputUsage />
<ContextReasoningUsage />
<ContextCacheUsage />
</ContextContentBody>
<ContextContentFooter />
</ContextContent>
</Context>
| Prop | Type | | maxTokens? | number | | usedTokens? | number | | usage? | LanguageModelUsage | | modelId? | ModelId | | ...props? | ComponentProps<HoverCard> |
| Prop | Type | | children? | React.ReactNode | | ...props? | ComponentProps<Button> |
| Prop | Type | | className? | string | | ...props? | ComponentProps<HoverCardContent> |
| Prop | Type | | children? | React.ReactNode | | ...props? | ComponentProps<"div"> |
| Prop | Type | | children? | React.ReactNode | | ...props? | ComponentProps<"div"> |
| Prop | Type | | children? | React.ReactNode | | ...props? | ComponentProps<"div"> |
All usage components (ContextInputUsage, ContextOutputUsage, ContextReasoningUsage, ContextCacheUsage) share the same props:
| Prop | Type | | children? | React.ReactNode | | className? | string | | ...props? | ComponentProps<"div"> |
The Context component uses a compound component pattern with React Context for data sharing:
<Context> - Root provider component that holds all context data<ContextTrigger> - Interactive trigger element (default: button with percentage)<ContextContent> - Hover card content container<ContextContentHeader> - Header section with progress visualization<ContextContentBody> - Body section for usage breakdowns<ContextContentFooter> - Footer section for total costThe component uses Intl.NumberFormat with compact notation for automatic formatting:
When a modelId is provided, the component automatically calculates costs using the tokenlens library:
Costs are formatted using Intl.NumberFormat with USD currency.
The component uses Tailwind CSS classes and follows your design system:
ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā