From af8b79eb5bf06a122eb32aa6e0054d94a02481df Mon Sep 17 00:00:00 2001 From: Dor Amir Date: Fri, 6 Mar 2026 12:52:54 -0500 Subject: [PATCH] fix: use is-not-None guards for per-second cost extraction MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Greptile review (confidence 3/5) correctly identified that the input_cost_per_second / output_cost_per_second fallback used the Python operator, which treats 0.0 as falsy — the same bug explicitly fixed for token-based pricing in this PR. Replace with explicit checks so a configured input_cost_per_second of 0.0 (free input) is honoured rather than silently falling back to output_cost_per_second. --- litellm/cost_calculator.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/litellm/cost_calculator.py b/litellm/cost_calculator.py index 3e8d84c14ca..9a98228cbcc 100644 --- a/litellm/cost_calculator.py +++ b/litellm/cost_calculator.py @@ -1139,9 +1139,14 @@ def completion_cost( # noqa: PLR0915 _metadata = _litellm_params.get("metadata", {}) or {} _model_info = _metadata.get("model_info", {}) or {} # Prefer input_cost_per_second; fall back to output_cost_per_second - _cost_per_second = _model_info.get( - "input_cost_per_second" - ) or _model_info.get("output_cost_per_second") + # Use `is not None` guards to correctly handle explicit 0.0 costs + _input_cost_per_second = _model_info.get("input_cost_per_second") + _output_cost_per_second = _model_info.get("output_cost_per_second") + _cost_per_second = ( + _input_cost_per_second + if _input_cost_per_second is not None + else _output_cost_per_second + ) if _cost_per_second is not None: custom_cost_per_second = _cost_per_second