fix(usage): use OpenRouter's billed cost instead of estimating locally#1127
Draft
fix(usage): use OpenRouter's billed cost instead of estimating locally#1127
Conversation
For OpenRouter (DAIV's default Anthropic route), langchain_openai's usage parser only emits cache_read, never cache_creation. The previous code passed cache_write_tokens=0 to genai_prices, which then priced cache-creation tokens at the base input rate instead of the 1.25x/2x cache-write rate. With the default 1h cache TTL, cache writes were being undercharged by about 50%, producing run costs visibly lower than LangSmith's. Switch to OpenRouter's authoritative cost field: - ChatOpenRouter subclasses ChatOpenAI and stashes usage.cost on the message's response_metadata so it survives streaming chunk merging (non-streaming already exposes it via llm_output propagation). - get_model routes OpenRouter calls through ChatOpenRouter and sets extra_body.usage.include + stream_usage so OpenRouter actually returns the cost field. - DaivUsageCallbackHandler aggregates per-model provider cost alongside the existing token usage; build_usage_summary prefers it over the local genai_prices estimate and tags each model entry with cost_source. Direct Anthropic / OpenAI / Google keep using genai_prices unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
For OpenRouter (DAIV's default Anthropic route), langchain_openai's usage parser only emits cache_read, never cache_creation. The previous code passed cache_write_tokens=0 to genai_prices, which then priced cache-creation tokens at the base input rate instead of the 1.25x/2x cache-write rate. With the default 1h cache TTL, cache writes were being undercharged by about 50%, producing run costs visibly lower than LangSmith's.
Switch to OpenRouter's authoritative cost field:
Direct Anthropic / OpenAI / Google keep using genai_prices unchanged.