Report model token counts in status --json

What

With the real-model proxy running, status --json --usage reads the proxy's request, input-token and output-token totals, in all and per model, into state as model_usage, stamped with read_at. status --json surfaces it under schema_version 3. It reads null while no proxy runs (the mock model and the direct arms record nothing), and real-model off clears it.

How

  • Endpoint: LiteLLM's own Prometheus /metrics on the loopback listener, pinned image v1.100.0. The proxy runs with no database, so its spend endpoints (/spend/logs, /global/spend/*) have nothing behind them; the config gains the prometheus callback, and /metrics asks for no key.
  • Series read: litellm_requests_metric_total, litellm_input_tokens_metric_total, litellm_output_tokens_metric_total, summed by the model label.
  • Pulled on demand by status --usage; no daemon.
  • The proxy answers /metrics with 401 unless require_auth_for_metrics_endpoint: false, so gce/litellm/config.yaml sets it. The listener binds 127.0.0.1; the alternative is handing the reader the master key. /metrics then redirects (307) to /metrics/, which usage.sh follows, and it exits non-zero on a body with no litellm_ series so a bad answer never reads as zero.
  • Redaction: those series carry key hashes and aliases, so gce/litellm/usage.sh runs on the VM and keeps three counts per model. Only model names and numbers cross ssh. No master key or virtual key is read, and none reaches state or output. status --json cuts model_usage to a fixed key list.
  • Word budget stays at its ceiling: the paragraph added to gce/operations.md is paid for by trims in the same file.

Test

The engine self-test feeds a stub /metrics body carrying a planted key hash, alias and prompt-like label, and asserts the totals land in state, the planted values appear nowhere in output or state, the mock model (no proxy) records none, and a failed read keeps the last reading.

Live run

One GCE VM, real-model proxy on Vertex (gemini-2.5-flash), model calls through the lab. status --json --usage | jq '.model_usage' returned {"read_at":"2026-11-17T06:35:20Z","source":"litellm /metrics","requests":12,"input_tokens":96,"output_tokens":147,"models":{"gemini-2.5-flash":{...}}}. The state file held no key or hash. The VM is deleted.

Closes #24 (closed)

Edited by Andrew Dunn

Merge request reports

Loading
Loading