mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-13 08:52:02 +00:00
fix(bedrock): price claude_platform calls from their own cost map rows (#48150)
Backport of #48150 to lts/1.108.x.
Cherry-picked from 560372456e (main).
This commit is contained in:
parent
ba5b54f50c
commit
4695ad1deb
5 changed files with 2295 additions and 207 deletions
|
|
@ -3,9 +3,13 @@ Helper util for handling bedrock-specific cost calculation
|
|||
- e.g.: prompt caching
|
||||
"""
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import TYPE_CHECKING, Final
|
||||
|
||||
from litellm.litellm_core_utils.llm_cost_calc.utils import generic_cost_per_token
|
||||
import litellm
|
||||
from litellm.litellm_core_utils.llm_cost_calc.utils import (
|
||||
generic_cost_per_token,
|
||||
get_provider_specific_geo_multiplier,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from litellm.types.utils import Usage
|
||||
|
|
@ -17,9 +21,13 @@ def cost_per_token(model: str, usage: "Usage", service_tier: str | None = None)
|
|||
|
||||
Follows the same logic as Anthropic's cost per token calculation.
|
||||
"""
|
||||
return generic_cost_per_token(
|
||||
model_info: Final = litellm.get_model_info(model=model, custom_llm_provider="bedrock")
|
||||
prompt_cost, completion_cost = generic_cost_per_token(
|
||||
model=model,
|
||||
usage=usage,
|
||||
custom_llm_provider="bedrock",
|
||||
service_tier=service_tier,
|
||||
model_info=model_info,
|
||||
)
|
||||
geo_multiplier: Final = get_provider_specific_geo_multiplier(model_info=model_info, usage=usage)
|
||||
return prompt_cost * geo_multiplier, completion_cost * geo_multiplier
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
41
tests/unit/llms/bedrock/test_cost_calculation.py
Normal file
41
tests/unit/llms/bedrock/test_cost_calculation.py
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import json
|
||||
from importlib.resources import files
|
||||
from typing import Final
|
||||
|
||||
import pytest
|
||||
|
||||
import litellm
|
||||
from litellm.types.utils import Usage
|
||||
|
||||
CLAUDE_PLATFORM_PREFIX: Final = "bedrock/claude_platform/"
|
||||
CLAUDE_PLATFORM_KEYS: Final = tuple(
|
||||
key
|
||||
for key in json.loads(files("litellm").joinpath("model_prices_and_context_window_backup.json").read_text())
|
||||
if key.startswith(CLAUDE_PLATFORM_PREFIX)
|
||||
)
|
||||
|
||||
|
||||
def test_claude_platform_rows_exist() -> None:
|
||||
assert CLAUDE_PLATFORM_KEYS
|
||||
|
||||
|
||||
@pytest.mark.parametrize("inference_geo", ["global", "us"])
|
||||
@pytest.mark.parametrize("key", CLAUDE_PLATFORM_KEYS)
|
||||
def test_claude_platform_cost_matches_claude_api_cost(local_model_cost_map: None, key: str, inference_geo: str) -> None:
|
||||
usage: Final = Usage(
|
||||
prompt_tokens=1_000,
|
||||
completion_tokens=200,
|
||||
cache_read_input_tokens=300,
|
||||
cache_creation_input_tokens=250,
|
||||
inference_geo=inference_geo,
|
||||
)
|
||||
|
||||
claude_platform_cost: Final = litellm.cost_per_token(
|
||||
model=key.removeprefix("bedrock/"), custom_llm_provider="bedrock", usage_object=usage
|
||||
)
|
||||
claude_api_cost: Final = litellm.cost_per_token(
|
||||
model=key.removeprefix(CLAUDE_PLATFORM_PREFIX), custom_llm_provider="anthropic", usage_object=usage
|
||||
)
|
||||
|
||||
assert claude_platform_cost == pytest.approx(claude_api_cost)
|
||||
assert sum(claude_platform_cost) > 0
|
||||
|
|
@ -136,6 +136,7 @@ def _enforce_bedrock_converse_models(
|
|||
info.get("litellm_provider") == "bedrock"
|
||||
and info.get("mode") == "chat"
|
||||
and model not in whitelist_models
|
||||
and BedrockModelInfo.get_bedrock_route(model) != "claude_platform"
|
||||
and not (
|
||||
(base_model := BedrockModelInfo.get_base_model(model)) != model
|
||||
and model_cost.get(base_model, {}).get("litellm_provider") == "bedrock_converse"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue