Skip to main content

cost_of

Function cost_of 

Source
pub fn cost_of(
    messages: &[AgentMessage],
    base_url: Option<&str>,
    fallback_model: &str,
) -> Option<Cost>
Expand description

The session’s $ cost: fold every Assistant { usage: Some(u), model } at the price of model — the message’s own id when set, else fallback_model — under base_url, summing micro-USD.

Returns None when NOT ONE turn could be priced (unknown model/provider ⇒ absent, never $0). Cost::unpriced_turns counts the turns that reported usage but had no price, so a mid-run model switch reads ~$0.0087 (3 turns unpriced) instead of silently under-reporting.

Contract:

  • one numerator is accumulated across priced turns and divided ONCE, so a long session does not lose a micro-USD per turn to truncation;
  • a turn with usage: None is neither priced nor counted unpriced (it reported nothing);
  • messages is the same slice crate::stats::session_stats folded, so stats.turns == priced + unpriced whenever cost is Some.

Does NOT promise: tier rates; a per-model base_url (a crate::streamfn::ModelProfiles override is ignored — the launch base is used for every turn); compaction does not rewrite the messages priced here.