1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//! The session's cost on its usage events (#1670), read from the durable
//! usage ledger as ACP's `usage_update` and `/spend` read it, so it counts
//! turns before a daemon restart and every provider request (retries,
//! compaction, titles, subagents) once.
//!
//! The ledger gains a request only once it completes, so while a response
//! streams, [`TurnCost`] adds what the provider has counted of it so far.
use rho_protocol::SessionEvent as WireEvent;
use rho_sdk::{model::ModelUsage, RunEvent};
use crate::{
app::session_runtime::{CurrentModel, SessionEvent},
usage::{
pricing::{catalog_cost_usd_micros, usage_difference},
report::{catalog_route_pricing, RoutePricing, Valuation},
},
};
/// One turn's pricing of the session's usage events: the ledger's total at
/// each model step and at the run's end, plus, between those, the response
/// under way.
pub(super) struct TurnCost {
/// The catalog price of the model the turn runs on, which values
/// usage the provider put no cost on.
pricing: RoutePricing,
/// The session's cost as the ledger last had it.
ledger: Option<u64>,
/// The run's usage when the ledger was last read: every request it
/// counts has completed, so the ledger holds it.
recorded: ModelUsage,
/// The run's usage as the provider last counted it.
run: ModelUsage,
/// The most the session has been reported to cost since the ledger was
/// last read. A response the provider discards leaves the
/// run's usage, though the ledger will count it, so the cost holds
/// here rather than fall until the next read.
shown: Option<u64>,
}
impl TurnCost {
/// A turn on `model`. Looks its catalog price up once: a stale row is
/// fine for an estimate the ledger settles.
pub(super) fn on(model: &CurrentModel) -> Self {
Self::priced_by(catalog_route_pricing(&model.provider, &model.model))
}
/// A turn on a model the catalog has no price for.
#[cfg(test)]
pub(super) fn unpriced() -> Self {
Self::priced_by(RoutePricing::Unknown)
}
fn priced_by(pricing: RoutePricing) -> Self {
Self {
pricing,
ledger: None,
recorded: ModelUsage::default(),
run: ModelUsage::default(),
shown: None,
}
}
/// `events`, the wire events of the turn's `event`, with the session's
/// cost so far on their usage event. Reads the ledger only for a usage
/// event that no response is streaming under.
pub(super) async fn priced(
&mut self,
event: &SessionEvent,
events: Vec<WireEvent>,
session: &rho_sdk::SessionId,
) -> Vec<WireEvent> {
if let SessionEvent::Run(RunEvent::UsageUpdated { usage }) = event {
return with_cost(events, self.streaming(usage));
}
if !has_usage(&events) {
return events;
}
let ledger = usd_micros(crate::usage::session_valuation(session).await);
self.read_ledger(ledger);
with_cost(events, ledger)
}
/// The session's cost with the response under way at `run`, the run's
/// usage so far: the ledger's total plus what the run used since.
fn streaming(&mut self, run: &ModelUsage) -> Option<u64> {
self.run = run.clone();
let unrecorded = usage_difference(run, Some(&self.recorded));
let unrecorded = unrecorded.cost_usd_micros.or_else(|| match &self.pricing {
RoutePricing::Catalog(metadata) => catalog_cost_usd_micros(&unrecorded, metadata),
RoutePricing::Local | RoutePricing::Unknown => None,
});
let cost = match (self.ledger, unrecorded) {
(None, None) => None,
(ledger, unrecorded) => Some(
ledger
.unwrap_or_default()
.saturating_add(unrecorded.unwrap_or_default()),
),
};
self.shown = self.shown.max(cost);
self.shown
}
/// Takes `ledger` as the session's cost with every request the run has
/// made so far in it.
fn read_ledger(&mut self, ledger: Option<u64>) {
self.ledger = ledger;
self.recorded = self.run.clone();
self.shown = None;
}
}
fn has_usage(events: &[WireEvent]) -> bool {
events
.iter()
.any(|event| matches!(event, WireEvent::Usage { .. }))
}
/// `events` with `cost` on their usage event: one runtime event maps to at
/// most one.
fn with_cost(mut events: Vec<WireEvent>, cost: Option<u64>) -> Vec<WireEvent> {
if let Some(WireEvent::Usage {
cost_usd_micros, ..
}) = events
.iter_mut()
.find(|event| matches!(event, WireEvent::Usage { .. }))
{
*cost_usd_micros = cost;
}
events
}
/// `events`, one runtime event's wire events outside a turn's run, with
/// `session`'s cost so far on their usage event. Reads the ledger only when
/// there is one.
pub(super) async fn priced(events: Vec<WireEvent>, session: &rho_sdk::SessionId) -> Vec<WireEvent> {
if !has_usage(&events) {
return events;
}
with_cost(
events,
usd_micros(crate::usage::session_valuation(session).await),
)
}
/// What `valuation` comes to in millionths of a dollar once a request had
/// a price; none, never 0, otherwise, as for a session that ran only local
/// models (the TUI statusline shows none either).
fn usd_micros(valuation: Valuation) -> Option<u64> {
match valuation {
Valuation::Usd(micros) => Some(micros),
Valuation::Local | Valuation::Unpriced => None,
}
}
#[cfg(test)]
#[path = "cost_tests.rs"]
mod tests;