feat(core): capture provider-reported cost in OpenAI-compatible streaming - #2243
Open
tripplen23 wants to merge 2 commits into
Open
feat(core): capture provider-reported cost in OpenAI-compatible streaming#2243tripplen23 wants to merge 2 commits into
tripplen23 wants to merge 2 commits into
Conversation
…ming OpenCode Go and other OpenAI-compatible gateways report per-request cost in the chat-completions stream (OpenCode Go emits a dedicated `inference-cost` chunk with a string `cost` field; some gateways send a number). The value was silently dropped because StreamingCompletionChunk ignores unknown fields, so callers could not report real spend without maintaining per-model price tables. Parse the top-level `cost` field (string or number) and thread it through CompatibleChunk and the streaming loop, mirroring how `usage` flows, then surface it on StreamingCompletionResponse::cost and propagate it into completion::Usage::cost via token_usage() so agent runs and telemetry see it. Adds `Usage::cost` (defaults to None; `Eq` now implemented manually since f64 is not Eq). No breaking changes to deserialization.
This was referenced Aug 2, 2026
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.
Fixes #2242
Summary
OpenAI-compatible gateways such as OpenCode Go report per-request monetary cost in the chat-completions stream, but rig drops it. OpenCode Go emits a dedicated
inference-costSSE chunk after the usage chunk:{choices: [], x-opencode-type: inference-cost, cost: 0.00006972, normalizedUsage: {...}}(
costarrives as a string; some gateways send a number.) Without this change, callers must re-derive cost from token counts using per-model price tables that drift over time.Changes
StreamingCompletionChunkparses the top-levelcostfield (string or number).CompatibleChunk→ the streaming loop →build_final_response, mirroring howusagealready flows. The last chunk carrying a cost wins.StreamingCompletionResponse::costand, viatoken_usage(),completion::Usage::cost— agent runs and telemetry see it without extra plumbing.Usage::costdefaults toNone;Eqis now implemented manually since the new field isOption<f64>(providers report finite decimals only).Tests
test_streaming_captures_provider_reported_cost: full stream with aninference-costchunk, asserts response cost +token_usage().cost.test_streaming_chunk_parses_cost_string_or_number: string, number, and absent cost.cargo test -p rig-core(1023 tests) and strict clippy pass locally.