Guides
Observe and meter token usage
Turn provider-reported token usage into metrics, label per-tenant counters with trusted identity, and report usage to an external metering service.
On this page
Turn provider-reported token usage into metrics, label per-tenant counters with
trusted identity, and report usage to an external metering service. Usage
extraction only observes; external_metering also rejects requests with 429
when the service reports no remaining balance. To enforce local limits, see
Limit token usage.
Prerequisites and configuration
Follow the guide prerequisites and start from one of these complete configurations:
- Screen requests with guardrails: request screening plus
stream_usage_injectandtoken_countwith OpenAI-format usage metrics. - The token-counting example:
model_to_header,token_usage_headers, andtoken_countwithout guardrails.
Both expose the admin metrics endpoint on 127.0.0.1:9901. Keep it private in
a deployed service.
Verify
After a completed request, fetch the metrics:
curl --fail-with-body http://127.0.0.1:9901/metrics
praxis_ai_prompt_tokens_total and praxis_ai_output_tokens_total appear once
the backend supplies usage and Praxis parses it. For streaming Chat
Completions, stream_usage_inject asks supporting backends for final SSE
usage, and the counts update when it arrives. Missing usage or a truncated
stream leaves the counts incomplete as billing evidence.
Usage extraction
token_count reads JSON or SSE usage in the configured provider dialect. When
you compose translators, set the dialect the filter actually sees. Cache and
reasoning fields may be missing, and translation can drop details; see the
compatibility matrix. Body-derived
counts arrive after the response headers are sent, so reordering filters does
not make token_usage_headers emit them as Praxis-Token-* headers. The
Bedrock InvokeModel header-only path is a separate case.
Per-tenant counters and metering
For bounded per-tenant counters, declare token_usage_metrics
before token_count, give it explicit tenant and model allowlists, and derive
identity from authenticated metadata rather than client headers. Prometheus
labels are neither authorization nor billing-grade settlement.
External metering
adds balance checks and usage reporting to a separate service.