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
//! Per-model usage accumulation for harness runs.
use std::collections::BTreeMap;
use std::future::poll_fn;
use std::pin::Pin;
use ag_contracts::{ContextFill, ModelUsage, PlanLogin, TurnUsage};
use ag_harness::catalog::{ModelId, ModelSpec};
use ag_harness::{Cost, EventStream, RunEventKind, Usage};
use futures_core::Stream as _;
use super::config::ModelSelection;
use crate::agent::usage::{cost_estimate, price};
/// Folds each completed model request of one run into per-model usage.
///
/// The harness prices requests only for models registered with prices, and
/// Agentty registers its models without them, so an unpriced request is
/// priced at the harness provider catalog's list price of the model that
/// answered it. A `ChatGPT` plan model has no price, as its plan bills a flat
/// fee, so its requests carry the API equivalent: the list price of the
/// same-named `OpenAI` API model, or no cost without one. A request's context
/// fill is its reported input against that model's window. The requests' usage
/// sums to the run outcome's usage.
pub(crate) struct UsageMeter {
/// Registration key of the run's model, which answers requests whose
/// event names no catalog model.
run_key: String,
/// Context window the run's model is fitted to.
run_window: Option<u64>,
/// Usage of the requests folded so far.
usage: TurnUsage,
}
impl UsageMeter {
/// Creates a meter for a run on `selection`, whose requests are fitted
/// to the window its profile resolves in `environment`, or to
/// `running_context_window` when a local server reports one.
pub(crate) fn new(
selection: &ModelSelection,
environment: &BTreeMap<String, String>,
running_context_window: Option<u64>,
) -> Self {
Self {
run_key: selection.key(),
run_window: selection.context_window(environment, running_context_window),
usage: TurnUsage::default(),
}
}
/// Folds `event` when it completes a model request and returns the
/// run's cumulative usage after it.
pub(crate) fn observe(&mut self, event: &RunEventKind) -> Option<&TurnUsage> {
let RunEventKind::ModelRequestCompleted {
cost, model, usage, ..
} = event
else {
return None;
};
let request = self.request_usage(*cost, model.as_ref(), usage);
self.usage.record(&request);
Some(&self.usage)
}
/// Folds every model request of `stream` until the run ends and returns
/// the run's usage.
pub(crate) async fn drain(mut self, mut stream: EventStream) -> TurnUsage {
while let Some(event) = poll_fn(|context| Pin::new(&mut stream).poll_next(context)).await {
self.observe(&event.kind);
}
self.finish()
}
/// Returns the usage of every request folded.
pub(crate) fn finish(self) -> TurnUsage {
self.usage
}
/// Converts one completed request answered by `model`.
fn request_usage(
&self,
cost: Option<Cost>,
model: Option<&ModelId>,
usage: &Usage,
) -> ModelUsage {
let key = model.map_or(self.run_key.as_str(), ModelId::as_str);
let selection = ModelSelection::from_key(key);
let spec = selection.as_ref().and_then(ModelSelection::catalog_spec);
let api_twin = selection.as_ref().and_then(ModelSelection::api_twin_spec);
let window = if key == self.run_key {
self.run_window
} else {
spec.as_ref().and_then(ModelSpec::context_window)
};
let model = ModelSelection::model_id_of(key);
// A request whose provider omitted its counts leaves every detail
// and its charge unknown; one that omitted only its output still
// reports what the model saw.
if usage.unreported_requests > 0 {
return ModelUsage {
context: (usage.input > 0).then(|| ContextFill::of_request(usage.input, window)),
input_tokens: usage.input,
output_tokens: usage.output,
..ModelUsage::unreported(model, 1)
};
}
let mut request = ModelUsage {
cache_read_tokens: Some(usage.cache_read),
cache_write_tokens: usage.cache_write,
context: Some(ContextFill::of_request(usage.input, window)),
input_tokens: usage.input,
output_tokens: usage.output,
reasoning_tokens: Some(usage.reasoning),
requests: 1,
..ModelUsage::new(model)
};
let pricing = spec.and_then(|spec| spec.pricing());
request.cost = cost
.map(|cost| cost_estimate(cost, &request, pricing))
.or_else(|| pricing.map(|pricing| price(&request, pricing)))
.or_else(|| {
api_twin
.and_then(|twin| twin.pricing())
.map(|pricing| price(&request, pricing).billed_to(PlanLogin::ChatGpt))
});
request
}
}
#[cfg(test)]
#[path = "usage_test.rs"]
mod tests;