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 pull request #39626 from BerriAI/litellm_batch_ui_logs

Mateo Wang5b2b5420af8a
diff --cc litellm/batches/batch_utils.py
index 959c7498479,871fdf79182..87f8fd3946e
--- a/litellm/batches/batch_utils.py
+++ b/litellm/batches/batch_utils.py
@@@ -23,29 -23,10 +23,31 @@@ class BatchCostUsageResult
models: list[str]
successful_requests: int
failed_requests: int
+ prompt_cost: float = 0.0
+ completion_cost: float = 0.0
+_COMPLETED_BATCH_STATUSES: Final = frozenset({"completed", "complete"})
+_TERMINAL_BATCH_STATUSES: Final = _COMPLETED_BATCH_STATUSES | frozenset({"failed", "cancelled", "expired"})
+
+
+def batch_cost_is_final(batch: Batch) -> bool:
+ """Whether this retrieve of the batch is the one to account its cost from.
+
+ A batch still in flight has nothing to price, and a "completed" batch can report
+ no output_file_id for a moment before the output populates; pricing either records
+ $0 under the batch's single spend row and pins it there. Final means a completed
+ batch whose output file has arrived or whose counts prove no line succeeded, or
+ any other terminal status (failed, cancelled, expired).
+ """
+ if batch.status not in _TERMINAL_BATCH_STATUSES:
+ return False
+ if batch.status not in _COMPLETED_BATCH_STATUSES or batch.output_file_id is not None:
+ return True
+ request_counts: Final = batch.request_counts
+ return request_counts is not None and request_counts.total > 0 and request_counts.completed == 0
+
+
async def calculate_batch_cost_and_usage(
file_content_dictionary: list[dict],
custom_llm_provider: Literal["openai", "azure", "vertex_ai", "hosted_vllm", "anthropic"],
diff --cc litellm/litellm_core_utils/litellm_logging.py
index e3c94866985,7e800139228..c5fcf0bd2a0
--- a/litellm/litellm_core_utils/litellm_logging.py
+++ b/litellm/litellm_core_utils/litellm_logging.py
@@@ -2965,12 -2944,13 +2978,18 @@@ class Logging(LiteLLMLoggingBaseClass)
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
+ self.set_cost_breakdown(
+ input_cost=batch_result.prompt_cost,
+ output_cost=batch_result.completion_cost,
+ total_cost=batch_result.cost,
+ cost_for_built_in_tools_cost_usd_dollar=0.0,
+ )
+ self.truncated_messages_for_logging = await truncate_base64_in_messages_async(
+ StandardLoggingPayloadSetup.append_system_prompt_messages(
+ kwargs=self.model_call_details, messages=self.model_call_details.get("messages")
+ )
+ )
start_time, end_time, result = self._success_handler_helper_fn(
start_time=start_time,
end_time=end_time,