Skip to content

Port upstream 0.65.0: LiteLLM spend-report fallback - #630

Open
Finesssee wants to merge 1 commit into
port/upstream-0.65.0from
port/micro-0.65.0-litellm-spend-fallback
Open

Finesssee wants to merge 1 commit into
port/upstream-0.65.0from
port/micro-0.65.0-litellm-spend-fallback

Conversation

@Finesssee

Copy link
Copy Markdown
Collaborator

Summary

When GET {base}/key/info returns 401, 403 or 404, LiteLLM now falls back to a self-scoped UTC month-to-date spend report instead of failing. It requests GET {base}/key/spend/report?start_date=YYYY-MM-01&end_date=YYYY-MM-DD, and on 401/403/404 again GET {base}/user/spend/report with the same query. No api_key or internal_user_id parameters are sent.

The body must be a non-empty array of rows with a finite total_cost >= 0. Each row is summed once and model_details is ignored. Empty, {}, null, non-JSON, [null], [{}], string, negative, 1e309 and overflowing-sum bodies are parse errors. The result is CostSnapshot USD used = sum, no limit, period Key spend only (<start>–<end> UTC) or User spend only (…) (en dash), plus an informational primary window carrying the same label (UsageSnapshot requires a primary lane; same pattern as ai&). It has no identity, no reset date and no budget percentage. Zero spend is valid.

Transport errors, 429, 5xx and 400 do not switch scope. A 401/403 on the final report maps to AuthRequired, and 404 or any other status maps to an error naming only the status. Report bodies are never echoed, and serde messages are not surfaced. The report body read is bounded to 4 MiB.

"Now" is injected (fetch_key_usage(.., now)) so the date and UTC month-boundary cases are testable.

One incidental fix: management_url used Url::join, which dropped a configured path prefix (https://h/proxy/v1/ produced /key/info instead of /proxy/key/info). It now appends the endpoint to the (/v1-stripped) prefix. That is required for the tagged paths /proxy/key/info, /proxy/key/spend/report and /proxy/user/spend/report.

Upstream reference

  • Release 0.65.0: "LiteLLM: show clearly scoped month-to-date spend when management routes are disabled" (upstream LiteLLM: support spend reports when management routes are disabled steipete/CodexBar#3834, edb71a2c1 / fix(litellm): fall back to self-scoped spend reports steipete/CodexBar#3853).
  • Tag-pinned (v0.65.0): Sources/CodexBarCore/Resources/Plugins/litellm.ts (request(..., allowUnavailable), keyRoot === undefined branch), Sources/CodexBarCore/Providers/LiteLLM/LiteLLMProviderDescriptor.swift (costPresenter), docs/litellm.md ("Spend-only deployments"), Tests/CodexBarTests/LiteLLMSpendReportTests.swift.
  • Tests port the upstream cases with synthetic fixtures (rows summing to 3.75, base https://proxy.example.com/proxy/v1/, 2026-09-01 to 2026-09-21, UTC boundary at 2027-01-01T00:00:01Z), using mockito (already a dev-dependency).

Ported / Deferred

Ported: everything in the scope above.

Not part of this item (predates 0.65.0): the tagged plugin's user/info and team/info follow-ups, user_id/team_id validation, the https-or-private-http endpoint policy, and preserving a ?query suffix on the base URL (the local code drops it, as before).

No UI code was changed. The cost has no limit and the label lives in the cost period and the informational primary window, following the existing ai& pattern. I did not add a card-render test.

Validation

Run on the pinned toolchain (cargo +1.98.0) with a process-local CARGO_TARGET_DIR:

  • cargo +1.98.0 fmt --all -- --check: clean
  • cargo +1.98.0 clippy --workspace --all-targets -- -D warnings: pass
  • cargo +1.98.0 test -p codexbar litellm: 14 passed, 0 failed (11 new, 3 existing)

Affected areas

  • Provider (rust/src/providers/litellm)
  • Settings, tray, float bar, frontend

UI proof

Not applicable.

@coderabbitai

coderabbitai Bot commented Sep 29, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: cfabdab9-e91a-4979-a1a8-c79250b0cd67

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

1 participant