Foresight.Usage (Foresight v0.2.0)

Copy Markdown View Source

Ledger of the chargeable calls made while serving one operation.

Retain and recall open a ledger in the calling process, the LLM adapter and the remote embedder record every call they make into it, and the operation closes it and reports the entries on its result under "usage". Each entry names the kind of call, the provider and model, the token counts the provider reported, the request id when one was returned, and whether the call succeeded, so a consumer can attribute cost deterministically and audit it against the provider's own records.

"usage" is nil when no chargeable call happened: local embedders and the Noop LLM record nothing, so the default configuration is unchanged.

Summary

Functions

Close the ledger and return its entries in call order.

Build an LLM entry from an LlmCore.LLM.Response-shaped struct or map.

Build an LLM entry for a call that failed before any usage was reported.

Record one call. A no-op when no ledger is open, so adapters can always call it. Missing token counts default to zero; "status" defaults to "ok".

Summarise entries for a result: token totals over successful calls plus the calls themselves. nil for an empty ledger.

Open a fresh ledger in the current process.

Types

entry()

@type entry() :: %{required(String.t()) => term()}

Functions

drain()

@spec drain() :: [entry()]

Close the ledger and return its entries in call order.

llm_entry(response, role)

@spec llm_entry(map(), atom() | String.t() | nil) :: entry()

Build an LLM entry from an LlmCore.LLM.Response-shaped struct or map.

llm_error_entry(provider, role)

@spec llm_error_entry(term(), atom() | String.t() | nil) :: entry()

Build an LLM entry for a call that failed before any usage was reported.

record(entry)

@spec record(map()) :: :ok

Record one call. A no-op when no ledger is open, so adapters can always call it. Missing token counts default to zero; "status" defaults to "ok".

report(entries)

@spec report([entry()]) :: map() | nil

Summarise entries for a result: token totals over successful calls plus the calls themselves. nil for an empty ledger.

start()

@spec start() :: :ok

Open a fresh ledger in the current process.