#2242·rig

feat: capture provider-reported cost from OpenAI-compatible streaming responses

Author: tripplen23Created Aug 2, 2026Updated Aug 2, 2026

Problem

Several OpenAI-compatible gateways report the monetary cost of a request in the chat-completions stream, but rig drops it. Notably OpenCode Go emits a dedicated inference-cost SSE chunk after the usage chunk:

json
{choices: [], x-opencode-type: inference-cost, cost: 0.00006972, normalizedUsage: {...}}

(the cost field is a string; some gateways send a number). Since StreamingCompletionChunk ignores unknown fields, the value is lost and callers cannot report real spend — they must either re-derive cost from token counts (approximate, requires per-model price tables that drift) or not report it.

Proposal

  • Parse the top-level cost field (string or number) in StreamingCompletionChunk.
  • Thread it through CompatibleChunk → the streaming loop → build_final_response, mirroring how usage already flows.
  • Surface it on StreamingCompletionResponse.cost and propagate it into completion::Usage::cost in token_usage(), so agent runs and telemetry see it without extra plumbing.

OpenRouter already models provider-reported cost (usage.cost); this extends the same capability to OpenAI-compatible streams where the field is a sibling of usage rather than nested inside it.

No breaking changes: the new field defaults to None and existing deserialization is unaffected.