Skip to content

Conversation

@qiqizjl
Copy link

@qiqizjl qiqizjl commented Dec 25, 2025

Summary

Fix incorrect metrics reporting when LLM plugin is not enabled.

Problem

When the LLM plugin is not enabled, the nginx variables for LLM metrics were defaulting to '0', which caused the metrics to be incorrectly reported as active even when no LLM functionality was being used.

Solution

Changed the default values from '0' to '' (empty string) for the following nginx variables:

  • llm_time_to_first_token
  • llm_prompt_tokens
  • llm_completion_tokens

Also updated the condition check in openai-base.lua to properly detect when LLM functionality is not active.

Changes

  • apisix/cli/ngx_tpl.lua: Changed default values from '0' to ''
  • apisix/plugins/ai-drivers/openai-base.lua: Updated condition check

🤖 Generated with Claude Code

Fix incorrect metrics reporting when LLM plugin is not enabled.
Change default values from '0' to '' for LLM-related nginx variables
(llm_time_to_first_token, llm_prompt_tokens, llm_completion_tokens)
to properly detect when LLM functionality is not active.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@dosubot dosubot bot added size:XS This PR changes 0-9 lines, ignoring generated files. bug Something isn't working labels Dec 25, 2025
@qiqizjl qiqizjl changed the title bugfix: fix metrics reporting when LLM plugin is not enabled fix: metrics reporting when LLM plugin is not enabled Dec 25, 2025
@Baoyuantop
Copy link
Contributor

Hi @qiqizjl, we made this change in #12549. Could you explain the original requirements of this PR? Changing the default value from '0' to '' may affect users' existing JSON log format.

@qiqizjl
Copy link
Author

qiqizjl commented Dec 25, 2025

Hi @qiqizjl, we made this change in #12549. Could you explain the original requirements of this PR? Changing the default value from '0' to '' may affect users' existing JSON log format.

We found in apisix/plugins/prometheus/exporter.lua#L376-L394 that metrics are reported when the value is not empty. When the LLM plugin is not enabled, the default value is 0, which results in invalid metrics.

@qiqizjl
Copy link
Author

qiqizjl commented Dec 25, 2025

Another possible solution is to check if request_type is ai_stream or ai_chat, and report accordingly. I tried changing the check to report if ctx.var.llm_time_to_first_token != 0, but this might affect abnormal requests. If possible, I can continue to refine this issue based on the discussion.

@flearc
Copy link
Contributor

flearc commented Dec 26, 2025

Another possible solution is to check if request_type is ai_stream or ai_chat, and report accordingly. I tried changing the check to report if ctx.var.llm_time_to_first_token != 0, but this might affect abnormal requests. If possible, I can continue to refine this issue based on the discussion.

    if llm_time_to_first_token ~= "0" then
        metrics.llm_latency: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_latency", ctx))))
    end
    if vars.llm_prompt_tokens ~= "0" then
        metrics.llm_prompt_tokens:inc(tonumber(vars.llm_prompt_tokens),
            gen_arr(route_id, service_id, consumer_name, balancer_ip,
            vars.request_type, vars.request_llm_model, vars.llm_model,
            unpack(extra_labels("llm_prompt_tokens", ctx))))
    end
    if vars.llm_completion_tokens ~= "0" then
        metrics.llm_completion_tokens:inc(tonumber(vars.llm_completion_tokens),
            gen_arr(route_id, service_id, consumer_name, balancer_ip,
            vars.request_type, vars.request_llm_model, vars.llm_model,
            unpack(extra_labels("llm_completion_tokens", ctx))))
    end

Adjust the if statement might help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants