Skip to main content

CostConfig

Struct CostConfig 

Source
#[non_exhaustive]
pub struct CostConfig { pub input_per_million: f64, pub output_per_million: f64, pub cache_read_per_million: f64, pub cache_write_per_million: f64, pub context_tiers: Vec<ContextTier>, }
Expand description

Cost per million tokens (input/output).

§A cache rate left at zero bills at the input rate

cache_read_per_million or cache_write_per_million at 0.0 means “not separately priced”, and cost_usd bills those tokens at the applicable input rate — the base input_per_million, or a ContextTier’s own input_per_million when that tier applies (a tier’s zero cache rate falls back to the tier’s input rate, never to a base cache rate). A vendor with no separate cache-write charge (OpenAI before GPT-5.6, Meta) is priced correctly without setting one, and a missing rate can no longer bill cached tokens as free. The rule applies only where the input rate is non-zero, so a fully zero config still costs $0. There is therefore no way to say “cache reads are free but input is not”; set a tiny positive rate if a vendor ever charges that.

§These are a snapshot, not an authority

Nothing is priced by default: constructors leave cost at None until the process opts in, with prices::enable_bundled (offline) or prices::enable_live (native). See prices.

The bundled rates live in src/provider/prices.json (see PriceTable), each entry verified against the vendor’s published pricing on the date it records. Vendors reprice, and a compiled-in number cannot notice — claude_sonnet_5 shipped Sonnet 4.6’s rates across 18 releases, v0.9.0 through v0.16.5, overstating every cost_usd for that model by 50%, and nothing detected it. You can override the bundled data without waiting for a release: live with prices::enable_live (native), process-wide with global::install_override or the YOAGENT_PRICES environment variable (both apply to configs built afterwards), or per config with ModelConfig::with_prices and ModelConfig::reprice.

§Context tiers

Some vendors charge more above a prompt-size threshold. Set context_tiers and cost_usd selects by the request’s prompt tokens (input + cache_read + cache_write).

The OpenAI presets set one at 272K: gpt_5_5, gpt_6_astra, gpt_6_sol, gpt_6_1_sol and gpt_6_luna (checked 2026-09-25 and 2026-10-18; gpt_5_5’s long-band cache-read rate is unverified — see its docs). claude_haiku_5_5 sets one at 100K and grok_4_7 at 200K. The other Anthropic presets do not: Anthropic bills their full 1M window at standard rates (Haiku 5.5 is the exception). Meta’s page says there is no long-context premium; Haiku 4.5 is flat because its window is 200K.

Note the derivation: prompt size is input + cache_read + cache_write, which holds only where the provider subtracts cached tokens out of input. bedrock.rs populates neither cache field, so a heavily-cached prompt reads small there and would select the cheap tier. Fix that before tiering a model Bedrock serves.

tests/price_audit.rs diffs every prices.json entry against models.dev; run it before a release:

cargo test --test price_audit -- --ignored --nocapture

ModelConfig::cost is a public field and CostConfig is Deserialize, so a caller never has to wait for a release — override it for a negotiated rate, or load rates from configuration:

yoagent::provider::prices::enable_bundled(); // opt in to the bundled prices
// A named preset is then priced; adjust one rate and keep the rest.
let mut config = ModelConfig::claude_sonnet_5();
config
    .cost
    .get_or_insert_with(CostConfig::default)
    .input_per_million = 1.80; // your negotiated rate
assert_eq!(config.cost.as_ref().unwrap().output_per_million, 10.0);

// A model the price table does not list has no price (`cost: None`).
// `get_or_insert_with` works here too — `if let Some(c) =
// config.cost.as_mut()` would silently do nothing — but set every rate
// you pay, since the rest start at zero (an unset cache rate then bills
// at the input rate):
let mut mistral = ModelConfig::mistral("mistral-large-latest", "Mistral Large");
assert!(mistral.cost.is_none());
// The rates you pay, from Mistral's pricing page (these are placeholders).
mistral.cost = Some(CostConfig::new(2.0, 6.0).with_cache_read(0.2));
assert_eq!(mistral.cost.as_ref().unwrap().input_per_million, 2.0);

// A model you run for free is `Some` with zero rates, and costs $0:
let mut local = ModelConfig::local("http://localhost:1234/v1", "qwen3");
local.cost = Some(CostConfig::new(0.0, 0.0));

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§input_per_million: f64§output_per_million: f64§cache_read_per_million: f64

Rate for cache-hit prompt tokens. 0.0 (the default) bills them at input_per_million; see the zero-rate rule.

§cache_write_per_million: f64

Rate for prompt tokens written to the cache. 0.0 (the default) bills them at input_per_million; see the zero-rate rule.

§context_tiers: Vec<ContextTier>

Rates that replace the above once a request’s prompt exceeds a threshold, ascending by threshold. Empty means one flat rate at every size.

A Vec rather than a single tier because vendors publish multi-step schedules and models.dev already represents this as an array — making it one tier would buy a second breaking release the first time a three-tier model appears, and it would break the serde key as well as the field type, invalidating every persisted config.

Implementations§

Source§

impl CostConfig

Source

pub fn new(input_per_million: f64, output_per_million: f64) -> Self

The two rates every priced model has.

Cache rates are set with with_cache_read and with_cache_write rather than as positional arguments. Four same-typed f64s in a row is a transposition waiting to happen, and no vendor publishes them in one order: Anthropic lists input / cache write / cache read / output, OpenAI lists input / cached input / output. Transcribing top-to-bottom from either page produced a wrong-but-compiling config, and is_configured returns true for a transposed one, so every downstream guard passes. That is exactly how claude_sonnet_5 billed 50% high for 18 releases. Two arguments still transpose, but output is always dearer than input, so the mistake is visible.

CostConfig is #[non_exhaustive], so downstream crates build it here rather than with a struct literal.

Source

pub fn with_cache_read(self, per_million: f64) -> Self

Rate for reading a cached prompt prefix. Unset (0.0), cache reads bill at the input rate.

Source

pub fn with_cache_write(self, per_million: f64) -> Self

Rate for writing a prompt prefix into the cache. Unset (0.0), cache writes bill at the input rate — right for a vendor with no separate write charge.

Source

pub fn with_context_tier(self, tier: ContextTier) -> Self

Charge higher rates above a prompt-size threshold.

Repeatable. Tiers are kept sorted by threshold so cost_usd can take the last one the prompt clears, and so declaration order cannot change what a config costs.

Source

pub fn is_configured(&self) -> bool

Whether any rate is set.

Not a “known price” check. In memory an all-zero CostConfig means free, and unknown pricing is ModelConfig::cost == None. This predicate matters at one point: deserializing a ModelConfig, where an all-zero cost object is the pre-0.19 encoding of “unknown” and loads as None (see ModelConfig::cost).

Source

pub fn cost_usd(&self, usage: &Usage) -> f64

Dollar cost of a usage record at these per-million-token rates.

A cache rate of 0.0 bills at the applicable band’s input rate (see the zero-rate rule).

Consumed by crate::Agent::session_cost_usd; also usable directly in after_turn callbacks for per-turn cost tracking.

Trait Implementations§

Source§

impl Clone for CostConfig

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CostConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CostConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for CostConfig

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for CostConfig

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for CostConfig

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for CostConfig

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> MaybeSend for T
where T: Send + ?Sized,

Source§

impl<T> MaybeSync for T
where T: Sync + ?Sized,

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more