Token Counter
Display 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 | — | Number of input tokens used |
outputTokensRequired | number | — | Number of output tokens used |
maxTokens | number | 4096 | Maximum token limit for the model |
inputCostPer1K | number | 0.03 | Cost per 1K input tokens (USD) |
outputCostPer1K | number | 0.06 | Cost per 1K output tokens (USD) |
showCost | boolean | true | Show cost estimation |
showBreakdown | boolean | true | Show token breakdown |
variant | 'default' | 'compact' | 'default' | Visual style variant |
warningThreshold | number | 70 | Warning threshold (percentage) |
dangerThreshold | number | 90 | Danger threshold (percentage) |
isRTL | boolean | false | Whether text direction is RTL |
label | string | — | Label for the counter |
labelAr | string | — | Label in Arabic |
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.
RTL Considerations
The counter works seamlessly in RTL layouts. All text, numbers, and badges adapt to RTL direction. Use Arabic number formatting automatically.
LTR (English)
RTL (العربية)
Related Components
Choose AI models with different token limits
Adjust max tokens parameter
Display messages that consume tokens