Repository navigation
feat(proxy): let an app set its own surcharge on customer requests - #52492
Conversation
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>
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>
Let administrators choose Teams, Projects, or Virtual Keys as the gateway-wide app scope and assign customer budgets from existing budget groups. Includes the shared budget-group integration from PR #52411 and builds on the existing app accounting implementation.
Use the existing dashboard controls and compact budget rows. Add app filtering to customer usage and retain trusted app context for budget rejections. Update request-header handling, tracing registry coverage, bounded app discovery, and optional caller-metadata tests.
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>
…itellm_app_customer_surcharge
…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>
…mber Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…ng sits beside 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>
…lback at each app surcharge Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
@greptileai please verify the Responses failure-metadata fix at 74a2b94. The callback-level regression checks the persisted spend breakdown with gateway fallback, a 5% app rate, and a 0% override. All other CI passes; the same five native-limiter cases remain as verified on parent #52477. |
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 74a2b94. Configure here.
…omer_provisioning Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…tomer_surcharge Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Use shared customer budget controls for app forms and resolve member budgets at one owner. Remove unused app lifecycle and listing routes. Parse request identity once, check each global and app scope once, and keep app counters alive through the existing database flush window.
Persist the original trusted customer and app billing context on managed jobs. Restore it in batch and background cost polling with the original request time, preserving admission windows and existing claim behavior. Recognize the India Bedrock model prefix in dashboard model setup.
…omer_provisioning Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…tomer_surcharge Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Set the required migration lock timeout and add the billing-context column only if it is absent. Use per-request completion test callbacks and await the MCP panel failure notification. Keep response, pricing, and complete callback-sequence assertions.
Use the upstream country-prefix handling to clear the model-setup merge conflict.
…' into litellm_app_customer_provisioning
…g' into litellm_app_customer_surcharge
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…tomer_surcharge Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…ne helper Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…tomer_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>
Merging this PR will not alter performance1 warning
Comparing Footnotes |
Handwritten 1-3 sentence description
let admin set per-app surcharge on 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
Builds on #52477 (the app model), #52406 (the gateway-wide customer surcharge) and #52485 (customer creation rules), which are all on
main. The change that belongs to this PR is the per-app surcharge, incustomer_cost_margin.py,app_auth.py, the app types and store, one migration and the app settings form.TLDR
Problem this solves:
How it solves it:
customer_margin, a fraction from 0 to 100turns the surcharge off for that appUser Flow
Before: an admin with a 2% gateway surcharge wants one app to charge its customers 5%, and cannot
{"customer_margin": 0.05}and the field is rejected as unknowncoworkerapp sends POST https://litellm-domain/v1/chat/completions withx-litellm-customer-org-alias: qa-surcharge-mixedx-litellm-response-costis the model cost times 1.02, the gateway rateAfter: that app's customer requests are billed at its own rate
coworker, enters 5 in "Surcharge on customer requests (%)" and saves, or sends the same PATCH and gets 200coworkerapp sends the same POST https://litellm-domain/v1/chat/completions withx-litellm-customer-org-alias: qa-surcharge-mixedx-litellm-response-costis the model cost times 1.05, andx-litellm-response-cost-originalis the model costsearchapp, which has no surcharge of its own, is still billed at 1.02coworkerrequest with no customer header is billed at the model costPre-Submission checklist
Screenshots / Proof of Fix
Real Postgres, one dedicated Redis on port 56379 for router and coordination state, 2 proxy workers on each proxy (two server PIDs each, both served requests), and real
anthropic/claude-haiku-4-5calls. The app definition is Teams, with teamscoworkerandsearchconfigured as apps, one key with no team, and a gateway customer surcharge of 2%.Env var names:
DATABASE_URL,LITELLM_MASTER_KEY,ANTHROPIC_API_KEY.config.yamlis a copy oflitellm/proxy/dev_config.yamlwithrouter_settings.redis_host/redis_portandgeneral_settings.coordination_redis.host/portset to127.0.0.1and56379.Before (378e4cb, #52485 merged with #52406)
customer_marginis rejected as an unknown fieldcoworkerrequest for a customer is billed at the gateway rate, 1.02 times the model costAfter (ed8098f, zero case at aec7713, key-bound case at fbde10b)
coworkerset to 5 in the UI. Its customer request returns 200search, with no surcharge of its own, and the key with no team are both billed at 1.02 for a customer requestcoworkerwith no customer headers is billed at 1.00, with nocustomer_marginfields in the spend logsearchset to 0 in the UI is billed at 1.00 with nocustomer_marginfields, while the key with no team stays at 1.02coworkercleared in the UI goes back to 1.02. A value of 2.5 bills at 1.025coworkerapp at 7.5% it is billed at the app's rate, with the app at 0 it is billed at 1.00 with nocustomer_marginfields, and with the app cleared it is billed at the gateway 1.02. The app's spend rises by the billed cost, from 0.08610576 to 0.08681526. A bound key with no team, or on a team that is not an app, is billed at 1.02PATCHwithcustomer_margin: 11returns 422. In the UI, 1001 and -1 show "Enter a surcharge between 0 and 1000 percent, or leave it empty." and send nothingTests
Type
🆕 New Feature
Caveats (if any)
Severe
ADD COLUMN IF NOT EXISTS, no row rewriteMedium
Low
litellm_metadata, or with no logging snapshot, are covered by unit tests onlyFinal Attestation
Note
High Risk
Touches billing, budget reservation, spend aggregation, and schema migrations—incorrect margin logic could mischarge customers or under-reserve budgets.
Overview
Adds a customer surcharge on requests billed to a customer organization: configurable gateway-wide via
cost_margin_config["customer"], with an optional per-appcustomer_marginthat replaces (not stacks on) the gateway rate—0disables surcharge for that app, unset falls back to the gateway.The surcharge flows through cost finalization, success/failure spend callbacks, and budget reservations, and is recorded in
cost_breakdownplus a newcustomer_margin_spendmetric rolled into daily spend tables, activity APIs, and customer usage UI (base cost vs surcharge).App auth now resolves apps for customer-bound keys and stamps
app_customer_marginon the hierarchy. Migrations addcustomer_marginon apps andcustomer_margin_spendon daily spend tables; the dashboard exposes app surcharge settings and gateway “customer requests” margin configuration.Reviewed by Cursor Bugbot for commit 74a2b94. Bugbot is set up for automated code reviews on this repo. Configure here.