Skip to main content
Version: Dev

Token Counting and Usage Estimation

AISIX records token counts reported by the upstream provider. When a supported endpoint omits a count, the gateway can estimate it locally so telemetry, spend, budgets, and token rate limits still reflect the request.

Estimation covers text and selected image inputs, but it does not make all provider counters comparable. Usage Reporting explains how estimated and provider-reported counts appear in organization-wide Dashboard totals.

When AISIX Estimates Tokens​

AISIX estimates missing token fields on Chat Completions, Completions, Messages, Responses, and Embeddings traffic. Common causes include an OpenAI-compatible relay that reports no usage, a caller disconnecting mid-stream, or an upstream error after a partial response.

Estimation follows these rules:

  • A count reported by the upstream is retained, including 0.
  • A missing field is estimated independently. AISIX does not replace the other fields the upstream reported.
  • Input tokens are estimated from the request sent upstream.
  • Output tokens are estimated from content delivered to the caller.
  • Endpoint families outside the supported set are not estimated.

Local estimates are close for OpenAI-family models and approximate for models with proprietary tokenizers.

The usage event sets usage_estimated, and Request Logs labels the row estimated. Spend and usage totals include estimated usage. Budgets and token rate limits also count it by default, although an organization can exclude estimated usage from those controls.

Estimate Non-Text Prompt Content​

These rules apply only to locally estimated counts. A provider-reported count always takes precedence.

Prompt ContentLocal Estimate
OpenAI-compatible image with low, auto, or no detail setting85 tokens. This includes images in tool output or results.
OpenAI-compatible inline image with another detail settingAISIX fits the image within 2000 pixels on the long side and 768 pixels on the short side, then counts 85 tokens plus 170 per 512-pixel tile. If AISIX cannot read the dimensions, it uses 300 by 300 pixels.
OpenAI-compatible remote image with another detail setting250 tokens. AISIX does not fetch the image to read its dimensions.
Anthropic-format image block85 tokens. The format carries no detail setting, so the estimate can be lower than Anthropic's count for a large image.
File, PDF, audio, encrypted reasoning content, thinking signature, or similar opaque payloadNot counted. An Anthropic document with a text or content source counts that text.
Base64 data: URI in a text fieldCounted as text. AISIX treats the URI as media only in an image, file, or audio field.

Estimates Returned to Callers​

AISIX adds estimated counters to response bodies it constructs so the caller and control-plane event report the same numbers.

EndpointWhere the Caller Reads the Estimate
/v1/chat/completionsThe response usage, including semantic-cache hits. A stream receives the estimate in the usage-only chunk before [DONE] only when the caller requested stream_options.include_usage: true.
/v1/responses, native or bridgedThe response usage, or the usage of the terminal streaming event such as response.completed.
/v1/completionsThe response usage.
/v1/embeddingsusage.prompt_tokens and usage.total_tokens when the upstream reported neither.
/v1/messages, bridgedThe response usage, or the closing stream message_delta.
/v1/messages, native AnthropicThe response usage on a non-streaming reply. Native streams relay the upstream's usage events and do not expose a local estimate.

The fill is per counter. When AISIX fills a count, it recomputes total_tokens as input plus output tokens; Anthropic-format usage carries no total. A response with no usage receives a usage object.

The caller-facing response does not indicate whether AISIX estimated a count. Use usage_estimated in the usage event or the estimated badge in Request Logs. A route that relays provider bytes verbatim, such as a passthrough route, does not modify the response body.

Compatibility with Earlier Gateways​

Gateways earlier than vNEXT use the behavior documented for AISIX 1.11:

  • A count reported as 0 is treated as missing and can be estimated.
  • The usage event retains the upstream's total_tokens even when AISIX estimates a component count, so the reported total might not equal the recorded components.
  • Native Responses, Completions, Embeddings, and non-streaming Messages responses return the upstream's usage rather than the local estimate. Bridged Responses and Messages streams add the estimate to the terminal event only when the upstream sent no usage at all.

Provider-Reported Counts​

AISIX stores upstream counters as reported rather than forcing relationships between them. Providers differ in whether completion_tokens includes reasoning and whether prompt_tokens includes cached input. A record can therefore show reasoning above completion or cached input above prompt.

The event also carries the upstream's total_tokens when present. AISIX does not recompute that reported value, and providers such as Anthropic may omit it. The total can differ from the sum of other counters because providers include different token classes.

An event with a locally estimated count does not retain the upstream total, because that total omits the estimate. The control plane instead prices the recorded component counters. Recorded Token Counts defines the complete event-field behavior.