Repository navigation
docs(ai-rate-limiting): give cost_expr examples for each API format - #14097
Open
0xLittleDream wants to merge 1 commit into
Open
0xLittleDream wants to merge 1 commit into
0xLittleDream wants to merge 1 commit into
Conversation
The cost_expr example, input_tokens + cache_creation_input_tokens + output_tokens, uses Anthropic Messages usage field names. On a route whose clients use the OpenAI Chat Completions format, the raw usage object has prompt_tokens and completion_tokens instead, so every variable in the example resolves to 0, the cost is 0, and the request is not counted. State that the expression must use the field names of the clients' API format, that a zero result is not counted, and add a Chat Completions example next to the Anthropic one, in the schema description and in the English and Chinese plugin docs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The
cost_exprexample given in theai-rate-limitingschema description and in the plugin documentation is:These are Anthropic Messages usage field names. With
limit_strategyset toexpression,cost_expris evaluated against the raw usage object of the client's API format. On a route whose clients use the OpenAI Chat Completions format, that object hasprompt_tokens,completion_tokensandtotal_tokens(ai-protocols/openai-chat.lua,extract_usage). None of the names in the example exist there, so each resolves to 0 ineval_cost_expr(), the cost is 0, and_M.logreturns at the info-leveltoken usage is 0, skip rate limitingline without counting the request. An operator who copies the example onto a Chat Completions route therefore has a token limit that counts nothing, with no error or warning at the default log level.The behaviour itself is intended and tested (missing variables default to 0 in
t/plugin/ai-rate-limiting-expression.tTEST 10, 11, 25 and 26; a zero cost leaves the quota unchanged in TEST 13). This PR changes documentation only:prompt_tokens + completion_tokens;input_tokensandoutput_tokens.The same change is made in the schema description (
apisix/plugins/ai-rate-limiting.lua) and in the English and Chinese plugin docs. No behaviour changes.This was first reported privately to security@apache.org, which assessed it as documentation hardening under §4.3 of the project's security threat model and suggested a public pull request.
A separate, optional follow-up could log at warn level when none of the variables named in
cost_exprare present in the usage object, in the same way the plugin already logs at error level when usage is missing altogether. I have left that out of this PR so that it stays documentation-only. I am glad to open it separately if the maintainers would like it.Verification
I loaded the edited
ai-rate-limiting.luain a Lua 5.4 interpreter, stubbing onlyapisix.coreandapisix.plugins.limit-count.init, and called_M.logwith the usage objects fromt/fixtures/openai/chat-basic.jsonandt/fixtures/anthropic/messages-with-cache.json:token usage is 0, skip rate limiting)prompt_tokens + completion_tokens(new Chat Completions example)Which issue(s) this PR fixes:
No issue was opened; see the description above.
Checklist
Credit
Reported by Abhinav Kumar (EmberTail), a project that tests spending and usage limits in AI tooling.
The analysis and this write-up were prepared with AI assistance; I ran the verification above and reviewed the change before submitting.
🤖 Generated with Claude Code