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
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
-
Install Docker.
-
Install cURL to send requests to the services for validation.
-
Have a running API7 Gateway instance.
-
Create a token from the Dashboard 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. Usedefaultif 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, then save its ID to an environment variable:
export SERVICE_ID=your-service-id # replace with your service ID
Built-in AI Logging
ai-proxy and ai-proxy-multi support a logging configuration with:
logging.summaries(boolean): logsrequest_model,model,duration,prompt_tokens,completion_tokens, andupstream_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.
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