Repository navigation
feat(proxy): add a surcharge on customer requests - #52406
Conversation
Co-Authored-By: Ishaan Jaffer <155045088+ishaan-berri@users.noreply.github.com>
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
|
Merging this PR will not alter performance1 warning
Comparing |
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
Co-Authored-By: Ishaan Jaffer <155045088+ishaan-berri@users.noreply.github.com>
Co-Authored-By: Ishaan Jaffer <155045088+ishaan-berri@users.noreply.github.com>
Co-Authored-By: Ishaan Jaffer <155045088+ishaan-berri@users.noreply.github.com>
Co-Authored-By: Ishaan Jaffer <155045088+ishaan-berri@users.noreply.github.com>
… requests A cost_margin_config entry keyed customer adds a percentage to requests made for a customer organization. The surcharge is part of spend, so customer budgets count it, and each request records it in cost_breakdown and in a customer_margin_spend column on the daily spend tables so cost before the surcharge stays exact Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…t for customer requests Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…s binding Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…o fields for a zero surcharge Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…ucket of any route Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
@greptileai please review the current fixes at c1f4aef, including billed-failure surcharge and nonzero rollup coverage. Build and test checks now pass. |
|
bugbot run Please review the current surcharge changes at c1f4aef for correctness and regressions, including customer attribution and budget accounting. Build and test checks now pass. |
…rd the surcharge without a snapshot Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
@greptileai please verify the failure-path fixes at 4542eb6: trusted key binding from either metadata bucket and surcharge breakdown on rows without a logging snapshot. Regression tests were added and build/test CI passes. Please reassess the remaining failure-path thread. |
|
bugbot run Please verify both billed-failure fixes at 4542eb6, including key-bound customer attribution and surcharge breakdown without a logging snapshot. Regression tests were added and build/test CI passes. |
…ide litellm_params Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
… through the failure callback Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
@greptileai please verify the Responses failure-metadata fix and callback-level regression at 54e12b5. Build/test CI passes; the test exercises the real failure callback and checks the spend-row surcharge. |
|
bugbot run Please verify the Responses failure-metadata fix and callback-level regression at 54e12b5. Build/test CI passes; the test exercises the real failure callback and checks the spend-row surcharge. |
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 54e12b5. Configure here.
…ges (#53940) Brings nine tests in line with behavior that intentionally changed: - SSO client removal: /sso/key/generate without SSO now redirects to /ui/login (#53205) - budget partial update: /budget/info returns the app relation keys (#52477, #52787, #53232) - performance alerts opt-out: /alerts/monitoring returns thresholds (#52139) - cost margin rejection: the error names 'customer' as a valid key (#52406) - regional two-image generation: Gemini gets n single-image requests (#52103) - customer app budget key rotation: the rotated key's cleanup tolerates /key/regenerate - DeepSeek 401: the upstream error type is relayed (#53217) - vLLM rejected predicate: the plain upstream message is relayed (#53217). The case still fails on the non-OpenAI error type that #51809 fixes - /callbacks/list e2e: the Rust spend logger is listed as spend_log (#52182)
Handwritten 1-3 sentence description
add per-app and global surcharge option for customer portal
Details
Link to Devin session: https://app.devin.ai/sessions/df57dd6feb764cd684428371c02d408b
Open in Devin Desktop: https://app.devin.ai/desktop/session/df57dd6feb764cd684428371c02d408b?variant=devin
Requested by: @moe-berri
TLDR
Problem this solves:
How it solves it:
customerentry incost_margin_config, e.g.0.02for 2%litellm_metadataormetadata, as on successcustomer_margin_percentandcustomer_margin_amountcustomer_margin_spendcolumncost_margin_percentfrom the earlier revision is removedUser Flow
Before: an admin who bills customers 2% on top of model cost has to compute it outside LiteLLM
{"customer": 0.02}and gets 400Invalid provider(s): customerx-litellm-customer-org-alias: acme-bankx-litellm-response-costequals the model cost, and the customer organization's spend shows only the model costAfter: the customer is billed with the surcharge, and internal traffic is not
{"customer": 0.02}, or picks Customer requests on the Cost Tracking page, and gets 200x-litellm-customer-org-alias: acme-bankx-litellm-response-costis the model cost times 1.02, andx-litellm-response-cost-originalis the model costCustomer usage page at /ui/customers/usage?org=. The first shot is a date range with no surcharged spend, which renders the same as before this PR. The second is the range with customer requests, where Base cost and Surcharge appear under Spend.
Pre-Submission checklist
Screenshots / Proof of Fix
Real Postgres, one dedicated Redis on port 56379 for router and coordination state, 2 proxy workers (two
Started server processPIDs in the log), and realanthropic/claude-haiku-4-5calls.Env var names:
DATABASE_URL,LITELLM_MASTER_KEY,ANTHROPIC_API_KEY.config.yamlis a copy oflitellm/proxy/dev_config.yamlwith these added.Shared setup, run against each proxy.
Before (b81933c)
x-litellm-customer-org-alias: acme-bankreturns 200After (6c4b67d)
x-litellm-customer-member-id: alice, one hasx-litellm-customer-org-alias: acme-bank"total_spend": 0.00011322and"total_customer_margin_spend": 2.22e-06UI, driven on the dashboard dev server against the same proxy. On the Cost Tracking page, Customer requests is saved at 2.0%, a fixed amount for it is refused, and its inline editor only takes a percentage. After eight more real requests for
acme-bank, the usage page shows Spend $0.34, Base cost $0.331 and Surcharge $0.0066, matching the database (spend 0.33762612, surcharge 0.00662012). The Logs page showscustomer_margin_amount: 0.00080092andcustomer_margin_percent: 0.02on a customer request.Tests
The five lint gates and
check_migrations_no_data_rewrites.pypass locally.Type
🆕 New Feature
Caveats (if any)
Severe
ADD COLUMN IF NOT EXISTS ... NOT NULL DEFAULT 0.0, no row rewriteMedium
Low
Final Attestation
Note
High Risk
Changes payment and budget paths (finalization, reservations, failure billing) plus a schema migration across all daily spend tables; mis-attribution or double-surcharging would directly affect customer charges.
Overview
Introduces a
customerentry incost_margin_configthat applies a percentage surcharge only on customer-attributed requests (customer org headers, key binding, or usage metadata), leaving internal traffic at base cost.The surcharge is woven through billing and enforcement: cost finalization and success callbacks adjust
response_costandcost_breakdown; budget reservations scale estimates the same way; billed failures get the surcharge too, including rows without a logging snapshot.customer_margin_spendis rolled up in daily spend tables (new DB column + migration), accounting, daily-activity APIs, and OpenAPI types.The Cost Tracking UI can configure a customer-only percentage margin; customer usage pages show base cost vs surcharge under total spend.
Reviewed by Cursor Bugbot for commit 54e12b5. Bugbot is set up for automated code reviews on this repo. Configure here.