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
//! The parameters a host gives the requests aither sends each model.
//!
//! A request aither builds starts from the parameters of the model that
//! serves it, as the host set them, and aither then sets the fields it owns
//! for that request:
//!
//! - [`cache`](Parameters::cache) on an agent's turns and compactions, from
//! its [`PromptCache`](crate::context::PromptCache);
//! - [`structured_outputs`](Parameters::structured_outputs) on a structured
//! request — a compaction, or the subagent router;
//! - [`max_tokens`](Parameters::max_tokens) and [`stream`](Parameters::stream)
//! on a prompt-cache keep-alive.
//!
//! Every other field reaches the provider exactly as the host set it.
use aither_core::llm::model::Parameters;
use crate::model_group::ModelTier;
/// The parameters of each model tier of something created on one model.
///
/// The advanced tier is the model it was created with. A tier given a model
/// of its own carries that model's parameters; a tier still on the created-
/// with model runs with that model's parameters, whenever either was set.
///
/// Each set is boxed: they are set once and only cloned onto requests, and
/// held inline they would weigh on every future that holds an agent or its
/// builder.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct TierParameters {
/// The parameters of the model created with.
shared: Box<Parameters>,
/// The balanced tier's own model's parameters, once it has one.
balanced: Option<Box<Parameters>>,
/// The fast tier's own model's parameters, once it has one.
fast: Option<Box<Parameters>>,
}
impl TierParameters {
/// Sets the parameters of the model created with.
pub fn set_shared(&mut self, parameters: Parameters) {
*self.shared = parameters;
}
/// Gives the balanced tier a model of its own, with `parameters`.
pub fn set_balanced(&mut self, parameters: Parameters) {
self.balanced = Some(Box::new(parameters));
}
/// Gives the fast tier a model of its own, with `parameters`.
pub fn set_fast(&mut self, parameters: Parameters) {
self.fast = Some(Box::new(parameters));
}
/// The parameters of the model behind `tier`.
pub fn get(&self, tier: ModelTier) -> &Parameters {
match tier {
ModelTier::Advanced => &self.shared,
ModelTier::Balanced => self.balanced.as_deref().unwrap_or(&self.shared),
ModelTier::Fast => self.fast.as_deref().unwrap_or(&self.shared),
}
}
}
/// A model and the parameters of the requests aither sends it, boxed like
/// [`TierParameters`].
#[derive(Debug, Clone)]
pub struct WithParameters<M> {
pub model: M,
pub parameters: Box<Parameters>,
}
/// Checks on what a request carried to the provider.
#[cfg(test)]
pub mod test_support {
use aither_core::llm::LLMRequest;
use aither_core::llm::model::{Parameters, ReasoningEffort};
/// Parameters a host gives a model, setting no field aither owns.
pub fn host_parameters(effort: ReasoningEffort, temperature: f32) -> Parameters {
Parameters::default()
.reasoning_effort(effort)
.include_reasoning(true)
.temperature(temperature)
.websearch(true)
}
/// `request`'s parameters without the cache controls and structured
/// output aither sets per request: what is left of the host's.
pub fn host_part(request: &LLMRequest) -> Parameters {
let mut parameters = request.parameters().clone().without_cache();
parameters.structured_outputs = false;
parameters
}
/// Asserts `request` carries the prompt-cache controls of an agent's
/// request: an `OpenAI` cache key and Claude breakpoints.
pub fn assert_cache_controls(request: &LLMRequest) {
let cache = &request.parameters().cache;
assert!(
cache
.openai
.as_ref()
.is_some_and(|openai| openai.key.is_some()),
"the request carries the prompt-cache key: {cache:?}"
);
assert!(
cache.claude.is_some(),
"the request carries the Claude cache breakpoints: {cache:?}"
);
}
}
#[cfg(test)]
mod tests {
use super::*;
use aither_core::llm::model::ReasoningEffort;
#[test]
fn a_tier_on_the_created_with_model_takes_its_parameters_in_either_order() {
let shared = Parameters::default().reasoning_effort(ReasoningEffort::High);
let fast = Parameters::default().temperature(0.1);
let mut before = TierParameters::default();
before.set_fast(fast.clone());
before.set_shared(shared.clone());
let mut after = TierParameters::default();
after.set_shared(shared.clone());
after.set_fast(fast.clone());
for tiers in [before, after] {
assert_eq!(tiers.get(ModelTier::Advanced), &shared);
assert_eq!(tiers.get(ModelTier::Balanced), &shared);
assert_eq!(tiers.get(ModelTier::Fast), &fast);
}
}
}