页面简介
How much of the context window the conversation has used.
正文摘录
SectionsIntroductionComponentsAI ElementsInstallationThemingCLITypesetSkillsRegistryChangelogComponentsAccordionAlertAlert DialogAspect RatioAttachmentAvatarBadgeBreadcrumbBubbleButtonButton GroupCalendarCardCarouselChartCheckboxCode BlockCollapsibleComboboxCommandContext MenuData TableDate PickerDialogDirectionDrawerDropdown MenuEmptyFieldHover CardInputInput GroupInput OTPItemKbdLabelMarkerMenubarMessageMessage ScrollerNative SelectNavigation MenuPaginationPopoverProgressQuestionnaireRadio GroupResizableScroll AreaSelectSeparatorSheetSidebarSkeletonSliderSonnerSpinnerSwitchTableTabsTextareaToastToggleToggle GroupTooltipTraceTypographyAI ElementsIntroductionChain of ThoughtChatCheckpointConfirmationContextFile TreeInline CitationMessageModel SelectorPlanPrompt InputQueueReasoningSourcesStack TraceSubagentSuggestionTaskTerminalTest ResultsToolGet StartedInstallationcomponents.jsonPackage ImportsThemingTypesetDark ModeCLIMonorepoSkillsJavaScriptFigmallms.txtLegacy Docs@shadcn/reactMessage ScrollerQuestionnaire@shadcn/helpersAI SDKTanStack AIFormsReact Hook FormTanStack FormFormischUtilitiesscroll-fadeshimmerRegistryIntroductionGetting StartedGitHub RegistriesRegistry DirectoryRegistry HealthExamplesNamespacesAuthenticationDynamic SearchMCP ServerOpen in v0FAQAPI Referenceregistry.jsonregistry-item.jsonContextCopy PagePreviousNextHow much of the context window the conversation has used.Base UIReact AriaRadix UI8% contextCopy"use client" import { Button } from "@/components/ui/button"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. CommandManualpnpmnpmyarnbunpnpm dlx shadcn@latest add hover-card progressCopy Usage Copy<HoverCard openDelay={10} closeDelay={100}> <HoverCardTrigger asChild> <Button variant="outline" size="sm"> {percent}% context </Button> </HoverCardTrigger> <HoverCardContent className="flex w-64 flex-col gap-3"> <div className="flex items-center justify-between gap-3 text-sm"> <span className="font-medium">Context</span> <span className="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. app/api/chat/route.tsCopyreturn result.toUIMessageStreamResponse({ messageMetadata: ({ part }) => { if (part.type === "finish") { return { usage: part.totalUsage } } }, }) Copyconst 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, 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. PromptCopyMigrate components/ai-elements/context.tsx from AI SDK Elements to the shadcn/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 shadcn@latest add hover-card progress button 2. Read https://ui.shadcn.com/docs/ai-elements/radix/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 asChild and a Button 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.Expand ConfirmationFile Tree On This PageInstallationUsageAI SDKMigrating from AI SDK ElementsDeploy your shadcn/ui app on VercelTrusted by OpenAI, Sonos, Adobe, and more.Vercel provides tools and infrastructure to deploy apps and features at scale.Deploy NowDeploy to Vercel