Token Counter
View on StorybookStorybookDisplay and track token usage with cost estimation for AI model interactions. Features progress bar, breakdown by input/output, and visual warnings when approaching limits.
Preview
Installation
Usage
Examples
With Cost Estimation
Warning States
Safe (< 70%)
Warning (70-90%)
Danger (> 90%)
Different Models with Pricing
GPT-4 (expensive)
GPT-3.5 Turbo (affordable)
Compact Variant
Custom Thresholds
Customize when warnings appear (50% warning, 75% danger):
Without Cost Display
Props
| Name | Type | Default | Description |
|---|---|---|---|
inputTokensRequired | number | — | |
outputTokensRequired | number | — | |
maxTokens | number | 4096 | |
inputCostPer1K | number | 0.03 | |
outputCostPer1K | number | 0.06 | |
showCost | boolean | true | Display cost estimate |
showBreakdown | boolean | true | |
variant | 'default' | 'compact' | 'default' | |
warningThreshold | number | 70 | Percentage of limit to show warning (default: 0.8) |
dangerThreshold | number | 90 | |
isRTL | boolean | false | |
label | string | — | |
labelAr | string | — |
Accessibility
Screen Reader
The component includes descriptive labels and tooltips. Token counts and costs are formatted with proper number formatting for screen readers.
Visual Indicators
Multiple visual cues indicate status: progress bar color, warning badge, and border color changes. This ensures warnings are visible even without relying solely on color.
Tooltips
Info icon provides context about tokens. Cost tooltip breaks down input/output costs separately for transparency.
Use Cases
Input length monitoring for API limits
Cost estimation before API calls
Context window usage display
Batch processing budget monitors
Best Practices
Do
- ✓ Update count in real-time as the user types
- ✓ Show visual warning as limit approaches
- ✓ Display both current count and maximum
Don't
- ✗ Don't block input when limit is reached — warn instead
- ✗ Don't use exact token counts when an estimate suffices
- ✗ Don't make the counter too prominent for short inputs