Skip to main content
Track token usage across agents with pluggable persistence backends for databases, APIs, or custom analytics systems.
The user runs agents normally; each completion flows through TokenCollector into the configured usage sink for analytics.

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

Query adapter for InMemoryTokenUsageSink:
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

The default NoOpTokenUsageSink has zero overhead. Only enable tracking when needed:
Buffer writes to reduce database load:
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:

Host Integration

Auto-wire usage tracking

Backend Injection

Custom backend services