From b6679d70359dd8fc9b3b5c20111ad77c567d37a6 Mon Sep 17 00:00:00 2001 From: Matt Busel Date: Sat, 14 Nov 2026 20:34:54 -0500 Subject: [PATCH] Add UsdBudget: USD-denominated Budget via llm-budget (feature usd-budget) InMemoryBudget counts tokens, but tokens are a leaky proxy for money: a million tokens differs by two orders of magnitude between haiku-class and frontier models, and the flat cache-read percent only approximates one provider's cache pricing. UsdBudget implements Budget over llm_budget::AgentBudget: every call is priced from a built-in table of 232 models (prompt-cache reads and writes at their real rates) and booked against a hard USD cap enforced with CAS reservations, with an audit log and a kill switch. The token-denominated trait reports micro-USD. - record_usage prices the full cache split at real rates - record (no split) prices every token at the output rate, worst case - construction rejects unknown models instead of silently pricing /usr/bin/bash - 7 unit tests; clippy -D warnings clean; pre-existing ladder doctest failure (needs 'testing' feature) untouched --- Cargo.lock | 66 +++++++- crates/borg/Cargo.toml | 5 + crates/borg/src/lib.rs | 4 + crates/borg/src/usd_budget.rs | 282 ++++++++++++++++++++++++++++++++++ 4 files changed, 351 insertions(+), 6 deletions(-) create mode 100644 crates/borg/src/usd_budget.rs diff --git a/Cargo.lock b/Cargo.lock index b36589b..6ad13b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -107,9 +107,11 @@ dependencies = [ "borg-macros", "criterion", "futures", + "llm-budget", "parking_lot", "serde", "serde_json", + "thiserror 2.0.18", "tokio", "tokio-util", "tracing", @@ -129,7 +131,7 @@ dependencies = [ "parking_lot", "serde", "serde_json", - "thiserror", + "thiserror 2.0.18", "tokio", "tokio-util", "tracing", @@ -161,7 +163,7 @@ dependencies = [ "rmcp", "serde", "serde_json", - "thiserror", + "thiserror 2.0.18", "tokio", "tokio-util", "tracing", @@ -178,7 +180,7 @@ dependencies = [ "serde", "serde_json", "tempfile", - "thiserror", + "thiserror 2.0.18", "tokio", "tokio-util", "tracing", @@ -194,7 +196,7 @@ dependencies = [ "serde", "serde_json", "similar", - "thiserror", + "thiserror 2.0.18", "tokio", ] @@ -427,6 +429,19 @@ dependencies = [ "syn 2.0.117", ] +[[package]] +name = "dashmap" +version = "5.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856" +dependencies = [ + "cfg-if", + "hashbrown 0.14.5", + "lock_api", + "once_cell", + "parking_lot_core", +] + [[package]] name = "dyn-clone" version = "1.0.20" @@ -591,6 +606,12 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + [[package]] name = "hashbrown" version = "0.15.5" @@ -731,6 +752,19 @@ version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" +[[package]] +name = "llm-budget" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11f50ffa1e7efd987ea86755676661d8eb488f87bf9f72434ad76eb6b81bb4cf" +dependencies = [ + "dashmap", + "serde", + "serde_json", + "thiserror 1.0.69", + "tokio", +] + [[package]] name = "lock_api" version = "0.4.14" @@ -1029,7 +1063,7 @@ dependencies = [ "schemars", "serde", "serde_json", - "thiserror", + "thiserror 2.0.18", "tokio", "tokio-stream", "tokio-util", @@ -1263,13 +1297,33 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl 1.0.69", +] + [[package]] name = "thiserror" version = "2.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4" dependencies = [ - "thiserror-impl", + "thiserror-impl 2.0.18", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", ] [[package]] diff --git a/crates/borg/Cargo.toml b/crates/borg/Cargo.toml index b9deea9..bf9effe 100644 --- a/crates/borg/Cargo.toml +++ b/crates/borg/Cargo.toml @@ -18,15 +18,20 @@ default = [] # Exposes `borg::testing::*` (FakeLlmClient, ScriptedAgent, …) so # consumers can test borg pipelines without re-implementing fakes. testing = [] +# `borg::UsdBudget`: a `Budget` that books real USD via the `llm-budget` +# crate's price table and hard cap, instead of counting tokens. +usd-budget = ["dep:llm-budget", "dep:thiserror"] [dependencies] async-stream.workspace = true async-trait.workspace = true borg-macros.workspace = true futures.workspace = true +llm-budget = { version = "0.3", optional = true } parking_lot.workspace = true serde.workspace = true serde_json.workspace = true +thiserror = { workspace = true, optional = true } tokio.workspace = true tokio-util.workspace = true tracing.workspace = true diff --git a/crates/borg/src/lib.rs b/crates/borg/src/lib.rs index 755c46f..0b57226 100644 --- a/crates/borg/src/lib.rs +++ b/crates/borg/src/lib.rs @@ -159,6 +159,8 @@ pub mod scheduled; pub mod streaming; pub mod suspend; pub mod tools; +#[cfg(feature = "usd-budget")] +pub mod usd_budget; mod agent; mod composition; @@ -218,6 +220,8 @@ pub use agent::{BorgAgent, BoxedAgent, Provider}; pub use budget::{ in_memory_budget, sub_budget, Budget, BudgetVerdict, InMemoryBudget, SubBudget, TokenUsage, }; +#[cfg(feature = "usd-budget")] +pub use usd_budget::{UsdBudget, UsdBudgetError, MICRO_USD}; pub use checkpoint::{ composition_fingerprint, CheckpointPolicy, CheckpointStore, InMemoryCheckpointStore, Snapshot, SNAPSHOT_SCHEMA_VERSION, diff --git a/crates/borg/src/usd_budget.rs b/crates/borg/src/usd_budget.rs new file mode 100644 index 0000000..bc05cea --- /dev/null +++ b/crates/borg/src/usd_budget.rs @@ -0,0 +1,282 @@ +//! USD-denominated budgets, backed by [`llm_budget`]. +//! +//! borg's built-in [`InMemoryBudget`](crate::InMemoryBudget) counts tokens. +//! Tokens are a proxy for money, but the proxy leaks: a million tokens on a +//! haiku-class model and a million tokens on a frontier model differ by two +//! orders of magnitude, and the cache-read discount only approximates the +//! real cache pricing of one provider. [`UsdBudget`] books actual dollars +//! instead: every completed call is priced with [`llm_budget`]'s built-in +//! price table (232 models, prompt-cache reads and writes included) and +//! charged against a hard USD cap. +//! +//! The [`Budget`] trait counts in `u64`, so this implementation denominates +//! in **micro-USD** (1 USD = 1,000,000 units): `limit()`, `consumed()` and +//! `remaining()` report micro-dollars, and `min_per_branch` in +//! [`admit_parallel`](Budget::admit_parallel) is an estimated micro-dollar +//! cost per branch. +//! +//! ```no_run +//! use borg::{Budget, TokenUsage, UsdBudget}; +//! +//! # fn main() -> Result<(), borg::UsdBudgetError> { +//! // Cap this composition tree at $0.25 of claude-haiku-class spend. +//! let budget = UsdBudget::new(0.25, "claude-haiku-4-5")?; +//! assert!(budget.can_invoke()); +//! +//! // After a call, charge what the provider reported: +//! let verdict = budget.record_usage(&TokenUsage::new(12_000, 400).with_cache(8_000, 0)); +//! # let _ = verdict; +//! # Ok(()) +//! # } +//! ``` + +use crate::budget::{Budget, BudgetVerdict, TokenUsage}; + +/// Micro-USD per dollar: the unit [`UsdBudget`] reports through the +/// token-denominated [`Budget`] trait. +pub const MICRO_USD: u64 = 1_000_000; + +/// Errors constructing a [`UsdBudget`]. +#[derive(Debug, thiserror::Error)] +#[non_exhaustive] +pub enum UsdBudgetError { + /// The model id is not in [`llm_budget`]'s price table, so usage could + /// not be priced. + #[error("unknown model for pricing: {0}")] + UnknownModel(String), + /// The underlying budget rejected its configuration. + #[error("budget config: {0}")] + Config(#[from] llm_budget::BudgetError), +} + +/// A [`Budget`] that tracks real dollars instead of tokens. +/// +/// Wraps [`llm_budget::AgentBudget`]: a hard cap enforced with +/// compare-and-swap reservations, an audit log of every charge, and a kill +/// switch. Model prices come from [`llm_budget::PriceTable::builtin`]; pass a +/// custom table with [`UsdBudget::with_price_table`] to pin or extend prices. +/// +/// All calls take `&self` and are safe to share across parallel branches, +/// matching the trait's contract. +pub struct UsdBudget { + budget: llm_budget::AgentBudget, + model: String, + prices: std::sync::Arc, +} + +impl UsdBudget { + /// A `limit_usd`-dollar cap for calls to `model`. + /// + /// `model` is any id in the built-in price table (provider-prefixed or + /// bare, e.g. `"claude-haiku-4-5"` or `"gpt-4o"`). The same + /// model prices every charge on this budget; compositions that mix + /// models need one budget per model wrapped in a + /// [`SubBudget`](crate::SubBudget)-style split, or a custom price table + /// keyed to a synthetic id. + /// + /// # Errors + /// [`UsdBudgetError::UnknownModel`] when `model` is not priced, or + /// [`UsdBudgetError::Config`] when `limit_usd` is not positive. + pub fn new(limit_usd: f64, model: impl Into) -> Result { + Self::with_price_table( + limit_usd, + model, + std::sync::Arc::new(llm_budget::PriceTable::builtin().clone()), + ) + } + + /// [`new`](Self::new) with a caller-supplied price table, for models the + /// built-in table does not price or to pin prices at a fixed date. + /// + /// # Errors + /// See [`new`](Self::new). + pub fn with_price_table( + limit_usd: f64, + model: impl Into, + prices: std::sync::Arc, + ) -> Result { + let model = model.into(); + if prices.get(&model).is_none() { + return Err(UsdBudgetError::UnknownModel(model)); + } + let budget = llm_budget::AgentBudget::builder("borg", limit_usd).build()?; + Ok(Self { + budget, + model, + prices, + }) + } + + /// The wrapped [`llm_budget::AgentBudget`], for the parts the [`Budget`] + /// trait has no spelling for: the audit log, the kill switch, tier + /// downgrades. + pub fn inner(&self) -> &llm_budget::AgentBudget { + &self.budget + } + + /// Price `usage` at this budget's model, in USD. + fn cost_of(&self, usage: &llm_budget::Usage) -> f64 { + // Construction validated the model, so this lookup cannot fail. + self.prices + .get(&self.model) + .map_or(0.0, |p| p.cost_usage(usage)) + } + + /// Book `cost_usd` and translate the outcome into a [`BudgetVerdict`]. + fn charge(&self, cost_usd: f64, input_tokens: u64, output_tokens: u64) -> BudgetVerdict { + let outcome = self.budget.charge_usd_force( + cost_usd, + llm_budget::ChargeContext::new() + .model(self.model.clone()) + .tokens(input_tokens, output_tokens), + ); + if outcome.total_spent_usd > self.budget.limit_usd() { + BudgetVerdict::Overdrawn + } else if outcome.over_cap || self.budget.remaining_usd() <= 0.0 { + BudgetVerdict::Exhausted + } else { + BudgetVerdict::Ok + } + } +} + +fn usd_to_micro(usd: f64) -> u64 { + if usd.is_finite() && usd > 0.0 { + (usd * MICRO_USD as f64).round() as u64 + } else { + 0 + } +} + +impl std::fmt::Debug for UsdBudget { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("UsdBudget") + .field("model", &self.model) + .field("limit_usd", &self.budget.limit_usd()) + .field("spent_usd", &self.budget.spent_usd()) + .finish_non_exhaustive() + } +} + +impl Budget for UsdBudget { + /// Settled spend so far, in micro-USD. + fn consumed(&self) -> u64 { + usd_to_micro(self.budget.spent_usd()) + } + + /// Unspent cap, in micro-USD. + fn remaining(&self) -> u64 { + usd_to_micro(self.budget.remaining_usd()) + } + + /// The cap, in micro-USD. + fn limit(&self) -> u64 { + usd_to_micro(self.budget.limit_usd()) + } + + fn can_invoke(&self) -> bool { + !self.budget.is_exhausted() && !self.budget.is_killed() + } + + /// Charge `tokens` with no usage split. Every token is priced at the + /// model's **output** rate — the worst case — so a caller that cannot + /// split input from output cannot silently underdraw. Prefer + /// [`record_usage`](Budget::record_usage) when the split is known. + fn record(&self, tokens: u64) -> BudgetVerdict { + let usage = llm_budget::Usage::new(0, tokens); + self.charge(self.cost_of(&usage), 0, tokens) + } + + /// Charge a completed call from its full [`TokenUsage`], with prompt-cache + /// reads and writes priced at the model's real cache rates. + fn record_usage(&self, usage: &TokenUsage) -> BudgetVerdict { + let u = llm_budget::Usage { + input_tokens: usage.input_tokens, + cached_input_tokens: usage.cache_read_tokens, + cache_write_tokens: usage.cache_creation_tokens, + output_tokens: usage.output_tokens, + }; + self.charge(self.cost_of(&u), usage.input_tokens, usage.output_tokens) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::budget::ParallelAdmission; + + fn haiku_budget(limit_usd: f64) -> UsdBudget { + UsdBudget::new(limit_usd, "claude-haiku-4-5").unwrap() + } + + #[test] + fn reports_limit_and_remaining_in_micro_usd() { + let b = haiku_budget(0.25); + assert_eq!(b.limit(), 250_000); + assert_eq!(b.consumed(), 0); + assert_eq!(b.remaining(), 250_000); + assert!(b.can_invoke()); + } + + #[test] + fn record_usage_prices_cache_reads_at_cache_rate() { + // 100k fresh input, 900k cache reads, 1k output on haiku + // ($1.00/M in, $0.10/M cached read, $5.00/M out): + // 0.1 * 1.00 + 0.9 * 0.10 + 0.001 * 5.00 = 0.10 + 0.09 + 0.005 = $0.195 + let b = haiku_budget(1.0); + let verdict = b.record_usage(&TokenUsage::new(1_000_000, 1_000).with_cache(900_000, 0)); + assert_eq!(verdict, BudgetVerdict::Ok); + let spent = b.budget.spent_usd(); + assert!((spent - 0.195).abs() < 1e-9, "spent {spent}"); + assert_eq!(b.consumed(), 195_000); + } + + #[test] + fn cap_exhausts_and_denies() { + // $0.001 cap; one 10k/1k haiku call costs 0.01 + 0.005 = $0.015. + let b = haiku_budget(0.001); + let verdict = b.record_usage(&TokenUsage::new(10_000, 1_000)); + assert_eq!(verdict, BudgetVerdict::Overdrawn); + assert!(!b.can_invoke()); + } + + #[test] + fn record_without_split_charges_output_rate() { + // 1k undifferentiated tokens at haiku's $5.00/M output rate = $0.005. + let b = haiku_budget(1.0); + assert_eq!(b.record(1_000), BudgetVerdict::Ok); + assert!((b.budget.spent_usd() - 0.005).abs() < 1e-12); + } + + #[test] + fn parallel_admission_runs_in_micro_usd() { + let b = haiku_budget(0.000030); // 30 micro-USD + assert_eq!(b.admit_parallel(5, 10), ParallelAdmission::Partial { allowed: 3 }); + } + + #[test] + fn unknown_model_is_rejected_at_construction() { + let err = match UsdBudget::new(1.0, "not-a-real-model") { + Ok(_) => panic!("unknown model must not construct"), + Err(e) => e, + }; + assert!(matches!(err, UsdBudgetError::UnknownModel(_))); + } + + #[test] + fn custom_price_table_prices_custom_models() { + let mut table = llm_budget::PriceTable::new(); + table.insert(llm_budget::ModelPrice { + model: "my-model".into(), + provider: "test".into(), + input_per_million: 1.0, + output_per_million: 2.0, + cache_read_per_million: None, + cache_write_per_million: None, + max_input_tokens: None, + }); + let b = UsdBudget::with_price_table(1.0, "my-model", std::sync::Arc::new(table)).unwrap(); + assert_eq!(b.record_usage(&TokenUsage::new(1_000, 1_000)), BudgetVerdict::Ok); + assert!((b.budget.spent_usd() - 0.003).abs() < 1e-12); + } +}