{"record":{"id":"5ec78acd5c1ce9a2","repo":"BerriAI/litellm","slug":"usage-object-and-custom-llm-provider-must-be-provi","errorCode":null,"errorMessage":"usage object and custom_llm_provider must be provided for realtime stream cost calculation. Got cost_per_token_usage_object={cost_per_token_usage_object}, custom_llm_provider={custom_llm_provider}","messagePattern":"usage object and custom_llm_provider must be provided for realtime stream cost calculation\\. Got cost_per_token_usage_object=(.+?), custom_llm_provider=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/cost_calculator.py","lineNumber":1497,"sourceCode":"                        completion_tokens_cost_usd_dollar=completion_cost_result,\n                        cost_for_built_in_tools_cost_usd_dollar=0.0,\n                        total_cost_usd_dollar=_final_cost,\n                        original_cost=original_cost,\n                        discount_percent=discount_percent,\n                        discount_amount=discount_amount,\n                        margin_percent=margin_percent,\n                        margin_fixed_amount=margin_fixed_amount,\n                        margin_total_amount=margin_total_amount,\n                        service_tier=service_tier,\n                        data_residency=data_residency,\n                    )\n\n                    return _final_cost\n                elif call_type == _AREALTIME_CALL_TYPE and isinstance(\n                    completion_response, LiteLLMRealtimeStreamLoggingObject\n                ):\n                    if cost_per_token_usage_object is None or custom_llm_provider is None:\n                        raise ValueError(\n                            f\"usage object and custom_llm_provider must be provided for realtime stream cost calculation. Got cost_per_token_usage_object={cost_per_token_usage_object}, custom_llm_provider={custom_llm_provider}\"\n                        )\n                    return handle_realtime_stream_cost_calculation(\n                        results=completion_response.results,\n                        combined_usage_object=cost_per_token_usage_object,\n                        custom_llm_provider=custom_llm_provider,\n                        litellm_model_name=model,\n                        data_residency=data_residency,\n                        litellm_logging_obj=litellm_logging_obj,\n                    )\n                elif call_type == _MCP_CALL_TYPE:\n                    from litellm.proxy._experimental.mcp_server.cost_calculator import (\n                        MCPCostCalculator,\n                    )\n\n                    return MCPCostCalculator.calculate_mcp_tool_call_cost(litellm_logging_obj=litellm_logging_obj)\n                # Calculate cost based on prompt_tokens, completion_tokens\n                if \"togethercomputer\" in model or \"together_ai\" in model or custom_llm_provider == \"together_ai\":","sourceCodeStart":1479,"sourceCodeEnd":1515,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/cost_calculator.py#L1479-L1515","documentation":"Realtime API streams (call_type 'arealtime') are priced from an aggregated usage object plus the provider name. If cost_per_token_usage_object or custom_llm_provider is None when completion_cost is called on a LiteLLMRealtimeStreamLoggingObject, LiteLLM raises ValueError because per-token realtime pricing cannot be resolved without both.","triggerScenarios":"Calling completion_cost(call_type='arealtime', completion_response=realtime_stream_obj) without passing cost_per_token_usage_object and custom_llm_provider; usually hit in custom logging callbacks that replicate LiteLLM's cost step manually.","commonSituations":"Custom success handlers on the proxy that call completion_cost with only the response; realtime sessions where usage aggregation was skipped; version changes that introduced the cost_per_token_usage_object parameter.","solutions":["Pass both arguments: completion_cost(..., cost_per_token_usage_object=aggregated_usage, custom_llm_provider='openai').","Let LiteLLM's default logging handle realtime cost — extend via custom callback reading kwargs['response_cost'] instead of recomputing.","Aggregate usage across the realtime stream (input_tokens/output_tokens) before computing cost.","Update LiteLLM if an older version didn't populate these fields for realtime."],"exampleFix":"# before\ncost = litellm.completion_cost(\n    completion_response=realtime_obj, call_type=\"arealtime\",\n)\n\n# after\ncombined = Usage(prompt_tokens=p_in, completion_tokens=p_out)\ncost = litellm.completion_cost(\n    completion_response=realtime_obj, call_type=\"arealtime\",\n    cost_per_token_usage_object=combined,\n    custom_llm_provider=\"openai\",\n)","handlingStrategy":"validation","validationCode":"if call_type == \"arealtime\":\n    assert cost_per_token_usage_object is not None, \"aggregate realtime usage before cost calc\"\n    assert custom_llm_provider is not None, \"provider required for realtime pricing\"","typeGuard":"def realtime_cost_ready(usage_obj, provider: str | None) -> bool:\n    return usage_obj is not None and provider is not None","tryCatchPattern":"try:\n    cost = litellm.completion_cost(\n        completion_response=realtime_obj, call_type=\"arealtime\",\n        cost_per_token_usage_object=combined_usage, custom_llm_provider=\"openai\",\n    )\nexcept ValueError as e:\n    if \"realtime stream cost calculation\" in str(e):\n        cost = None  # recompute after aggregating usage\n    else:\n        raise","preventionTips":["In realtime callbacks, read the precomputed kwargs['response_cost'] instead of calling completion_cost yourself.","Aggregate input/output tokens across the realtime session before billing.","Set custom_llm_provider explicitly for all realtime cost calls."],"tags":["litellm","cost-calculation","realtime","usage"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}