Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 8 additions & 2 deletions langfuse/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,14 @@ def _parse_cost(usage: Optional[Any] = None) -> Any:

# OpenRouter is returning total cost of the invocation
# https://openrouter.ai/docs/use-cases/usage-accounting#cost-breakdown
if hasattr(usage, "cost") and isinstance(getattr(usage, "cost"), float):
return {"total": getattr(usage, "cost")}
cost = (
usage.get("cost") if isinstance(usage, dict) else getattr(usage, "cost", None)
)

# bool is a subclass of int, so it has to be excluded explicitly to avoid
# turning a stray `"cost": true` into a cost of 1.0
if isinstance(cost, (int, float)) and not isinstance(cost, bool):
return {"total": float(cost)}

return None

Expand Down
48 changes: 48 additions & 0 deletions tests/unit/test_parse_cost.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from langfuse.openai import _parse_cost


class _Usage:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)


def test_none_usage():
assert _parse_cost(None) is None


def test_object_with_float_cost():
assert _parse_cost(_Usage(cost=0.0021)) == {"total": 0.0021}


def test_object_without_cost():
assert _parse_cost(_Usage(prompt_tokens=10)) is None


def test_dict_with_float_cost():
"""OpenRouter usage arrives as a dict on the streaming path.

_parse_usage already branches on isinstance(usage, dict) and both are called
with the same object, so cost has to handle a dict too.
"""
assert _parse_cost({"cost": 0.0021, "prompt_tokens": 10}) == {"total": 0.0021}


def test_dict_without_cost():
assert _parse_cost({"prompt_tokens": 10}) is None


def test_integer_cost_is_not_dropped():
"""A free or fully cached model reports "cost": 0, which is an int, not a float."""
assert _parse_cost({"cost": 0}) == {"total": 0.0}
assert _parse_cost(_Usage(cost=0)) == {"total": 0.0}
assert _parse_cost(_Usage(cost=2)) == {"total": 2.0}


def test_bool_cost_is_rejected():
assert _parse_cost({"cost": True}) is None
assert _parse_cost(_Usage(cost=False)) is None


def test_non_numeric_cost_is_rejected():
assert _parse_cost({"cost": "0.0021"}) is None
assert _parse_cost({"cost": None}) is None