use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use rig_agent::agent::{
AgentHook, CompletionCallAction, CompletionCallEvent, HookContext, ModelTurnAction,
ModelTurnFinished,
};
use rig_agent::completion::AssistantContent;
use crate::cost::{cost_for_model, CostLedger, ModelPricing};
use crate::error::LlmWasmError;
use crate::types::Usage;
impl From<&rig_agent::completion::Usage> for Usage {
fn from(u: &rig_agent::completion::Usage) -> Self {
let input = u.input_tokens.unwrap_or(0);
let read = u.cached_input_tokens.unwrap_or(0).min(input);
let written = u.cache_creation_input_tokens.unwrap_or(0).min(input - read);
let output = u.output_tokens.unwrap_or(0);
let reasoning = u.reasoning_tokens.unwrap_or(0).min(output);
Usage::new(input - read - written, output - reasoning)
.with_cache_read(read)
.with_cache_write(written)
.with_reasoning(reasoning)
}
}
#[derive(Debug, Clone)]
pub struct CostLedgerHook {
ledger: Arc<Mutex<CostLedger>>,
model: String,
pricing: Option<ModelPricing>,
stop_below_usd: f64,
}
impl CostLedgerHook {
pub fn new(model: impl Into<String>, budget_usd: f64) -> Self {
Self::with_ledger(
Arc::new(Mutex::new(CostLedger::with_budget(budget_usd))),
model,
)
}
pub fn with_ledger(ledger: Arc<Mutex<CostLedger>>, model: impl Into<String>) -> Self {
Self {
ledger,
model: model.into(),
pricing: None,
stop_below_usd: 0.0,
}
}
pub fn with_pricing(mut self, pricing: ModelPricing) -> Self {
self.pricing = Some(pricing);
self
}
pub fn stop_below_usd(mut self, usd: f64) -> Self {
self.stop_below_usd = if usd.is_finite() { usd.max(0.0) } else { 0.0 };
self
}
pub fn ledger(&self) -> Arc<Mutex<CostLedger>> {
Arc::clone(&self.ledger)
}
pub fn model(&self) -> &str {
&self.model
}
pub fn spent_usd(&self) -> f64 {
self.lock().total_usd()
}
pub fn turn_cost_usd(&self, usage: &rig_agent::completion::Usage) -> Result<f64, LlmWasmError> {
let ours = Usage::from(usage);
if let Some(pricing) = &self.pricing {
return Ok(pricing.cost(&ours));
}
match cost_for_model(&self.model, &ours) {
Err(LlmWasmError::UnknownModel(model)) => usage
.cost
.as_ref()
.filter(|c| c.is_complete() && c.total.is_finite() && c.total >= 0.0)
.map(|c| c.total)
.ok_or(LlmWasmError::UnknownModel(model)),
other => other,
}
}
fn lock(&self) -> MutexGuard<'_, CostLedger> {
self.ledger.lock().unwrap_or_else(PoisonError::into_inner)
}
fn stop_reason(&self, ledger: &CostLedger) -> String {
format!(
"USD budget reached for {}: spent ${:.6} of ${:.6}",
self.model,
ledger.total_usd(),
ledger.budget_usd().unwrap_or(f64::INFINITY)
)
}
}
impl AgentHook for CostLedgerHook {
fn name(&self) -> Option<String> {
Some(format!(
"llm_wasm::CostLedgerHook({}, stop_below={})",
self.model, self.stop_below_usd
))
}
async fn on_completion_call(
&self,
_ctx: &HookContext,
_event: CompletionCallEvent<'_>,
) -> CompletionCallAction {
let ledger = self.lock();
match ledger.remaining_usd() {
Some(left) if left <= self.stop_below_usd || ledger.exceeded_budget() => {
CompletionCallAction::Stop(self.stop_reason(&ledger))
}
_ => CompletionCallAction::Continue,
}
}
async fn on_model_turn_finished(
&self,
_ctx: &HookContext,
event: ModelTurnFinished<'_>,
) -> ModelTurnAction {
let cost = match self.turn_cost_usd(&event.usage) {
Ok(cost) => cost,
Err(e) => {
return ModelTurnAction::Stop(format!(
"cannot price this turn, so the budget cannot hold: {e}"
))
}
};
let mut ledger = self.lock();
if let Err(e) = ledger.record_paid_usd(cost) {
return ModelTurnAction::Stop(e.to_string());
}
let spent = match ledger.remaining_usd() {
Some(left) => left <= 0.0 || ledger.exceeded_budget(),
None => false,
};
if spent
&& event
.content
.iter()
.any(|c| matches!(c, AssistantContent::ToolCall(_)))
{
ModelTurnAction::Stop(self.stop_reason(&ledger))
} else {
ModelTurnAction::Continue
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use rig_agent::completion::Usage as RigUsage;
#[test]
fn rig_usage_is_split_by_billed_kind() {
let u = RigUsage::new()
.input_tokens(10_000)
.cached_input_tokens(8_000)
.cache_creation_input_tokens(1_000)
.output_tokens(500)
.reasoning_tokens(200);
assert_eq!(
Usage::from(&u),
Usage::new(1_000, 300)
.with_cache_read(8_000)
.with_cache_write(1_000)
.with_reasoning(200)
);
let odd = RigUsage::new()
.input_tokens(10)
.cached_input_tokens(8)
.cache_creation_input_tokens(8)
.reasoning_tokens(5);
assert_eq!(
Usage::from(&odd),
Usage::new(0, 0).with_cache_read(8).with_cache_write(2)
);
assert_eq!(Usage::from(&RigUsage::new()), Usage::default());
}
#[test]
fn builder_and_accessors() {
let hook = CostLedgerHook::new("gpt-4o", 1.0).stop_below_usd(f64::NAN);
assert_eq!(hook.model(), "gpt-4o");
assert_eq!(hook.stop_below_usd, 0.0);
assert_eq!(hook.spent_usd(), 0.0);
assert_eq!(hook.ledger().lock().unwrap().budget_usd(), Some(1.0));
assert!(hook.name().unwrap().contains("gpt-4o"));
}
}