You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "AlinsRan (via GitHub)" <gi...@apache.org> on 2026/06/08 23:30:56 UTC
[PR] feat(prometheus): add built-in LLM histograms for TTFT and token distribution [apisix]
AlinsRan opened a new pull request, #13487:
URL: https://github.com/apache/apisix/pull/13487
### Description
This PR adds three built-in Prometheus histogram metrics for the AI Gateway, complementing the existing LLM metrics:
- **`apisix_llm_ttft`** — LLM time to first token (milliseconds), observed for streaming (`ai_stream`) requests only. The existing `apisix_llm_latency` mixes streaming TTFT and non-streaming total latency in one series; this dedicated metric keeps the TTFT distribution semantically consistent so it can be used for streaming latency SLOs.
- **`apisix_llm_prompt_tokens_dist`** / **`apisix_llm_completion_tokens_dist`** — histograms of prompt/completion tokens per request. The existing `apisix_llm_prompt_tokens` / `apisix_llm_completion_tokens` are counters (totals only); these histograms add a distribution so quantiles such as p95 prompt size can be computed.
Buckets are configurable via `plugin_attr.prometheus`:
- `llm_ttft_buckets` (unit: millisecond, defaults to the standard latency buckets)
- `llm_prompt_tokens_buckets` / `llm_completion_tokens_buckets` (unit: token)
Token histogram default buckets are tuned to real-world token ranges with the upper bound at 1M to cover large-context models. The OTel GenAI semantic conventions define the instrument type and unit for these but do not prescribe bucket boundaries, so the defaults are chosen empirically and remain overridable.
The existing counters, gauge, and `apisix_llm_latency` are left unchanged for backward compatibility.
#### Which issue(s) this PR fixes:
Fixes #
### Checklist
- [x] I have explained the need for this PR and the problem it solves
- [x] I have explained the changes or the new features added to this PR
- [x] I have added tests corresponding to this change
- [x] I have updated the documentation to reflect this change
- [x] I have verified that this change is backward compatible
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(prometheus): add built-in LLM histograms for TTFT and token distribution [apisix]
Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan merged PR #13487:
URL: https://github.com/apache/apisix/pull/13487
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(prometheus): add built-in LLM histograms for TTFT and token distribution [apisix]
Posted by "nic-6443 (via GitHub)" <gi...@apache.org>.
nic-6443 commented on code in PR #13487:
URL: https://github.com/apache/apisix/pull/13487#discussion_r3378681434
##########
apisix/plugins/prometheus/exporter.lua:
##########
@@ -366,16 +416,36 @@ function _M.http_log(conf, ctx)
gen_arr(route_id, service_id, consumer_name, balancer_ip,
vars.request_type, vars.request_llm_model, vars.llm_model,
unpack(extra_labels("llm_latency", ctx))))
+
+ -- Only streaming requests expose a real TTFT; for non-streaming the
+ -- var holds the total response time, which would pollute the TTFT
+ -- distribution, so record llm_ttft for ai_stream only.
+ if vars.request_type == "ai_stream" then
+ metrics.llm_ttft:observe(tonumber(llm_time_to_first_token),
+ gen_arr(route_id, service_id, consumer_name, balancer_ip,
+ vars.request_type, vars.request_llm_model, vars.llm_model,
+ unpack(extra_labels("llm_ttft", ctx))))
+ end
Review Comment:
> `apisix_llm_ttft` — LLM time to first token (milliseconds), observed for streaming (ai_stream) requests only. The existing apisix_llm_latency mixes streaming TTFT and non-streaming total latency in one series; this dedicated metric keeps the TTFT distribution semantically consistent so it can be used for streaming latency SLOs.
Then we should also adjust the existing `apisix_llm_latency`. In stream request, set it to the time when the entire response is completed. Otherwise, `apisix_llm_ttft` and `apisix_llm_latency` will have overlapping functions.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(prometheus): add built-in LLM histograms for TTFT and token distribution [apisix]
Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on code in PR #13487:
URL: https://github.com/apache/apisix/pull/13487#discussion_r3385965442
##########
apisix/plugins/prometheus/exporter.lua:
##########
@@ -366,16 +416,36 @@ function _M.http_log(conf, ctx)
gen_arr(route_id, service_id, consumer_name, balancer_ip,
vars.request_type, vars.request_llm_model, vars.llm_model,
unpack(extra_labels("llm_latency", ctx))))
+
+ -- Only streaming requests expose a real TTFT; for non-streaming the
+ -- var holds the total response time, which would pollute the TTFT
+ -- distribution, so record llm_ttft for ai_stream only.
+ if vars.request_type == "ai_stream" then
+ metrics.llm_ttft:observe(tonumber(llm_time_to_first_token),
+ gen_arr(route_id, service_id, consumer_name, balancer_ip,
+ vars.request_type, vars.request_llm_model, vars.llm_model,
+ unpack(extra_labels("llm_ttft", ctx))))
+ end
Review Comment:
`apisix_llm_latency`:
- `type="total"` — full response latency (`ai_chat` and `ai_stream`). For streaming this now uses `apisix_upstream_response_time` (time until the whole response completes) instead of the TTFT.
- `type="ttft"` — time to first token (`ai_stream` only; non-streaming responses have no first-token moment).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org