Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:BerriAI Litellm Check Batch Cost

From Leeroopedia
Revision as of 12:08, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/BerriAI_Litellm_Check_Batch_Cost.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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:

  1. Decodes the unified object ID to extract the model ID and batch ID.
  2. Queries the LLM provider via llm_router.aretrieve_batch to check the current batch status.
  3. If the batch is completed and has an output_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_usage to compute cost and token usage from the batch output.
    • Creates a LiteLLMLogging object and calls async_success_handler to log the cost through the standard logging pipeline (which updates spend tracking in the database).
  4. Marks completed jobs as complete in 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

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment