External Metering

Pre-request balance check and post-response token usage reporting against an external metering service

Category: Setup-dependent integration
Task: Pre-request balance check and post-response token usage reporting against an external metering service

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

Run it: Use ghcr.io/praxis-proxy/ai:0.4.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.

# External Metering
#
# Pre-request balance check and post-response token usage reporting
# against an external metering service. Identity headers are captured
# from configurable tenant headers (default prefix: x-tenant-) and
# stripped before forwarding upstream.
#
listeners:
  - name: default
    address: "127.0.0.1:8080"
    filter_chains:
      - main

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

      - filter: external_metering
        metering_url: "http://127.0.0.1:9090"
        # The metering service in this example listens on loopback, a
        # non-public address, so callouts must be explicitly opted in.
        allow_private_endpoint: true
        timeout_seconds: 5
        feature_key: "inference-tokens"
        source: "ai-gateway"
        fail_open: true
        identity_header_prefix: "x-tenant-"
        # Namespace the identity_header_guard filter writes captured headers
        # under; must match that filter's metadata_namespace when both run.
        # identity_metadata_namespace: "identity"
        # Optional fallbacks for deployments where an upstream authentication
        # layer does not inject identity headers. Without default_username,
        # requests carrying no identity header are not metered at all.
        # default_username: "anonymous"
        # default_model: "unknown"

      - filter: token_count
        provider: openai

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

insecure_options:
  allow_private_endpoints: true # example proxies to local backends