Accepted Proposal #00210ai
Response-Based Token Counting from Provider JSON
A filter that extracts token usage from AI provider response bodies and writes the counts to filter metadata for downstream consumers.
What?
A filter that extracts token usage from AI provider response bodies and writes the counts to filter metadata for downstream consumers.
The filter reads the upstream response body, identifies the provider,
and delegates JSON parsing to private provider-specific parsers.
Once token counts are resolved, they are written to FilterContext
(#212) so that downstream filters (rate limiting, logging, cost
tracking, header injection) can consume them without coupling to
provider-specific formats.
For streaming responses, the filter accumulates SSE chunks until the
stream completes, then parses the final usage payload from the
terminal chunk or [DONE] sentinel (#211).
Provider extraction sources:
| Provider | Source | Field path |
|---|---|---|
| OpenAI | JSON body | usage.prompt_tokens, usage.completion_tokens |
| Anthropic | JSON body | usage.input_tokens, usage.output_tokens |
| Google (Gemini) | JSON body | usageMetadata.promptTokenCount, usageMetadata.candidatesTokenCount |
| Bedrock (Converse) | JSON body | usage.inputTokens, usage.outputTokens |
| Bedrock (InvokeModel) | HTTP response headers | x-amzn-bedrock-input-token-count, x-amzn-bedrock-output-token-count |
| Azure | JSON body | Same as OpenAI |
Note: Bedrock InvokeModel is the only provider that does not return token counts in the response body. Counts are delivered as HTTP response headers instead, making its extraction path fundamentally different from all other providers. This distinction is reflected in the filter’s private provider strategy and the How? design here.
Goals
- Extract token usage from non-streaming provider responses
- Extract token usage from streaming (SSE) provider responses
- Write
token_input,token_output, andtoken_totaltoFilterContext - Keep all provider-specific parsing private to the token usage filter subsystem
- Avoid CPU-bound client-side estimation when provider counts are available
Non-Goals
- Client-side token estimation (tiktoken-style pre-request counting)
- Token-based rate limiting (separate concern, reads from
FilterContext) - Injecting token headers into downstream responses (#214)
- Provider response translation or normalization beyond token fields
Why?
Motivation
AI providers return token usage as the authoritative, zero-CPU-cost source of truth — either in response bodies (OpenAI, Anthropic, Google, Bedrock Converse) or in HTTP response headers (Bedrock InvokeModel). Without a filter that extracts and centralises these counts, every downstream system (rate limiter, logger, cost tracker) must independently implement provider-specific parsing and SSE accumulation logic.
This filter is the entry point of the Token Counting epic (#20).
It reads provider responses once and makes counts available to all
downstream filters via FilterContext, keeping provider-specific
logic in one place.
Provider-returned counts are preferred over client-side estimation because they are accurate, require no tokenizer dependencies, and add no CPU overhead to the request path.
User Stories
As a rate limiting filter, I need token counts written to
FilterContextso that I can enforce per-user token budgets without parsing AI response bodies myself.As a cost tracking system, I need accurate input and output token counts per request so that I can attribute spend to the correct account without re-implementing provider JSON schemas.
As a logging filter, I need token usage available in
FilterContextat response time so that I can emit structured usage records for every AI request.As a platform operator, I need a single filter to handle all supported providers so that adding a new AI backend does not require updating multiple downstream filters.
Open Questions
Provider identification
The filter needs to know which provider schema to use when parsing a response. Should provider identity be resolved from the upstream route configuration, a request header set by a preceding filter, or auto-detected from the response body shape? Auto-detection avoids configuration overhead but may be ambiguous for providers with overlapping schemas (e.g., Azure mirrors OpenAI).
Streaming completion signal
For SSE streams, the filter must detect when the stream has ended
to trigger final parsing. Should it rely solely on the [DONE]
sentinel, on stream close, or on both? Providers do not uniformly
emit [DONE] (Google Gemini omits it), so stream close may need
to be the authoritative signal.
Streaming token accumulation strategy per provider
Providers differ in how and when token counts appear across SSE events:
- OpenAI: both
prompt_tokensandcompletion_tokensarrive together in the final chunk, just before the[DONE]sentinel. - Anthropic: counts are split across two distinct event types —
input_tokensappears in themessage_startevent at the beginning of the stream, whileoutput_tokensappears in themessage_deltaevent near the end. The filter must track both events independently and holdinput_tokensin state until the stream completes. - Google (Gemini): usage appears in
usageMetadataon the final chunk; no[DONE]sentinel is emitted, so stream close is the trigger.
This means a single “read the last chunk” strategy is insufficient. The How? design must specify a per-provider accumulation model, or a general event-tagging mechanism that each provider’s parser populates.
Bedrock InvokeModel extraction path
Bedrock InvokeModel returns token counts as HTTP response headers
(x-amzn-bedrock-input-token-count, x-amzn-bedrock-output-token-count),
not in the JSON body. Should this filter handle header-based extraction
directly, or should it be scoped out as a separate extraction path with
a dedicated design in the How? section? If included, the filter must
inspect response headers before body parsing, and #216 must be updated
to reflect this.
Partial usage data
Some providers include incremental usage fields in intermediate SSE chunks as well as the final chunk. Should the filter accumulate and sum these, or only use the final chunk’s usage payload? Summing intermediate chunks could double-count if the final chunk already contains the total.
How?
Requirements
- A
token_countfilter infilters/src/token_usage/count.rs. - The filter accepts a single required YAML key
providerthat selects the extraction strategy. - For non-streaming responses the full body is buffered and parsed once at end-of-stream.
- For SSE streaming responses all chunks are buffered and the assembled event stream is scanned for token fields once the stream closes.
- For Bedrock InvokeModel, token counts are read from HTTP response
headers in
on_response; no body parsing is performed. - Counts are written to filter metadata via the subsystem’s private
set_token_usagehelper so that downstream filters (header injection, logging, rate limiting) can read them without parsing provider JSON themselves. - The filter delegates all provider-specific JSON and SSE parsing to private
modules within
filters/src/token_usage/.
Answering the Open Questions
Provider identification
Provider identity is supplied explicitly via the provider: YAML key.
Auto-detection is not implemented. This keeps the filter stateless and
unambiguous — Azure and OpenAI share the same JSON schema, so
auto-detection would be unreliable for those two.
Supported values: openai, anthropic, google, bedrock,
bedrock_invoke_model, azure.
Streaming completion signal
The filter uses BodyMode::StreamBuffer — the proxy buffers all response
body bytes and calls on_response_body once with end_of_stream: true.
No per-chunk inspection is needed. Stream close (connection end) is the
authoritative trigger, which correctly handles both providers that emit
[DONE] (OpenAI) and providers that do not (Google Gemini).
Streaming token accumulation strategy per provider
| Provider | Strategy |
|---|---|
| OpenAI / Azure / Bedrock Converse | Scan all data: lines; return the last one that parses successfully — usage appears once in the terminal chunk |
| Google (Gemini) | Same as above — usage is in usageMetadata on the final chunk; no [DONE] sentinel |
| Anthropic | Two-pass scan: collect input_tokens from the message_start event, collect output_tokens from the message_delta event; combine at end |
| Bedrock InvokeModel | Header-only; no SSE parsing |
Bedrock InvokeModel extraction path
Handled directly by this filter in on_response. When provider: bedrock_invoke_model is configured, the filter reads
x-amzn-bedrock-input-token-count and x-amzn-bedrock-output-token-count
from the upstream response headers and stores normalized token metadata there.
response_body_access returns BodyAccess::None for this provider so no
body buffering occurs.
Partial usage data
Only the final assembled payload is parsed. Intermediate SSE chunks that contain partial usage fields are ignored. This avoids double-counting for providers (e.g., Anthropic) that report counts in multiple events — each relevant event is read exactly once, not summed.
File Layout
filters/src/token_usage/
mod.rs # normalized usage type and metadata contract
count.rs # TokenCountFilter and private provider strategy
count/tests.rs # filter-boundary tests
headers.rs # TokenUsageHeadersFilter
providers.rs # provider JSON parsers
streaming.rs # provider SSE event parsers
Filter Struct and Config
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct TokenCountConfig {
provider: ProviderKind,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
enum ProviderKind {
OpenAi,
Anthropic,
Google,
Bedrock,
BedrockInvokeModel,
Azure,
}
pub struct TokenCountFilter {
provider: ProviderKind,
}
HttpFilter Trait Implementation
| Hook | Behaviour |
|---|---|
on_request | No-op; returns Continue |
on_response | Detects text/event-stream content type and stores an is_sse flag in FilterContext metadata. For bedrock_invoke_model, reads token headers and stores normalized usage metadata |
response_body_access | BodyAccess::None for bedrock_invoke_model; BodyAccess::ReadOnly for all others |
response_body_mode | BodyMode::Stream for bedrock_invoke_model (no buffering needed); BodyMode::StreamBuffer { max_bytes: Some(8 MiB) } for all others |
on_response_body | Processes streaming SSE frames incrementally or assembles bounded JSON; writes normalized usage metadata at completion |
SSE Extraction Detail
extract_from_sse(provider, data)
├── Anthropic → extract_anthropic_sse (two-event scan)
└── all others → extract_last_usage_from_sse (scan data: lines, keep last valid)
extract_anthropic_sse scans the full buffered text, accumulates
input_tokens from message_start and output_tokens from
message_delta, then constructs a TokenUsage only when both are present.
FilterContext Metadata
Token counts are stored under three keys accessible to downstream filters:
| Key | Value |
|---|---|
token.input | Input/prompt token count as u64 |
token.output | Output/completion token count as u64 |
token.total | Sum (or provider-supplied total) as u64 |
Written via the token usage subsystem’s private metadata helper.
Module Registration
Add to filter/src/builtins/http/ai/mod.rs:
#[cfg(feature = "ai-inference")]
mod token_count;
#[cfg(feature = "ai-inference")]
pub use token_count::TokenCountFilter;
Add to filter/src/builtins/http/mod.rs:
#[cfg(feature = "ai-inference")]
pub use ai::TokenCountFilter;
Add to filter/src/builtins/mod.rs:
#[cfg(feature = "ai-inference")]
pub use http::TokenCountFilter;
Register in filter/src/registry.rs:
registry.register_http("token_count", TokenCountFilter::from_config);
YAML Configuration Example
listeners:
- name: gateway
address: "127.0.0.1:8080"
filter_chains:
- token-counting
filter_chains:
- name: token-counting
filters:
- filter: token_count
provider: openai # openai | anthropic | google | bedrock | bedrock_invoke_model | azure
- filter: access_log
- filter: router
routes:
- path_prefix: "/"
cluster: ai-provider
- filter: load_balancer
clusters:
- name: ai-provider
endpoints:
- "127.0.0.1:8000"