Repository navigation
fix(proxy): attribute retry attempt spend rows to their deployment and keep them out of daily request counts - #47658
Conversation
…t and keep them out of daily request counts Each <request_id>-attempt-<n> row now carries the model, provider, api_base and model group of the deployment that billed it (looked up by model_info.id through the proxy router, injected as deployment_lookup) instead of copying the final row's, and is stamped internal_call_origin=billed_retry_attempt so the daily spend tables keep its spend and tokens while counting zero api_requests and failed_requests for it
…t file's untouched lines as they were
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
@litellm-stampbot |
litellm-stampbotTaking a look at this PR now. I'll update this comment with the review result |
|
bugbot run |
Merging this PR will not alter performance
|
…ternal_call_origin so model usage keeps their spend Bugbot: an internal_call_origin stamp made build_model_usage_transaction drop the attempt rows from LiteLLM_DailyModelUsage. The rows now carry their own SpendLogsMetadata flag, which only the daily request counters read
|
|
|
@BugBot run |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
… so the basedpyright gate holds
PR overviewAll previously flagged issues have been addressed. No open security concerns remain on this pull request. Security reviewNo open security issues remain on this pull request. Fixed/addressed: 1 · PR risk: 0/10 |
…er so the basedpyright gate holds
…n _deployment_provider
…d strip the flag from caller metadata Bugbot: _record_request_error counted every attempt row as a caller failure in LiteLLM_DailyRequestErrors. Veria: a caller could send metadata.billed_retry_attempt to zero its own request counts, so the key joins the untrusted metadata fields the proxy strips on the way in
|
@BugBot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 560f673. Configure here.
…tadata so the basedpyright gate holds
…deployment_and_counts
…basedpyright gate holds
…d keep them out of daily request counts (#47658) * fix(proxy): attribute retry attempt spend rows to their own deployment and keep them out of daily request counts Each <request_id>-attempt-<n> row now carries the model, provider, api_base and model group of the deployment that billed it (looked up by model_info.id through the proxy router, injected as deployment_lookup) instead of copying the final row's, and is stamped internal_call_origin=billed_retry_attempt so the daily spend tables keep its spend and tokens while counting zero api_requests and failed_requests for it * fix(proxy): declare DeploymentLookup as a TypeAlias and leave the test file's untouched lines as they were * fix(proxy): mark attempt rows with billed_retry_attempt instead of internal_call_origin so model usage keeps their spend Bugbot: an internal_call_origin stamp made build_model_usage_transaction drop the attempt rows from LiteLLM_DailyModelUsage. The rows now carry their own SpendLogsMetadata flag, which only the daily request counters read * fix(proxy): read the billed_retry_attempt flag through a typed helper so the basedpyright gate holds * fix(proxy): keep the typed litellm_params local in _deployment_provider so the basedpyright gate holds * fix(proxy): annotate the validated litellm_params as LiteLLM_Params in _deployment_provider * fix(proxy): keep billed retry attempts out of daily request errors and strip the flag from caller metadata Bugbot: _record_request_error counted every attempt row as a caller failure in LiteLLM_DailyRequestErrors. Veria: a caller could send metadata.billed_retry_attempt to zero its own request counts, so the key joins the untrusted metadata fields the proxy strips on the way in * fix(proxy): stamp billed_retry_attempt explicitly in the spend log metadata so the basedpyright gate holds --------- Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com> (cherry picked from commit 5dce13d)
A timed-out request that the router retried writes one spend row per billed attempt since #47416. Those attempt rows copied the final row's model, provider and api_base even when the attempt ran on a different deployment, and every attempt row also counted as one more request in the daily spend tables, so one caller request with two earlier attempts showed up as three failed requests. Each attempt row now names the deployment it actually ran on, and the daily tables keep its spend and tokens while counting only the caller's one request
Details
TLDR
Problem this solves:
How it solves it:
model_info.idthrough the proxy routerapi_baseand model groupbilled_retry_attemptmetadata flag, which the proxy strips from caller metadata on the way inUser Flow
Before: an operator reading spend logs after a retried timeout sees a row that names the wrong deployment, and the daily request count triples
"model": "essay", whereessaytimes out and falls back toessay-fallback(OpenAIgpt-4.1)-attempt-1row that ran on the OpenAI deployment readsanthropic/claude-sonnet-4-6, provideranthropic, api_basehttps://api.anthropic.com/v1/messagesAfter: the same request writes rows that name the deployment each attempt ran on, and the usage page counts one request
"model": "essay"-attempt-1row now readsopenai/gpt-4.1, provideropenai, api_basehttps://api.openai.com/v1, model groupessay-fallbackAffected release
regression since #47416
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or moreScreenshots / Proof of Fix
Live proxy started from the checkout (
python -m litellm.proxy.proxy_cli --config config.yaml --detailed_debug --port 4000) against a real Postgres 14database_url, real Anthropic and OpenAI calls, no mocks. The 1500 word essay prompt against a 1.5 s timeout makes every attempt time out after the provider has accepted the prompt, so each attempt is billed. Two deployments, so the earlier attempts and the fallback run on different providers:The psql columns are
request_id | model | custom_llm_provider | api_base | model_id | model_group | spend | prompt_tokens | status | attempted_retries | internal_call_originforLiteLLM_SpendLogsanddate | model | custom_llm_provider | api_requests | successful_requests | failed_requests | spend | prompt_tokensforLiteLLM_DailyUserSpend. The tables were truncated before each run and the daily rows were read after the 10 s batch write. Before was captured on main at e6065b9, before the attempt-row pricing change that current main carries, so its-attempt-0row bills atgpt-4.1's rate (4.8e-05) while the After row bills at its own model's rate (7.2e-05); that difference comes from main, not this PRBefore (e6065b9, main)
T0=2026-10-20T23:29:43Z, then the requestThe spend rows written for it
The daily spend row(s) after the batch write
The
-attempt-1row ran ondep-openai-gpt-4-1but readsanthropic/claude-sonnet-4-6,anthropic,https://api.anthropic.com/v1/messages, and the daily row counts 3 requests and 3 failed requests for one HTTP callAfter (2ae784d)
T0=2026-10-20T23:30:47Z, then the requestThe spend rows written for it
The daily spend row(s) after the batch write
The
-attempt-1row now readsopenai/gpt-4.1,openai,https://api.openai.com/v1, model groupessay-fallback, both attempt rows carry thebilled_retry_attemptflag, and the daily rows count 1 request and 1 failed request while keeping the spend and tokens of all three rowsTests
tests/unit/proxy/hooks/test_proxy_track_cost_callback.py::test_attempt_rows_are_attributed_to_the_deployment_that_billed_themand::test_attempt_rows_add_spend_but_not_requests_to_daily_spendbuild the spend-log payloads and daily transactions the hook produces for the same two-deployment timeout. Both fail with the fix stashed. Mutations checked: making the deployment lookup return nothing flips the first test ('anthropic/claude-sonnet-4-6' != 'openai/gpt-4.1'), and dropping thebilled_retry_attemptflag flips the second ([1, 1, 1] == [1, 0, 0]). The second test also checks the attempt rows still reachLiteLLM_DailyModelUsage(Bugbot's finding on the first revision, which usedinternal_call_originand lost them there)Type
🐛 Bug Fix
Caveats (if any)
Medium
api_baseis the deployment's configured one, empty when none is configured (the-attempt-0row above), since the URL a failed attempt was sent to is not recordedLow
Final Attestation
Link to Devin session: https://app.devin.ai/sessions/4023355bc34e489da61e54adcd4fb889
Open in Devin Desktop: https://app.devin.ai/desktop/session/4023355bc34e489da61e54adcd4fb889?variant=devin
Requested by: @kerrylu-berri
Note
Medium Risk
Touches proxy spend logging, daily usage rollups, and failure retry accounting; incorrect handling could mis-attribute cost or skew usage metrics, but scope is limited to billed retry attempt rows.
Overview
Fixes a regression where per-attempt spend rows written on failed requests (retries/fallbacks since #47416) inherited the final attempt’s model, provider, and
api_base, and each row inflated daily request counters.Earlier billed attempts now resolve the router deployment by
deployment_idand copy that deployment’s model, provider, configuredapi_base, and model group into the spend log and standard logging payload. Rows are stamped withbilled_retry_attemptmetadata (new constant and logging field).Daily spend aggregation still records spend, tokens, and model usage for those rows but sets
api_requests/ success / failure counts to zero, same idea as internal sub-calls. Request-error rollup treats them as non–caller-request as well.Adds
deployment_params_providerfor consistent provider attribution and unit tests for deployment attribution and daily request counting.Reviewed by Cursor Bugbot for commit 560f673. Bugbot is set up for automated code reviews on this repo. Configure here.