pub struct Cost { /* private fields */ }Expand description
Monetary cost in microdollars (1 USD = 1,000,000 microdollars).
Uses integer arithmetic to avoid floating-point accumulation errors.
The invariant total == input + output is enforced by the
constructor and maintained through deserialization.
§Examples
use llm_stack_core::Cost;
let cost = Cost::new(300_000, 150_000).expect("no overflow");
assert_eq!(cost.total_microdollars(), 450_000);
assert!((cost.total_usd() - 0.45).abs() < f64::EPSILON);Implementations§
Source§impl Cost
impl Cost
Sourcepub fn new(input: u64, output: u64) -> Option<Self>
pub fn new(input: u64, output: u64) -> Option<Self>
Creates a new Cost, returning None if input + output
would overflow u64.
Sourcepub fn input_microdollars(&self) -> u64
pub fn input_microdollars(&self) -> u64
Cost of the input (prompt) in microdollars.
Sourcepub fn output_microdollars(&self) -> u64
pub fn output_microdollars(&self) -> u64
Cost of the output (completion) in microdollars.
Sourcepub fn total_microdollars(&self) -> u64
pub fn total_microdollars(&self) -> u64
Total cost (input + output) in microdollars.
Sourcepub fn checked_add(&self, rhs: &Self) -> Option<Self>
pub fn checked_add(&self, rhs: &Self) -> Option<Self>
Returns the sum of two costs, or None on overflow.
Sourcepub fn total_usd(&self) -> f64
pub fn total_usd(&self) -> f64
Total cost in US dollars, for display purposes.
Uses floating-point division — prefer
total_microdollars for arithmetic.
Trait Implementations§
Source§impl AddAssign for Cost
impl AddAssign for Cost
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Performs the
+= operation. Read moreSource§impl<'de> Deserialize<'de> for Cost
impl<'de> Deserialize<'de> for Cost
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
impl Eq for Cost
impl StructuralPartialEq for Cost
Auto Trait Implementations§
impl Freeze for Cost
impl RefUnwindSafe for Cost
impl Send for Cost
impl Sync for Cost
impl Unpin for Cost
impl UnwindSafe for Cost
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.