use std::collections::HashSet;
use std::sync::{Mutex, OnceLock};
const NO_SAMPLING_FAMILIES: &[&str] = &[
"claude-opus-5",
"claude-opus-4-8",
"claude-opus-4-7",
"claude-sonnet-5",
"claude-fable-5",
"fable-5",
];
pub fn normalize_model(model: &str) -> String {
let ident = model.split_once(':').map_or(model, |(_, rest)| rest);
ident.trim().to_lowercase()
}
const SAMPLING_PARAMS: &[&str] = &["temperature", "top_p", "top_k"];
const REJECTION_WORDS: &[&str] = &[
"deprecat",
"unsupported",
"not supported",
"not permitted",
"unexpected",
"removed",
"cannot be used",
"no longer",
];
fn learned_no_sampling() -> &'static Mutex<HashSet<String>> {
static LEARNED: OnceLock<Mutex<HashSet<String>>> = OnceLock::new();
LEARNED.get_or_init(Mutex::default)
}
pub fn supports_sampling_params(model: &str) -> bool {
let ident = normalize_model(model);
if learned_no_sampling()
.lock()
.is_ok_and(|learned| learned.contains(&ident))
{
return false;
}
!NO_SAMPLING_FAMILIES
.iter()
.any(|family| ident.starts_with(family))
}
pub fn mark_sampling_unsupported(model: &str) {
if let Ok(mut learned) = learned_no_sampling().lock() {
learned.insert(normalize_model(model));
}
}
pub fn is_sampling_rejection(message: &str) -> bool {
let low = message.to_lowercase();
SAMPLING_PARAMS.iter().any(|param| low.contains(param))
&& REJECTION_WORDS.iter().any(|word| low.contains(word))
}
pub const DEFAULT_CONTEXT_WINDOW: u64 = 8_192;
const CONTEXT_WINDOWS: &[(&str, u64)] = &[
("claude", 200_000),
("gpt-5", 400_000),
("gpt-4.1", 1_047_576),
("gpt-4o", 128_000),
("gpt-4-turbo", 128_000),
("gpt-4", 8_192),
("gpt-3.5", 16_385),
("o4-mini", 200_000),
("o3", 200_000),
("o1", 200_000),
("llama-3.1", 131_072),
("llama3.1", 131_072),
("llama-3.3", 131_072),
("llama3.3", 131_072),
("qwen", 32_768),
("mixtral", 32_768),
("mistral", 32_768),
("gemma", 8_192),
("gemini", 1_048_576),
("phi", 16_384),
];
pub const DEFAULT_OLLAMA_NUM_CTX_MAX: u32 = 32_768;
pub fn ollama_num_ctx(
section: Option<&crate::config::ProviderConfig>,
discover: impl FnOnce() -> Option<u64>,
) -> u32 {
if let Some(num_ctx) = section
.and_then(|section| section.num_ctx)
.filter(|&n| n > 0)
{
return num_ctx;
}
discover()
.filter(|&window| window > 0)
.map_or(DEFAULT_CONTEXT_WINDOW as u32, |window| {
u32::try_from(window).unwrap_or(u32::MAX)
})
}
pub fn context_window_for(model_ref: &str, config: &crate::config::Config) -> u64 {
context_window_with(model_ref, config, |_, _| None)
}
pub fn context_window_with(
model_ref: &str,
config: &crate::config::Config,
discover: impl FnOnce(&str, &str) -> Option<u64>,
) -> u64 {
let (provider, model) = model_ref.split_once(':').unwrap_or(("", model_ref));
for key in [model_ref, model] {
if let Some(&window) = config.model.context_window.get(key).filter(|&&w| w > 0) {
return window;
}
}
if provider == "ollama" {
return u64::from(ollama_num_ctx(config.providers.get(provider), || {
discover(provider, model)
}));
}
if !provider.is_empty() {
if let Some(window) = discover(provider, model).filter(|&window| window > 0) {
return window;
}
}
if provider == "llamacpp" {
return DEFAULT_CONTEXT_WINDOW;
}
let ident = model.trim().to_lowercase();
CONTEXT_WINDOWS
.iter()
.find(|(needle, _)| ident.contains(needle))
.map_or(DEFAULT_CONTEXT_WINDOW, |&(_, window)| window)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn context_windows_follow_overrides_then_the_server_then_the_table() {
let paths = crate::config::ConfigPaths {
user: None,
project: std::path::PathBuf::from("no-such-dir/config.toml"),
project_trust: crate::config::ProjectTrust::Trusted,
};
let mut config = crate::config::load_config(&paths, &Default::default()).unwrap();
assert_eq!(
context_window_for("openrouter:openai/gpt-4o-mini", &config),
128_000
);
assert_eq!(context_window_for("openai:gpt-4-0613", &config), 8_192);
assert_eq!(
context_window_for("openai:gpt-4.1-mini", &config),
1_047_576
);
assert_eq!(context_window_for("local:mystery", &config), 8_192);
assert_eq!(context_window_for("ollama:llama3.1", &config), 8_192);
assert_eq!(context_window_for("llamacpp:qwen3", &config), 8_192);
assert_eq!(
context_window_with("openrouter:qwen/qwen3.7-max", &config, |provider, model| {
assert_eq!((provider, model), ("openrouter", "qwen/qwen3.7-max"));
Some(262_144)
}),
262_144,
"a server's own figure beats the table"
);
config
.model
.context_window
.insert("gpt-4o-mini".into(), 64_000);
assert_eq!(
context_window_for("openrouter:gpt-4o-mini", &config),
64_000
);
config
.model
.context_window
.insert("openrouter:gpt-4o-mini".into(), 32_000);
assert_eq!(
context_window_for("openrouter:gpt-4o-mini", &config),
32_000
);
}
#[test]
fn ollama_asks_for_the_section_setting_else_what_the_server_reports() {
let section = |num_ctx, num_ctx_max| crate::config::ProviderConfig {
num_ctx,
num_ctx_max,
..Default::default()
};
assert_eq!(ollama_num_ctx(None, || Some(262_144)), 262_144);
assert_eq!(ollama_num_ctx(None, || Some(4_096)), 4_096);
assert_eq!(ollama_num_ctx(None, || None), 8_192);
assert_eq!(ollama_num_ctx(None, || Some(0)), 8_192);
assert_eq!(
ollama_num_ctx(Some(§ion(Some(131_072), Some(16_384))), || {
panic!("asked")
}),
131_072
);
let paths = crate::config::ConfigPaths {
user: None,
project: std::path::PathBuf::from("no-such-dir/config.toml"),
project_trust: crate::config::ProjectTrust::Trusted,
};
let mut config = crate::config::load_config(&paths, &Default::default()).unwrap();
let window = context_window_with("ollama:qwen3.5:latest", &config, |provider, model| {
assert_eq!((provider, model), ("ollama", "qwen3.5:latest"));
Some(65_536)
});
assert_eq!(window, 65_536);
config
.model
.context_window
.insert("qwen3.5:latest".into(), 100_000);
assert_eq!(
context_window_for("ollama:qwen3.5:latest", &config),
100_000
);
}
#[test]
fn removed_sampling_families_match_by_prefix() {
assert!(!supports_sampling_params("claude-opus-5-20260101"));
assert!(!supports_sampling_params("anthropic:Claude-Sonnet-5"));
assert!(supports_sampling_params("gpt-4o-mini"));
assert!(supports_sampling_params("claude-sonnet-4-5"));
}
#[test]
fn a_rejected_sampling_parameter_is_learned_for_the_session() {
assert!(is_sampling_rejection(
"Error code: 400 - `temperature` is deprecated for this model."
));
assert!(!is_sampling_rejection("temperature must be at most 1"));
assert!(!is_sampling_rejection("model is unsupported"));
assert!(supports_sampling_params("anthropic:claude-test-learned-9"));
mark_sampling_unsupported("anthropic:Claude-Test-Learned-9");
assert!(!supports_sampling_params("claude-test-learned-9"));
}
}