How It Works
Quick Start
1
Basic Usage
Enable usage tracking with the global collector:
2
Query Usage Data
Access tracked usage data for analytics:
Usage Sink Protocols
TokenUsageSinkProtocol
Interface for persisting token usage data to any backend:Built-in Sinks
Usage Query Protocols
UsageQueryProtocol
Interface for reading usage data from your backend:Built-in Query Adapters
InMemoryUsageQuery
InMemoryUsageQuery
Query adapter for
InMemoryTokenUsageSink:TokenCollectorUsageQuery
TokenCollectorUsageQuery
Query adapter for global
TokenCollector:Integration with Host
Wire usage tracking into PraisonAIUI backends:Custom Sink Example
total_tokens on the metrics object already equals input_tokens + output_tokens, so custom sinks can store it directly without re-deriving it.
Complete example with PostgreSQL backend:
Best Practices
Use NoOp sink in production by default
Use NoOp sink in production by default
The default
NoOpTokenUsageSink has zero overhead. Only enable tracking when needed:Batch writes for high throughput
Batch writes for high throughput
Buffer writes to reduce database load:
Include custom metadata
Include custom metadata
Add business context to usage records:
Usage Metrics Format
Token metrics objects contain these fields:cached_tokens is a subset of input_tokens, not a separate bucket — it counts prompt tokens the provider served from cache. The same holds for reasoning_tokens and audio tokens, which are subsets of the totals the provider already reports. See How total_tokens is computed for the full explanation.
The invariant total_tokens == input_tokens + output_tokens holds in every nested block below:
Related
Host Integration
Auto-wire usage tracking
Backend Injection
Custom backend services

