# Monitor AI Traffic and Track LLM Costs

This guide explains how to observe AI traffic and estimate LLM costs using built-in AI logging, APISIX context variables, and standard observability integrations.

## Overview[​](#overview "Direct link to Overview")

AI observability is different from traditional API observability. For LLM workloads, you need token-level visibility, model attribution, and latency breakdowns such as time to first token.

With API7 AI Gateway, you can collect:

* Request and response model metadata.
* Prompt and completion token counts.
* End-to-end and upstream timing signals.
* Optional payload-level logs for request/response content.

The gateway does not calculate billing directly. Cost tracking is derived by mapping token usage to provider pricing.

## Prerequisites[​](#prerequisites "Direct link to Prerequisites")

* Install [Docker](https://docs.docker.com/get-docker/).

* Install [cURL](https://curl.se/) to send requests to the services for validation.

* Have a running API7 Gateway instance.

* Create a [token from the Dashboard](https://docs.api7.ai/api7-gateway/reference/obtain-dashboard-token.md) and save it to an environment variable:

  ```
  export API_KEY=your-dashboard-token   # replace with your Dashboard token
  ```

* Replace `{gateway_group_id}` with your gateway group ID. Use `default` if you are following the quickstart.

* If you are following the Admin API examples, create or reuse a service in API7 Gateway. If you do not have one yet, follow [Create or Reuse a Service](https://docs.api7.ai/api7-gateway/ai-gateway/get-started.md#create-or-reuse-a-service), then save its ID to an environment variable:

  ```
  export SERVICE_ID=your-service-id         # replace with your service ID
  ```

## Built-in AI Logging[​](#built-in-ai-logging "Direct link to Built-in AI Logging")

`ai-proxy` and `ai-proxy-multi` support a `logging` configuration with:

* `logging.summaries` (boolean): logs `request_model`, `model`, `duration`, `prompt_tokens`, `completion_tokens`, and `upstream_response_time`.
* `logging.payloads` (boolean): logs request messages, stream flag, and response text content.

Enable logging at route scope (or in shared plugin policy where applicable):

* Admin API
* ADC

```
curl -k "https://localhost:7443/apisix/admin/routes/ai-observability?gateway_group_id={gateway_group_id}" -X PUT \

  -H "X-API-KEY: ${API_KEY}" \

  -H "Content-Type: application/json" \

  --data-binary @- <<EOF

  {

    "name": "ai-observability",

    "service_id": "$SERVICE_ID",

    "paths": ["/ai"],

    "plugins": {

      "ai-proxy": {

        "provider": "openai",

        "auth": { "header": { "Authorization": "Bearer $OPENAI_API_KEY" } },

        "options": { "model": "gpt-4o" },

        "logging": {

          "summaries": true,

          "payloads": false

        }

      }

    }

  }

EOF
```

❶ Enable AI logging on the `ai-proxy` plugin.

❷ Log summary-level fields (model + token/timing metadata) for cost and performance analysis.

❸ Keep payload logging disabled by default to reduce sensitive content exposure.

adc.yaml

```
services:

  - name: AI Observability

    routes:

      - uris:

          - /ai

        name: ai-observability

        plugins:

          ai-proxy:

            provider: openai

            auth:

              header:

                Authorization: "Bearer ${OPENAI_API_KEY}"

            options:

              model: gpt-4o

            logging:

              summaries: true

              payloads: false

    labels:

      docs-example: ai-observability
```

❶ Enable AI logging on the `ai-proxy` plugin.

❷ Log summary-level fields (model + token/timing metadata) for cost and performance analysis.

❸ Keep payload logging disabled by default to reduce sensitive content exposure.

Preview the changes owned by this example and confirm that the diff contains no unintended updates or deletions:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=ai-observability
```

Synchronize the reviewed configuration:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=ai-observability
```

For multi-model routes, apply the same `logging` fields in each `ai-proxy-multi` instance:

* Admin API
* ADC

```
curl -k "https://localhost:7443/apisix/admin/routes/ai-observability-multi?gateway_group_id={gateway_group_id}" -X PUT \

  -H "X-API-KEY: ${API_KEY}" \

  -H "Content-Type: application/json" \

  --data-binary @- <<EOF

  {

    "service_id": "$SERVICE_ID",

    "name": "ai-observability-multi",

    "paths": ["/ai-multi"],

    "plugins": {

      "ai-proxy-multi": {

        "instances": [

          {

            "name": "openai-primary",

            "provider": "openai",

            "auth": { "header": { "Authorization": "Bearer $OPENAI_API_KEY" } },

            "options": { "model": "gpt-4o-mini" },

            "logging": {

              "summaries": true,

              "payloads": false

            },

            "weight": 1

          }

        ]

      }

    }

  }

EOF
```

❶ Configure `logging.summaries` and `logging.payloads` per instance on `ai-proxy-multi`.

adc.yaml

```
services:

  - name: AI Observability Multi

    routes:

      - uris:

          - /ai-multi

        name: ai-observability-multi

        plugins:

          ai-proxy-multi:

            instances:

              - name: openai-primary

                provider: openai

                auth:

                  header:

                    Authorization: "Bearer ${OPENAI_API_KEY}"

                options:

                  model: gpt-4o-mini

                logging:

                  summaries: true

                  payloads: false

                weight: 1

    labels:

      docs-example: ai-observability-multi
```

❶ Configure `logging.summaries` and `logging.payloads` per instance on `ai-proxy-multi`.

Preview the changes owned by this example and confirm that the diff contains no unintended updates or deletions:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=ai-observability-multi
```

Synchronize the reviewed configuration:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=ai-observability-multi
```

For the full configuration reference, see [`ai-proxy`](https://docs.api7.ai/hub/ai-proxy.md) and [`ai-proxy-multi`](https://docs.api7.ai/hub/ai-proxy-multi.md).

## APISIX Variables for AI Traffic[​](#apisix-variables-for-ai-traffic "Direct link to APISIX Variables for AI Traffic")

The following APISIX context variables can be used in access logs, external logging plugins, and custom observability pipelines:

| Variable                  | Description                                                          |
| ------------------------- | -------------------------------------------------------------------- |
| `llm_time_to_first_token` | Time to first token (TTFT) for streaming responses, in milliseconds. |
| `llm_prompt_tokens`       | Prompt token count reported by the LLM provider.                     |
| `llm_completion_tokens`   | Completion token count reported by the LLM provider.                 |
| `llm_model`               | Actual model that generated the response.                            |
| `request_llm_model`       | Model requested by the client.                                       |

`llm_response_text` is a field added to structured log records when AI payload logging is enabled, not a built-in access-log variable. Read it from the structured record instead of using `$llm_response_text` in an access-log format.

## Prometheus Integration[​](#prometheus-integration "Direct link to Prometheus Integration")

Use the standard [`prometheus`](https://docs.api7.ai/hub/prometheus.md) plugin to export gateway metrics. For AI traffic, the built-in metrics are:

* `apisix_llm_latency` (histogram) — latency in milliseconds. In API7 Enterprise 3.10.1 and later, `type` distinguishes full response latency (`total`) from streaming time to first token (`ttft`). Version 3.10.0 has no `type` label and records `llm_time_to_first_token`, which is TTFT for streaming traffic.
* `apisix_llm_prompt_tokens` (counter) — cumulative prompt token count reported by the provider.
* `apisix_llm_completion_tokens` (counter) — cumulative completion token count reported by the provider.
* `apisix_llm_active_connections` (gauge) — current active LLM connections.

These families have resource and model labels, including `route`, `service`, `consumer`, `request_llm_model`, and `llm_model`. There is no built-in `provider` label. Group by the requested or selected model, or join provider information from your own inventory. See [Metrics Reference](https://docs.api7.ai/api7-gateway/observability/metrics-reference.md#ai-metrics) for the complete metric families, labels, and release boundaries.

Example PromQL queries:

```
# Current active LLM connections across all routes

sum(apisix_llm_active_connections)
```

```
# Top routes by active LLM connections

topk(10, sum by (route) (apisix_llm_active_connections))
```

```
# Alert condition example: sustained high active connections

avg_over_time((sum(apisix_llm_active_connections))[5m:]) > 200
```

Adjust labels and thresholds to your deployment and traffic profile.

## Logging AI Requests to External Systems[​](#logging-ai-requests-to-external-systems "Direct link to Logging AI Requests to External Systems")

You can forward structured logs with AI metadata to systems such as ELK, Loki, or Splunk by combining APISIX logging plugins with AI context variables.

Example structured log payload:

```
{

  "route_id": "27bcdea2-7586-47ad-9262-a3adf4b6699e",

  "service_id": "d8402098-2f80-4e08-afab-21b9ebc62090",

  "model": "deepseek-chat",

  "request_model": "",

  "prompt_tokens": 11,

  "completion_tokens": 16,

  "duration_ms": 3991,

  "ttft_ms": 2159,

  "upstream_response_time": 1133

}
```

note

* `route_id` and `service_id` are the gateway-assigned UUIDs for the route and service.
* `request_model` is empty when the client does not specify a model explicitly; the model is then determined by the plugin configuration.
* `duration_ms` is not a built-in AI log field — use `$latency` (total request time) and `$upstream_latency` (upstream response time) in your log format configuration to measure request duration.

Use summary logging for default production telemetry. Enable payload logging only when you need short-term debugging with appropriate data handling controls.

## Building a Cost Dashboard[​](#building-a-cost-dashboard "Direct link to Building a Cost Dashboard")

API7 Gateway exposes token usage signals, while cost is calculated externally:

`estimated_cost = (prompt_tokens × prompt_price_per_token) + (completion_tokens × completion_price_per_token)`

Typical dashboard panels in Grafana:

* Cost by consumer (hourly/daily).
* Cost by model.
* Prompt vs. completion token split.
* Spend trend and budget threshold alerts.

Implementation approach:

1. Export AI logs/metrics.
2. Enrich records with provider pricing metadata.
3. Compute estimated cost in your log or metrics pipeline.
4. Visualize and alert in Grafana.

For token governance controls, see [Token-Based Rate Limiting and Quota Management](https://docs.api7.ai/api7-gateway/ai-gateway/use-cases/token-rate-limiting-and-quota-management.md).

## OpenTelemetry Tracing[​](#opentelemetry-tracing "Direct link to OpenTelemetry Tracing")

Use the standard [`opentelemetry`](https://docs.api7.ai/hub/opentelemetry.md) plugin to trace AI request lifecycle latency across:

* Client request ingress.
* Gateway processing.
* Upstream LLM call.
* Response egress.

This helps isolate whether latency is caused by client-side behavior, gateway plugins, or upstream model/provider response time.

## Next Steps[​](#next-steps "Direct link to Next Steps")

* [Token-Based Rate Limiting and Quota Management](https://docs.api7.ai/api7-gateway/ai-gateway/use-cases/token-rate-limiting-and-quota-management.md) — Enforce token budgets per route, consumer, and model.
* [Multi-LLM Routing and Fallback](https://docs.api7.ai/api7-gateway/ai-gateway/use-cases/multi-llm-routing-and-fallback.md) — Improve reliability and optimize provider/model usage.
* For plugin details, see [`ai-proxy`](https://docs.api7.ai/hub/ai-proxy.md), [`ai-proxy-multi`](https://docs.api7.ai/hub/ai-proxy-multi.md), [`prometheus`](https://docs.api7.ai/hub/prometheus.md), and [`opentelemetry`](https://docs.api7.ai/hub/opentelemetry.md).
