Skip to main content

CostModel

Struct CostModel 

Source
pub struct CostModel {
    pub input_per_mtok: Option<f64>,
    pub output_per_mtok: Option<f64>,
    pub cache_read_input_per_mtok: Option<f64>,
    pub cache_write_input_per_mtok: Option<f64>,
    pub pricing_tiers: Vec<TokenPricingTier>,
    pub size_mb: Option<u64>,
    pub ram_mb: Option<u64>,
}

Fields§

§input_per_mtok: Option<f64>

USD per 1M input tokens (remote models).

§output_per_mtok: Option<f64>

USD per 1M output tokens (remote models).

§cache_read_input_per_mtok: Option<f64>

USD per 1M cache-read input tokens. Unlike protocol-wide cache multipliers, this is model-specific and comes from the provider’s published catalog.

§cache_write_input_per_mtok: Option<f64>

USD per 1M cache-write input tokens, when the provider charges one.

§pricing_tiers: Vec<TokenPricingTier>

Prompt-size pricing overrides, sorted by increasing threshold. The highest threshold not greater than the prompt size wins.

§size_mb: Option<u64>

On-disk size in MB (local models).

§ram_mb: Option<u64>

RAM required during inference in MB.

Implementations§

Source§

impl CostModel

Source

pub fn prices_for(&self, prompt_tokens: usize) -> TokenPrices

Source

pub fn estimated_usd( &self, prompt_tokens: usize, output_tokens: usize, cache_read_tokens: usize, cache_write_tokens: usize, ) -> f64

Estimated request cost from the provider’s declared token prices. Unknown price components contribute zero; callers that need to distinguish unknown pricing should inspect prices_for first.

This is the routing-score input, not a display or billing figure. The zero-fill is load-bearing here — adaptive_router normalizes the result into a 0..1 cost score, and it separately neutralizes models with no pricing at all, so changing the fill would change routing. For anything a human reads, use estimated_usd_bounded, which refuses to bill an unrated bucket at zero and says which way it can be wrong.

Source

pub fn estimated_usd_bounded( &self, tier_prompt_tokens: Option<usize>, uncached_input_tokens: usize, output_tokens: usize, cache_read_tokens: usize, cache_write_tokens: usize, ) -> Option<ApproxCost>

Cost estimate for a figure a person will read, carrying which way it can be wrong.

Differs from estimated_usd in refusing to invent numbers. A token bucket the provider charges for but whose rate this catalog does not declare is never billed at zero, and a figure that might be wrong never presents itself as exact.

tier_prompt_tokens selects the prompt-size pricing tier and is the parameter callers most often get wrong:

  • Some(n) — the prompt size of one request. Tiers resolve exactly.
  • None — the caller cannot say (a lifetime accumulator has summed many requests and lost their boundaries). Base rates are used and the result is flagged in whichever direction the model’s own tiers run.

Passing a summed token count as Some is the bug this signature exists to prevent: thirty 10K-token requests sum to 300K, which crosses a 272K threshold that no individual request came near, and every token ever sent gets priced at the high-context rate — roughly double, stated with total confidence.

Two rejected alternatives, for the next person who wants tiers on an aggregate. Pricing lifetime totals at the tier their sum lands in is the bug above. Pricing everything at the highest declared tier does yield a true ceiling, but a useless one — it doubles the figure for a user whose prompts never approached the threshold, which is the same confident wrongness in the other direction. Resolving tiers properly needs per-request prompt sizes, which means crate::ModelProfile would have to accumulate per-tier token buckets at record time; that is a real feature with a persisted-schema change, not something to fake here from data that has already been summed away.

Returns None when no defensible number exists — either the model declares no rate card at all, or a bucket carrying tokens has no rate and no usable substitute. Output deliberately has no input-rate fallback: it runs 1.5x–8x input across this catalog, far enough out of range that no marker could rescue the number. Which buckets substitute, and which way each substitution can be wrong, is decided in bucket_rates from observed provider pricing — notably cache reads and cache writes do not share a direction. None means unpriced, and a caller must render it as such, not as free.

Trait Implementations§

Source§

impl Clone for CostModel

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 CostModel

Source§

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

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

impl Default for CostModel

Source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for CostModel

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 JsonSchema for CostModel

Source§

fn schema_name() -> String

The name of the generated JSON Schema. Read more
Source§

fn schema_id() -> Cow<'static, str>

Returns a string that uniquely identifies the schema produced by this type. Read more
Source§

fn json_schema(generator: &mut SchemaGenerator) -> Schema

Generates a JSON Schema for this type. Read more
Source§

fn is_referenceable() -> bool

Whether JSON Schemas generated for this type should be re-used where possible using the $ref keyword. Read more
Source§

impl Serialize for CostModel

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

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> ErasedDestructor for T
where T: 'static,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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