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?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
--data-binary @- <<EOF
{
"id": "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