feat(openai): add openrouter cost tracking #1311
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Important
Adds cost tracking for OpenRouter in Langfuse OpenAI integration by updating
_create_langfuse_update()
and introducing_parse_cost()
inlangfuse/openai.py
.langfuse/openai.py
by updating_create_langfuse_update()
to includecost_details
._parse_cost()
to extract cost fromusage
if available._create_langfuse_update()
now calls_parse_cost()
to addcost_details
to the update._parse_cost()
checks forcost
attribute inusage
and returns it if it's a float.This description was created by
for 7ca7ff0. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Summary
This PR adds cost tracking support for OpenRouter API usage in Langfuse's OpenAI integration. The change introduces a new
_parse_cost()
function that extracts cost information from OpenRouter's usage response and integrates it into the existing generation update workflow.The implementation is straightforward: it checks if the usage object has a
cost
attribute (which OpenRouter provides as the total USD cost of the API call) and maps it to thecost_details.total
field in the generation update. This follows OpenRouter's documented API format where cost is provided directly as a float value, unlike standard OpenAI responses that require model pricing lookups.The change integrates cleanly with the existing usage parsing infrastructure in
langfuse/openai.py
. The cost parsing is called alongside the existing_parse_usage()
function at line 497, maintaining consistency with the current architecture. This allows users to see actual costs in Langfuse when using OpenRouter models without needing to manually configure custom model definitions for cost tracking.Confidence score: 5/5