Skip to content

feat(ai-proxy): add latency and usage in access log and prometheus metrics#12518

Merged
Revolyssup merged 11 commits into
apache:masterfrom
Revolyssup:revolyssup/token-usage
Aug 19, 2025
Merged

feat(ai-proxy): add latency and usage in access log and prometheus metrics#12518
Revolyssup merged 11 commits into
apache:masterfrom
Revolyssup:revolyssup/token-usage

Conversation

@Revolyssup

@Revolyssup Revolyssup commented Aug 13, 2025

Copy link
Copy Markdown
Contributor

Description

This PR adds two things:

  1. It adds latency and token info for ai-proxy plugin in the access log for easy debugging.
  2. It adds prometheus metrics for ai related requests and adds two more labels request_type to distinguish between normal requests and ai related requests & llm_model

Which issue(s) this PR fixes:

Fixes #

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

@dosubot dosubot Bot added the size:XL This PR changes 500-999 lines, ignoring generated files. label Aug 13, 2025
@Revolyssup
Revolyssup marked this pull request as draft August 13, 2025 09:19
@dosubot dosubot Bot added the enhancement New feature or request label Aug 13, 2025
@Revolyssup
Revolyssup marked this pull request as ready for review August 14, 2025 07:06
"override": {
"endpoint": "http://localhost:16724"
}
"auth": {"header": {"Authorization": "Bearer token"}},

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is to avoid the error coming from luajit that string too long.

@kayx23
kayx23 self-requested a review August 14, 2025 09:21

@kayx23 kayx23 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For ai-proxy and ai-proxy-mutli examples of using these variable in access log, you can directly revert the doc changes in #12502 (Might need to remove request_llm_model)

Prometheus doc changes LGTM.

| consumer | Name of the Consumer associated with a request. Default to an empty string if no Consumer is associated with the request. |
| node | IP address of the upstream node. |
| request_type | traditional_http / ai_chat / ai_stream |
| llm_model | For non-traditional_http requests, name of the llm_model |

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the underscore?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The _ because this will be shown like this in the prometheus metrics.

Comment thread apisix/plugins/ai-proxy-multi.lua
Comment thread conf/config.yaml.example
enable_access_log: true # Enable HTTP proxy access logging.
access_log: logs/access.log # Location of the access log.
access_log_buffer: 16384 # buffer size of access log.
# available variables:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the variables available to be logged should not be documented here i guess, as there are several more variables that can be added to the logs.

membphis
membphis previously approved these changes Aug 18, 2025

@membphis membphis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread apisix/plugins/ai-request-rewrite.lua
Comment thread apisix/cli/ngx_tpl.lua

set $request_type 'traditional_http';

set $llm_time_to_first_token '';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in exporter.lua you have named the metric as llm_latency. It's better to have the name consistent across the codebase and docs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think its okay to keep this variable more explicit here. and lkeep it llm_latency in prometheus metric.

@dosubot dosubot Bot added size:XXL This PR changes 1000+ lines, ignoring generated files. and removed size:XL This PR changes 500-999 lines, ignoring generated files. labels Aug 18, 2025

:::note

The usage in this example will become available in APISIX 3.13.0.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this note pls

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread docs/en/latest/plugins/ai-proxy.md Outdated
:::note

The usage will become available in APISIX 3.13.0.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@kayx23 kayx23 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc LGTM

@Revolyssup
Revolyssup merged commit cd32958 into apache:master Aug 19, 2025
32 checks passed
@Revolyssup
Revolyssup deleted the revolyssup/token-usage branch August 19, 2025 14:07
jizhuozhi pushed a commit to jizhuozhi/apisix that referenced this pull request Oct 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request size:XXL This PR changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants