Implementation:BerriAI Litellm Check Batch Cost
| Attribute | Value |
|---|---|
| Sources | enterprise/litellm_enterprise/proxy/common_utils/check_batch_cost.py |
| Domains | Cost Tracking, Batches, Background Jobs, Enterprise |
| Last Updated | 2026-02-15 16:00 GMT |
Overview
CheckBatchCost is a background polling utility that checks the completion status of managed batch jobs and tracks their cost and usage through the LiteLLM logging pipeline.
Description
This class polls the LiteLLM_ManagedObjectTable database table for batch jobs that are in validating, in_progress, or finalizing status. For each active job, it:
- Decodes the unified object ID to extract the model ID and batch ID.
- Queries the LLM provider via
llm_router.aretrieve_batchto check the current batch status. - If the batch is
completedand has anoutput_file_id:- Retrieves the output file content via the managed files hook.
- Parses the file content into a dictionary.
- Looks up the deployment to determine the LLM provider and model name.
- Calls
calculate_batch_cost_and_usageto compute cost and token usage from the batch output. - Creates a
LiteLLMLoggingobject and callsasync_success_handlerto log the cost through the standard logging pipeline (which updates spend tracking in the database).
- Marks completed jobs as
completein the database.
The class requires ProxyLogging, PrismaClient, and Router instances.
Usage
Instantiate and call check_batch_cost periodically (e.g., via a background scheduler) to ensure batch job costs are tracked after completion.
Code Reference
Source Location
enterprise/litellm_enterprise/proxy/common_utils/check_batch_cost.py
Signature
class CheckBatchCost:
def __init__(
self,
proxy_logging_obj: ProxyLogging,
prisma_client: PrismaClient,
llm_router: Router,
): ...
async def check_batch_cost(self): ...
Import
from litellm_enterprise.proxy.common_utils.check_batch_cost import CheckBatchCost
I/O Contract
Inputs
| Parameter | Type | Description |
|---|---|---|
proxy_logging_obj |
ProxyLogging |
Proxy logging object used to access registered hooks (e.g., managed_files).
|
prisma_client |
PrismaClient |
Database client to query managed object table. |
llm_router |
Router |
LLM router for retrieving batch status and deployment info. |
Outputs
| Output | Type | Description |
|---|---|---|
| Cost logging | Side effect | Batch cost and usage logged through LiteLLMLogging.async_success_handler.
|
| Status update | Side effect | Completed jobs updated to "complete" status in the database.
|
Usage Examples
from litellm_enterprise.proxy.common_utils.check_batch_cost import CheckBatchCost
checker = CheckBatchCost(
proxy_logging_obj=proxy_logging_obj,
prisma_client=prisma_client,
llm_router=llm_router,
)
# Run as a periodic background task
await checker.check_batch_cost()
Related Pages
- BerriAI_Litellm_Check_Responses_Cost -- Similar polling for responses API cost tracking
- BerriAI_Litellm_Managed_Files -- Managed files hook used to retrieve batch output files