fix(web): filter AI usage-limit 402 errors out of Sentry - #5058
Open
eshurakov wants to merge 1 commit into
Open
Conversation
The AI gateway returns 402 (usage_limit_exceeded) when the caller's own credit balance is exhausted, and the AI SDK surfaces that as AI_APICallError with statusCode 402 - e.g. Kilo Bot calling the gateway on behalf of a user who is out of credits in bot-session-callback processing. That is expected per-user billing state, not an application bug, so reporting it to Sentry is noise. Drop these events in beforeSend alongside the existing tRPC 4xx filter. Upstream provider 402s (our provider account) are unaffected: the gateway converts them to a 503 and reports them itself via captureProxyError. Co-authored-by: kiloconnect[bot] <240665456+kiloconnect[bot]@users.noreply.github.com>
Contributor
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Executive SummaryReviewed the Sentry Files Reviewed (2 files)
Reviewed by kimi-k3 · Input: 62.1K · Output: 13.5K · Cached: 633.9K Review guidance: REVIEW.md from base branch |
Contributor
|
Would it be better to fix the source? |
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.
Summary
Filters
AI_APICallErrorwithstatusCode: 402out of Sentry inbeforeSend, alongside the existing tRPC 4xx filter.Why
The AI gateway returns 402 (
usage_limit_exceeded— "Add credits to continue, or switch to a free model",llm-proxy-helpers.ts) when the caller's own credit balance is exhausted. Server-side callers acting on behalf of a user — e.g. Kilo Bot processing/api/internal/bot-session-callback— surface this as an unhandledAI_APICallError(statusCode 402), whichcaptureExceptionthen reports. Each event just means "a user ran out of credits mid-workflow": expected per-user billing state, not an application bug, and it scales with free/low-credit usage.This is safe to filter globally because upstream provider 402s (our provider account) never reach AI SDK callers: the gateway converts them to a 503 and reports them itself via
captureProxyError({ trackInSentry: true })inapp/api/openrouter/[...path]/route.ts, so that failure mode stays visible.Changes
apps/web/sentry.server.config.ts: add exportedisAIUsageLimitErrorpredicate (duck-typed like the existingisTRPC4xxError, so theaipackage stays out of the Sentry init bundle) and apply it inbeforeSend.apps/web/src/sentry.server.config.test.ts: tests for the predicate, including one built from the realAPICallErrorclass to pin the duck-type contract.Verification
jest src/sentry.server.config.test.ts: 24/24 pass (run with a scratch config identical tojest.config.tsminus the DB-backed global setup — this sandbox has no Docker/Postgres; the standard config's setup requires it. The suite itself has no DB dependency, so CI should behave the same).oxlinton the changed test file: clean.oxfmtapplied to both files.tsgotypecheck of both changed files: clean. Fullapps/webtypecheck not run (its@kilocode/trpcbuild prerequisite exceeded the sandbox time limit).