# `Imp.Usage`
[🔗](https://github.com/deepfates/imp/blob/v0.18.1/lib/imp/usage.ex#L1)

Per-prediction LM usage ledger.

Ports DSPy 3.2.1's `track_usage` / `UsageTracker` surface
(dspy/utils/usage_tracker.py): when the `:track_usage` setting is true,
`Imp.Predict.call` runs inside `track/1`, every LM call that reports
provider usage is recorded against its model, and the aggregate lands on the
returned prediction (read it with `Imp.Prediction.get_lm_usage/1`).

The tracker is a per-process stack. Nested trackers shadow outer ones for the
duration of the inner call, exactly as DSPy's `settings.context(usage_tracker=...)`
does; `Imp.Predict.call` adds its own tracker's usage to the enclosing one,
whether the call succeeded or failed. A streamed call (`Imp.collect/3`,
`Imp.stream/3` with `provider_stream: true`) runs in another process and
sends its usage back, so a tracker around it counts it. Parallel program runs (`Imp.Predict.Parallel`) execute in separate
processes, so each result carries only its own usage — the isolation DSPy's
thread-local tracker provides.

# `maybe_record`

Records one LM call's usage from an LM result, if a frame is active.

The model key and usage entry come from the result's provider metadata
(`Imp.LM.Result` envelope, `:req_llm` entry). Results without usage
metadata record nothing.

# `record`

Adds a usage entry for `model_key` to the innermost active frame.

# `track`

Runs `fun` with a fresh usage frame and returns `{result, usage}`.

`usage` maps a model key (for example `"openai/gpt-4o-mini"`) to a merged
usage entry, the same shape DSPy's `UsageTracker.get_total_tokens()` returns.

# `tracking?`

True when a usage frame is active in this process.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
