diff --git a/docs/my-website/docs/sdk_custom_pricing.md b/docs/my-website/docs/sdk_custom_pricing.md index ac956db472..c857711510 100644 --- a/docs/my-website/docs/sdk_custom_pricing.md +++ b/docs/my-website/docs/sdk_custom_pricing.md @@ -2,10 +2,7 @@ Register custom pricing for sagemaker completion model. -For cost per second pricing, register `input_cost_per_second`. If your provider -charges for audio output duration (e.g., TTS), also set `output_cost_per_second`. -Values of `0` are treated as not billable, so `output_cost_per_second: 0` will -not override `input_cost_per_second`. +For cost per second pricing, you **just** need to register `input_cost_per_second`. ```python # !pip install boto3 diff --git a/litellm/llms/openai/cost_calculation.py b/litellm/llms/openai/cost_calculation.py index 339ce9caed..e5349db3af 100644 --- a/litellm/llms/openai/cost_calculation.py +++ b/litellm/llms/openai/cost_calculation.py @@ -105,21 +105,25 @@ def cost_per_second( prompt_cost = 0.0 completion_cost = 0.0 ## Speech / Audio cost calculation - output_cost_per_second = model_info.get("output_cost_per_second") - input_cost_per_second = model_info.get("input_cost_per_second") - - if output_cost_per_second is not None and output_cost_per_second > 0: + if ( + "output_cost_per_second" in model_info + and model_info["output_cost_per_second"] is not None + ): verbose_logger.debug( - f"For model={model} - output_cost_per_second: {output_cost_per_second}; duration: {duration}" + f"For model={model} - output_cost_per_second: {model_info.get('output_cost_per_second')}; duration: {duration}" ) ## COST PER SECOND ## - completion_cost = output_cost_per_second * duration - if input_cost_per_second is not None and input_cost_per_second > 0: + completion_cost = model_info["output_cost_per_second"] * duration + elif ( + "input_cost_per_second" in model_info + and model_info["input_cost_per_second"] is not None + ): verbose_logger.debug( - f"For model={model} - input_cost_per_second: {input_cost_per_second}; duration: {duration}" + f"For model={model} - input_cost_per_second: {model_info.get('input_cost_per_second')}; duration: {duration}" ) ## COST PER SECOND ## - prompt_cost = input_cost_per_second * duration + prompt_cost = model_info["input_cost_per_second"] * duration + completion_cost = 0.0 return prompt_cost, completion_cost diff --git a/tests/test_litellm/test_cost_calculator.py b/tests/test_litellm/test_cost_calculator.py index 42bcfca231..4d6599fc1b 100644 --- a/tests/test_litellm/test_cost_calculator.py +++ b/tests/test_litellm/test_cost_calculator.py @@ -192,40 +192,6 @@ def test_transcription_cost_falls_back_to_duration(): assert pytest.approx(cost, rel=1e-6) == expected_cost -def test_transcription_cost_prefers_input_when_output_zero(monkeypatch): - from litellm import completion_cost - - os.environ["LITELLM_LOCAL_MODEL_COST_MAP"] = "True" - litellm.model_cost = litellm.get_model_cost_map(url="") - - model_name = "custom-whisper-input-only" - custom_model_info = { - "input_cost_per_second": 0.00005, - "output_cost_per_second": 0.0, - "litellm_provider": "openai", - "mode": "audio_transcription", - "supported_endpoints": ["/v1/audio/transcriptions"], - } - monkeypatch.setattr( - litellm, - "model_cost", - {**litellm.model_cost, model_name: custom_model_info}, - ) - - response = TranscriptionResponse(text="demo text") - response.duration = 300.0 - - cost = completion_cost( - completion_response=response, - model=model_name, - custom_llm_provider="openai", - call_type="atranscription", - ) - - expected_cost = 300.0 * 0.00005 - assert pytest.approx(cost, rel=1e-6) == expected_cost - - def test_handle_realtime_stream_cost_calculation(): from litellm.cost_calculator import RealtimeAPITokenUsageProcessor