Skip to main content

adk_audio/
metering.rs

1//! How audio providers bill, for metering their calls against the current run.
2//!
3//! A cloud provider reports the vendor and model its calls are billed under
4//! ([`SttProvider::billing`](crate::SttProvider::billing),
5//! [`TtsProvider::billing`](crate::TtsProvider::billing)); a local model bills nothing and
6//! reports `None`. [`TranscribeTool`](crate::TranscribeTool) and
7//! [`SpeakTool`](crate::SpeakTool) meter every billed call through
8//! [`adk_core::meter_usage`], against the run the tool call belongs to:
9//!
10//! | Call | Unit |
11//! |------|------|
12//! | Transcription | [`UsageUnit::AudioMillis`] of the audio sent |
13//! | Synthesis | [`UsageUnit::Characters`] of the text sent |
14//!
15//! The bundled cloud providers carry no per-unit price, so their cost is unknown: under a
16//! run budget's cost cap or a spend-ledger limit the call is refused before it is sent
17//! (opt out of the budget check with `RunBudget::allow_unpriced_models`), and without a cap
18//! the ledger commits the run's per-call spend estimate. A provider built with a price
19//! ([`AudioBilling::with_rate`]) is held and committed at that price.
20//!
21//! # Example
22//!
23//! ```
24//! use adk_audio::AudioBilling;
25//! use adk_core::{UnitRate, UsageUnit, UsageVector};
26//!
27//! // $0.18 per audio hour, billed in whole seconds rounded down.
28//! let billing = AudioBilling::new("muse", "muse-voice-transcribe-1.0").with_rate(
29//!     UnitRate::new(UsageUnit::AudioMillis, 180_000, 3_600_000).rounded_down_to(1_000),
30//! );
31//! let (call, usage) = billing.meter(UsageVector::new().with(UsageUnit::AudioMillis, 90_000));
32//! assert_eq!(call.max_cost_micro_usd, Some(4_500));
33//! assert_eq!(usage.cost_micro_usd, Some(4_500));
34//! ```
35
36use adk_core::{MeteredUsage, UnitRate, UsageCall, UsageVector};
37
38#[cfg(doc)]
39use adk_core::UsageUnit;
40
41/// The vendor and model an audio provider's calls are billed under, and their price when
42/// known.
43#[derive(Debug, Clone, PartialEq, Eq)]
44#[non_exhaustive]
45pub struct AudioBilling {
46    /// The spend key vendor, such as `deepgram` or `openai`.
47    pub vendor: String,
48    /// The model billed, named in budget errors and logs.
49    pub model: String,
50    /// The price per unit, or `None` when unknown.
51    pub rate: Option<UnitRate>,
52}
53
54impl AudioBilling {
55    /// Calls to `model` billed by `vendor`, at an unknown price.
56    pub fn new(vendor: impl Into<String>, model: impl Into<String>) -> Self {
57        Self { vendor: vendor.into(), model: model.into(), rate: None }
58    }
59
60    /// Prices every call at `rate`.
61    pub fn with_rate(mut self, rate: UnitRate) -> Self {
62        self.rate = Some(rate);
63        self
64    }
65
66    /// The metered call and its usage for a call that sends `units`, priced on the rate's
67    /// unit. The input decides an audio call's billed amount, so the call's bound and its
68    /// cost are the same.
69    pub fn meter(&self, units: UsageVector) -> (UsageCall, MeteredUsage) {
70        let cost = self.rate.map(|rate| rate.cost_micro_usd(units.get(rate.unit)));
71        let call = UsageCall::new(self.vendor.clone(), self.model.clone());
72        let usage = MeteredUsage::new(units);
73        match cost {
74            Some(cost) => (call.with_max_cost_micro_usd(cost), usage.with_cost_micro_usd(cost)),
75            None => (call, usage),
76        }
77    }
78}