Skip to content

feat(proxy): let an app set its own surcharge on customer requests - #52492

Merged
devin-ai-integration[bot] merged 68 commits into
mainfrom
litellm_app_customer_surcharge
Nov 18, 2026
Merged

devin-ai-integration[bot] merged 68 commits into
mainfrom
litellm_app_customer_surcharge

Conversation

@moe-berri

@moe-berri moe-berri commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

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, in customer_cost_margin.py, app_auth.py, the app types and store, one migration and the app settings form.

TLDR

Problem this solves:

  • The customer surcharge is one gateway-wide percentage
  • An app cannot charge its customers a different margin

How it solves it:

  • Each app gets an optional customer_margin, a fraction from 0 to 10
  • It replaces the gateway surcharge for that app's customer requests
  • 0 turns the surcharge off for that app
  • Not set means the gateway surcharge applies
  • A request with no customer is never surcharged
  • Budgets and budget reservations use the same rate
  • Keys bound to a customer get the app's rate too
  • App settings get a "Surcharge on customer requests (%)" field

User Flow

Before: an admin with a 2% gateway surcharge wants one app to charge its customers 5%, and cannot

  1. Admin sends PATCH https://litellm-domain/management/v1/apps/{app_id} with {"customer_margin": 0.05} and the field is rejected as unknown
  2. The coworker app sends POST https://litellm-domain/v1/chat/completions with x-litellm-customer-org-alias: qa-surcharge-mixed
  3. x-litellm-response-cost is the model cost times 1.02, the gateway rate

After: that app's customer requests are billed at its own rate

  1. Admin opens https://litellm-domain/ui/apps, selects coworker, enters 5 in "Surcharge on customer requests (%)" and saves, or sends the same PATCH and gets 200
  2. The coworker app sends the same POST https://litellm-domain/v1/chat/completions with x-litellm-customer-org-alias: qa-surcharge-mixed
  3. x-litellm-response-cost is the model cost times 1.05, and x-litellm-response-cost-original is the model cost
  4. The search app, which has no surcharge of its own, is still billed at 1.02
  5. The same coworker request with no customer header is billed at the model cost

App surcharge saved at 5 percent

Customer usage with mixed rates

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally
  • My PR passes all required CI/CD checks
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished

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-5 calls. The app definition is Teams, with teams coworker and search configured as apps, one key with no team, and a gateway customer surcharge of 2%.

python litellm/proxy/proxy_cli.py --config config.yaml --num_workers 2 --detailed_debug --reload --use_v2_migration_resolver 2>&1 | tee litellm.log

Env var names: DATABASE_URL, LITELLM_MASTER_KEY, ANTHROPIC_API_KEY. config.yaml is a copy of litellm/proxy/dev_config.yaml with router_settings.redis_host/redis_port and general_settings.coordination_redis.host/port set to 127.0.0.1 and 56379.

curl -X PATCH $BASE/config/cost_margin_config -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"customer": 0.02}'
curl -X PUT $BASE/management/v1/apps/definition -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"resource_type": "team"}'
curl -X POST $BASE/management/v1/apps -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"resource_id": "<team id>"}'
# the app surcharge, set in the UI in the run below
curl -X PATCH $BASE/management/v1/apps/<app_id> -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"customer_margin": 0.05}'
# the request, sent with each key, with and without the customer header
curl -i -X POST $BASE/v1/chat/completions -H "Authorization: Bearer $APP_KEY" -H "Content-Type: application/json" \
  -H "x-litellm-customer-org-alias: qa-surcharge-mixed" \
  -d '{"model": "anthropic-haiku-4-5", "messages": [{"role": "user", "content": "Write a detailed technical tutorial on database indexing and query planning."}], "max_tokens": 8000}'

Before (378e4cb, #52485 merged with #52406)

  1. The PATCH with customer_margin is rejected as an unknown field
  2. The coworker request for a customer is billed at the gateway rate, 1.02 times the model cost

After (ed8098f, zero case at aec7713, key-bound case at fbde10b)

  1. coworker set to 5 in the UI. Its customer request returns 200
x-litellm-response-cost: 0.0420378
x-litellm-response-cost-original: 0.040036
spend log cost_breakdown: customer_margin_percent 0.05, customer_margin_amount 0.0020018
  1. search, with no surcharge of its own, and the key with no team are both billed at 1.02 for a customer request
x-litellm-response-cost: 0.01023672
x-litellm-response-cost-original: 0.010036
  1. coworker with no customer headers is billed at 1.00, with no customer_margin fields in the spend log
  2. search set to 0 in the UI is billed at 1.00 with no customer_margin fields, while the key with no team stays at 1.02
x-litellm-response-cost: 0.010036
x-litellm-response-cost-original: 0.010036
  1. coworker cleared in the UI goes back to 1.02. A value of 2.5 bills at 1.025
  2. A key bound to a customer through its metadata, sent with no customer headers (at fbde10b, base cost 0.00066 each). On the coworker app at 7.5% it is billed at the app's rate, with the app at 0 it is billed at 1.00 with no customer_margin fields, 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.02
app at 7.5%: x-litellm-response-cost: 0.0007095   x-litellm-response-cost-original: 0.00066
app at 0:    x-litellm-response-cost: 0.00066     x-litellm-response-cost-original: 0.00066
app cleared: x-litellm-response-cost: 0.0006732   x-litellm-response-cost-original: 0.00066
  1. The customer usage page for the organization shows Spend $0.09, Base cost $0.0902 and Surcharge $0.0028. The database has spend 0.09302068, base 0.090216 and surcharge 0.00280468
  2. A 5% request raises the customer organization's spend and the app's budget spend by the billed cost
  3. A real provider response blocked by a post-call content filter returns 400 and writes a failure row with original $0.000065, billed $0.00006825 and margin 0.05
  4. A surcharge saved through the UI applied on the first request served by each worker, 0.47s and 0.92s after the save
  5. PATCH with customer_margin: 11 returns 422. In the UI, 1001 and -1 show "Enter a surcharge between 0 and 1000 percent, or leave it empty." and send nothing

Tests

tests/unit/proxy/spend_tracking/test_customer_cost_margin.py
tests/unit/proxy/spend_tracking/test_cost_finalization.py
tests/unit/proxy/spend_tracking/test_budget_reservation.py
tests/unit/proxy/hooks/test_proxy_track_cost_callback.py
tests/unit/proxy/auth/test_customer_provisioning.py
2300 passed, 1 skipped
src/app/(dashboard)/apps/_components (vitest): all passed

Type

🆕 New Feature

Caveats (if any)

Severe

  • Boot migration adds one nullable column to the app table
    • ADD COLUMN IF NOT EXISTS, no row rewrite

Medium

  • A customer-bound key now also counts toward its app's spend and budget
  • The app's rate replaces the gateway rate, it does not add to it

Low

  • Only the English locale has the new strings
  • An in-flight budget reservation was not isolated in the live run
  • Billed failures with the stamped hierarchy in litellm_metadata, or with no logging snapshot, are covered by unit tests only

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

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-app customer_margin that replaces (not stacks on) the gateway rate—0 disables 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_breakdown plus a new customer_margin_spend metric 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_margin on the hierarchy. Migrations add customer_margin on apps and customer_margin_spend on 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.

devin-ai-integration Bot and others added 30 commits November 18, 2026 01:21
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>
…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>
moe-berri and others added 4 commits November 18, 2026 11:29
…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>

Copy link
Copy Markdown
Contributor Author

@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.

Copy link
Copy Markdown
Contributor Author

bugbot run

Please verify the Responses failure-metadata fix and callback-level regression at 74a2b94, including 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.

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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.

moe-berri and others added 2 commits November 18, 2026 14:35
…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>
ishaan-j-berri and others added 12 commits November 19, 2026 00:20
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.
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>
Base automatically changed from litellm_app_customer_provisioning to main November 18, 2026 19:42
@devin-ai-integration
devin-ai-integration Bot requested a review from a team November 18, 2026 19:42
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration
devin-ai-integration Bot enabled auto-merge (squash) November 18, 2026 19:45
@devin-ai-integration
devin-ai-integration Bot merged commit 2568c47 into main Nov 18, 2026
59 of 61 checks passed
@devin-ai-integration
devin-ai-integration Bot deleted the litellm_app_customer_surcharge branch November 18, 2026 19:56
@codspeed

codspeed Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 45 untouched benchmarks


Comparing litellm_app_customer_surcharge (aab8cbd) with main (facccd8)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (a435090) during the generation of this report, so facccd8 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

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.

4 participants