Skip to content

fix(cost): bill a deployment's per-second rates on native Anthropic /v1/messages - #42417

Merged
mateo-berri merged 13 commits into
mainfrom
litellm_per_second_pricing_responses_messages
Oct 29, 2026
Merged

mateo-berri merged 13 commits into
mainfrom
litellm_per_second_pricing_responses_messages

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

A deployment priced per second under litellm_params on a native Anthropic model now bills that rate on /v1/messages, as chat and Responses already do. Before, Messages billed the per-token price

A non-streamed native Messages call bills from logging setup, as main did, to the end of the response the proxy reports, so the cost header and /spend/logs agree. Other overrides and routes bill as on main

After upgrading, Messages spend and the budgets it counts against move only for Claude deployments priced per second under litellm_params, which billed the per-token price before

Original

TLDR

Problem this solves:

  • A per-second Claude deployment bills per token on native /v1/messages, in the cost header and /spend/logs

How it solves it:

  • The native Messages handler passes the deployment's per-second rates to cost tracking
  • A non-streamed call bills from logging setup to the end of the response the proxy reports, so the cost header and spend log agree

User Flow

Before: an admin who priced a Claude deployment per second sees /v1/messages bill the per-token price

  1. The admin adds claude-per-second for anthropic/claude-haiku-5-5 with per-second rates of 0.000420 under litellm_params
  2. POST https://litellm-domain/v1/chat/completions or /v1/responses with that model bills the rate times the call's seconds
  3. POST https://litellm-domain/v1/messages with that model returns x-litellm-response-cost: 6.5e-06, the per-token price, streamed or not, and GET https://litellm-domain/spend/logs?request_id= matches
  4. A Claude Code user pointed at the proxy with that model sees per-token spend in GET https://litellm-domain/spend/logs?api_key=

After: the same Messages requests bill the per-second rate, like the other two routes

  1. The admin adds the same deployment
  2. POST https://litellm-domain/v1/chat/completions or /v1/responses bills as before
  3. POST https://litellm-domain/v1/messages returns x-litellm-response-cost: 0.000401, which is 0.00042 × the call's 0.956 s, the spend log matches, and a stream bills its whole length
  4. The same Claude Code user's calls bill 0.00042 × each call's seconds

Relevant issues

Follow-up to #42403, which made per-second rates bill on /v1/chat/completions

Design decisions

The native Messages handler passes only the deployment's per-second rates into its logging refresh, the set chat registers per call. Other overrides, such as a long-context rate, bill as on main. Per-second pricing bills once at the first rate set, per #43614

A non-streamed native Messages call bills from logging setup to the end of the response the proxy reports, read off the logging object since the plain-dict result cannot carry it. Main billed Messages from logging setup too, after auth, so a deployment priced per second under model_info bills the same window as before, and the cost header now agrees with the spend log. A streamed call bills the whole stream

Alternatives considered
  • Pass every custom pricing key the deployment sets, as Responses does since fix(responses): honor per-call custom pricing on the native Responses API path #48119: an earlier commit here did, and a deployment with only a long-context rate then billed a 231k-token Messages call about 200 times what proxy chat bills for the same call, since proxy chat ignores that rate (an older bug, tracked separately)
  • Copy the rates in the logging refresh every non-chat route shares: an earlier commit here did, and it also repriced generateContent and 20+ other routes, with streamed generateContent sending a per-second cost header its per-token spend log contradicted
  • Bill from proxy receive, as chat does: an earlier commit here did, and deployments priced per second under model_info, which Messages already billed per second, then also billed auth and request reading, so their spend rose on upgrade
  • Mirror the per-second fields onto model_info: changes /model/info, the models page, and every consumer of the deployment, with two copies of one rate
  • Re-merge the deployment's params inside the router: covers the proxy but not SDK callers that pre-create a logging object
  • Rebuild the logging state after routing: double-registers the request with every callback

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • 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, and I have addressed or replied to each of its findings before requesting a maintainer review

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Each leg is two 2-worker proxies at the named commit on one Postgres database, alternating requests, on real provider keys and the real Claude Code TUI

Config and shell setup
model_list:
  - model_name: claude-per-second
    litellm_params:
      model: anthropic/claude-haiku-5-5
      api_key: os.environ/ANTHROPIC_API_KEY
      input_cost_per_second: 0.000420
      output_cost_per_second: 0.000420
  - model_name: claude-per-second-model-info
    litellm_params:
      model: anthropic/claude-haiku-5-5
      api_key: os.environ/ANTHROPIC_API_KEY
    model_info:
      input_cost_per_second: 0.000420
      output_cost_per_second: 0.000420
  - model_name: claude-cost-map
    litellm_params:
      model: anthropic/claude-haiku-5-5
      api_key: os.environ/ANTHROPIC_API_KEY
  - model_name: claude-long-context
    litellm_params:
      model: anthropic/claude-haiku-5-5
      api_key: os.environ/ANTHROPIC_API_KEY
      input_cost_per_token_above_200k_tokens: 0.0001
      output_cost_per_token_above_200k_tokens: 0.0003
  - model_name: vertex-claude-per-second
    litellm_params:
      model: vertex_ai/claude-haiku-5-5
      vertex_project: os.environ/VERTEXAI_PROJECT
      vertex_location: global
      input_cost_per_second: 0.000420
      output_cost_per_second: 0.000420
  - model_name: per-second-priced
    litellm_params:
      model: openai/gpt-5.4-nano
      api_key: os.environ/OPENAI_API_KEY
      input_cost_per_second: 0.000420
      output_cost_per_second: 0.000420
  - model_name: gemini-per-second
    litellm_params:
      model: gemini/gemini-3.8-flash
      api_key: os.environ/GEMINI_API_KEY
      input_cost_per_second: 0.000420
      output_cost_per_second: 0.000420
general_settings:
  master_key: os.environ/LITELLM_MASTER_KEY

Before ran on ports 21115 and 29758, after on 30671 and 20716

H=(-H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json")
FILTER='^x-litellm-response-cost|^x-litellm-response-duration-ms|^x-litellm-call-id'
ROW='import json,sys; print([{k: r.get(k) for k in ("model","call_type","spend","prompt_tokens","total_tokens")} for r in json.load(sys.stdin)])'
long_body() { python3 -c 'import json; print(json.dumps({"model": "claude-long-context", "max_tokens": 16, "messages": [{"role": "user", "content": " ".join(f"item{i % 997}" for i in range(77000)) + "\n\nReply with the word done."}]}))'; }
VKEY=$(curl -s http://localhost:<second port>/key/generate "${H[@]}" -d '{"models": ["claude-per-second"]}' | python3 -c 'import json,sys; print(json.load(sys.stdin)["key"])')
ANTHROPIC_BASE_URL=http://localhost:<first port> ANTHROPIC_API_KEY=$VKEY ANTHROPIC_MODEL=claude-per-second \
  ANTHROPIC_DEFAULT_HAIKU_MODEL=claude-per-second ANTHROPIC_SMALL_FAST_MODEL=claude-per-second \
  CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 claude
CC_ROWS='import json,sys; from datetime import datetime as D; t=lambda r,k: D.fromisoformat(r[k].replace("Z","+00:00")); [print({k: r[k] for k in ("model","call_type","spend","prompt_tokens","completion_tokens")} | {"seconds": round((t(r,"endTime")-t(r,"startTime")).total_seconds(), 3)}) for r in sorted(json.load(sys.stdin), key=lambda r: r["startTime"])]'

The per-second price is 0.00042 × the call's duration in seconds, so around 0.0003 to 0.0008 for these calls, while claude-haiku-5-5's per-token price for them is around 1e-05. The 231k-token cases show a deployment's long-context rate bills the same on both legs and both routes, and the Gemini case shows generateContent bills the same way on both legs, since this PR leaves both alone

Case Before spend (3f5e42d) After spend (2383124)
Messages 6.5e-06 0.000401
Messages, streamed 9.9e-05 0.000547
Messages, Vertex AI 6.5e-06 0.000432
Messages, Vertex AI, streamed 9.9e-05 0.000706
Messages, 231k-token prompt 0.116 0.116
Chat, 231k-token prompt 0.116 0.116
Responses 0.00122 0.000605
Chat completions 0.000377 0.000613
Messages, no custom rates 6.5e-06 6.5e-06
Messages, OpenAI per-second 0.000305 0.000357
Messages, rates under model_info 0.0004 0.000346
Chat, rates under model_info 0.000341 0.000344
Gemini generateContent 0.00109 0.000964
Claude Code, larger call 0.00349 0.000465
Claude Code, smaller call 0.000213 0.000769
Claude Code, before (3f5e42d)
  1. In Claude Code started with the env in the setup block against port 21115, type Count from one to thirty in words, nothing else and press Enter

pr42417-2383124b40-claude-code-before.png

  1. curl -s "http://localhost:29758/spend/logs?api_key=$VKEY" "${H[@]}" | python3 -c "$CC_ROWS"
  2. Observed, each row at the per-token price (0.000213 is 1175 × 1e-07 + 191 × 5e-07), where 0.00042 × the seconds would be about 0.00073 and 0.00076
{'model': 'anthropic/claude-haiku-5-5', 'call_type': 'anthropic_messages', 'spend': 0.003494075, 'prompt_tokens': 27305, 'completion_tokens': 162, 'seconds': 1.742}
{'model': 'anthropic/claude-haiku-5-5', 'call_type': 'anthropic_messages', 'spend': 0.000213, 'prompt_tokens': 1175, 'completion_tokens': 191, 'seconds': 1.808}

Claude Code, after (2383124)

  1. In Claude Code started with the same env against port 30671, type the same prompt and press Enter

pr42417-2383124b40-claude-code-after.png

  1. curl -s "http://localhost:20716/spend/logs?api_key=$VKEY" "${H[@]}" | python3 -c "$CC_ROWS"
  2. Observed, each row at 0.00042 × the call's seconds from logging setup to the response (1.830 s and 1.108 s, a little longer than the logged start-to-end seconds)
{'model': 'anthropic/claude-haiku-5-5', 'call_type': 'anthropic_messages', 'spend': 0.0007687755060195923, 'prompt_tokens': 1175, 'completion_tokens': 151, 'seconds': 1.803}
{'model': 'anthropic/claude-haiku-5-5', 'call_type': 'anthropic_messages', 'spend': 0.0004654969024658203, 'prompt_tokens': 27296, 'completion_tokens': 133, 'seconds': 1.031}

Before, curl cases (3f5e42d)

POST /v1/messages on the per-second Claude deployment
  1. curl -s -D - http://localhost:29758/v1/messages "${H[@]}" -d '{"model": "claude-per-second", "max_tokens": 32, "messages": [{"role": "user", "content": "Say hi in three words"}]}' -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: f07ae05e-febc-4ff0-9610-822b0687c9b4, x-litellm-response-cost: 6.5e-06, x-litellm-response-duration-ms: 1397.279977798462, client_total_seconds: 2.006862
  3. curl -s "http://localhost:29758/spend/logs?request_id=f07ae05e-febc-4ff0-9610-822b0687c9b4" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'anthropic/claude-haiku-5-5', 'call_type': 'anthropic_messages', 'spend': 6.5e-06, 'prompt_tokens': 15, 'total_tokens': 25}] (the model's per-token price, so the per-second rate is ignored)
POST /v1/messages with stream on the per-second Claude deployment
  1. curl -s -N -D - http://localhost:21115/v1/messages "${H[@]}" -d '{"model": "claude-per-second", "max_tokens": 200, "stream": true, "messages": [{"role": "user", "content": "Count from 1 to 40 in words"}]}' -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: d96f4a4e-684d-4e38-80eb-c446133f3483, client_total_seconds: 1.717417 (a streamed response sends its headers before the cost is known)
  3. curl -s "http://localhost:21115/spend/logs?request_id=d96f4a4e-684d-4e38-80eb-c446133f3483" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'anthropic/claude-haiku-5-5', 'call_type': 'anthropic_messages', 'spend': 9.9e-05, 'prompt_tokens': 20, 'total_tokens': 214}] (the model's per-token price, so the per-second rate is ignored)
POST /v1/messages on a per-second Claude deployment on Vertex AI
  1. curl -s -D - http://localhost:21115/v1/messages "${H[@]}" -d '{"model": "vertex-claude-per-second", "max_tokens": 32, "messages": [{"role": "user", "content": "Say hi in three words"}]}' -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: 591ab350-0d1d-4d58-bd73-7b34a2b11910, x-litellm-response-cost: 6.5e-06, x-litellm-response-duration-ms: 922.7819442749023, client_total_seconds: 0.946678
  3. curl -s "http://localhost:21115/spend/logs?request_id=591ab350-0d1d-4d58-bd73-7b34a2b11910" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'vertex_ai/claude-haiku-5-5', 'call_type': 'anthropic_messages', 'spend': 6.5e-06, 'prompt_tokens': 15, 'total_tokens': 25}] (the model's per-token price, so the per-second rate is ignored)
POST /v1/messages with stream on the Vertex AI deployment
  1. curl -s -N -D - http://localhost:29758/v1/messages "${H[@]}" -d '{"model": "vertex-claude-per-second", "max_tokens": 200, "stream": true, "messages": [{"role": "user", "content": "Count from 1 to 40 in words"}]}' -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: 61104ece-3ee4-4864-bdd9-21c6af762cae, client_total_seconds: 1.552626 (a streamed response sends its headers before the cost is known)
  3. curl -s "http://localhost:29758/spend/logs?request_id=61104ece-3ee4-4864-bdd9-21c6af762cae" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'vertex_ai/claude-haiku-5-5', 'call_type': 'anthropic_messages', 'spend': 9.9e-05, 'prompt_tokens': 20, 'total_tokens': 214}] (the model's per-token price, so the per-second rate is ignored)
POST /v1/messages with a 231k-token prompt on a long-context-priced deployment
  1. curl -s -D - http://localhost:29758/v1/messages "${H[@]}" --data-binary @<(long_body) -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: 0002fce1-bb18-4d98-a8b4-d7d2a0bdb62d, x-litellm-response-cost: 0.1155155, x-litellm-response-duration-ms: 8347.465991973877, client_total_seconds: 8.375509
  3. curl -s "http://localhost:29758/spend/logs?request_id=0002fce1-bb18-4d98-a8b4-d7d2a0bdb62d" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'anthropic/claude-haiku-5-5', 'call_type': 'anthropic_messages', 'spend': 0.1155155, 'prompt_tokens': 231016, 'total_tokens': 231019}] (231016 prompt tokens at the model's own 100k-tier price, the same on both routes and both legs, since this PR leaves that rate alone)
POST /v1/chat/completions with the same prompt and deployment
  1. curl -s -D - http://localhost:21115/v1/chat/completions "${H[@]}" --data-binary @<(long_body) -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: 8a4585c1-38b9-484e-8a5a-98e9348a6bca, x-litellm-response-cost: 0.1155155, x-litellm-response-duration-ms: 3077.3379802703857, client_total_seconds: 3.098125
  3. curl -s "http://localhost:21115/spend/logs?request_id=8a4585c1-38b9-484e-8a5a-98e9348a6bca" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'anthropic/claude-haiku-5-5', 'call_type': 'acompletion', 'spend': 0.1155155, 'prompt_tokens': 231016, 'total_tokens': 231019}] (231016 prompt tokens at the model's own 100k-tier price, the same on both routes and both legs, since this PR leaves that rate alone)
POST /v1/responses on the per-second Claude deployment
  1. curl -s -D - http://localhost:21115/v1/responses "${H[@]}" -d '{"model": "claude-per-second", "input": "Say hi in three words"}' -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: ec28ed8f-a12a-4904-9f8c-f16ba4e4d032, x-litellm-response-cost: 0.001218663239479065, x-litellm-response-duration-ms: 3099.4319915771484, client_total_seconds: 3.183473
  3. curl -s "http://localhost:21115/spend/logs?request_id=ec28ed8f-a12a-4904-9f8c-f16ba4e4d032" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'anthropic/claude-haiku-5-5', 'call_type': 'aresponses', 'spend': 0.001218663239479065, 'prompt_tokens': 15, 'total_tokens': 25}] (0.00042 × 2.902 s, the per-second rate times the call's duration)
POST /v1/chat/completions on the per-second Claude deployment
  1. curl -s -D - http://localhost:29758/v1/chat/completions "${H[@]}" -d '{"model": "claude-per-second", "messages": [{"role": "user", "content": "Say hi in three words"}]}' -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: 7aebd4eb-a6d1-480f-b05c-c6a4725e6045, x-litellm-response-cost: 0.00037707558631896977, x-litellm-response-duration-ms: 897.799015045166, client_total_seconds: 0.953078
  3. curl -s "http://localhost:29758/spend/logs?request_id=7aebd4eb-a6d1-480f-b05c-c6a4725e6045" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'anthropic/claude-haiku-5-5', 'call_type': 'acompletion', 'spend': 0.0003770755863189698, 'prompt_tokens': 15, 'total_tokens': 25}] (0.00042 × 0.898 s, the per-second rate times the call's duration)
POST /v1/messages on a Claude deployment without custom rates
  1. curl -s -D - http://localhost:21115/v1/messages "${H[@]}" -d '{"model": "claude-cost-map", "max_tokens": 32, "messages": [{"role": "user", "content": "Say hi in three words"}]}' -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: 922a13a4-9716-42e6-aa37-6a1db749f704, x-litellm-response-cost: 6.5e-06, x-litellm-response-duration-ms: 933.466911315918, client_total_seconds: 0.962592
  3. curl -s "http://localhost:21115/spend/logs?request_id=922a13a4-9716-42e6-aa37-6a1db749f704" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'anthropic/claude-haiku-5-5', 'call_type': 'anthropic_messages', 'spend': 6.5e-06, 'prompt_tokens': 15, 'total_tokens': 25}] (the model's per-token price, unchanged)
POST /v1/messages on the per-second OpenAI deployment
  1. curl -s -D - http://localhost:29758/v1/messages "${H[@]}" -d '{"model": "per-second-priced", "max_tokens": 32, "messages": [{"role": "user", "content": "Say hi in three words"}]}' -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: 989bf660-4dc4-4c4b-957f-cd6cc26b501d, x-litellm-response-cost: 0.00030531987190246583, x-litellm-response-duration-ms: 770.9200382232666, client_total_seconds: 0.814789
  3. curl -s "http://localhost:29758/spend/logs?request_id=989bf660-4dc4-4c4b-957f-cd6cc26b501d" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'openai/gpt-5.4-nano', 'call_type': 'anthropic_messages', 'spend': 0.0003053198719024658, 'prompt_tokens': 11, 'total_tokens': 18}] (0.00042 × 0.727 s, the per-second rate times the call's duration)
POST /v1/messages on a Claude deployment priced per second under model_info
  1. curl -s -D - http://localhost:21115/v1/messages "${H[@]}" -d '{"model": "claude-per-second-model-info", "max_tokens": 32, "messages": [{"role": "user", "content": "Say hi in three words"}]}' -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: bf646a76-9822-4871-ac85-ca2967e50bbe, x-litellm-response-cost: 0.00040353186607360844, x-litellm-response-duration-ms: 960.8230590820312, client_total_seconds: 1.010143
  3. curl -s "http://localhost:21115/spend/logs?request_id=bf646a76-9822-4871-ac85-ca2967e50bbe" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'anthropic/claude-haiku-5-5', 'call_type': 'anthropic_messages', 'spend': 0.0004003822946548462, 'prompt_tokens': 15, 'total_tokens': 24}] (0.00042 × 0.953 s, the per-second rate times the call's duration, while the cost header said 0.000404 for the same call)
POST /v1/chat/completions on the same model_info deployment
  1. curl -s -D - http://localhost:29758/v1/chat/completions "${H[@]}" -d '{"model": "claude-per-second-model-info", "messages": [{"role": "user", "content": "Say hi in three words"}]}' -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: ea73b6b7-b78a-4af5-be0f-f6b8036dfbfc, x-litellm-response-cost: 0.0003413011693954468, x-litellm-response-duration-ms: 812.6218318939209, client_total_seconds: 0.824834
  3. curl -s "http://localhost:29758/spend/logs?request_id=ea73b6b7-b78a-4af5-be0f-f6b8036dfbfc" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'anthropic/claude-haiku-5-5', 'call_type': 'acompletion', 'spend': 0.0003413011693954468, 'prompt_tokens': 15, 'total_tokens': 25}] (0.00042 × 0.813 s, the per-second rate times the call's duration)
POST generateContent on a per-second Gemini deployment
  1. curl -s -D - http://localhost:21115/v1beta/models/gemini-per-second:generateContent "${H[@]}" -d '{"contents": [{"role": "user", "parts": [{"text": "Say hi in three words"}]}]}' -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: e49d5fa4-1e4c-483e-a130-af1a36edca72, x-litellm-response-cost: 0.001092, x-litellm-response-duration-ms: 4011.7578506469727, client_total_seconds: 4.061602
  3. curl -s "http://localhost:21115/spend/logs?request_id=e49d5fa4-1e4c-483e-a130-af1a36edca72" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'gemini/gemini-3.8-flash', 'call_type': 'agenerate_content', 'spend': 0.001092, 'prompt_tokens': 6, 'total_tokens': 296}] (per-token on both legs, since this PR leaves generateContent alone)

After, curl cases (2383124)

POST /v1/messages on the per-second Claude deployment
  1. curl -s -D - http://localhost:20716/v1/messages "${H[@]}" -d '{"model": "claude-per-second", "max_tokens": 32, "messages": [{"role": "user", "content": "Say hi in three words"}]}' -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: de846b7f-0d85-4035-befe-239c8669874d, x-litellm-response-cost: 0.0004013956689834595, x-litellm-response-duration-ms: 1112.9329204559326, client_total_seconds: 1.481285
  3. curl -s "http://localhost:20716/spend/logs?request_id=de846b7f-0d85-4035-befe-239c8669874d" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'anthropic/claude-haiku-5-5', 'call_type': 'anthropic_messages', 'spend': 0.0004013956689834595, 'prompt_tokens': 15, 'total_tokens': 25}] (0.00042 × 0.956 s, the per-second rate times the call's duration)
POST /v1/messages with stream on the per-second Claude deployment
  1. curl -s -N -D - http://localhost:30671/v1/messages "${H[@]}" -d '{"model": "claude-per-second", "max_tokens": 200, "stream": true, "messages": [{"role": "user", "content": "Count from 1 to 40 in words"}]}' -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: 61506593-5e84-4c6a-a647-1e98e9102972, client_total_seconds: 1.583149 (a streamed response sends its headers before the cost is known)
  3. curl -s "http://localhost:30671/spend/logs?request_id=61506593-5e84-4c6a-a647-1e98e9102972" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'anthropic/claude-haiku-5-5', 'call_type': 'anthropic_messages', 'spend': 0.0005472306489944459, 'prompt_tokens': 20, 'total_tokens': 214}] (0.00042 × 1.303 s, the per-second rate times the call's duration)
POST /v1/messages on a per-second Claude deployment on Vertex AI
  1. curl -s -D - http://localhost:30671/v1/messages "${H[@]}" -d '{"model": "vertex-claude-per-second", "max_tokens": 32, "messages": [{"role": "user", "content": "Say hi in three words"}]}' -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: d3b4029e-1814-4178-9b8f-273b28db97f5, x-litellm-response-cost: 0.0004316108465194702, x-litellm-response-duration-ms: 1047.4748611450195, client_total_seconds: 1.070254
  3. curl -s "http://localhost:30671/spend/logs?request_id=d3b4029e-1814-4178-9b8f-273b28db97f5" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'vertex_ai/claude-haiku-5-5', 'call_type': 'anthropic_messages', 'spend': 0.0004316108465194702, 'prompt_tokens': 15, 'total_tokens': 47}] (0.00042 × 1.028 s, the per-second rate times the call's duration)
POST /v1/messages with stream on the Vertex AI deployment
  1. curl -s -N -D - http://localhost:20716/v1/messages "${H[@]}" -d '{"model": "vertex-claude-per-second", "max_tokens": 200, "stream": true, "messages": [{"role": "user", "content": "Count from 1 to 40 in words"}]}' -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: 2be26c60-7c8c-4160-b920-ed8641a999b4, client_total_seconds: 1.701180 (a streamed response sends its headers before the cost is known)
  3. curl -s "http://localhost:20716/spend/logs?request_id=2be26c60-7c8c-4160-b920-ed8641a999b4" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'vertex_ai/claude-haiku-5-5', 'call_type': 'anthropic_messages', 'spend': 0.000705567684173584, 'prompt_tokens': 20, 'total_tokens': 214}] (0.00042 × 1.680 s, the per-second rate times the call's duration)
POST /v1/messages with a 231k-token prompt on a long-context-priced deployment
  1. curl -s -D - http://localhost:20716/v1/messages "${H[@]}" --data-binary @<(long_body) -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: 4db7c9d6-a70c-4842-a86e-b20b1416f65f, x-litellm-response-cost: 0.1155155, x-litellm-response-duration-ms: 6606.694221496582, client_total_seconds: 6.622003
  3. curl -s "http://localhost:20716/spend/logs?request_id=4db7c9d6-a70c-4842-a86e-b20b1416f65f" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'anthropic/claude-haiku-5-5', 'call_type': 'anthropic_messages', 'spend': 0.1155155, 'prompt_tokens': 231016, 'total_tokens': 231019}] (231016 prompt tokens at the model's own 100k-tier price, the same on both routes and both legs, since this PR leaves that rate alone)
POST /v1/chat/completions with the same prompt and deployment
  1. curl -s -D - http://localhost:30671/v1/chat/completions "${H[@]}" --data-binary @<(long_body) -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: ed2273db-bb6f-41a0-bb47-8f60e43477f6, x-litellm-response-cost: 0.1155155, x-litellm-response-duration-ms: 6715.348958969116, client_total_seconds: 6.735340
  3. curl -s "http://localhost:30671/spend/logs?request_id=ed2273db-bb6f-41a0-bb47-8f60e43477f6" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'anthropic/claude-haiku-5-5', 'call_type': 'acompletion', 'spend': 0.1155155, 'prompt_tokens': 231016, 'total_tokens': 231019}] (231016 prompt tokens at the model's own 100k-tier price, the same on both routes and both legs, since this PR leaves that rate alone)
POST /v1/responses on the per-second Claude deployment
  1. curl -s -D - http://localhost:30671/v1/responses "${H[@]}" -d '{"model": "claude-per-second", "input": "Say hi in three words"}' -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: e9b317c8-1e5e-4344-b06a-b8b8d259dd90, x-litellm-response-cost: 0.0006051506996154786, x-litellm-response-duration-ms: 1518.6598300933838, client_total_seconds: 1.578033
  3. curl -s "http://localhost:30671/spend/logs?request_id=e9b317c8-1e5e-4344-b06a-b8b8d259dd90" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'anthropic/claude-haiku-5-5', 'call_type': 'aresponses', 'spend': 0.0006051506996154786, 'prompt_tokens': 15, 'total_tokens': 25}] (0.00042 × 1.441 s, the per-second rate times the call's duration)
POST /v1/chat/completions on the per-second Claude deployment
  1. curl -s -D - http://localhost:20716/v1/chat/completions "${H[@]}" -d '{"model": "claude-per-second", "messages": [{"role": "user", "content": "Say hi in three words"}]}' -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: eb3e9b38-2422-4eb2-b26b-47238c44aa2f, x-litellm-response-cost: 0.0006133255863189698, x-litellm-response-duration-ms: 1460.299015045166, client_total_seconds: 1.490187
  3. curl -s "http://localhost:20716/spend/logs?request_id=eb3e9b38-2422-4eb2-b26b-47238c44aa2f" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'anthropic/claude-haiku-5-5', 'call_type': 'acompletion', 'spend': 0.0006133255863189698, 'prompt_tokens': 15, 'total_tokens': 25}] (0.00042 × 1.460 s, the per-second rate times the call's duration)
POST /v1/messages on a Claude deployment without custom rates
  1. curl -s -D - http://localhost:30671/v1/messages "${H[@]}" -d '{"model": "claude-cost-map", "max_tokens": 32, "messages": [{"role": "user", "content": "Say hi in three words"}]}' -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: ca52120c-17a3-4284-bd57-3e87f199e453, x-litellm-response-cost: 6.5e-06, x-litellm-response-duration-ms: 878.0059814453125, client_total_seconds: 0.913901
  3. curl -s "http://localhost:30671/spend/logs?request_id=ca52120c-17a3-4284-bd57-3e87f199e453" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'anthropic/claude-haiku-5-5', 'call_type': 'anthropic_messages', 'spend': 6.5e-06, 'prompt_tokens': 15, 'total_tokens': 25}] (the model's per-token price, unchanged)
POST /v1/messages on the per-second OpenAI deployment
  1. curl -s -D - http://localhost:20716/v1/messages "${H[@]}" -d '{"model": "per-second-priced", "max_tokens": 32, "messages": [{"role": "user", "content": "Say hi in three words"}]}' -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: 4e08d4ee-4752-4d23-a7d8-781bc2bd29c8, x-litellm-response-cost: 0.0003567110681533814, x-litellm-response-duration-ms: 882.375955581665, client_total_seconds: 0.926071
  3. curl -s "http://localhost:20716/spend/logs?request_id=4e08d4ee-4752-4d23-a7d8-781bc2bd29c8" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'openai/gpt-5.4-nano', 'call_type': 'anthropic_messages', 'spend': 0.0003567110681533814, 'prompt_tokens': 11, 'total_tokens': 18}] (0.00042 × 0.849 s, the per-second rate times the call's duration)
POST /v1/messages on a Claude deployment priced per second under model_info
  1. curl -s -D - http://localhost:30671/v1/messages "${H[@]}" -d '{"model": "claude-per-second-model-info", "max_tokens": 32, "messages": [{"role": "user", "content": "Say hi in three words"}]}' -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: e314b8b8-ddd6-4ccb-a1c3-dcf9f9248223, x-litellm-response-cost: 0.00034632227897644045, x-litellm-response-duration-ms: 836.169958114624, client_total_seconds: 0.850504
  3. curl -s "http://localhost:30671/spend/logs?request_id=e314b8b8-ddd6-4ccb-a1c3-dcf9f9248223" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'anthropic/claude-haiku-5-5', 'call_type': 'anthropic_messages', 'spend': 0.0003463222789764405, 'prompt_tokens': 15, 'total_tokens': 25}] (0.00042 × 0.825 s, the per-second rate times the call's duration)
POST /v1/chat/completions on the same model_info deployment
  1. curl -s -D - http://localhost:20716/v1/chat/completions "${H[@]}" -d '{"model": "claude-per-second-model-info", "messages": [{"role": "user", "content": "Say hi in three words"}]}' -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: fa4703f8-73d9-4655-8916-cec4fb3ef7c3, x-litellm-response-cost: 0.0003435339975357056, x-litellm-response-duration-ms: 817.9380893707275, client_total_seconds: 0.830839
  3. curl -s "http://localhost:20716/spend/logs?request_id=fa4703f8-73d9-4655-8916-cec4fb3ef7c3" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'anthropic/claude-haiku-5-5', 'call_type': 'acompletion', 'spend': 0.0003435339975357056, 'prompt_tokens': 15, 'total_tokens': 25}] (0.00042 × 0.818 s, the per-second rate times the call's duration)
POST generateContent on a per-second Gemini deployment
  1. curl -s -D - http://localhost:30671/v1beta/models/gemini-per-second:generateContent "${H[@]}" -d '{"contents": [{"role": "user", "parts": [{"text": "Say hi in three words"}]}]}' -o /dev/null -w 'client_total_seconds: %{time_total}\n' | grep -iE "$FILTER|^client_total_seconds"
  2. Observed: x-litellm-call-id: d341c210-1a5c-446a-b501-b5ca09007c38, x-litellm-response-cost: 0.0009645, x-litellm-response-duration-ms: 4130.451917648315, client_total_seconds: 4.316297
  3. curl -s "http://localhost:30671/spend/logs?request_id=d341c210-1a5c-446a-b501-b5ca09007c38" "${H[@]}" | python3 -c "$ROW"
  4. Observed: [{'model': 'gemini/gemini-3.8-flash', 'call_type': 'agenerate_content', 'spend': 0.0009645, 'prompt_tokens': 6, 'total_tokens': 262}] (per-token on both legs, since this PR leaves generateContent alone)

Type

🐛 Bug Fix

Caveats (if any)

Low

  • Messages bills from logging setup, as on main, so a non-streamed call bills less than its duration header and than chat
  • Billed seconds run a little past the spend row's start-to-end, as on chat
  • Bedrock has no live leg here
  • generateContent still bills per-second rates per token, as on main
  • Proxy chat and Messages ignore a deployment's long-context rate, an older bug
  • SDK litellm.max_budget passes seconds as milliseconds, an older bug

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
Bugbot summary

[!NOTE]
Medium Risk
Changes spend calculation and timing for deployments priced per second on /v1/messages; scope is narrowed to per-second fields only, but billing headers and budgets can shift for those deployments.

Overview
Native /v1/messages now honors deployment per-second rates under litellm_params (like chat and Responses), instead of falling back to per-token pricing. The Anthropic Messages handler merges only per_second_pricing() into the logging refresh so other custom overrides (e.g. long-context token rates) stay unchanged on this route.

Non-streamed spend uses response_ms_since_logging_start in _response_time_ms_for_cost, billing from logging setup through the stamped response window so x-litellm-response-cost and /spend/logs match. Streamed calls still bill via get_response_ms() over the full stream.

GenericLiteLLMParams gains custom_pricing_overrides() and per_second_pricing(); Responses drops its local _per_call_custom_pricing helper in favor of the shared method.

Reviewed by Cursor Bugbot for commit 2383124. Bugbot is set up for automated code reviews on this repo. Configure here.

…aying at $0

The zero-cost diagnostic tests from #42345 used a per-second-only entry as their
misconfigured fixture, which this branch now bills. Switch that fixture to a
per-query-only entry, which is still selected as the deployment's own pricing and
still prices chat usage at $0, and add a per-second test asserting the call
duration is billed with no diagnostic

Also let a caller's explicit total_time outrank the logging window in
completion_cost, so the SDK precedence stays stamped response, caller, logging
…/v1/messages

The proxy builds the request's logging state before the router picks a
deployment, and only the chat path re-read the deployment's
input_cost_per_second / output_cost_per_second afterwards. The shared
update step every other route runs (Logging.update_from_kwargs) now
seeds those custom pricing keys from the router-merged kwargs too, so
custom_pricing flips on and the cost calculator prices the call against
the router-registered deployment id on every surface.
@devin-ai-integration

devin-ai-integration Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@chilkotiKartik

Copy link
Copy Markdown

Review for PR #42417: Bill a deployment's per-second rates on /v1/responses and /v1/messages

Endpoint Pricing Consistency Analysis

  1. Anthropic & Custom Route Coverage:
    Extending duration billing to /v1/messages (Anthropic native format) and /v1/responses ensures parity with /v1/chat/completions.

  2. Async Context Lifetimes:
    Validate that response_start_time and response_end_time are accurately attached to LiteLLMResponse before dispatching to Prometheus / SpendLogs listeners.

@greptile-apps

greptile-apps Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Critical impact] The PR appears safe to merge, with the previously reported duration-unit issue conceded as originating outside this PR and no new actionable failures found

Findings

  1. P1 Duration units are mismatched ▶

Summary

This PR propagates deployment per-second pricing into native Anthropic Messages calls and aligns non-streamed cost calculation with the response timing window used by spend logging

  • Adds immutable helpers for selecting custom and per-second pricing overrides
  • Adjusts timing metadata to exclude proxy work before logging setup where required
  • Adds regression coverage for streamed and non-streamed Messages billing and pricing isolation

Reviews (9) · Last reviewed commit: "fix(cost): bill native /v1/messages from..." · Reviewed by Greptile

if isinstance(stamped, (int, float)):
return float(stamped)
if total_time:
return total_time

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.

P1 Duration units are mismatched

When _response_ms is absent, total_time is seconds, but this path treats it as milliseconds, underbilling duration-priced calls by 1000x

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.

This helper lands in #42403, whose tip this branch starts from, so the seconds-vs-ms question is raised there and merges into this PR

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.

You’re right—the helper and its seconds-vs-milliseconds behavior come from the stacked #42403 change, not from the #42417 changes being reviewed here. This PR only reuses that helper for the newly covered routes, so the finding should be attributed to #42403 rather than block #42417.

The unit-handling issue should still be addressed in #42403 or a follow-up against that change.

@codspeed

codspeed Bot commented Sep 22, 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

✅ 38 untouched benchmarks


Comparing litellm_per_second_pricing_responses_messages (2383124) with main (032342d)1

Open in CodSpeed

Footnotes

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

@codecov

codecov Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

…icing_responses_messages

# Conflicts:
#	litellm/cost_calculator.py
#	tests/test_litellm/test_cost_calculator.py
@mateo-berri mateo-berri added run-ci and removed run-ci labels Sep 22, 2026
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

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

Stale Bugbot comment from a previous run.

…reports

A /v1/messages result is a plain dict, so the proxy keeps its response time on
the logging object. The cost step now reads it there for a non-streamed call,
so the cost header and the spend log bill the same duration.
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

…e /v1/messages route

Move the custom pricing copy from the shared logging refresh into the native
Messages handler, reusing the helper the Responses handler already had, so
generateContent and other routes keep their current billing
@mateo-berri mateo-berri changed the title fix(cost): bill a deployment's per-second rates on /v1/responses and /v1/messages fix(cost): bill a deployment's per-second rates on native Anthropic /v1/messages Oct 29, 2026
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

…cking

Other deployment overrides keep billing as on main, matching chat's per-second gate
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@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 2383124. Configure here.

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

LGTM

@mateo-berri
mateo-berri merged commit 444423a into main Oct 29, 2026
95 of 97 checks passed
@mateo-berri
mateo-berri deleted the litellm_per_second_pricing_responses_messages branch October 29, 2026 12:29
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.

2 participants