Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion litellm/llms/fireworks_ai/cost_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,32 @@ def cost_per_token(model: str, usage: Usage) -> Tuple[float, float]:
)

## CALCULATE INPUT COST
# Extract cached and cache-creation tokens from usage details
cached_tokens = 0
cache_creation_tokens = 0
if getattr(usage, "prompt_tokens_details", None) is not None:
cached_tokens = (
getattr(usage.prompt_tokens_details, "cached_tokens", 0) or 0
)
cache_creation_tokens = (
getattr(usage.prompt_tokens_details, "cache_creation_tokens", 0) or 0
)

prompt_cost: float = usage["prompt_tokens"] * model_info["input_cost_per_token"]
cache_read_cost: float = model_info.get("cache_read_input_token_cost") or 0.0
cache_creation_cost: float = (
model_info.get("cache_creation_input_token_cost") or 0.0
)
# Non-cached tokens are billed at the standard input rate
non_cached_tokens = usage["prompt_tokens"] - cached_tokens - cache_creation_tokens

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 non_cached_tokens can go negative with malformed usage data

If the API ever returns cached_tokens + cache_creation_tokens > prompt_tokens (e.g. a rounding discrepancy or inconsistent response), non_cached_tokens is negative and prompt_cost ends up negative. Clamping to zero makes the cost calculation resilient to such inconsistencies.

Suggested change
non_cached_tokens = usage["prompt_tokens"] - cached_tokens - cache_creation_tokens
non_cached_tokens = max(0, usage["prompt_tokens"] - cached_tokens - cache_creation_tokens)


prompt_cost: float = (
non_cached_tokens * model_info["input_cost_per_token"]
+ cached_tokens * cache_read_cost
+ cache_creation_tokens * cache_creation_cost
)

## CALCULATE OUTPUT COST
completion_cost = usage["completion_tokens"] * model_info["output_cost_per_token"]

return prompt_cost, completion_cost

Loading