use crate::config::ModelOverride;
use crate::support::settings;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Json {
Schema,
Object,
None,
}
impl Json {
fn parse(value: &str) -> Option<Self> {
match value {
"schema" => Some(Self::Schema),
"object" => Some(Self::Object),
"none" => Some(Self::None),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModelLimits {
pub max_output_tokens: u64,
pub temperature: bool,
pub json: Json,
pub reasoning: bool,
pub reasoning_effort: Option<String>,
}
type Row = (&'static str, u64, bool, Json, bool, Option<&'static str>);
const fn row(
prefix: &'static str,
tokens: u64,
temperature: bool,
json: Json,
reasoning: bool,
effort: Option<&'static str>,
) -> Row {
(prefix, tokens, temperature, json, reasoning, effort)
}
const OPENAI: &[Row] = &[
row("gpt-3.5-turbo", 4096, true, Json::Object, false, None),
row("gpt-4", 8192, true, Json::None, false, None),
row("gpt-4-turbo", 4096, true, Json::Object, false, None),
row("gpt-4-1106", 4096, true, Json::Object, false, None),
row("gpt-4-0125", 4096, true, Json::Object, false, None),
row("gpt-4o", 16384, true, Json::Schema, false, None),
row("gpt-4o-2024-05-13", 4096, true, Json::Object, false, None),
row("gpt-4o-mini", 16384, true, Json::Schema, false, None),
row("gpt-4.1", 32768, true, Json::Schema, false, None),
row("o1", 100_000, false, Json::Schema, true, None),
row("o1-mini", 65536, false, Json::None, true, None),
row("o3", 100_000, false, Json::Schema, true, None),
row("o4-mini", 100_000, false, Json::Schema, true, None),
row("gpt-5", 128_000, false, Json::Schema, true, Some("minimal")),
row("gpt-5.", 128_000, false, Json::Schema, true, None),
row("gpt-5.1", 128_000, false, Json::Schema, false, Some("none")),
row("gpt-5.2", 128_000, false, Json::Schema, false, Some("none")),
row("gpt-5.4", 128_000, false, Json::Schema, false, Some("none")),
row("gpt-5.5", 128_000, false, Json::Schema, false, Some("none")),
row("gpt-5.6", 128_000, false, Json::Schema, false, Some("none")),
row("gpt-6", 128_000, false, Json::Schema, true, Some("low")),
row("gpt-6-sol", 128_000, false, Json::Schema, false, Some("none")),
row("gpt-6-luna", 128_000, false, Json::Schema, false, Some("none")),
];
const CLAUDE: &[Row] = &[
row("claude-sonnet-4-5", 64_000, true, Json::None, false, None),
row("claude-haiku-4-5", 64_000, true, Json::None, false, None),
row("claude-opus-4-5", 64_000, true, Json::None, false, None),
row("claude-opus-4-6", 128_000, true, Json::None, false, None),
row("claude-sonnet-4-6", 128_000, true, Json::None, false, None),
row("claude-opus-4-7", 128_000, false, Json::None, false, None),
row("claude-opus-4-8", 128_000, false, Json::None, false, None),
row("claude-opus-5", 128_000, false, Json::None, false, None),
row("claude-sonnet-5", 128_000, false, Json::None, false, None),
row("claude-fable-5", 128_000, false, Json::None, false, None),
row("claude-mythos-5", 128_000, false, Json::None, false, None),
row("claude-opus-5-5", 128_000, false, Json::None, true, None),
row("claude-sonnet-5-5", 128_000, false, Json::None, true, None),
row("claude-haiku-5-5", 128_000, false, Json::None, true, None),
row("claude-fable-5-1", 128_000, false, Json::None, true, None),
row("claude-mythos-5-1", 128_000, false, Json::None, true, None),
];
const UNKNOWN: Row = row("", 4096, false, Json::None, false, None);
impl ModelLimits {
pub fn for_model(provider: &str, model: &str) -> Self {
let table = if provider == "claude" { CLAUDE } else { OPENAI };
let mut rows: Vec<(String, Self)> = table
.iter()
.map(|(prefix, tokens, temperature, json, reasoning, effort)| {
(
(*prefix).to_owned(),
Self {
max_output_tokens: *tokens,
temperature: *temperature,
json: *json,
reasoning: *reasoning,
reasoning_effort: effort.map(str::to_owned),
},
)
})
.collect();
settings(|config| {
for (prefix, row) in &config.models {
match rows.iter_mut().find(|(known, _)| known == prefix) {
Some((_, limits)) => limits.apply(row),
None => {
let mut limits = Self::unknown();
limits.apply(row);
rows.push((prefix.clone(), limits));
}
}
}
});
rows.into_iter()
.filter(|(prefix, _)| model.starts_with(prefix.as_str()))
.max_by_key(|(prefix, _)| prefix.len())
.map_or_else(Self::unknown, |(_, limits)| limits)
}
fn unknown() -> Self {
let (_, tokens, temperature, json, reasoning, effort) = UNKNOWN;
Self {
max_output_tokens: tokens,
temperature,
json,
reasoning,
reasoning_effort: effort.map(str::to_owned),
}
}
fn apply(&mut self, row: &ModelOverride) {
if let Some(tokens) = row.max_output_tokens {
self.max_output_tokens = tokens.max(1);
}
if let Some(temperature) = row.temperature {
self.temperature = temperature;
}
if let Some(json) = &row.json {
self.json = Json::parse(json).unwrap_or(Json::None);
}
if let Some(reasoning) = row.reasoning {
self.reasoning = reasoning;
}
if let Some(effort) = &row.reasoning_effort {
self.reasoning_effort = Some(effort.clone());
}
}
}