Token Usage & Analytics¶
The Token Usage & Analytics system tracks LLM consumption across Swisper. Every LLM call — whether from an interactive conversation or a background job — is recorded with token counts broken down by category (structured output, streaming, embedding) and by graph node. Data is accumulated in Redis during a request's lifecycle, then persisted to PostgreSQL on completion. An admin-only analytics API provides per-user summaries, aggregated node-type breakdowns, and paginated usage reports.
Key Components¶
| Component | Purpose |
|---|---|
| Token Usage Tracking | Per-request accumulator: initialize_tracking() → record_llm_usage() per call → persist_to_postgres() on completion |
| Two-tier storage | Redis hash during request lifecycle (TTL 1 hour), PostgreSQL for permanent storage |
| TokenUsage model | Stores interactive usage per request: total/prompt/completion tokens, node breakdown (JSONB) |
| BackgroundJobTokenUsage model | Stores background job usage per run: total tokens, LLM type breakdown (JSONB) |
| Analytics API | Admin-only endpoints for per-user summaries, node-type aggregations, and LLM-type aggregations |
Documentation Sections¶
- Overview — What this module does, who it serves, and how tracking works
- Architecture — System design, data model, two-tier storage, and analytics API