langchain
f84b5342 - feat(openrouter): surface `cost` and `cost_details` in `response_metadata` (#35461)

Commit
55 days ago
feat(openrouter): surface `cost` and `cost_details` in `response_metadata` (#35461) ## Description OpenRouter returns `cost` and `cost_details` in its API response `usage` object, 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 `cost` and `cost_details` in `response_metadata` for 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:** ```json { "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:** ```python result = chat.invoke("hello") result.response_metadata["cost"] # 0.000075 result.response_metadata["cost_details"] # {...} ``` ## Changes - **`_create_chat_result`**: Surface `cost` and `cost_details` from `token_usage` into `response_metadata` (non-streaming) - **`_convert_chunk_to_message_chunk`**: Same for streaming `AIMessageChunk` - Added `PLR0912` to `noqa` comments (new branches pushed count over threshold) - Added two unit tests: one verifying cost fields are present when returned, one verifying they're absent when not in usage ## 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 --------- Co-authored-by: Mason Daugherty <mason@langchain.dev>
Author
Parents
Loading