Skip to content

Count streamed calls in Imp.Usage and give them an estimated cost - #289

Merged
deepfates merged 1 commit into
mainfrom
fix/streamed-usage
Oct 8, 2026
Merged

deepfates merged 1 commit into
mainfrom
fix/streamed-usage

Conversation

@deepfates

@deepfates deepfates commented Oct 8, 2026 •

Copy link
Copy Markdown
Owner

Fixes #262.

Problem

  • Imp.Usage.track/1 around Imp.collect/3 (with provider_stream: true) returned %{}. That held whether the stream worked or failed, because the program runs in its own task process.
  • With track_usage on, Imp.Predict.call kept its usage frame to itself and threw it away on error, so an enclosing tracker never saw that usage.
  • A streamed call had estimated_cost: nil. Imp used ReqLLM's metadata-handle usage only when no chunk had reported any (:when_missing), so ReqLLM's catalog price never got through.

Change

  • Imp.Streaming: the stream task tracks its own usage and sends it to the consumer before its result. The consumer records it in its own frame. This also happens when the consumer halts early.
  • Imp.Predict.call: with track_usage on, the predictor's usage is added to any enclosing tracker, whether the call succeeds or fails. The prediction's get_lm_usage doesn't change.
  • Imp.Clients.ReqLLM: the handle's usage fills in fields the chunks lack, and the chunks' value wins where both have one. A streamed call now gets the same estimated_cost as the same call made without streaming.

Tests

Two tests in test/req_llm_stream_end_test.exs go through ReqLLM against a local HTTP server. Both fail on main and pass here:

  • A priced OpenRouter-shaped stream has cost equal to the provider's figure. Its estimated_cost equals what the same call returns without streaming. Imp.Usage.track around Imp.collect records both.
  • A stream that fails after usage arrived is counted by Imp.Usage.track, with track_usage both off and on. Run against main with only the Imp.Predict change reverted, the track_usage: true case fails.

mix check's test command passes. So do mix dialyzer.check and mix quality.check.

RELEASE_NOTES.md still lists the streamed-usage Known limit, because it describes 0.9.0, where the limit is still real. The changelog says this change retires it, so it can come out of the next release's notes.

Whichever of this and #288 merges second will hit a conflict on the CHANGELOG.md ### Fixed heading under Unreleased. It is a one-line merge.

A streamed call runs its program in another process, so a usage tracker
around Imp.collect saw nothing. The stream's process now tracks its own
usage and sends it to the consumer, which records it in its frame,
including after a failure or a halted stream.

Imp.Predict with track_usage on kept its frame to itself, and dropped it on
error. It now adds that usage to an enclosing tracker either way.

A streamed call's usage took ReqLLM's metadata-handle usage only when no
chunk reported any, so ReqLLM's catalog price never reached it and
estimated_cost was nil. The handle's usage now fills the fields the chunks
lack; the chunks' figures stand where both report one.

Fixes #262.
@deepfates
deepfates merged commit 8e1f40d into main Oct 8, 2026
10 checks passed
@deepfates
deepfates deleted the fix/streamed-usage branch October 8, 2026 07:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Streamed calls: Imp.Usage can't see them, and they carry no estimated_cost

1 participant