use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use crate::interface::scene::TokenBudget;
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
Default,
serde::Serialize,
serde::Deserialize,
schemars::JsonSchema,
)]
pub struct Spend {
pub input_tokens: u64,
pub output_tokens: u64,
#[serde(rename = "cache_creation_input_tokens")]
pub cache_creation_tokens: u64,
#[serde(rename = "cache_read_input_tokens")]
pub cache_read_tokens: u64,
}
impl Spend {
pub fn counted_tokens(&self) -> u64 {
self.input_tokens
.saturating_add(self.cache_creation_tokens)
.saturating_add(self.output_tokens)
}
pub fn all_tokens(&self) -> u64 {
self.counted_tokens().saturating_add(self.cache_read_tokens)
}
pub fn as_usage(&self) -> crate::interface::model::Usage {
let narrow = |n: u64| u32::try_from(n).unwrap_or(u32::MAX);
crate::interface::model::Usage {
input_tokens: narrow(self.input_tokens),
output_tokens: narrow(self.output_tokens),
cache_creation_input_tokens: narrow(self.cache_creation_tokens),
cache_read_input_tokens: narrow(self.cache_read_tokens),
}
}
pub fn max(self, other: Self) -> Self {
Self {
input_tokens: self.input_tokens.max(other.input_tokens),
output_tokens: self.output_tokens.max(other.output_tokens),
cache_creation_tokens: self.cache_creation_tokens.max(other.cache_creation_tokens),
cache_read_tokens: self.cache_read_tokens.max(other.cache_read_tokens),
}
}
}
impl From<crate::interface::model::Usage> for Spend {
fn from(u: crate::interface::model::Usage) -> Self {
Self {
input_tokens: u.input_tokens as u64,
output_tokens: u.output_tokens as u64,
cache_creation_tokens: u.cache_creation_input_tokens as u64,
cache_read_tokens: u.cache_read_input_tokens as u64,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum Scope {
Turn,
Goal,
Task,
Session,
}
impl Scope {
pub fn whose(self) -> &'static str {
match self {
Self::Turn => "this turn's",
Self::Goal => "the goal's",
Self::Task => "the task's",
Self::Session => "this session's",
}
}
}
#[derive(Debug)]
pub struct SpendNode {
scope: Scope,
limit: AtomicU64,
all: Tally,
delegated: Tally,
above: Option<Arc<SpendNode>>,
own_session: Option<Arc<SpendNode>>,
}
impl Default for SpendNode {
fn default() -> Self {
Self::turn(None, None)
}
}
impl SpendNode {
pub fn session(limit: Option<u64>) -> Self {
Self::node(Scope::Session, limit, None)
}
pub fn goal(limit: Option<u64>, session: Arc<SpendNode>) -> Self {
Self::node(Scope::Goal, limit, Some(session))
}
pub fn task(limit: Option<u64>, session: Arc<SpendNode>) -> Self {
Self::node(Scope::Task, limit, Some(session))
}
pub fn turn(limit: Option<u64>, above: Option<Arc<SpendNode>>) -> Self {
Self::node(Scope::Turn, limit, above)
}
fn node(scope: Scope, limit: Option<u64>, above: Option<Arc<SpendNode>>) -> Self {
Self {
scope,
limit: AtomicU64::new(limit.unwrap_or(0)),
all: Tally::default(),
delegated: Tally::default(),
above,
own_session: None,
}
}
pub fn counting_into(mut self, session: Arc<SpendNode>) -> Self {
self.own_session = Some(session);
self
}
pub fn scope(&self) -> Scope {
self.scope
}
pub fn limit(&self) -> Option<u64> {
Some(self.limit.load(Ordering::Relaxed)).filter(|&l| l > 0)
}
pub fn set_limit(&self, limit: Option<u64>) {
self.limit.store(limit.unwrap_or(0), Ordering::Relaxed);
}
pub fn add(&self, call: crate::interface::model::Usage) {
let call = Spend::from(call);
self.all.add(call);
if let Some(own) = &self.own_session {
own.all.add(call);
}
let mut delegated = self.scope == Scope::Turn;
let mut above = self.above.as_deref();
while let Some(node) = above {
node.all.add(call);
if delegated && node.scope == Scope::Turn {
node.delegated.add(call);
}
delegated &= node.scope == Scope::Turn;
above = node.above.as_deref();
}
}
pub fn read(&self) -> Spend {
self.all.read()
}
pub fn delegated(&self) -> Spend {
self.delegated.read()
}
pub fn restore(&self, spent: Spend) {
self.all.set(spent);
}
pub fn whole_turn(&self) -> Spend {
let mut turn = self;
while let Some(above) = turn.above.as_deref().filter(|a| a.scope == Scope::Turn) {
turn = above;
}
turn.read()
}
pub fn find(&self, scope: Scope) -> Option<&SpendNode> {
self.chain().find(|node| node.scope == scope)
}
fn chain(&self) -> impl Iterator<Item = &SpendNode> {
std::iter::successors(Some(self), |node| node.above.as_deref())
}
fn ceilings(&self) -> impl Iterator<Item = (Scope, u64, u64)> + '_ {
self.chain().filter_map(|node| {
let limit = node.limit()?;
Some((node.scope, limit, node.read().counted_tokens()))
})
}
pub fn remaining(&self) -> Option<(Scope, Remaining)> {
self.ceilings()
.map(|(scope, limit, spent)| {
let tokens = limit.saturating_sub(spent);
(scope, Remaining { tokens, limit })
})
.min_by_key(|(_, left)| left.tokens)
}
pub fn judge(&self) -> (Scope, Spending) {
let mut warned = None;
for (scope, limit, spent) in self.ceilings() {
if spent >= limit {
return (scope, Spending::Exhausted { limit });
}
if warned.is_none() && spent >= limit.saturating_mul(9) / 10 {
warned = Some((scope, limit));
}
}
match warned {
Some((scope, limit)) => (
scope,
Spending::Warn {
reminder: format!(
"<system-reminder>\n{} token budget is nearly exhausted. Keep your \
response concise and wrap up.\n</system-reminder>",
capitalized(scope.whose())
),
limit,
},
),
None => (Scope::Turn, Spending::WithinBudget),
}
}
}
fn capitalized(text: &str) -> String {
let mut chars = text.chars();
match chars.next() {
Some(first) => first.to_uppercase().chain(chars).collect(),
None => String::new(),
}
}
pub fn exhausted_message(scope: Scope, limit: u64) -> String {
format!("{} token budget of {limit} is used up", scope.whose())
}
#[derive(Debug, Default)]
struct Tally {
input_tokens: AtomicU64,
output_tokens: AtomicU64,
cache_creation_tokens: AtomicU64,
cache_read_tokens: AtomicU64,
}
impl Tally {
fn add(&self, call: Spend) {
for (total, part) in [
(&self.input_tokens, call.input_tokens),
(&self.output_tokens, call.output_tokens),
(&self.cache_creation_tokens, call.cache_creation_tokens),
(&self.cache_read_tokens, call.cache_read_tokens),
] {
total.fetch_add(part, Ordering::Relaxed);
}
}
fn set(&self, to: Spend) {
self.input_tokens.store(to.input_tokens, Ordering::Relaxed);
self.output_tokens
.store(to.output_tokens, Ordering::Relaxed);
self.cache_creation_tokens
.store(to.cache_creation_tokens, Ordering::Relaxed);
self.cache_read_tokens
.store(to.cache_read_tokens, Ordering::Relaxed);
}
fn read(&self) -> Spend {
Spend {
input_tokens: self.input_tokens.load(Ordering::Relaxed),
output_tokens: self.output_tokens.load(Ordering::Relaxed),
cache_creation_tokens: self.cache_creation_tokens.load(Ordering::Relaxed),
cache_read_tokens: self.cache_read_tokens.load(Ordering::Relaxed),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Remaining {
pub tokens: u64,
pub limit: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Spending {
WithinBudget,
Warn {
reminder: String,
limit: u64,
},
Exhausted {
limit: u64,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct OutputProgress {
pub accumulated: u64,
pub target: u64,
pub continuations: u32,
pub this_delta: u64,
pub last_delta: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OutputTarget {
Reached,
KeepGoing { nudge: String },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ContextBudget {
pub compact_threshold: usize,
pub compact_keep_recent: usize,
pub hard_cap: Option<usize>,
}
pub trait BudgetPolicy: Send + Sync {
fn on_usage(&self, _spend: &Spend) -> Spending {
Spending::WithinBudget
}
fn remaining(&self, _spend: &Spend) -> Option<Remaining> {
None
}
fn on_output_target(&self, _progress: &OutputProgress) -> OutputTarget {
OutputTarget::Reached
}
fn context_budget(&self, scene: &TokenBudget) -> ContextBudget {
ContextBudget {
compact_threshold: scene.compact_threshold,
compact_keep_recent: scene.compact_keep_recent,
hard_cap: None,
}
}
}
pub const MIN_CALL_TOKENS: u64 = 1024;
#[derive(Default)]
pub struct EngineBudget {
pub compact_threshold: Option<usize>,
pub compact_keep_recent: Option<usize>,
}
impl BudgetPolicy for EngineBudget {
fn on_output_target(&self, p: &OutputProgress) -> OutputTarget {
let met = (p.target as f64 * 0.9) as u64;
let diminishing = p.continuations >= 3 && p.this_delta < 500 && p.last_delta < 500;
if diminishing || p.accumulated >= met {
return OutputTarget::Reached;
}
let remaining = p.target.saturating_sub(p.accumulated);
OutputTarget::KeepGoing {
nudge: format!(
"\
<system-reminder>
Continue working. Used {}/{} output tokens ({} remaining).
</system-reminder>",
p.accumulated, p.target, remaining
),
}
}
fn context_budget(&self, scene: &TokenBudget) -> ContextBudget {
ContextBudget {
compact_threshold: self.compact_threshold.unwrap_or(scene.compact_threshold),
compact_keep_recent: self
.compact_keep_recent
.unwrap_or(scene.compact_keep_recent),
hard_cap: None,
}
}
}
pub struct Capped<P> {
pub inner: P,
pub context_hard_cap: usize,
}
impl<P: BudgetPolicy> BudgetPolicy for Capped<P> {
fn on_usage(&self, spend: &Spend) -> Spending {
self.inner.on_usage(spend)
}
fn remaining(&self, spend: &Spend) -> Option<Remaining> {
self.inner.remaining(spend)
}
fn on_output_target(&self, progress: &OutputProgress) -> OutputTarget {
self.inner.on_output_target(progress)
}
fn context_budget(&self, scene: &TokenBudget) -> ContextBudget {
ContextBudget {
hard_cap: Some(self.context_hard_cap),
..self.inner.context_budget(scene)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn engine() -> EngineBudget {
EngineBudget::default()
}
fn call(input: u32, output: u32) -> crate::interface::model::Usage {
crate::interface::model::Usage {
input_tokens: input,
output_tokens: output,
..Default::default()
}
}
fn turn_capped(limit: u64) -> SpendNode {
SpendNode::turn(Some(limit), None)
}
#[test]
fn the_bill_warns_at_ninety_percent_and_stops_at_the_cap() {
let turn = turn_capped(1_000);
turn.add(call(800, 99));
assert_eq!(turn.judge(), (Scope::Turn, Spending::WithinBudget));
turn.add(call(0, 1));
assert!(matches!(turn.judge(), (Scope::Turn, Spending::Warn { .. })));
turn.add(call(0, 100));
assert_eq!(
turn.judge(),
(Scope::Turn, Spending::Exhausted { limit: 1_000 }),
"the check is `>=`; at the cap the turn is over, not one call later"
);
}
#[test]
fn what_remains_is_the_tightest_ceiling_up_the_chain() {
let session = Arc::new(SpendNode::session(Some(10_000)));
session.restore(Spend {
input_tokens: 7_000,
..Default::default()
});
let goal = Arc::new(SpendNode::goal(Some(5_000), session.clone()));
let turn = Arc::new(SpendNode::turn(Some(4_000), Some(goal.clone())));
let delegate = SpendNode::turn(None, Some(turn.clone()));
delegate.add(call(1_000, 500));
assert_eq!(
delegate.remaining(),
Some((
Scope::Session,
Remaining {
tokens: 1_500,
limit: 10_000
}
))
);
assert_eq!(goal.read().counted_tokens(), 1_500);
assert_eq!(turn.remaining().unwrap().1.tokens, 1_500);
session.set_limit(None);
assert_eq!(
delegate.remaining(),
Some((
Scope::Turn,
Remaining {
tokens: 2_500,
limit: 4_000
}
))
);
assert_eq!(SpendNode::default().remaining(), None);
}
#[test]
fn a_ceiling_reached_anywhere_up_the_chain_ends_the_turn() {
let session = Arc::new(SpendNode::session(Some(1_000)));
let turn = SpendNode::turn(Some(1_000_000), Some(session.clone()));
turn.add(call(1_000, 0));
assert_eq!(
turn.judge(),
(Scope::Session, Spending::Exhausted { limit: 1_000 })
);
let next = SpendNode::turn(None, Some(session));
assert_eq!(next.remaining().unwrap().1.tokens, 0);
}
#[test]
fn delegates_spending_at_once_lose_nothing() {
let shared = Arc::new(turn_capped(40_000));
let usage = crate::interface::model::Usage {
input_tokens: 3,
output_tokens: 2,
cache_creation_input_tokens: 1,
cache_read_input_tokens: 7,
};
let spenders: Vec<_> = (0..8)
.map(|_| {
let shared = shared.clone();
std::thread::spawn(move || {
for _ in 0..1_000 {
shared.add(usage);
}
})
})
.collect();
for spender in spenders {
spender.join().unwrap();
}
assert_eq!(
shared.read(),
Spend {
input_tokens: 24_000,
output_tokens: 16_000,
cache_creation_tokens: 8_000,
cache_read_tokens: 56_000,
}
);
assert_eq!(shared.judge().1, Spending::Exhausted { limit: 40_000 });
}
#[test]
fn a_delegates_call_is_spent_by_every_node_above_it_once() {
let session = Arc::new(SpendNode::session(None));
let root = Arc::new(SpendNode::turn(None, Some(session.clone())));
let child_session = Arc::new(SpendNode::session(None));
let child = Arc::new(
SpendNode::turn(None, Some(root.clone())).counting_into(child_session.clone()),
);
let grandchild = SpendNode::turn(None, Some(child.clone()));
root.add(call(1, 0));
child.add(call(10, 0));
grandchild.add(call(100, 0));
assert_eq!(root.read().input_tokens, 111);
assert_eq!(root.delegated().input_tokens, 110);
assert_eq!(child.read().input_tokens, 110);
assert_eq!(child.delegated().input_tokens, 100);
assert_eq!(grandchild.whole_turn().input_tokens, 111);
assert_eq!(session.read().input_tokens, 111);
assert_eq!(session.delegated().input_tokens, 0);
assert_eq!(child_session.read().input_tokens, 10);
assert_eq!(
grandchild.find(Scope::Session).unwrap().read().input_tokens,
111
);
}
#[test]
fn a_tasks_ceiling_is_its_own_and_its_calls_are_the_sessions() {
let session = Arc::new(SpendNode::session(Some(10_000)));
let budgeted = Arc::new(SpendNode::task(Some(3_000), session.clone()));
let sibling = SpendNode::turn(None, Some(session.clone()));
let child_session = Arc::new(SpendNode::session(None));
let turn =
SpendNode::turn(None, Some(budgeted.clone())).counting_into(child_session.clone());
turn.add(call(2_000, 500));
sibling.add(call(4_000, 0));
assert_eq!(budgeted.read().counted_tokens(), 2_500);
assert_eq!(session.read().counted_tokens(), 6_500);
assert_eq!(child_session.read().counted_tokens(), 2_500);
assert_eq!(budgeted.delegated(), Spend::default());
assert_eq!(session.delegated(), Spend::default());
assert_eq!(turn.whole_turn(), turn.read());
assert_eq!(
turn.remaining(),
Some((
Scope::Task,
Remaining {
tokens: 500,
limit: 3_000
}
))
);
assert_eq!(
sibling.remaining().map(|(scope, _)| scope),
Some(Scope::Session)
);
turn.add(call(500, 0));
assert_eq!(
turn.judge(),
(Scope::Task, Spending::Exhausted { limit: 3_000 })
);
assert_eq!(sibling.judge().1, Spending::WithinBudget);
session.restore(Spend {
input_tokens: 9_900,
..Default::default()
});
let fresh = SpendNode::task(Some(3_000), session);
assert_eq!(
fresh.remaining(),
Some((
Scope::Session,
Remaining {
tokens: 100,
limit: 10_000
}
)),
"a task's ceiling loosened its session's"
);
}
#[test]
fn no_ceiling_configured_means_none() {
let turn = SpendNode::default();
turn.add(call(u32::MAX, 0));
assert_eq!(turn.judge().1, Spending::WithinBudget);
assert_eq!(turn.remaining(), None);
}
#[test]
fn a_cache_read_is_not_counted_and_a_cache_write_is() {
let spend = Spend {
input_tokens: 400,
output_tokens: 600,
cache_creation_tokens: 2_000,
cache_read_tokens: 30_000,
};
assert_eq!(spend.counted_tokens(), 3_000);
assert_eq!(spend.all_tokens(), 33_000);
}
#[test]
fn the_engines_ceiling_does_not_move_when_the_cache_fills() {
let turn = turn_capped(1_000);
turn.add(crate::interface::model::Usage {
input_tokens: 400,
output_tokens: 300,
cache_creation_input_tokens: 100,
cache_read_input_tokens: 90_000,
});
assert!(
matches!(turn.judge().1, Spending::WithinBudget),
"the cache moved a 1000-token ceiling that nobody asked to move"
);
struct CountsTheCache;
impl BudgetPolicy for CountsTheCache {
fn on_usage(&self, spend: &Spend) -> Spending {
if spend.all_tokens() >= 1_000 {
Spending::Exhausted { limit: 1_000 }
} else {
Spending::WithinBudget
}
}
}
assert_eq!(
CountsTheCache.on_usage(&turn.whole_turn()),
Spending::Exhausted { limit: 1_000 },
"the parts a policy needs to reach the other answer are not reachable"
);
}
#[test]
fn a_spend_is_written_with_the_wire_names_of_a_usage() {
let spend = Spend::from(crate::interface::model::Usage {
input_tokens: 11,
output_tokens: 7,
cache_creation_input_tokens: 5,
cache_read_input_tokens: 23,
});
assert_eq!(
serde_json::to_value(spend).unwrap(),
serde_json::to_value(spend.as_usage()).unwrap()
);
}
fn progress(accumulated: u64, continuations: u32, this: u64, last: u64) -> OutputProgress {
OutputProgress {
accumulated,
target: 100_000,
continuations,
this_delta: this,
last_delta: last,
}
}
#[test]
fn ninety_percent_of_the_output_target_counts_as_reached() {
let p = engine();
assert!(matches!(
p.on_output_target(&progress(89_000, 0, 1_000, 0)),
OutputTarget::KeepGoing { .. }
));
assert_eq!(
p.on_output_target(&progress(90_000, 0, 1_000, 0)),
OutputTarget::Reached
);
}
#[test]
fn diminishing_returns_take_all_three_conditions() {
let p = engine();
assert_eq!(
p.on_output_target(&progress(10_000, 3, 400, 400)),
OutputTarget::Reached
);
assert!(matches!(
p.on_output_target(&progress(10_000, 2, 400, 400)),
OutputTarget::KeepGoing { .. },
),);
assert!(matches!(
p.on_output_target(&progress(10_000, 3, 600, 400)),
OutputTarget::KeepGoing { .. }
));
}
#[test]
fn a_long_run_of_substantial_answers_keeps_going() {
assert!(matches!(
engine().on_output_target(&progress(10_000, 20, 1_000, 1_000)),
OutputTarget::KeepGoing { .. }
));
}
#[test]
fn the_scene_decides_the_threshold_until_a_deployment_caps_it() {
let scene = TokenBudget {
compact_threshold: 150_000,
compact_keep_recent: 4,
};
let engine_only = EngineBudget::default().context_budget(&scene);
assert_eq!(engine_only.compact_threshold, 150_000);
assert_eq!(
engine_only.hard_cap, None,
"the shape of the gap this fills"
);
let capped = Capped {
inner: EngineBudget::default(),
context_hard_cap: 120_000,
}
.context_budget(&scene);
assert_eq!(capped.hard_cap, Some(120_000));
assert_eq!(
capped.compact_threshold, 150_000,
"a ceiling is not a trigger — the scene still says when to compact"
);
}
#[test]
fn the_settings_compaction_numbers_override_the_scenes_when_set() {
let scene = TokenBudget {
compact_threshold: 150_000,
compact_keep_recent: 20,
};
let configured = EngineBudget {
compact_threshold: Some(60_000),
compact_keep_recent: Some(4),
}
.context_budget(&scene);
assert_eq!(
(configured.compact_threshold, configured.compact_keep_recent),
(60_000, 4)
);
let half = EngineBudget {
compact_keep_recent: Some(4),
..EngineBudget::default()
}
.context_budget(&scene);
assert_eq!(
(half.compact_threshold, half.compact_keep_recent),
(150_000, 4)
);
}
}