Repository navigation
fix(cost): split xai spend rows across input and output instead of booking the total as output - #46320
Conversation
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…n math Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
|
|
@litellm-stampbot |
litellm-stampbot: risk highNot approved at Rules (
Judge (gpt-6-luna): high, scoped behavior change, shared helper, moderate diff
Pushes do not re-run this check. Comment |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
Main CircleCI job integration-cost fails test_case_bills_expected_cost[xai-grok-5-provider_reported_cost] since this merged, as cases/misc.py still expects input_cost 0.0 (pipeline 91241) https://app.circleci.com/pipelines/github/BerriAI/litellm/91241 |
TLDR
Problem this solves:
input_cost 0andoutput_cost = totalon every live request, streamed or notusage.costHow it solves it:
cost_per_token(xai) splits xAI's reported total by our token-math ratio, sum stays xAI's number_stamp_streaming_usage_costalso records_hidden_params["response_cost"], so the spend row reuses the first pricing passcost_in_usd_tickslike real xAI responses,XAI_GROK_4_7_STREAMloses itsknown_bugUser Flow
Before: an operator reading the spend row for a grok-4.7 call sees the whole charge booked as output
"model": "xai/grok-4.7","stream": trueusage.cost: 0.001554, the right totalspend 0.001554,cost_breakdown.input_cost 0.0,output_cost 0.001554After: the same row attributes the charge to both directions
usage.costinput_cost 0.000768(96 fresh + 1152 cached prompt tokens),output_cost 0.000834(139 completion tokens),spend 0.001602Linear ticket
Resolves LIT-9540
Pre-Submission checklist
uv run pytest tests/unit/<your_test_file>.py -vScreenshots / Proof of Fix
Live proxy on
localhost:4010(merge base) andlocalhost:4011(PR tip), sameconfig.yaml(xai/grok-4.7,api_key: os.environ/XAI_API_KEY), fresh Postgres each, real xAI callsBefore (origin/main)
curl -s -N http://localhost:4010/v1/chat/completions -H 'Authorization: Bearer sk-proof' -d '{"model":"xai/grok-4.7","messages":[{"role":"user","content":"Say hello in five words."}],"stream":true,"stream_options":{"include_usage":true},"max_tokens":40}' | grep usage | tail -1{"usage": {"completion_tokens": 131, "prompt_tokens": 1248, "total_tokens": 1379, "completion_tokens_details": {"reasoning_tokens": 123, ...}, "prompt_tokens_details": {"cached_tokens": 1152, "text_tokens": 1248, ...}, "cost": 0.001554}}curl -s 'http://localhost:4010/spend/logs?request_id=6054e79e-718a-428a-886d-36aa0eeba9f3' -H 'Authorization: Bearer sk-proof'{"model": "xai/grok-4.7", "spend": 0.001554, "prompt_tokens": 1248, "completion_tokens": 131, "cost_breakdown": {"input_cost": 0.0, "output_cost": 0.001554, "total_cost": 0.001554, "cache_read_cost": 0.000576, "reasoning_cost": 0.000738}}After (38450d6)
curl -s -N http://localhost:4011/v1/chat/completions -H 'Authorization: Bearer sk-proof' -d '{"model":"xai/grok-4.7","messages":[{"role":"user","content":"Say hello in five words."}],"stream":true,"stream_options":{"include_usage":true},"max_tokens":40}' | grep usage | tail -1{"usage": {"completion_tokens": 139, "prompt_tokens": 1248, "total_tokens": 1387, "completion_tokens_details": {"reasoning_tokens": 132, ...}, "prompt_tokens_details": {"cached_tokens": 1152, "text_tokens": 1248, ...}, "cost": 0.001602}}curl -s 'http://localhost:4011/spend/logs?request_id=3e54407d-02d2-4a47-85bf-26253d8969b2' -H 'Authorization: Bearer sk-proof'{"model": "xai/grok-4.7", "spend": 0.001602, "prompt_tokens": 1248, "completion_tokens": 139, "cost_breakdown": {"input_cost": 0.0007679999999999999, "output_cost": 0.000834, "total_cost": 0.001602, "cache_read_cost": 0.000576, "reasoning_cost": 0.0007920000000000001}}Check: 96 × $2/M + 1152 × $0.5/M = $0.000768 input, 139 × $6/M = $0.000834 output, sum $0.001602 = xAI's
cost_in_usd_ticksfor that callSuite:
tests/integration/cost_calculationxai cases, 7 passed on the tip.xai-grok-4.7-streamon main with the fix stashed:input_cost 0.0 != expected 0.00368Type
🐛 Bug Fix
Caveats (if any)
Medium
spendstays xAI's invoiceLow
cost_per_tokenfalls back to(0.0, reported)for an xai model missing from the cost map, same as before/v1/responsesstreams stampusage.costthe same way (_stamp_responses_usage_cost) but are not changed here, they were not re-priced in the suiteFinal Attestation
Link to Devin session: https://app.devin.ai/sessions/e4ef6bdad80443da875abe630a4ece6c
Open in Devin Desktop: https://app.devin.ai/desktop/session/e4ef6bdad80443da875abe630a4ece6c?variant=devin
Requested by: @kerrylu-berri