Skip to content

[ISSUE #8386] Report unavailable AI token usage outcomes instead of silently omitting accounting - #8539

Open
konlue wants to merge 2 commits into
apache:masterfrom
konlue:fix/8386-token-limiter-unknown-usage
Open

konlue wants to merge 2 commits into
apache:masterfrom
konlue:fix/8386-token-limiter-unknown-usage

Conversation

@konlue

@konlue konlue commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Fixes #8386.

The token limiter's response observer accounted usage only when extraction succeeded: an oversized payload, malformed JSON, a missing usage object or a cancelled observation all ended in OptionalLong.empty() and the accounting consumer was never invoked — a silent omission indistinguishable from "nothing to account". This PR makes every response yield exactly one explicit outcome, reusing the shared AiUsage contract from shenyu-common (#7955) rather than a second ledger: the extractor's bounded parser keeps its 64 KiB budget and now records a fixed reason for every unavailable outcome — LIMIT_EXCEEDED when the nonstreaming buffer, an SSE event or the gzip inflation budget is exhausted, INVALID for malformed JSON, non-object usage and non-integral or negative token counts, and UNKNOWN for responses that report no usage at all or that end before any usage arrives (cancellations included). A known zero stays a reported zero.

What changed

ResponseUsageExtractor carries a failure reason and exposes finishOutcome() returning the AiUsage record; finish() remains as a thin reported/empty view. The decorator's accounting seam is one Consumer<AiUsage> invoked exactly once per response from the existing doFinally hook, so there is no duplicate debit and no extra body subscription, and forwarded bytes are untouched. The plugin's consumer debits the existing Redis counter through the unchanged Lua script only for REPORTED counts; every other outcome produces a single bounded WARN diagnostic naming the status only — no response content, no credentials, no high-cardinality caller labels. Reservation-enabled consumers must not settle or release reserved consumption on an unavailable outcome (never an assumed zero); that reconciliation stays with the reservation tasks, and the seam's javadoc documents it.

Tests

AiTokenLimiterPluginTest drives the decorator/accounting boundary, not just the parser: below/at/above-bound JSON fixtures separate REPORTED from an explicit LIMIT_EXCEEDED while the bound and the forwarded bytes stay intact; an oversized SSE event and gzip budget exhaustion carry the same size-limit reason; missing usage reports UNKNOWN (previously silent), malformed counts report INVALID, and a cancelled observation with no captured usage reports UNKNOWN while usage captured before cancellation stays recorded; a consumer unit test verifies only reported counts debit the counter. All outcomes are red against the previous silent-omit behavior, and the existing gzip-boundary, chunking, zero-vs-unknown and cancellation coverage is preserved on the new outcome vocabulary. Module results on the current master baseline: shenyu-plugin-ai-token-limiter 66 tests (was 44) — all green; checkstyle clean.

…d of silently omitting accounting

The response usage observer now emits exactly one AiUsage outcome per response through the accounting consumer: a reported completion count (a known zero stays reported), or an explicitly unavailable usage with the shared apache#7955 vocabulary — LIMIT_EXCEEDED for the 64 KiB buffer, SSE-event and gzip budgets, INVALID for malformed JSON and non-integral or negative counts, UNKNOWN for missing usage and observations that end before any usage arrives. Only reported counts debit the existing Redis counter exactly once per request; every other outcome produces a single bounded WARN diagnostic naming the status only, with no response content, credentials or caller labels. No extra body subscription is introduced, forwarded bytes stay untouched, and reservation-enabled consumers are documented never to settle or release reserved consumption on an unavailable outcome. Tests pin each outcome through the response decorator/accounting boundary with below/at/above-bound JSON fixtures, oversized SSE events, missing usage, malformed counts, cancellation and gzip exhaustion, red against the previous silent omission.
No caller remains after the outcome-typed finishOutcome() replaced the reported/empty view; keeping the one-shot delegate would let a stale caller consume the single finish and turn the response's real outcome into UNKNOWN.

@Aias00 Aias00 left a comment

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.

Stream Agent analysis

Reviewed the complete captured diff for the AI token-limiter accounting change. The extractor now emits exactly one explicit AiUsage outcome per response (REPORTED / UNKNOWN / INVALID / LIMIT_EXCEEDED) via finishOutcome(), and the decorator's accounting consumer debits Redis only for REPORTED counts, replacing the previous silent OptionalLong.empty() omission.

Evidence: ["Complete diff (complete_diff=true) reviewed across all four changed files: AiTokenLimiterPlugin.java plus the three test suites; no diff pages unread.","Accounting seam verified: finishResponse calls usageRecorder.accept(extractor.finishOutcome()) once; applyUsageOutcome debits the unchanged Redis Lua counter only for REPORTED with non-null tokens and emits one bounded status-only WARN otherwise; forwarded body bytes asserted unchanged.","CI metadata: 35 checks, passed=true, diagnosis CI_PASSED, no failed checks. No local tests executed. Supplementary resource_read/list/search for non-diff source failed (FULL_DIFF attachment, no base/head source IDs); not needed for coverage."]

Next steps: []

Verdict: PASS; publication: APPROVE (EXPLICIT_PASS)

Findings: []

Scope: captured GitHub evidence only; no local tests were executed.

Task: 94524c64-d1ed-4894-9218-f98dbe175bd2 Commit: 262e0f3badf79bd36e04753a779deef4fbca9b71

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enhancement][AI Token Limiter] Report oversized-response usage as unknown instead of silently omitting accounting

2 participants