GeminiTokenUsageTracker class
Real-time token usage and cost tracker for Gemini Live API sessions.
Listens to or receives LiveServerMessage and UsageMetadata emitted by the Gemini Live WebSocket, aggregating token counts across modalities:
- Audio (input & output)
- Video (camera stream input)
- Image (still photos input)
- Text (prompts & output transcripts)
Automatically calculates estimated costs in USD and KRW based on official Google AI pricing for the active model.
- Inheritance
-
- Object
- ChangeNotifier
- GeminiTokenUsageTracker
Constructors
- GeminiTokenUsageTracker({String model = 'gemini-3.5-live-translate-preview', double usdToKrwRate = 1400.0, GeminiPricingRates? pricingRates})
Properties
- audioInputTokens → int
-
Audio input tokens processed.
no setter
- audioOutputTokens → int
-
Audio output tokens generated.
no setter
- cachedTokens → int
-
Number of tokens in cached content.
no setter
- estimatedCostKrw → double
-
Estimated cost in KRW using usdToKrwRate.
no setter
- estimatedCostUsd → double
-
Estimated cost in USD under Google AI Paid Tier.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasListeners → bool
-
Whether any listeners are currently registered.
no setterinherited
- imageInputTokens → int
-
Image input tokens processed (still images).
no setter
- model → String
-
The model identifier used in the session.
final
- pricingRates → GeminiPricingRates
-
Pricing rates configuration. If not provided, automatically resolved from model.
final
- promptTokens → int
-
Total number of prompt (input) tokens processed.
no setter
- responseTokens → int
-
Total number of candidate response (output) tokens generated.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- textInputTokens → int
-
Text input tokens processed.
no setter
- textOutputTokens → int
-
Text output tokens generated.
no setter
- thoughtsTokens → int
-
Number of internal reasoning/thoughts tokens.
no setter
- totalTokens → int
-
Total tokens consumed (prompt + response).
no setter
- usdToKrwRate → double
-
Currency conversion rate from USD to KRW (default: 1400.0).
final
- videoInputTokens → int
-
Video input tokens processed (camera frames).
no setter
Methods
-
addListener(
VoidCallback listener) → void -
Register a closure to be called when the object changes.
inherited
-
dispose(
) → void -
Discards any resources used by the object.
inherited
-
formatCost(
{bool showKrw = true}) → String - Formatted cost string with optional Korean Won conversion.
-
formatTokens(
) → String -
Formatted token count string (e.g.
1,250 tokens). -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notifyListeners(
) → void -
Call all the registered listeners.
inherited
-
recordMessage(
LiveServerMessage message) → void - Records token usage from a top-level LiveServerMessage.
-
recordUsage(
UsageMetadata usage) → void - Records token usage directly from UsageMetadata.
-
removeListener(
VoidCallback listener) → void -
Remove a previously registered closure from the list of closures that are
notified when the object changes.
inherited
-
reset(
) → void - Resets all accumulated token counters and costs to zero.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited