Usage Meter
spectrumui/usage-meterFreeBlock
A context-window meter with token counts and estimated cost.
Install it
npx shadcn@latest add https://ui.spectrumhq.in/r/usage-meter.jsonSource
1'use client';23import { cn } from '@/lib/utils';4import type { UsageState } from './types';56export type UsageMeterVariant = 'Bar' | 'Inline';78export interface UsageMeterProps {9 usage: UsageState;10 variant?: UsageMeterVariant;11 className?: string;12}1314function formatTokens(count: number) {15 if (count < 1000) return `${count}`;16 return `${(count / 1000).toFixed(1)}k`;17}1819export function UsageMeter({ usage, variant = 'Bar', className }: UsageMeterProps) {20 const used = usage.promptTokens + usage.completionTokens;21 const ratio = Math.min(1, used / usage.contextWindow);22 const percent = Math.round(ratio * 100);2324 const barColor =25 ratio > 0.926 ? 'bg-red-500/80'27 : ratio > 0.7528 ? 'bg-amber-500/80'29 : 'bg-neutral-900 dark:bg-neutral-100';3031 if (variant === 'Inline') {32 return (33 <div34 className={cn(35 'flex w-fit items-center gap-2.5 font-mono text-[11.5px] tabular-nums text-neutral-500 dark:text-neutral-400',36 className,37 )}38 >39 <span aria-hidden className="h-1 w-16 overflow-hidden rounded-full bg-black/[0.07] dark:bg-white/[0.09]">40 <span41 className={cn('block h-full rounded-full transition-[width] duration-500 ease-[cubic-bezier(0.23,1,0.32,1)]', barColor)}42 style={{ width: `${percent}%` }}43 />44 </span>45 <span>46 {formatTokens(used)} / {formatTokens(usage.contextWindow)}47 </span>48 {usage.estimatedCostUsd !== undefined && (49 <span className="text-neutral-400 dark:text-neutral-600">50 ${usage.estimatedCostUsd.toFixed(4)}51 </span>52 )}53 </div>54 );55 }5657 return (58 <div className={cn('w-full max-w-[380px]', className)}>59 <div className="flex items-baseline justify-between gap-3">60 <span className="text-[12.5px] font-medium text-neutral-800 dark:text-neutral-200">61 Context window62 </span>63 <span className="font-mono text-[11.5px] tabular-nums text-neutral-500 dark:text-neutral-400">64 {formatTokens(used)} / {formatTokens(usage.contextWindow)} · {percent}%65 </span>66 </div>6768 <div69 role="meter"70 aria-valuemin={0}71 aria-valuemax={usage.contextWindow}72 aria-valuenow={used}73 aria-label="Context window usage"74 className="mt-2 h-1.5 overflow-hidden rounded-full bg-black/[0.06] dark:bg-white/[0.08]"75 >76 <span77 className={cn(78// … truncated
Files it writes
More from spectrumui
All 182 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Agent Planagent-plan | spectrumui | Blocks | Free | 1 dep | |
| Agent Stepsagent-steps | spectrumui | Blocks | Free | 1 dep · 2 files | |
| Approval Cardapproval-card | spectrumui | Blocks | Free | 1 dep | |
| Chat Empty Statechat-empty-state | spectrumui | Blocks | Free | 1 dep · 2 files | |
| Citation Sourcescitation-sources | spectrumui | Blocks | Free | no deps · 2 files | |
| Code Blockcode-block | spectrumui | Blocks | Free | 1 dep | |
| Conversation Listconversation-list | spectrumui | Blocks | Free | 1 dep | |
| Diff Viewdiff-view | spectrumui | Blocks | Free | 1 dep |
