Skip to content

fix(bedrock): bill completion tokens on OpenAI-shaped invoke streams - #53850

Open
chaznm wants to merge 7 commits into
BerriAI:mainfrom
chaznm:litellm_bedrock_invoke_stream_openai_usage
Open

chaznm wants to merge 7 commits into
BerriAI:mainfrom
chaznm:litellm_bedrock_invoke_stream_openai_usage

Conversation

@chaznm

@chaznm chaznm commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Handwritten 1-3 sentence description

Streaming Qwen3 or Mistral Large 3 through the Bedrock invoke pass-through only billed the prompt, since LiteLLM couldn't read the OpenAI-style chunks those models stream back. Now it reads them and bills the token counts Bedrock reports, same as the non-streamed call

Details

TLDR

Problem this solves:

  • Streamed Bedrock invoke pass-through for Qwen3 and Mistral Large 3 bills only the prompt
  • Pass-through streams for invoke ids LiteLLM maps to no provider, such as DeepSeek V3 and GLM 4.7, write no spend row

How it solves it:

  • The Bedrock stream decoder now parses OpenAI-shaped chunks
  • Usage comes from the chunk, else from Bedrock's end-of-stream token counts
  • Kimi on the chat route now goes through the same parser
  • A pass-through stream that ends in an abort or error chunk keeps its spend row
  • Invoke ids LiteLLM maps to no provider get the default decoder and bill Bedrock's end-of-stream counts
Decisions and alternatives
  1. When a chunk carries its own usage (the caller sent stream_options.include_usage), it wins over the amazon-bedrock-invocationMetrics trailer. The two agreed on every live call below. The alternative was to always read the trailer
  2. The trailer's cache fields are not mapped, because Bedrock sent none. Repeating a 740-token prompt live against Qwen3, Kimi K2.5 and Kimi K2 Thinking returned only input and output token counts and latencies. The alternative was to copy the Anthropic decoder's cache mapping, which we can add once a model sends them
  3. Kimi on bedrock/invoke/moonshotai... already had its own OpenAI chunk decoder for the chat route. It now shares the new helper, so a Kimi chat stream without stream_options.include_usage logs Bedrock's own counts where main logged LiteLLM's estimate, 35 prompt tokens against 15 in the re-run below. The proxy adds include_usage by default, so this shows only with general_settings.always_include_stream_usage: false or through the SDK. With it, the chat, messages, and responses rows match main below
  4. Mistral Large 2 streams choices[].message instead of choices[].delta, so its chat stream text is still empty, same as main. That stream now writes a spend row with Bedrock's token counts, where main wrote none. Bedrock charges for those tokens, so the row matches the AWS bill. The alternatives were to also read choices[].message so the text arrives, which is a separate bug with its own fix, or to leave the row unbilled until then, which hides a charge AWS still makes
  5. fix(bedrock): keep llama's token counts and stop_reason on native streams #51533 (Llama only) and fix(proxy): propagate Invoke cache-token counts to usage #34498 (Anthropic only) touch the same decoder but neither covers OpenAI-shaped chunks, so this builds on neither
  6. lts/1.108.x has the same gap, so the twin PR on that line carries the same change
  7. When Bedrock's last event has no token counts, usage falls back to LiteLLM's own count, as on main, and the stream still finishes. The alternative was to fail the stream, which would cut off the caller's answer over a field used only for billing
  8. The change also reaches /v1/chat/completions for OpenAI-shaped models on bedrock/invoke/, such as gpt-oss. Their chunks now go through the same parser and end-of-stream check as other OpenAI-shaped streams, so a stream that ends without a finish_reason fails and can trigger a fallback, where main ended it as stop with empty text. Live, gpt-oss on bedrock/invoke/ answers 400 on both legs before any chunk arrives, because Bedrock rejects the request's model field, a separate bug. The alternative was to end such a stream quietly, which returns a cut-off answer as complete
  9. When an OpenAI-shaped pass-through stream ends in an abort finish or an error chunk, the collector skips that chunk and still writes the spend row from the chunks before it, counted by LiteLLM. Main wrote a prompt-only row for such a stream, and without this the new parser's error would drop the row. No live stream sent either chunk, so unit tests cover it. The alternatives were to drop the row, which hides a charge, or to read Bedrock's counts off the failed chunk, which no stream has shown carrying them
  10. Invoke ids that LiteLLM maps to no provider, such as DeepSeek V3, GLM 4.7, MiniMax M2 and Gemma 3, now get the default Bedrock decoder in the pass-through, where main skipped spend tracking for them. The 21 such ids that answered live below all stream OpenAI-shaped chunks and now bill Bedrock's counts. The alternative was to keep skipping them until each id is added to the provider map
  11. An unmapped id can also stream another shape, such as an application inference profile ARN in front of Claude or Llama. The default decoder keeps no usage for those events, so for unmapped ids the pass-through bills Bedrock's end-of-stream counts unless the chunk already carries usage. Mapped ids keep their own decoder's usage, since Nova's carries cache counts that Bedrock's last event lacks. The alternatives were LiteLLM's own count, which logs 0 completion tokens for a Claude stream, or picking a decoder from the stream's first chunk, which adds a second shape detection path for one config shape

User Flow

Before: a developer streaming Qwen3 or Mistral Large 3 through the Bedrock pass-through gets the full answer, but the proxy bills only the prompt

  1. They send POST https://litellm-domain/bedrock/model/bedrock-qwen3-32b/invoke-with-response-stream with a chat messages body
  2. The stream returns the full answer, and its last event carries Bedrock's counts, 20 tokens in and 15 out
  3. GET https://litellm-domain/spend/logs?request_id= shows 15 prompt tokens, 0 completion tokens and $0.00000225, while the same request sent to /invoke without streaming logs 20 and 15 at $0.000012

After: the same stream bills both sides, matching the non-streamed call

  1. They send the same POST https://litellm-domain/bedrock/model/bedrock-qwen3-32b/invoke-with-response-stream
  2. The stream returns the same answer with the same counts in its last event
  3. GET https://litellm-domain/spend/logs?request_id= shows 20 prompt tokens, 15 completion tokens and $0.000012

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

Before ran at the merge base 030bfe2 and After at fbc852b, with later re-runs below. Each leg is one proxy with 2 workers and its own Postgres database, calling real Bedrock in us-west-2. The legs ran without Redis, since nothing the change touches is shared across workers.

Last updated: c865e9c. QA ran on fbc852b. Changes since QA: a count fallback, kept rows for aborted streams, Bedrock's counts for unmapped invoke ids, and two merges of main. Verification: the re-runs below

Shared setup: config.yaml, proxy command, env vars and shell helpers

Every case sends Count from 1 to 5. with max_tokens 40, and each spend lookup uses the request's own x-litellm-call-id response header, saved with -D <case>

Env vars: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN, LITELLM_MASTER_KEY, DATABASE_URL

model_list:
  - model_name: bedrock-qwen3-32b
    litellm_params:
      model: bedrock/qwen.qwen3-32b-v1:0
      aws_region_name: us-west-2
  - model_name: bedrock-mistral-large-3
    litellm_params:
      model: bedrock/mistral.mistral-large-3-675b-instruct
      aws_region_name: us-west-2
  - model_name: bedrock-invoke-qwen3-32b
    litellm_params:
      model: bedrock/invoke/qwen.qwen3-32b-v1:0
      aws_region_name: us-west-2
  - model_name: bedrock-invoke-mistral-large-3
    litellm_params:
      model: bedrock/invoke/mistral.mistral-large-3-675b-instruct
      aws_region_name: us-west-2
  - model_name: bedrock-kimi-k2-5
    litellm_params:
      model: bedrock/invoke/moonshotai.kimi-k2.5
      aws_region_name: us-west-2
general_settings:
  master_key: os.environ/LITELLM_MASTER_KEY
  database_url: os.environ/DATABASE_URL
python litellm/proxy/proxy_cli.py --config config.yaml --port $PORT --num_workers 2
decode_events() { strings | grep -o '"bytes":"[^"]*"' | cut -d'"' -f4 | while read -r b; do print -r -- "$b" | base64 -d | jq -c '{content: [.choices[]?.delta.content], finish: [.choices[]?.finish_reason], usage, metrics: ."amazon-bedrock-invocationMetrics"}'; done; }
sse() { grep '^data: {' | sed 's/^data: //' | jq -c "$1"; }

Spend rows read back through GET /spend/logs?request_id=, as prompt / completion tokens and spend

Case Before 030bfe2 After fbc852b
Qwen3 stream 15 / 0, $0.00000225 20 / 15, $0.000012
Qwen3 stream, include_usage 15 / 0, $0.00000225 20 / 15, $0.000012
Qwen3 /invoke 20 / 15, $0.000012 20 / 15, $0.000012
Mistral Large 3 stream 15 / 0, $0.0000075 11 / 14, $0.0000265
Mistral Large 3 stream, include_usage 15 / 0, $0.0000075 11 / 18, $0.0000325
Mistral Large 3 /invoke 11 / 21, $0.000037 11 / 14, $0.0000265
Kimi chat stream 35 / 14, $0.000063 35 / 14, $0.000063
Kimi /v1/messages stream 35 / 14, $0.000063 35 / 10, $0.000051
Kimi /v1/responses stream 35 / 14, $0.000063 35 / 14, $0.000063
Qwen3 converse chat stream 20 / 15, $0.000012 20 / 20, $0.000015
Qwen3 invoke chat stream 400, 15 / 0, $0 400, 15 / 0, $0
Mistral 3 invoke chat stream 400, 15 / 0, $0 400, 15 / 0, $0

Each After row matches the counts in Bedrock's last event. Mistral and Kimi answers vary per run, so their counts move between legs

A direct Bedrock call reports the same counts as the After rows

A direct boto3 call to Bedrock with the same body, outside the proxy, reports 20 / 15 for Qwen3 and 11 / 14 for Mistral Large 3

direct_bedrock.py, run with the same AWS env vars as the proxy

import json
import sys

import boto3

client = boto3.client("bedrock-runtime", region_name="us-west-2")
body = {"messages": [{"role": "user", "content": "Count from 1 to 5."}], "max_tokens": 40}
for model_id in sys.argv[1:]:
    print(f"## {model_id}")
    stream = client.invoke_model_with_response_stream(modelId=model_id, body=json.dumps(body))
    for event in stream["body"]:
        chunk = json.loads(event["chunk"]["bytes"])
        metrics = chunk.get("amazon-bedrock-invocationMetrics")
        print(json.dumps({
            "content": [c.get("delta", {}).get("content") for c in chunk.get("choices", [])],
            "finish": [c.get("finish_reason") for c in chunk.get("choices", [])],
            "usage": chunk.get("usage"),
            "metrics": metrics and {k: metrics[k] for k in ("inputTokenCount", "outputTokenCount")},
        }))
$ ~/Development/litellm-worktrees/work-a5f6c120-merged/.venv/bin/python direct_bedrock.py qwen.qwen3-32b-v1:0 mistral.mistral-large-3-675b-instruct
## qwen.qwen3-32b-v1:0
{"content": [""], "finish": [null], "usage": null, "metrics": null}
{"content": ["1, 2, 3, 4, 5."], "finish": ["stop"], "usage": null, "metrics": {"inputTokenCount": 20, "outputTokenCount": 15}}
## mistral.mistral-large-3-675b-instruct
{"content": [""], "finish": [null], "usage": null, "metrics": null}
{"content": ["Here you go!\n\n1\n2\n3\n4\n5"], "finish": ["stop"], "usage": null, "metrics": {"inputTokenCount": 11, "outputTokenCount": 14}}

Before (030bfe2)

Qwen3 stream

  1. Stream the reply through the pass-through
$ curl -sN -D bedrock-qwen3-32b-stream http://127.0.0.1:30122/bedrock/model/bedrock-qwen3-32b/invoke-with-response-stream -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40}' | decode_events
{"content":[""],"finish":[null],"usage":null,"metrics":null}
{"content":["1, 2, 3, 4, 5."],"finish":["stop"],"usage":null,"metrics":{"inputTokenCount":20,"outputTokenCount":15,"invocationLatency":388,"firstByteLatency":388}}
  1. Read its spend row
$ curl -s 'http://127.0.0.1:30122/spend/logs?request_id=ddeddefc-5340-4c93-b302-6c821ccabc80' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "bedrock-qwen3-32b-stream", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"bedrock-qwen3-32b-stream","model":"bedrock/qwen.qwen3-32b-v1:0","call_type":"allm_passthrough_route","spend":0.00000225,"prompt_tokens":15,"completion_tokens":0}

Other cases

Before: every other case's commands and output
$ curl -sN -D bedrock-qwen3-32b-stream-usage http://127.0.0.1:30122/bedrock/model/bedrock-qwen3-32b/invoke-with-response-stream -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40,"stream_options":{"include_usage":true}}' | decode_events
{"content":[""],"finish":[null],"usage":null,"metrics":null}
{"content":["1, 2, 3, 4, 5."],"finish":["stop"],"usage":null,"metrics":null}
{"content":[],"finish":[],"usage":{"completion_tokens":15,"prompt_tokens":20,"total_tokens":35},"metrics":{"inputTokenCount":20,"outputTokenCount":15,"invocationLatency":274,"firstByteLatency":274}}

$ curl -s -D bedrock-qwen3-32b-invoke http://127.0.0.1:30122/bedrock/model/bedrock-qwen3-32b/invoke -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40}' | jq -c '{content: .choices[0].message.content, usage}'
{"content":"1, 2, 3, 4, 5.","usage":{"completion_tokens":15,"prompt_tokens":20,"total_tokens":35}}

$ curl -sN -D bedrock-mistral-large-3-stream http://127.0.0.1:30122/bedrock/model/bedrock-mistral-large-3/invoke-with-response-stream -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40}' | decode_events
{"content":[""],"finish":[null],"usage":null,"metrics":null}
{"content":["Sure, here you go:\n\n1, 2, 3"],"finish":[null],"usage":null,"metrics":null}
{"content":[", 4, 5"],"finish":["stop"],"usage":null,"metrics":{"inputTokenCount":11,"outputTokenCount":20,"invocationLatency":533,"firstByteLatency":416}}

$ curl -sN -D bedrock-mistral-large-3-stream-usage http://127.0.0.1:30122/bedrock/model/bedrock-mistral-large-3/invoke-with-response-stream -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40,"stream_options":{"include_usage":true}}' | decode_events
{"content":[""],"finish":[null],"usage":null,"metrics":null}
{"content":["Here you go:\n\n1, 2, 3, 4, 5"],"finish":["stop"],"usage":null,"metrics":null}
{"content":[],"finish":[],"usage":{"completion_tokens":18,"prompt_tokens":11,"total_tokens":29},"metrics":{"inputTokenCount":11,"outputTokenCount":18,"invocationLatency":253,"firstByteLatency":253}}

$ curl -s -D bedrock-mistral-large-3-invoke http://127.0.0.1:30122/bedrock/model/bedrock-mistral-large-3/invoke -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40}' | jq -c '{content: .choices[0].message.content, usage}'
{"content":"Here's the count from 1 to 5:\n\n1\n2\n3\n4\n5","usage":{"completion_tokens":21,"prompt_tokens":11,"total_tokens":32}}

$ curl -sN -D bedrock-kimi-k2-5-chat http://127.0.0.1:30122/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"bedrock-kimi-k2-5","stream":true,"stream_options":{"include_usage":true},"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40}' | sse '{content: [.choices[]?.delta.content], finish: [.choices[]?.finish_reason], usage}'
{"content":[""],"finish":[null],"usage":null}
{"content":[" 1, 2, 3, 4, 5"],"finish":[null],"usage":null}
{"content":[null],"finish":["stop"],"usage":null}
{"content":[],"finish":[],"usage":{"completion_tokens":14,"prompt_tokens":35,"total_tokens":49,"completion_tokens_details":{"reasoning_tokens":0,"text_tokens":14},"cost":0.000063}}

$ curl -sN -D bedrock-invoke-qwen3-32b-chat http://127.0.0.1:30122/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"bedrock-invoke-qwen3-32b","stream":true,"stream_options":{"include_usage":true},"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40}' | sse '{content: [.choices[]?.delta.content], finish: [.choices[]?.finish_reason], usage}'

$ curl -sN -D bedrock-invoke-mistral-large-3-chat http://127.0.0.1:30122/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"bedrock-invoke-mistral-large-3","stream":true,"stream_options":{"include_usage":true},"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40}' | sse '{content: [.choices[]?.delta.content], finish: [.choices[]?.finish_reason], usage}'

$ curl -sN -D bedrock-qwen3-32b-chat http://127.0.0.1:30122/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"bedrock-qwen3-32b","stream":true,"stream_options":{"include_usage":true},"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40}' | sse '{content: [.choices[]?.delta.content], finish: [.choices[]?.finish_reason], usage}'
{"content":[""],"finish":[null],"usage":null}
{"content":["1, 2, 3, 4, 5."],"finish":[null],"usage":null}
{"content":[null],"finish":["stop"],"usage":null}
{"content":[],"finish":[],"usage":{"completion_tokens":15,"prompt_tokens":20,"total_tokens":35,"completion_tokens_details":{"reasoning_tokens":0,"text_tokens":15},"prompt_tokens_details":{"cached_tokens":0,"text_tokens":20,"cache_write_tokens":0,"cache_creation_tokens":0},"cost":0.000012,"cache_creation_input_tokens":0,"cache_read_input_tokens":0}}

$ curl -sN -D kimi-messages http://127.0.0.1:30122/v1/messages -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"bedrock-kimi-k2-5","stream":true,"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40}' | sse 'select(.type=="content_block_delta" or .type=="message_delta") | {type, text: .delta.text, usage}'
{"type":"content_block_delta","text":" 1, 2, 3, 4, 5","usage":null}
{"type":"message_delta","text":null,"usage":{"input_tokens":35,"output_tokens":14}}

$ curl -sN -D kimi-responses http://127.0.0.1:30122/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"bedrock-kimi-k2-5","stream":true,"input":"Count from 1 to 5.","max_output_tokens":40}' | sse 'select(.type=="response.completed") | {text: .response.output[-1].content[0].text, usage: .response.usage}'
{"text":" 1, 2, 3, 4, 5","usage":{"input_tokens":35,"output_tokens":14,"output_tokens_details":{"reasoning_tokens":0,"text_tokens":14},"total_tokens":49,"cost":0.000063}}

$ curl -s 'http://127.0.0.1:30122/spend/logs?request_id=25379de8-70da-421c-86c1-1e1925dfb6b5' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "bedrock-qwen3-32b-stream-usage", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"bedrock-qwen3-32b-stream-usage","model":"bedrock/qwen.qwen3-32b-v1:0","call_type":"allm_passthrough_route","spend":0.00000225,"prompt_tokens":15,"completion_tokens":0}

$ curl -s 'http://127.0.0.1:30122/spend/logs?request_id=df161df1-f5f5-47a2-ae05-dd0d636dbd33' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "bedrock-qwen3-32b-invoke", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"bedrock-qwen3-32b-invoke","model":"bedrock/qwen.qwen3-32b-v1:0","call_type":"allm_passthrough_route","spend":0.000012,"prompt_tokens":20,"completion_tokens":15}

$ curl -s 'http://127.0.0.1:30122/spend/logs?request_id=5507a342-ed0c-46b6-a4a3-1b25510f81ac' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "bedrock-mistral-large-3-stream", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"bedrock-mistral-large-3-stream","model":"bedrock/mistral.mistral-large-3-675b-instruct","call_type":"allm_passthrough_route","spend":0.000007499999999999999,"prompt_tokens":15,"completion_tokens":0}

$ curl -s 'http://127.0.0.1:30122/spend/logs?request_id=01e861b2-8cee-4416-af21-22aa9ceaaa73' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "bedrock-mistral-large-3-stream-usage", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"bedrock-mistral-large-3-stream-usage","model":"bedrock/mistral.mistral-large-3-675b-instruct","call_type":"allm_passthrough_route","spend":0.000007499999999999999,"prompt_tokens":15,"completion_tokens":0}

$ curl -s 'http://127.0.0.1:30122/spend/logs?request_id=93c97f79-e5d5-4a1d-9534-b7184d3e77bf' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "bedrock-mistral-large-3-invoke", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"bedrock-mistral-large-3-invoke","model":"bedrock/mistral.mistral-large-3-675b-instruct","call_type":"allm_passthrough_route","spend":0.000037,"prompt_tokens":11,"completion_tokens":21}

$ curl -s 'http://127.0.0.1:30122/spend/logs?request_id=dab32772-a465-4341-b401-ef594fa53bdf' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "bedrock-kimi-k2-5-chat", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"bedrock-kimi-k2-5-chat","model":"bedrock/invoke/moonshotai.kimi-k2.5","call_type":"acompletion","spend":0.000063,"prompt_tokens":35,"completion_tokens":14}

$ curl -s 'http://127.0.0.1:30122/spend/logs?request_id=7b179a98-b518-412e-97d7-e560e7e82fbf' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "bedrock-invoke-qwen3-32b-chat", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"bedrock-invoke-qwen3-32b-chat","model":"bedrock/invoke/qwen.qwen3-32b-v1:0","call_type":"acompletion","spend":0.0,"prompt_tokens":15,"completion_tokens":0}

$ curl -s 'http://127.0.0.1:30122/spend/logs?request_id=5c31919a-2aa1-4942-a9d3-696ff22580d7' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "bedrock-invoke-mistral-large-3-chat", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"bedrock-invoke-mistral-large-3-chat","model":"bedrock/invoke/mistral.mistral-large-3-675b-instruct","call_type":"acompletion","spend":0.0,"prompt_tokens":15,"completion_tokens":0}

$ curl -s 'http://127.0.0.1:30122/spend/logs?request_id=dd87952a-02f9-4177-a169-8aabca94dd82' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "bedrock-qwen3-32b-chat", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"bedrock-qwen3-32b-chat","model":"bedrock/qwen.qwen3-32b-v1:0","call_type":"acompletion","spend":0.000012,"prompt_tokens":20,"completion_tokens":15}

$ curl -s 'http://127.0.0.1:30122/spend/logs?request_id=960993bf-68e5-42b0-9293-6c36af2ee018' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "kimi-messages", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"kimi-messages","model":"bedrock/invoke/moonshotai.kimi-k2.5","call_type":"anthropic_messages","spend":0.000063,"prompt_tokens":35,"completion_tokens":14}

$ curl -s 'http://127.0.0.1:30122/spend/logs?request_id=d9868b7c-a05f-4c6b-8fc2-a828eba234da' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "kimi-responses", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"kimi-responses","model":"bedrock/invoke/moonshotai.kimi-k2.5","call_type":"aresponses","spend":0.000063,"prompt_tokens":35,"completion_tokens":14}

$ head -1 bedrock-invoke-qwen3-32b-chat
HTTP/1.1 400 Bad Request

$ head -1 bedrock-invoke-mistral-large-3-chat
HTTP/1.1 400 Bad Request

After (fbc852b)

Qwen3 stream

  1. Stream the reply through the pass-through
$ curl -sN -D bedrock-qwen3-32b-stream http://127.0.0.1:28749/bedrock/model/bedrock-qwen3-32b/invoke-with-response-stream -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40}' | decode_events
{"content":[""],"finish":[null],"usage":null,"metrics":null}
{"content":["1, 2, 3, 4, 5."],"finish":["stop"],"usage":null,"metrics":{"inputTokenCount":20,"outputTokenCount":15,"invocationLatency":251,"firstByteLatency":251}}
  1. Read its spend row
$ curl -s 'http://127.0.0.1:28749/spend/logs?request_id=78c398b6-4777-432c-9c5a-2d471ba7d523' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "bedrock-qwen3-32b-stream", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"bedrock-qwen3-32b-stream","model":"bedrock/qwen.qwen3-32b-v1:0","call_type":"allm_passthrough_route","spend":0.000012,"prompt_tokens":20,"completion_tokens":15}

Other cases

After: every other case's commands and output
$ curl -sN -D bedrock-qwen3-32b-stream-usage http://127.0.0.1:28749/bedrock/model/bedrock-qwen3-32b/invoke-with-response-stream -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40,"stream_options":{"include_usage":true}}' | decode_events
{"content":[""],"finish":[null],"usage":null,"metrics":null}
{"content":["1, 2, 3, 4, 5."],"finish":["stop"],"usage":null,"metrics":null}
{"content":[],"finish":[],"usage":{"completion_tokens":15,"prompt_tokens":20,"total_tokens":35},"metrics":{"inputTokenCount":20,"outputTokenCount":15,"invocationLatency":368,"firstByteLatency":367}}

$ curl -s -D bedrock-qwen3-32b-invoke http://127.0.0.1:28749/bedrock/model/bedrock-qwen3-32b/invoke -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40}' | jq -c '{content: .choices[0].message.content, usage}'
{"content":"1, 2, 3, 4, 5.","usage":{"completion_tokens":15,"prompt_tokens":20,"total_tokens":35}}

$ curl -sN -D bedrock-mistral-large-3-stream http://127.0.0.1:28749/bedrock/model/bedrock-mistral-large-3/invoke-with-response-stream -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40}' | decode_events
{"content":[""],"finish":[null],"usage":null,"metrics":null}
{"content":["1, 2, 3, 4, 5"],"finish":["stop"],"usage":null,"metrics":{"inputTokenCount":11,"outputTokenCount":14,"invocationLatency":193,"firstByteLatency":193}}

$ curl -sN -D bedrock-mistral-large-3-stream-usage http://127.0.0.1:28749/bedrock/model/bedrock-mistral-large-3/invoke-with-response-stream -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40,"stream_options":{"include_usage":true}}' | decode_events
{"content":[""],"finish":[null],"usage":null,"metrics":null}
{"content":["Here you go:\n\n1, 2, 3, 4, 5"],"finish":["stop"],"usage":null,"metrics":null}
{"content":[],"finish":[],"usage":{"completion_tokens":18,"prompt_tokens":11,"total_tokens":29},"metrics":{"inputTokenCount":11,"outputTokenCount":18,"invocationLatency":277,"firstByteLatency":277}}

$ curl -s -D bedrock-mistral-large-3-invoke http://127.0.0.1:28749/bedrock/model/bedrock-mistral-large-3/invoke -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40}' | jq -c '{content: .choices[0].message.content, usage}'
{"content":"Here you go:\n\n1\n2\n3\n4\n5","usage":{"completion_tokens":14,"prompt_tokens":11,"total_tokens":25}}

$ curl -sN -D bedrock-kimi-k2-5-chat http://127.0.0.1:28749/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"bedrock-kimi-k2-5","stream":true,"stream_options":{"include_usage":true},"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40}' | sse '{content: [.choices[]?.delta.content], finish: [.choices[]?.finish_reason], usage}'
{"content":[""],"finish":[null],"usage":null}
{"content":[" 1, 2, 3, 4, 5"],"finish":[null],"usage":null}
{"content":[null],"finish":["stop"],"usage":null}
{"content":[],"finish":[],"usage":{"completion_tokens":14,"prompt_tokens":35,"total_tokens":49,"completion_tokens_details":{"reasoning_tokens":0,"text_tokens":14},"cost":0.000063}}

$ curl -sN -D bedrock-invoke-qwen3-32b-chat http://127.0.0.1:28749/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"bedrock-invoke-qwen3-32b","stream":true,"stream_options":{"include_usage":true},"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40}' | sse '{content: [.choices[]?.delta.content], finish: [.choices[]?.finish_reason], usage}'

$ curl -sN -D bedrock-invoke-mistral-large-3-chat http://127.0.0.1:28749/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"bedrock-invoke-mistral-large-3","stream":true,"stream_options":{"include_usage":true},"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40}' | sse '{content: [.choices[]?.delta.content], finish: [.choices[]?.finish_reason], usage}'

$ curl -sN -D bedrock-qwen3-32b-chat http://127.0.0.1:28749/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"bedrock-qwen3-32b","stream":true,"stream_options":{"include_usage":true},"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40}' | sse '{content: [.choices[]?.delta.content], finish: [.choices[]?.finish_reason], usage}'
{"content":[""],"finish":[null],"usage":null}
{"content":["1. One  \n2. Two  \n3. Three  \n4. Four  \n5. Five"],"finish":[null],"usage":null}
{"content":[null],"finish":["stop"],"usage":null}
{"content":[],"finish":[],"usage":{"completion_tokens":20,"prompt_tokens":20,"total_tokens":40,"completion_tokens_details":{"reasoning_tokens":0,"text_tokens":20},"prompt_tokens_details":{"cached_tokens":0,"text_tokens":20,"cache_write_tokens":0,"cache_creation_tokens":0},"cost":0.000015,"cache_creation_input_tokens":0,"cache_read_input_tokens":0}}

$ curl -sN -D kimi-messages http://127.0.0.1:28749/v1/messages -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"bedrock-kimi-k2-5","stream":true,"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40}' | sse 'select(.type=="content_block_delta" or .type=="message_delta") | {type, text: .delta.text, usage}'
{"type":"content_block_delta","text":" 1\n2\n3\n4\n5","usage":null}
{"type":"message_delta","text":null,"usage":{"input_tokens":35,"output_tokens":10}}

$ curl -sN -D kimi-responses http://127.0.0.1:28749/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"bedrock-kimi-k2-5","stream":true,"input":"Count from 1 to 5.","max_output_tokens":40}' | sse 'select(.type=="response.completed") | {text: .response.output[-1].content[0].text, usage: .response.usage}'
{"text":" 1, 2, 3, 4, 5","usage":{"input_tokens":35,"input_tokens_details":{"audio_tokens":0,"cached_tokens":32},"output_tokens":14,"output_tokens_details":{"reasoning_tokens":0,"text_tokens":14},"total_tokens":49,"cost":0.000063}}

$ curl -s 'http://127.0.0.1:28749/spend/logs?request_id=58694f27-16f7-4db2-864b-811c6741465f' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "bedrock-qwen3-32b-stream-usage", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"bedrock-qwen3-32b-stream-usage","model":"bedrock/qwen.qwen3-32b-v1:0","call_type":"allm_passthrough_route","spend":0.000012,"prompt_tokens":20,"completion_tokens":15}

$ curl -s 'http://127.0.0.1:28749/spend/logs?request_id=b97d04f9-1df1-425d-9cd7-392070ab004f' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "bedrock-qwen3-32b-invoke", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"bedrock-qwen3-32b-invoke","model":"bedrock/qwen.qwen3-32b-v1:0","call_type":"allm_passthrough_route","spend":0.000012,"prompt_tokens":20,"completion_tokens":15}

$ curl -s 'http://127.0.0.1:28749/spend/logs?request_id=54b1e759-a585-4dfd-b3eb-bea32a8e18e6' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "bedrock-mistral-large-3-stream", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"bedrock-mistral-large-3-stream","model":"bedrock/mistral.mistral-large-3-675b-instruct","call_type":"allm_passthrough_route","spend":0.0000265,"prompt_tokens":11,"completion_tokens":14}

$ curl -s 'http://127.0.0.1:28749/spend/logs?request_id=cd5b3863-07d0-4a8e-9052-3a34d659ccac' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "bedrock-mistral-large-3-stream-usage", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"bedrock-mistral-large-3-stream-usage","model":"bedrock/mistral.mistral-large-3-675b-instruct","call_type":"allm_passthrough_route","spend":0.0000325,"prompt_tokens":11,"completion_tokens":18}

$ curl -s 'http://127.0.0.1:28749/spend/logs?request_id=24e4a870-9c8e-4513-914d-8518f9600f57' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "bedrock-mistral-large-3-invoke", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"bedrock-mistral-large-3-invoke","model":"bedrock/mistral.mistral-large-3-675b-instruct","call_type":"allm_passthrough_route","spend":0.0000265,"prompt_tokens":11,"completion_tokens":14}

$ curl -s 'http://127.0.0.1:28749/spend/logs?request_id=aeb940d3-98c8-46d1-96c9-3eb7892dfde7' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "bedrock-kimi-k2-5-chat", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"bedrock-kimi-k2-5-chat","model":"bedrock/invoke/moonshotai.kimi-k2.5","call_type":"acompletion","spend":0.000063,"prompt_tokens":35,"completion_tokens":14}

$ curl -s 'http://127.0.0.1:28749/spend/logs?request_id=eb23762f-f339-4c9e-818e-3f9897b5991b' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "bedrock-invoke-qwen3-32b-chat", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"bedrock-invoke-qwen3-32b-chat","model":"bedrock/invoke/qwen.qwen3-32b-v1:0","call_type":"acompletion","spend":0.0,"prompt_tokens":15,"completion_tokens":0}

$ curl -s 'http://127.0.0.1:28749/spend/logs?request_id=9f23489d-8a75-404e-baed-6542b63f7afb' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "bedrock-invoke-mistral-large-3-chat", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"bedrock-invoke-mistral-large-3-chat","model":"bedrock/invoke/mistral.mistral-large-3-675b-instruct","call_type":"acompletion","spend":0.0,"prompt_tokens":15,"completion_tokens":0}

$ curl -s 'http://127.0.0.1:28749/spend/logs?request_id=b8448a0f-4baf-4dd7-9f42-81aec9caec39' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "bedrock-qwen3-32b-chat", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"bedrock-qwen3-32b-chat","model":"bedrock/qwen.qwen3-32b-v1:0","call_type":"acompletion","spend":0.000015,"prompt_tokens":20,"completion_tokens":20}

$ curl -s 'http://127.0.0.1:28749/spend/logs?request_id=6d270468-2592-4f24-b609-d396ceae28ba' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "kimi-messages", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"kimi-messages","model":"bedrock/invoke/moonshotai.kimi-k2.5","call_type":"anthropic_messages","spend":0.000051,"prompt_tokens":35,"completion_tokens":10}

$ curl -s 'http://127.0.0.1:28749/spend/logs?request_id=63de7cee-0611-4e79-a0ea-ad5bf43d50d3' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "kimi-responses", model, call_type, spend, prompt_tokens, completion_tokens}'
{"case":"kimi-responses","model":"bedrock/invoke/moonshotai.kimi-k2.5","call_type":"aresponses","spend":0.000063,"prompt_tokens":35,"completion_tokens":14}

$ head -1 bedrock-invoke-qwen3-32b-chat
HTTP/1.1 400 Bad Request

$ head -1 bedrock-invoke-mistral-large-3-chat
HTTP/1.1 400 Bad Request

Other Bedrock models that stream OpenAI-shaped chunks

Spend rows for the other models

The decoder change reaches every Bedrock invoke model whose stream sends OpenAI-shaped chunks, so /live-pr-risk ran six more models through the same two proxies. The pass-through streams send the same messages body as above, Mistral 7B sends its prompt body, and the two chat streams go through /v1/chat/completions with include_usage. The full commands and output for both legs are in this gist

Case Before 030bfe2 After fbc852b After stream's counts
gpt-oss-120b stream 15 / 0, $0.00000225 75 / 40, $0.00003525 75 / 40
Mistral Large 2 stream 15 / 0, $0.00003 11 / 29, $0.000196 11 / 29
Magistral Small stream 15 / 0, $0.0000075 11 / 26, $0.0000445 11 / 26
Kimi K2 Thinking stream 15 / 0, $0.000009 15 / 40, $0.000109 15 / 40
Qwen3 Coder 30B stream 15 / 0, $0.00000225 16 / 14, $0.0000108 16 / 14
Mistral 7B stream 0 / 38, $0.0000076 0 / 34, $0.0000068 17 / 37
Mistral Large 2 invoke chat stream no spend row 20 / 15, $0.00013 20 / 15
Mistral 7B invoke chat stream 15 / 38, $0.00000985 15 / 38, $0.00000985 15 / 38

Mistral 7B streams the older outputs shape, which this PR does not touch, and its rows match between legs apart from answer length

Re-run at a32e9b0

Kimi and three-turn rows for both legs

Every After row matches the counts in that stream's last event, across three-turn Qwen3 and Mistral Large 3 conversations. Before bills only LiteLLM's estimate of the prompt. These paths are unchanged since a32e9b0

Both legs used general_settings.always_include_stream_usage: false, so the Kimi chat stream's row has to come from Bedrock's last event. Each conversation turn resends the whole history through the pass-through. The full commands and output for both legs are in this gist

Case Before 551a18b After a32e9b0
Kimi chat stream, no include_usage 15 / 14, $0.000051 35 / 14, $0.000063
Kimi chat, not streamed 35 / 14, $0.000063 35 / 14, $0.000063
Qwen3 stream, turn 1 15 / 0, $0.00000225 20 / 14, $0.0000114
Qwen3 stream, turn 2 46 / 0, $0.0000069 53 / 15, $0.00001695
Qwen3 stream, turn 3 77 / 0, $0.00001155 88 / 19, $0.0000246
Mistral Large 3 stream, turn 1 15 / 0, $0.0000075 11 / 26, $0.0000445
Mistral Large 3 stream, turn 2 45 / 0, $0.0000225 49 / 28, $0.0000665
Mistral Large 3 stream, turn 3 75 / 0, $0.0000375 90 / 33, $0.0000945
gpt-oss invoke chat stream 400, 15 / 0, $0 400, 15 / 0, $0
gpt-oss invoke chat 400, 15 / 0, $0 400, 15 / 0, $0

Re-run at c865e9c

Qwen3 and Mistral Large 3 still bill their streams' counts, and 21 more unmapped invoke ids now bill Bedrock's counts where main wrote no row

Rows for both legs, and Qwen3 commands and output
Model Before 551a18b After c865e9c Stream's counts
Qwen3 32B 15 / 0, $0.00000225 20 / 20, $0.000015 20 / 20
Mistral Large 3 15 / 0, $0.0000075 11 / 22, $0.0000385 11 / 22
21 unmapped ids no spend row Bedrock's counts rows below

A Claude stream behind an application inference profile ARN answered 403 on both legs, since the QA role cannot invoke one, so the unit test covers that shape

Each leg is one proxy with 2 workers on its own Postgres database, set up as above with the sweep models in config.yaml. All 37 Bedrock chat ids in the cost map that LiteLLM maps to no provider were tried, except the 6 us-gov. ids, which need GovCloud. The config, both case scripts and every row for both legs are in this gist

Id Before 551a18b After c865e9c Stream's counts
deepseek.v3-v1:0 no spend row 12 / 15, $0.00003216 12 / 15
deepseek.v3.2 no spend row 12 / 14, $0.00003334 12 / 14
global.xai.grok-4.6 no spend row 39 / 40, $0.000318 39 / 40
global.xai.grok-4.7 no spend row 39 / 40, $0.000318 39 / 40
google.gemma-3-12b-it no spend row 17 / 17, $0.00000646 17 / 17
google.gemma-3-27b-it no spend row 17 / 22, $0.00001227 17 / 22
google.gemma-3-4b-it no spend row 17 / 16, $0.00000196 17 / 16
minimax.minimax-m2 no spend row 30 / 40, $0.000057 30 / 40
minimax.minimax-m2.1 no spend row 46 / 40, $0.0000618 46 / 40
minimax.minimax-m2.5 no spend row 46 / 40, $0.0000618 46 / 40
nvidia.nemotron-nano-12b-v2 no spend row 23 / 40, $0.0000286 23 / 40
nvidia.nemotron-nano-3-30b no spend row 24 / 19, $0.000006 24 / 19
nvidia.nemotron-nano-9b-v2 no spend row 20 / 40, $0.0000104 20 / 40
nvidia.nemotron-super-3-120b no spend row 24 / 15, $0.00001335 24 / 15
us.deepseek.r1-v1:0 no spend row 11 / 40, $0.00023085 11 / 40
us.xai.grok-4.6 no spend row 12 / 40, $0.0002904 12 / 40
us.xai.grok-4.7 no spend row 12 / 40, $0.0002904 12 / 40
writer.palmyra-vision-7b no spend row 12 / 40, $0.0000258 12 / 40
zai.glm-4.7 no spend row 13 / 14, $0.0000386 13 / 14
zai.glm-4.7-flash no spend row 13 / 14, $0.00000651 13 / 14
zai.glm-5 no spend row 13 / 15, $0.000061 13 / 15

Bedrock rejected the other 10 ids in this account on both legs, so they have no counts to bill: claude-sonnet-4-5-20250929-v1:0 (400), deepseek.r1-v1:0 (400), eu.deepseek.v3.2 (400), global.zai.glm-5.3 (403), us.deepseek.v3.2 (400), us.writer.palmyra-x4-v1:0 (404), us.writer.palmyra-x5-v1:0 (404), us.zai.glm-5.3 (403), writer.palmyra-x4-v1:0 (400), writer.palmyra-x5-v1:0 (400)

Ids that keep their own decoder bill the same on both legs, which decision 11 relies on. Nova's last event reports 0 / 0 in Bedrock's invocation metrics, so only its own usage is right. The commit only reads the response stream, so the request sent to Bedrock is unchanged, and its invoke_handler.py part only moves the trailer read into a function the chat route calls the same way. Commands and output are files 7 to 11 of the gist

Case Before 551a18b After c865e9c Stream's counts
Claude Haiku 5.5 invoke stream 18 / 15, $0.00001023 18 / 15, $0.00001023 18 / 15
Nova 2 Lite invoke stream 54 / 28, $0.00009482 54 / 28, $0.00009482 54 / 28
Qwen3 converse stream 20 / 15, $0.000012 20 / 15, $0.000012 20 / 15

Before (551a18b)

$ curl -sN -D bedrock-qwen3-32b-stream http://127.0.0.1:27676/bedrock/model/bedrock-qwen3-32b/invoke-with-response-stream -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40}' | decode_events
{"text":"","metrics":null}
{"text":"1. One  \n2. Two  \n3. Three  \n4. Four  \n5. Five","metrics":{"inputTokenCount":20,"outputTokenCount":20}}

$ curl -s 'http://127.0.0.1:27676/spend/logs?request_id=554c176e-97e4-40ac-a09e-75cef4650cad' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "bedrock-qwen3-32b-stream", model: (.model | sub("[0-9]{12}"; "<account>")), call_type, spend, prompt_tokens, completion_tokens}'
{"case":"bedrock-qwen3-32b-stream","model":"bedrock/qwen.qwen3-32b-v1:0","call_type":"allm_passthrough_route","spend":0.00000225,"prompt_tokens":15,"completion_tokens":0}

After (c865e9c)

$ curl -sN -D bedrock-qwen3-32b-stream http://127.0.0.1:20431/bedrock/model/bedrock-qwen3-32b/invoke-with-response-stream -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"messages":[{"role":"user","content":"Count from 1 to 5."}],"max_tokens":40}' | decode_events
{"text":"","metrics":null}
{"text":"1. One  \n2. Two  \n3. Three  \n4. Four  \n5. Five","metrics":{"inputTokenCount":20,"outputTokenCount":20}}

$ curl -s 'http://127.0.0.1:20431/spend/logs?request_id=b729fac2-bbc5-474d-b3ce-ae7b6d5b94fe' -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '.[] | {case: "bedrock-qwen3-32b-stream", model: (.model | sub("[0-9]{12}"; "<account>")), call_type, spend, prompt_tokens, completion_tokens}'
{"case":"bedrock-qwen3-32b-stream","model":"bedrock/qwen.qwen3-32b-v1:0","call_type":"allm_passthrough_route","spend":0.000015,"prompt_tokens":20,"completion_tokens":20}

Observations from the run, each the same on both legs, so this PR leaves them alone

  • bedrock/invoke/ Qwen3 and Mistral 3 chat streams answer 400
  • Mistral Large 2 chat stream text is empty
  • Mistral 7B pass-through stream logs 0 prompt tokens
  • gpt-oss bedrock/invoke/ chat answers 400

Type

🐛 Bug Fix
✅ Test

Caveats (if any)

High

  • Unmapped invoke ids with non-OpenAI streams logged wrong rows. Fixed at c865e9c (decision 11)
    • Only a unit test covers Claude and Llama ARN streams, since the QA role cannot invoke an application inference profile

Low

  • Cached prompts streamed without include_usage bill at the full input price, since Bedrock's last event has no cache counts
    • moonshotai.kimi-k3 and moonshotai.kimi-k3.1 have a cache read price, so their cached tokens bill 10x and 20x that rate
  • us.deepseek.r1-v1:0 bills Bedrock's counts but logs empty response text. Main wrote no row for it

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
Touches Bedrock streaming decode and proxy spend logging across many invoke models; billing and stream-finish behavior can change where metrics or error chunks differ from main.

Overview
Fixes Bedrock invoke streaming so prompt and completion tokens are billed from OpenAI-shaped chunks and Bedrock’s end-of-stream amazon-bedrock-invocationMetrics, instead of prompt-only or missing spend rows on passthrough.

The default AWSEventStreamDecoder now treats chunks with choices like OpenAI stream events (shared with Moonshot/Kimi), and fills usage from invocation metrics when the chunk has no usage but the trailer reports counts. Passthrough BedrockEventStreamCollector applies the same metrics for unmapped invoke models (Qwen3, Mistral Large 3, DeepSeek V3, etc.), skips fatal OpenAIError chunks so aborted streams still log spend, and no longer drops the decoder when invoke_provider is unset.

Unit tests cover Moonshot invoke streams, OpenAI-shaped passthrough billing (including include_usage precedence), failure tails, and Anthropic/Llama-shaped unmapped streams.

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

… streams

Qwen3 and Mistral Large 3 stream OpenAI chat.completion.chunk JSON over invoke-with-response-stream, with the token counts only in the amazon-bedrock-invocationMetrics trailer. The shared Bedrock event stream decoder had no branch for those chunks, so the pass-through logged no text and no completion tokens. Decode them with the OpenAI chunk parser and fold the trailer into usage when the chunk carries none
@veria-ai

veria-ai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

No security issues were found in this pull request.

Reviews

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@greptile-apps

greptile-apps Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change reaches Bedrock invoke streaming usage and spend logging across mapped and unmapped model IDs.

The required live QA must cover the latest billing change before merging.

Findings

  1. P2 Live proof predates billing change ▶

Summary

Adds support for billing OpenAI-shaped Bedrock invoke streams from reported token counts.

  • Bedrock invoke streams now read OpenAI-shaped chunks and their token counts.
  • Unmapped Bedrock invoke streams can now produce spend rows.
  • Failed Bedrock stream endings keep earlier chunks available for spend logging.

Reviews (5) · Last reviewed commit: "fix(bedrock): bill Bedrock's own token c..." · Reviewed by Greptile

…ounts

A trailer without inputTokenCount or outputTokenCount now leaves usage to LiteLLM's own count, as before, instead of failing the stream. The Kimi token count test now runs through a fake HTTP transport, and the pass-through helper types its choices
@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.

Stale Bugbot comment from a previous run.

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

… streams from unmapped model ids

A model id the invoke resolver does not know, such as an application
inference profile ARN, streams through the generic decoder, which keeps
no usage for Anthropic- or Llama-shaped events. Fold the
amazon-bedrock-invocationMetrics trailer into those chunks so the spend
row carries Bedrock's counts instead of LiteLLM's own estimate
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

Comment on lines +332 to +336
bedrock_usage: Final = (
invocation_metrics_usage(event) if bills_invocation_metrics and generic_chunk.get("usage") is None else None
)
return _with_invoke_trace(
response_chunk if bedrock_usage is None else response_chunk.model_copy(update={"usage": bedrock_usage}), trace

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.

P2 Live proof predates billing change

This code adds reported token counts to unmapped invoke streams, but live proof stops at 6865eee3d57. The repository requires live QA at the latest product commit, c865e9cb1494b5f9642ddf45c5f29dc9041a08d6. Before merging, rerun this billing path through the proxy against real Bedrock calls and record the results.

Rule Used: Apply this rule only to PRs that fix LLM translation bugs or add LLM translation features. LLM translation means converting requests, responses, streaming events, tool calls, and errors between the API a caller uses on LiteLLM (any endpoint or SDK fu... (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next 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.

Re-ran live at c865e9c on real Bedrock. Qwen3, Mistral Large 3 and 21 unmapped ids bill Bedrock's counts, with the rows in the proof

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

This branch has not been deployed

No deployments
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