model/context: per-model ctx window overrides + expose window size in /api/state

This commit is contained in:
damocles 2026-05-20 15:20:07 +02:00 committed by Mara
parent 9064cd3c57
commit 770cbaccf9
3 changed files with 68 additions and 23 deletions

View file

@ -303,8 +303,30 @@ pub fn default_model() -> &'static str {
/// Overridable at runtime via `HIVE_CONTEXT_WINDOW_TOKENS` (useful for
/// future models or when the operator knows the exact limit). The env
/// var takes precedence over the model-name heuristic.
///
/// Resolution order (first match wins):
/// 1. `HIVE_CONTEXT_WINDOW_TOKENS_<KEY>` — per-model override where KEY
/// (case-insensitive) is a substring of the active model name.
/// Set by `hyperhive.contextWindowTokens.<key>` in `agent.nix`.
/// 2. `HIVE_CONTEXT_WINDOW_TOKENS` — global override (any model).
/// 3. Auto-derive: haiku → 200 000, sonnet / opus → 1 000 000.
#[must_use]
pub fn context_window_tokens(model: &str) -> u64 {
let m = model.to_ascii_lowercase();
// Per-model overrides: HIVE_CONTEXT_WINDOW_TOKENS_<KEY_UPPER> where
// KEY (lowercased) must be a non-empty substring of the model name.
for (key, val) in std::env::vars() {
if let Some(suffix) = key.strip_prefix("HIVE_CONTEXT_WINDOW_TOKENS_") {
if !suffix.is_empty() && m.contains(&suffix.to_ascii_lowercase()) {
if let Ok(v) = val.trim().parse::<u64>() {
if v > 0 {
return v;
}
}
}
}
}
// Global override.
if let Ok(s) = std::env::var("HIVE_CONTEXT_WINDOW_TOKENS") {
if let Ok(v) = s.trim().parse::<u64>() {
if v > 0 {
@ -312,7 +334,7 @@ pub fn context_window_tokens(model: &str) -> u64 {
}
}
}
let m = model.to_ascii_lowercase();
// Auto-derive from model family.
if m.contains("sonnet") || m.contains("opus") {
1_000_000
} else {