Examples

Token Usage Metrics

Opt-in, cardinality-bounded per-tenant token counters

Praxis AI v0.12.1 (alpha) 2 min read

Category: Setup-dependent integration
Task: Opt-in, cardinality-bounded per-tenant token counters

Prerequisites: The external service, credentials, or certificates referenced by this configuration.

Run it: Use ghcr.io/praxis-proxy/ai:0.12.1 and follow the container quickstart to mount and start the configuration.

This configuration comes from the selected release. The example has not been run here; external services are not bundled.

Download the source file.

Companion resources from the same snapshot:

# Token Usage Prometheus Metrics
#
# Opt-in, cardinality-bounded per-tenant token counters.
#
# Emits praxis_ai_tokens_total{tenant, model, kind} after token_count
# extracts usage. A production authentication filter must populate the exact
# unnamespaced x-tenant-username metadata key from verified credentials.
# identity_header_guard only strips/captures client headers; its namespaced
# metadata is deliberately not trusted by token_usage_metrics.
#
# Both tenant and model labels use explicit allowlists. Authenticated values
# outside an allowlist map to the fixed `other` buckets.
#
listeners:
  - name: default
    address: "127.0.0.1:8080"
    filter_chains:
      - main

filter_chains:
  - name: main
    filters:
      - filter: router
        routes:
          - path_prefix: "/"
            cluster: backend

      # Defense in depth: strip raw identity headers before proxying. The
      # resulting namespaced metadata is unverified and is not used by metrics.
      - filter: identity_header_guard
        prefix: "x-tenant-"
        metadata_namespace: "identity"

      - filter: model_to_header

      - filter: token_usage_metrics
        tenant_metadata_key: "x-tenant-username"
        allowed_tenants:
          - token-metrics-example-alice
        allowed_models:
          - gpt-4o
        overflow_tenant: other
        overflow_model: other

      - filter: token_count
        provider: openai

      - filter: load_balancer
        clusters:
          - name: backend
            endpoints:
              - "127.0.0.1:3000"

admin:
  metrics_address: "127.0.0.1:9901"

insecure_options:
  allow_private_endpoints: true # example proxies to local backends