Token, cost and context accounting for one turn.
A turn keeps one accounting map. initial-usage seeds it, add-usage folds in
each provider response's reported tokens and response time and add-cost folds in
each response's price. turn-cost and turn-utilization read the totals when the
turn ends.
Every function is pure; the caller owns the map and where it lives.
Token, cost and context accounting for one turn. A turn keeps one accounting map. `initial-usage` seeds it, `add-usage` folds in each provider response's reported tokens and response time and `add-cost` folds in each response's price. `turn-cost` and `turn-utilization` read the totals when the turn ends. Every function is pure; the caller owns the map and where it lives.
(add-cost acc response-cost)Add one priced response to the turn's running cost. An unpriced response leaves
acc unchanged, so a turn served only by unpriced models keeps a nil
:accrued-cost.
Add one priced response to the turn's running cost. An unpriced response leaves `acc` unchanged, so a turn served only by unpriced models keeps a nil `:accrued-cost`.
(add-usage acc api-usage duration-ms)Fold one response's provider usage into acc. Token totals accumulate; the
:last-iter-* fields describe only the latest response. Reasoning tokens count
only when the provider reports them. duration-ms is the response time; nil
means the time was not measured. A nil api-usage leaves acc unchanged.
Fold one response's provider usage into `acc`. Token totals accumulate; the `:last-iter-*` fields describe only the latest response. Reasoning tokens count only when the provider reports them. `duration-ms` is the response time; nil means the time was not measured. A nil `api-usage` leaves `acc` unchanged.
(cache-created-tokens response-cost)Prompt-cache tokens one priced response wrote, or nil when it wrote none.
Prompt-cache tokens one priced response wrote, or nil when it wrote none.
(initial-usage previous-usage)Accounting map at turn start. Until the turn measures its first request, the
session's latest persisted request (:last-request-tokens of previous-usage)
stands in for context pressure. :accrued-cost stays nil until a response is
priced. :responses keeps the output tokens and response time of each response.
Accounting map at turn start. Until the turn measures its first request, the session's latest persisted request (`:last-request-tokens` of `previous-usage`) stands in for context pressure. `:accrued-cost` stays nil until a response is priced. `:responses` keeps the output tokens and response time of each response.
(latest-request-tokens acc)Input tokens of the latest measured request: this turn's latest response, or the session's previous request before the turn measured one.
Input tokens of the latest measured request: this turn's latest response, or the session's previous request before the turn measured one.
(measured-utilization acc window fold-budget)Context utilization after the turn's latest folded response, against an input
window and soft fold-budget.
Context utilization after the turn's latest folded response, against an input `window` and soft `fold-budget`.
(pending-utilization acc input-tokens window fold-budget)Context utilization of a request that measured input-tokens before its usage
is folded into acc, against an input window and soft fold-budget.
Context utilization of a request that measured `input-tokens` before its usage is folded into `acc`, against an input `window` and soft `fold-budget`.
(pricing model provider extra-body turn-features)Pricing context of a turn. model and provider price a response that names no
serving route; extra-body and turn-features select a provider's fast-mode
price multiplier.
Pricing context of a turn. `model` and `provider` price a response that names no serving route; `extra-body` and `turn-features` select a provider's fast-mode price multiplier.
(response-cost pricing api-usage served-model served-provider)Tokens and cost of one provider response, priced by the model and provider that
served it: a fallback response must not bill at the selected model's rates. A
missing served-model prices at the turn's model; a missing served-provider
falls back to the usage's routing data, then to the turn's provider.
Returns {:tokens {"input" … "output" … "cached" … "cache_created" …} :cost-usd :cost-map}, with "reasoning" only when the provider reported it, or nil when
the response carried no usage. :cost-map is the priced breakdown add-cost
accumulates; it is nil for an unpriced model.
Tokens and cost of one provider response, priced by the model and provider that
served it: a fallback response must not bill at the selected model's rates. A
missing `served-model` prices at the turn's model; a missing `served-provider`
falls back to the usage's routing data, then to the turn's provider.
Returns `{:tokens {"input" … "output" … "cached" … "cache_created" …} :cost-usd
:cost-map}`, with `"reasoning"` only when the provider reported it, or nil when
the response carried no usage. `:cost-map` is the priced breakdown `add-cost`
accumulates; it is nil for an unpriced model.(turn-cost acc pricing)Final token totals, cost and output rate of a turn. The cost is the sum of the
per-response prices in acc; a turn without a priced response is estimated at the
rates of the turn's model. :tokens-per-second is present only when the turn
measured the time of every response.
Final token totals, cost and output rate of a turn. The cost is the sum of the per-response prices in `acc`; a turn without a priced response is estimated at the rates of the turn's model. `:tokens-per-second` is present only when the turn measured the time of every response.
(turn-utilization acc context-limit fold-budget prompt-cache-status)Context utilization at the end of a turn, with Svar's prompt-cache status attached when one was reported.
Context utilization at the end of a turn, with Svar's prompt-cache status attached when one was reported.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |