Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Merge remote-tracking branch 'origin/litellm_internal_staging' into litellm_internal_copy_37077

mateo-berri678a63684650
diff --cc tests/test_litellm/batches/test_batch_utils.py
index 177a4e354ca,68e40ac2c81..573882ebfca
--- a/tests/test_litellm/batches/test_batch_utils.py
+++ b/tests/test_litellm/batches/test_batch_utils.py
@@@ -1322,89 -1323,53 +1323,141 @@@ async def test_output_file_content_bedr
assert "model" not in captured
+# =========================================================================== #
+# _handle_completed_batch threads the deployment's model identity + pricing
+# =========================================================================== #
+
+
+def _bedrock_row(model: str, input_tokens: int, output_tokens: int) -> dict[str, object]:
+ return {
+ "modelInput": {"messages": [{"role": "user", "content": [{"type": "text", "text": "hi"}]}]},
+ "modelOutput": {
+ "model": model,
+ "id": "msg_1",
+ "type": "message",
+ "role": "assistant",
+ "content": [{"type": "text", "text": "ok"}],
+ "stop_reason": "end_turn",
+ "usage": {
+ "input_tokens": input_tokens,
+ "output_tokens": output_tokens,
+ "cache_creation_input_tokens": 0,
+ "cache_read_input_tokens": 0,
+ },
+ },
+ "recordId": "r",
+ }
+
+
+async def test_handle_completed_bedrock_batch_prices_from_deployment_model(monkeypatch) -> None:
+ """A bedrock batch must price from the deployment model, not the response model."""
+ rows = [_bedrock_row("claude-sonnet-4-6", 18, 10)] * 100
+
+ async def fake_fetch(batch: object, custom_llm_provider: str, litellm_params: dict | None = None) -> bytes:
+ return _vertex_jsonl(rows)
+
+ monkeypatch.setattr(bu, "_fetch_batch_output_file_content", fake_fetch)
+
+ cost, usage, _ = await bu._handle_completed_batch(
+ _batch("of"),
+ custom_llm_provider="bedrock",
+ model_name="bedrock/global.anthropic.claude-sonnet-4-6",
+ )
+
+ assert (usage.prompt_tokens, usage.completion_tokens, usage.total_tokens) == (1800, 1000, 2800)
+ # 3e-06 / 1.5e-05 on-demand, halved for batch.
+ assert cost == pytest.approx(1800 * 3e-06 / 2 + 1000 * 1.5e-05 / 2)
+
+ # The response model alone cannot price a bedrock batch: this is the $0 bug.
+ zero_cost, zero_usage, _ = await bu._handle_completed_batch(
+ _batch("of"),
+ custom_llm_provider="bedrock",
+ model_name=None,
+ )
+ assert zero_cost == 0.0
+ assert zero_usage.total_tokens == 2800
+
+
+async def test_handle_completed_batch_honors_deployment_pricing(monkeypatch) -> None:
+ """A deployment's configured rates must win over the global cost map."""
+ rows = [_success_row(model="gemini-2.5-flash", usage=_usage(60, 75))]
+
+ async def fake_fetch(batch: object, custom_llm_provider: str, litellm_params: dict | None = None) -> bytes:
+ return _vertex_jsonl(rows)
+
+ monkeypatch.setattr(bu, "_fetch_batch_output_file_content", fake_fetch)
+
+ free_cost, _, _ = await bu._handle_completed_batch(
+ _batch("of"),
+ custom_llm_provider="vertex_ai",
+ model_name="vertex_ai/gemini-2.5-flash",
+ model_info={
+ "input_cost_per_token": 0.0,
+ "output_cost_per_token": 0.0,
+ "input_cost_per_token_batches": 0.0,
+ "output_cost_per_token_batches": 0.0,
+ },
+ )
+ assert free_cost == 0.0
+
+ billed_cost, _, _ = await bu._handle_completed_batch(
+ _batch("of"),
+ custom_llm_provider="vertex_ai",
+ model_name="vertex_ai/gemini-2.5-flash",
+ model_info=None,
+ )
+ assert billed_cost > 0.0
++
++
+ # =========================================================================== #
+ # _get_batch_job_usage_from_response_body: bedrock usage shapes
+ # =========================================================================== #
+
+
+ def test_bedrock_converse_shaped_batch_usage_is_parsed():
+ body = {"model": "us.amazon.nova-lite-v1:0", "usage": {"inputTokens": 2202, "outputTokens": 540, "totalTokens": 2742}}
+ usage = bu._get_batch_job_usage_from_response_body(body, custom_llm_provider="bedrock")
+ assert (usage.prompt_tokens, usage.completion_tokens, usage.total_tokens) == (2202, 540, 2742)
+
+
+ def test_bedrock_converse_batch_usage_totals_default_when_absent():
+ body = {"model": "us.amazon.nova-lite-v1:0", "usage": {"inputTokens": 10, "outputTokens": 4}}
+ usage = bu._get_batch_job_usage_from_response_body(body, custom_llm_provider="bedrock")
+ assert (usage.prompt_tokens, usage.completion_tokens, usage.total_tokens) == (10, 4, 14)
+
+
+ def test_bedrock_converse_batch_usage_includes_cache_tokens():
+ body = {
+ "model": "us.amazon.nova-lite-v1:0",
+ "usage": {
+ "inputTokens": 100,
+ "outputTokens": 20,
+ "totalTokens": 120,
+ "cacheReadInputTokens": 800,
+ "cacheWriteInputTokens": 200,
+ },
+ }
+ usage = bu._get_batch_job_usage_from_response_body(body, custom_llm_provider="bedrock")
+ assert usage.prompt_tokens == 1100
+ assert usage.completion_tokens == 20
+ assert usage.prompt_tokens_details.cached_tokens == 800
+ assert usage.prompt_tokens_details.cache_creation_tokens == 200
+
+
+ def test_bedrock_anthropic_shaped_batch_usage_still_parsed():
+ """Anthropic-shaped bedrock output (what an Anthropic model's batch emits) must not regress."""
+ body = {"model": "claude-sonnet-4-6", "usage": {"input_tokens": 18, "output_tokens": 10}}
+ usage = bu._get_batch_job_usage_from_response_body(body, custom_llm_provider="bedrock")
+ assert (usage.prompt_tokens, usage.completion_tokens, usage.total_tokens) == (18, 10, 28)
+
+
+ def test_unparsable_bedrock_batch_usage_warns(caplog):
+ """An unrecognized usage shape must be visible, not a silent $0."""
+ body = {"model": "amazon.titan-text-lite-v1", "usage": {"inputTextTokenCount": 42}}
+ with caplog.at_level(logging.WARNING):
+ usage = bu._get_batch_job_usage_from_response_body(body, custom_llm_provider="bedrock")
+ assert usage.total_tokens == 0
+ assert "does not understand" in caplog.text
+ assert "inputTextTokenCount" in caplog.text