# Token Counting and Usage Estimation

AISIX records nonzero token counts reported by the upstream provider. When a supported endpoint omits a count or reports `0`, 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](https://docs.api7.ai/ai-gateway/cloud/usage-reporting.md) explains how estimated and provider-reported counts appear in organization-wide Dashboard totals.

## When AISIX Estimates Tokens[​](#when-aisix-estimates-tokens "Direct link to When AISIX Estimates Tokens")

AISIX estimates missing or zero 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 nonzero count reported by the upstream is retained.
* A missing or zero field is estimated independently. AISIX does not replace the other nonzero 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](https://docs.api7.ai/ai-gateway/traffic-controls/budgets.md#leave-estimated-usage-out-of-budgets).

## Estimate Non-Text Prompt Content[​](#estimate-non-text-prompt-content "Direct link to Estimate Non-Text Prompt Content")

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

| Prompt Content                                                                               | Local Estimate                                                                                                                                                                                                |
| -------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| OpenAI-compatible image with `low`, `auto`, or no detail setting                             | 85 tokens. This includes images in tool output or results.                                                                                                                                                    |
| OpenAI-compatible inline image with another detail setting                                   | AISIX 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 setting                                   | 250 tokens. AISIX does not fetch the image to read its dimensions.                                                                                                                                            |
| Anthropic-format `image` block                                                               | 85 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 payload | Not counted. An Anthropic document with a `text` or `content` source counts that text.                                                                                                                        |
| Base64 `data:` URI in a text field                                                           | Counted as text. AISIX treats the URI as media only in an image, file, or audio field.                                                                                                                        |

## Estimates Returned to Callers[​](#estimates-returned-to-callers "Direct link to Estimates Returned to Callers")

AISIX adds estimated counters to response bodies it constructs:

| Endpoint                          | Where the Caller Reads the Estimate                                                                  |
| --------------------------------- | ---------------------------------------------------------------------------------------------------- |
| `/v1/chat/completions`, buffered  | The response `usage`, including semantic-cache hits.                                                 |
| `/v1/chat/completions`, streaming | The usage-only chunk before `[DONE]` when the caller requested `stream_options.include_usage: true`. |
| `/v1/responses`, bridged          | The response `usage`, or the `usage` of the terminal streaming event.                                |
| `/v1/messages`, bridged           | The response `usage`, or the closing stream `message_delta`.                                         |

The fill is per counter. A nonzero count reported by the upstream stands; a missing or zero count is filled. When AISIX fills input or output, it recomputes `total_tokens` as their sum.

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. Native provider responses and routes that relay provider bytes verbatim keep the provider's response body unchanged.

## Provider-Reported Counts[​](#provider-reported-counts "Direct link to 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.

[Recorded Token Counts](https://docs.api7.ai/ai-gateway/observability/metrics-and-usage-events.md#recorded-token-counts) defines the complete event-field behavior.
