ENยทESยทDEยทPTยทFR
โŒ˜K

Token Usage & Cost Tracking

Track token consumption per model and session. Set daily budgets to control spending. Check stats from Telegram with /cost.

How It Works

  1. Every LLM call records input and output tokens to the token_usage SQLite table
  2. Each record includes: model name, session ID, token counts, and timestamp
  3. Optionally, set a daily token budget that blocks LLM calls once exceeded
  4. Budget resets automatically at midnight UTC

Configuration

KeyTypeDefaultDescription
daily_token_budgetintegernullMaximum total tokens (input + output) per day. Null = unlimited.
config.toml
[state]
daily_token_budget = 1000000  # 1M tokens per day
Budget Scope
The daily budget is global โ€” it counts all tokens across all sessions and models. When exceeded, LLM calls return an error until midnight UTC.

Telegram /cost Command

Send /cost in Telegram to view usage statistics:

text
Token usage (last 24h):
  Input:  12,450 tokens
  Output: 8,230 tokens

Token usage (last 7d):
  Input:  87,320 tokens
  Output: 52,180 tokens

Top models (7d):
  gemini-3-flash-preview: 98,400 tokens
  gemini-3-pro-preview: 41,100 tokens

Database Schema

KeyTypeDefaultDescription
idINTEGER PKautoAuto-incrementing primary key
session_idTEXTโ€”Which user/chat session made the call
modelTEXTโ€”Which LLM model was used
input_tokensINTEGERโ€”Tokens sent to the model
output_tokensINTEGERโ€”Tokens generated by the model
created_atTEXTnowUTC timestamp of the call

What’s Tracked

  • Input tokens (context + user message) per LLM call
  • Output tokens (model response) per LLM call
  • Model name for per-model breakdowns
  • Session ID for per-user tracking
  • Timestamp for time-windowed queries (24h, 7d)