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 'berri/litellm_internal_staging' into litellm_managed_batches_observability

mubashir1osmani24a6d4de94ef
diff --cc enterprise/litellm_enterprise/proxy/common_utils/check_batch_cost.py
index 3a6692b4d67,a8e46349917..95eb384360e
--- a/enterprise/litellm_enterprise/proxy/common_utils/check_batch_cost.py
+++ b/enterprise/litellm_enterprise/proxy/common_utils/check_batch_cost.py
@@@ -703,17 -704,24 +704,22 @@@ class CheckBatchCost
f"{_file_attr}={_raw_file_id!r}: {_e}"
)
- # Pass deployment model_info so custom batch pricing
- # (input_cost_per_token_batches etc.) is used for cost calc
- deployment_model_info = deployment_info.model_info.model_dump() if deployment_info.model_info else {}
+ # Pass the deployment's router-registered pricing (litellm_params custom
+ # rates merged with the model's published rates) so custom batch pricing
+ # (input_cost_per_token_batches etc.) is used for cost calc, exactly as
+ # the inline retrieve path does.
+ deployment_model_info = deployment_pricing_model_info(
+ model_id=model_id,
+ deployment_model=litellm_model_name,
+ )
- batch_cost, batch_usage, batch_models = (
- await calculate_batch_cost_and_usage(
- file_content_dictionary=file_content_as_dict,
- custom_llm_provider=llm_provider, # type: ignore
- model_name=model_name,
- model_info=deployment_model_info,
- )
+ batch_result = await calculate_batch_cost_and_usage(
+ file_content_dictionary=file_content_as_dict,
+ custom_llm_provider=llm_provider, # type: ignore
+ model_name=model_name,
- model_info=deployment_model_info, # type: ignore[arg-type]
++ model_info=deployment_model_info,
)
logging_obj = LiteLLMLogging(
- model=batch_models[0],
+ model=batch_result.models[0],
messages=[{"role": "user", "content": "<retrieve_batch>"}],
stream=False,
call_type="aretrieve_batch",
diff --cc litellm/batches/batch_utils.py
index 057c9978879,c2cbb9604e5..bae8e198223
--- a/litellm/batches/batch_utils.py
+++ b/litellm/batches/batch_utils.py
@@@ -59,7 -48,8 +59,8 @@@ async def _handle_completed_batch
custom_llm_provider: Literal["openai", "azure", "vertex_ai", "hosted_vllm", "anthropic"],
model_name: str | None = None,
litellm_params: dict | None = None,
+ model_info: ModelInfo | None = None,
-) -> tuple[float, Usage, list[str]]:
+) -> BatchCostUsageResult:
"""Fetch a completed batch's output file and aggregate its cost, usage, and
models in a single pass over the JSONL lines, so the parsed file content is
never materialized in memory.
@@@ -79,39 -72,25 +83,40 @@@
# The generic retrieval helper keeps raising for callers that explicitly ask
# for a missing output file.
if batch.output_file_id is None:
- return 0.0, Usage(prompt_tokens=0, completion_tokens=0, total_tokens=0), []
+ return BatchCostUsageResult(
+ cost=0.0,
+ usage=Usage(prompt_tokens=0, completion_tokens=0, total_tokens=0),
+ models=[],
+ successful_requests=0,
+ failed_requests=await _count_error_file_failed_requests(
+ batch, custom_llm_provider=custom_llm_provider, litellm_params=litellm_params
+ ),
+ )
file_content = await _fetch_batch_output_file_content(batch, custom_llm_provider, litellm_params=litellm_params)
+ error_file_failed_requests: Final = await _count_error_file_failed_requests(
+ batch, custom_llm_provider=custom_llm_provider, litellm_params=litellm_params
+ )
- if (
- custom_llm_provider == "vertex_ai"
- and model_name
- and getattr(litellm, "disable_vertex_batch_output_transformation", False)
- ):
- batch_cost, batch_usage = calculate_vertex_ai_batch_cost_and_usage(
- _get_file_content_as_dictionary(file_content), model_name
+ output_file_result: Final = (
+ calculate_vertex_ai_batch_cost_and_usage(_get_file_content_as_dictionary(file_content), model_name)
+ if (
+ custom_llm_provider == "vertex_ai"
+ and model_name
+ and getattr(litellm, "disable_vertex_batch_output_transformation", False)
)
- return batch_cost, batch_usage, [model_name]
+ else _aggregate_batch_cost_usage_models(
+ entries=_iter_batch_input_entries(file_content),
+ custom_llm_provider=custom_llm_provider,
+ model_name=model_name,
++ model_info=model_info,
+ )
+ )
- return _aggregate_batch_cost_usage_models(
- entries=_iter_batch_input_entries(file_content),
- custom_llm_provider=custom_llm_provider,
- model_name=model_name,
- model_info=model_info,
+ if not error_file_failed_requests:
+ return output_file_result
+ return dataclasses_replace(
+ output_file_result, failed_requests=output_file_result.failed_requests + error_file_failed_requests
)
diff --cc litellm/litellm_core_utils/litellm_logging.py
index 20db79022d2,10e681b816e..58300d87a5a
--- a/litellm/litellm_core_utils/litellm_logging.py
+++ b/litellm/litellm_core_utils/litellm_logging.py
@@@ -2597,17 -2680,21 +2684,19 @@@ class Logging(LiteLLMLoggingBaseClass)
result.usage = batch_usage
elif should_compute_batch_data:
- (
- response_cost,
- batch_usage,
- batch_models,
- ) = await _handle_completed_batch(
+ batch_result: Final = await _handle_completed_batch(
batch=result,
custom_llm_provider=self.custom_llm_provider,
+ model_name=self.get_deployment_model_for_cost(),
litellm_params=self.litellm_params,
+ model_info=self.get_router_deployment_model_info(),
)
- result._hidden_params["response_cost"] = response_cost
- result._hidden_params["batch_models"] = batch_models
- result.usage = batch_usage
+ result._hidden_params["response_cost"] = batch_result.cost
+ result._hidden_params["batch_models"] = batch_result.models
+ result._hidden_params["batch_successful_requests"] = batch_result.successful_requests # pyright: ignore[reportPrivateUsage] # rebind-ok: same pattern as above
+ result._hidden_params["batch_failed_requests"] = batch_result.failed_requests # pyright: ignore[reportPrivateUsage] # rebind-ok: same pattern as above
+ result.usage = batch_result.usage
start_time, end_time, result = self._success_handler_helper_fn(
start_time=start_time,