# External Metering — Model Policy
#
# Same preflight as external-metering.yaml, with model-policy enforcement
# enabled: the entitlement response's `modelAllowed` verdict decides whether
# the tenant may use the requested public model, independently of budget.
#
# Prerequisites (not started by this config): a metering service answering
# the balance check on 127.0.0.1:9090 and an inference backend on
# 127.0.0.1:3000. Without the metering service the preflight rejects with
# 503 — the model gate is always fail-closed, regardless of fail_open.
#
# Usage:
#   cargo run -p praxis-ai-proxy -- -c examples/configs/external-metering-model-policy.yaml
#   curl -X POST http://localhost:8080/v1/chat/completions \
#     -H "Content-Type: application/json" \
#     -H "x-tenant-username: alice" \
#     -d '{"model":"gpt-4","messages":[{"role":"user","content":"hi"}]}'
#
# Entitlement response contract (both verdicts are optional per gate, but a
# missing one is never an implicit allow):
#   {"hasAccess": true|false, "modelAllowed": true|false}
#
# Decision contract with model_policy_check enabled:
#   - modelAllowed: false            -> 403 before any provider request,
#                                       regardless of billing fail_open, and
#                                       taking precedence over budget denial
#   - hasAccess: false               -> 429 token budget exhausted
#   - verdict unavailable (missing,  -> 503 metering system unavailable:
#     null, wrong type, unusable        model authorization is always
#     payload, non-2xx, timeout,        fail-closed, independent of the
#     transport error)                  billing fail_open below; an
#                                       unavailable hasAccess instead follows
#                                       billing fail_open (true: continue,
#                                       false: 503)
#
# Model resolution for the preflight: the trusted {prefix}model identity
# header, the model_to_provider client model, and (when no client model was
# recorded — a recorded rewrite leaves the body naming the provider target)
# the request body's top-level "model" field (bounded pre-read, 10 MiB;
# larger bodies reject with 413) must all agree when present. Reporting
# fallbacks
# (default_username / default_model) are never authorized: when model policy
# is enabled, a request without a trusted tenant identity or a resolvable
# public model is rejected with 403 before dispatch, and so is a request
# whose model sources conflict, including a body "model" member that is
# ambiguous (duplicate key spellings, whether plain or escaped) or
# undecodable (an escaped or non-string value) (403 "conflicting model
# inputs for model authorization") — authorizing one source would not
# authorize the model the provider receives. A single escaped key
# spelling such as "\u006dodel" still names "model" and is a normal
# model source. When identity comes from raw {prefix}* request
# headers, the ingress boundary must strip client-supplied copies and set
# the headers from authenticated data: the filter cannot distinguish
# client-set headers.
#
# Stable rejection bodies: 403 "model not allowed for this user" (explicit
# denial), 429 "token budget exhausted", 503 "metering system unavailable"
# (unavailable verdict under a fail-closed gate), 403 "tenant identity
# required for model authorization" / "request model required for model
# authorization" / "conflicting model inputs for model authorization".

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"
        provider: "openai"
        # Billing gate availability: admit when the metering service is
        # unreachable or returns an unusable budget verdict.
        fail_open: true
        identity_header_prefix: "x-tenant-"
        # Model-policy gate: enforce the modelAllowed verdict. Disabled by
        # default; enabling it requires a trusted tenant identity and a
        # resolvable public model on every metered request. The gate is
        # always fail-closed: an unavailable model verdict rejects with 503
        # regardless of billing fail_open above.
        model_policy_check: true

      - 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
