Skip to content

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

Description

@deepfates

What's true now (0.7.0). Two gaps in how usage is recorded for streamed calls. Both were found in the 0.7.0 release fact-check.

  1. Imp.Usage doesn't see streamed calls. A streamed call runs in a separate process (Imp.Streaming.start_program_stream), so Imp.Usage.track/1 wrapped around Imp.collect/3, or around a streamed Imp.call, returns %{}. That's true even when the stream succeeds. Separately, with track_usage, Imp.Predict.call drops its frame on error (lib/imp/predict.ex:93-97), so a failed call's usage never reaches the caller. The usage is on the run's :model_response events and nowhere else.
  2. A streamed call has no estimate. estimated_cost is nil because Imp takes ReqLLM's metadata-handle usage only when no chunk reported any (put_handle_value(:usage, …, :when_missing), lib/imp/clients/req_llm.ex ~2226). The handle does carry priced usage (cost, total_cost and the rest). Merging those priced fields into the chunk usage gives a streamed call the same cost/estimated_cost as the non-streamed call. That was checked with a temporary patch, since reverted.

Target.

  • Imp.Usage.track reports streamed calls, successful or failed, the same way it reports non-streamed ones.
  • A streamed call gets estimated_cost when ReqLLM priced it.

Done when.

  • A test of Imp.Usage.track around Imp.collect records a streamed call's usage and cost, and the same for a stream that failed after usage arrived. Both fail on 0.7.0.
  • A streamed OpenRouter call through LocalHTTP has cost equal to the provider's figure and estimated_cost equal to the catalog figure.
  • The 0.7.0 Known limit is removed.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions