Repository navigation
Count streamed calls in Imp.Usage and give them an estimated cost - #289
Merged
Merged
Conversation
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
force-pushed
the
fix/streamed-usage
branch
from
October 8, 2026 06:54
41747ed to
81f4dc5
Compare
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 #262.
Problem
Imp.Usage.track/1aroundImp.collect/3(withprovider_stream: true) returned%{}. That held whether the stream worked or failed, because the program runs in its own task process.track_usageon,Imp.Predict.callkept its usage frame to itself and threw it away on error, so an enclosing tracker never saw that usage.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: withtrack_usageon, the predictor's usage is added to any enclosing tracker, whether the call succeeds or fails. The prediction'sget_lm_usagedoesn'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 sameestimated_costas the same call made without streaming.Tests
Two tests in
test/req_llm_stream_end_test.exsgo through ReqLLM against a local HTTP server. Both fail onmainand pass here:costequal to the provider's figure. Itsestimated_costequals what the same call returns without streaming.Imp.Usage.trackaroundImp.collectrecords both.Imp.Usage.track, withtrack_usageboth off and on. Run againstmainwith only theImp.Predictchange reverted, thetrack_usage: truecase fails.mix check's test command passes. So domix dialyzer.checkandmix quality.check.RELEASE_NOTES.mdstill 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### Fixedheading under Unreleased. It is a one-line merge.