Skip to content
Merged
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
13 changes: 13 additions & 0 deletions langfuse/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ def _create_langfuse_update(

if usage is not None:
update["usage_details"] = _parse_usage(usage)
update["cost_details"] = _parse_cost(usage)

generation.update(**update)

Expand Down Expand Up @@ -523,6 +524,18 @@ def _parse_usage(usage: Optional[Any] = None) -> Any:
return usage_dict


Copy link
Contributor

Choose a reason for hiding this comment

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

When setting cost_details, consider supporting cases where usage is a dict (as _parse_usage does) rather than relying solely on attribute access.

def _parse_cost(usage: Optional[Any] = None) -> Any:
if usage is None:
return

# 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")}

return None


def _extract_streamed_response_api_response(chunks: Any) -> Any:
completion, model, usage = None, None, None
metadata = {}
Expand Down
Loading