feat(openrouter): surface cost and cost_details in response_metadata#35461
Merged
Mason Daugherty (mdrxy) merged 3 commits intolangchain-ai:masterfrom Mar 1, 2026
Merged
Conversation
OpenRouter returns `cost` and `cost_details` in its API response usage object, but these fields were being dropped by `_create_usage_metadata()` which only extracts token counts. This change surfaces both fields in `response_metadata` for both non-streaming (`_create_chat_result`) and streaming (`_convert_chunk_to_message_chunk`) paths, allowing users to access actual API costs without manual estimation from token counts.
Mason Daugherty (mdrxy)
requested changes
Mar 1, 2026
Member
Mason Daugherty (mdrxy)
left a comment
There was a problem hiding this comment.
I do see one blocker in the streaming path:
- In
_streamand_astream, final-chunk metadata currently replacesmessage_chunk.response_metadata:message_chunk = message_chunk.model_copy(update={"response_metadata": generation_info})
- Since cost fields are added earlier in
_convert_chunk_to_message_chunk, they can be dropped iffinish_reasonmetadata is set on the same chunk.
Could you merge metadata instead of replacing it (e.g. merge existing message_chunk.response_metadata with generation_info)?
Also, please add streaming regression tests (stream + astream) asserting cost/cost_details survive on the final chunk when usage includes them.
…preserve cost fields The final streaming chunk overwrote response_metadata with generation_info, dropping cost/cost_details set by _convert_chunk_to_message_chunk. Now merges existing metadata with generation_info in both _stream and _astream paths. Adds regression tests for sync and async streaming to verify cost fields survive alongside finish_reason on the final chunk.
Contributor
Author
|
Mason Daugherty (@mdrxy) Good catch — thanks for the review! Fixed in c79c26b:
All 194 unit tests pass. |
cost and cost_details in response_metadata
f84b534
into
langchain-ai:master
18 of 20 checks passed
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.
Description
OpenRouter returns
costandcost_detailsin its API responseusageobject, providing the actual cost of each API call. Currently,_create_usage_metadata()only extracts token counts and drops these cost fields.This PR surfaces both
costandcost_detailsinresponse_metadatafor both non-streaming and streaming paths, allowing users to access actual API costs directly from the response without manual estimation from token counts.Example response from OpenRouter:
{ "usage": { "prompt_tokens": 100, "completion_tokens": 50, "cost": 0.000075, "cost_details": { "upstream_inference_cost": 0.00007745, "upstream_inference_prompt_cost": 0.00000895, "upstream_inference_completions_cost": 0.0000685 } } }After this change:
Changes
_create_chat_result: Surfacecostandcost_detailsfromtoken_usageintoresponse_metadata(non-streaming)_convert_chunk_to_message_chunk: Same for streamingAIMessageChunkPLR0912tonoqacomments (new branches pushed count over threshold)Issue
N/A — discovered while integrating OpenRouter in a production pipeline. The cost data is already returned by the API but was being silently dropped.
Dependencies
None.
Twitter handle
@hamza_kyamanywa