Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
feat(cli): resolve context windows per model
thinhlpg1c28199a698a
diff --git a/src-tauri/src/core/cli/mod.rs b/src-tauri/src/core/cli/mod.rsindex d91d1cf44..e54bfec59 100644--- a/src-tauri/src/core/cli/mod.rs+++ b/src-tauri/src/core/cli/mod.rs@@ -7,6 +7,7 @@ pub mod brand;pub mod journal;pub mod login;pub mod mcp;+mod model_capabilities;mod path_refs;pub mod run_report;pub mod providers;@@ -739,9 +740,13 @@ struct PersistTarget {/// run of bare numbers, which would be trivial to transpose at a call site.#[derive(Debug, Clone, Copy)]pub(crate) struct SessionLimits {- /// Context window limit in tokens for the model. Defaults to 128K if agent.toml- /// doesn't set it. Used to display `ctx N/K` in the header and trigger compaction.+ /// Context window limit in tokens for the model. Resolution order is the+ /// configured `[agent].context_window` override, then the built-in model+ /// catalog, then a 128K fallback. Used to display `ctx N/K` in the header+ /// and trigger compaction.pub context_window: u64,+ /// Where `context_window` came from: configured override, catalog, or fallback.+ pub context_window_source: crate::core::cli::model_capabilities::ContextWindowSource,/// Tokens reserved for the model's response. Defaults to 16K if unset./// Compaction triggers at `context_window - reserve_tokens`.pub reserve_tokens: u64,@@ -933,13 +938,19 @@ fn prepare_agent_session(flags.sandbox,);+ // Resolution order: configured `[agent].context_window` override, then the+ // built-in model catalog, then the 128K fallback.+ let resolved_window =+ crate::core::cli::model_capabilities::resolve_context_window(&model, cfg.agent.context_window);+Ok(AgentSession {args,permission_requests,model,smol_model,limits: SessionLimits {- context_window: cfg.agent.context_window.unwrap_or(128_000),+ context_window: resolved_window.tokens,+ context_window_source: resolved_window.source,reserve_tokens: cfg.agent.compaction_reserve_tokens.unwrap_or(16_384),max_tokens: cfg.agent.max_tokens,max_session_tokens: cfg.budget.max_tokens.unwrap_or(DEFAULT_MAX_SESSION_TOKENS),diff --git a/src-tauri/src/core/cli/model_capabilities.rs b/src-tauri/src/core/cli/model_capabilities.rsnew file mode 100644index 000000000..4574c96bd--- /dev/null+++ b/src-tauri/src/core/cli/model_capabilities.rs@@ -0,0 +1,181 @@+//! Pure per-model context-window resolution for the CLI.+//!+//! The effective context window comes from the selected model unless the+//! project explicitly configures one. This module resolves that without any+//! provider metadata request: a small fixed catalog of known model families+//! covers the models Jan ships, and everything else falls back to a+//! conservative default. The resolved value drives the header gauge and+//! proactive compaction, and is deliberately never sent up as a generation+//! parameter.++pub(crate) const FALLBACK_CONTEXT_WINDOW: u64 = 128_000;++/// Where a resolved context window came from. `Configured` is authoritative:+/// `[agent].context_window` wins outright. `Catalog` is the built-in model+/// family table. `Fallback` is the conservative default for an unknown model.+#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub(crate) enum ContextWindowSource {+ Configured,+ Catalog,+ Fallback,+}++#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub(crate) struct ResolvedContextWindow {+ pub(crate) tokens: u64,+ pub(crate) source: ContextWindowSource,+}++impl ContextWindowSource {+ /// Short label shown in the header: `ctx N/K <configured|catalog|fallback>`.+ pub(crate) fn label(self) -> &'static str {+ match self {+ ContextWindowSource::Configured => "configured",+ ContextWindowSource::Catalog => "catalog",+ ContextWindowSource::Fallback => "fallback",+ }+ }+}++/// Strip exactly one configured provider qualifier (`anthropic/...`) when the+/// first segment is one of Jan's catalog providers. A user-provided model id is+/// normally the bare id, but `--model anthropic/claude-sonnet-4-6` and the+/// desktop selection can carry the provider prefix; both must resolve alike.+fn strip_provider_qualifier(model_id: &str) -> &str {+ let mut parts = model_id.splitn(2, '/');+ let first = parts.next().unwrap_or("");+ match (first, parts.next()) {+ ("anthropic" | "openai" | "google" | "tokamak" | "jan", Some(rest)) => rest,+ _ => model_id,+ }+}++/// Look up the catalog window for a bare model id (provider qualifier already+/// stripped). `None` means the id matches no known family -> fallback.+fn catalog_window(model_id: &str) -> Option<u64> {+ // Claude family: the two newest releases get 1M, everything else claude 200K.+ if model_id.starts_with("claude-") {+ let is_new = ["haiku", "sonnet", "opus"]+ .iter()+ .any(|family| {+ let prefix = format!("claude-{family}-4-6");+ let prefix7 = format!("claude-{family}-4-7");+ model_id.starts_with(&prefix) || model_id.starts_with(&prefix7)+ });+ return Some(if is_new { 1_000_000 } else { 200_000 });+ }++ // Codex variants are matched before the base gpt-5.x rows they contain.+ if model_id == "gpt-5.1-codex"+ || model_id == "gpt-5.2-codex"+ || model_id == "gpt-5.3-codex"+ {+ return Some(272_000);+ }+ if model_id == "gpt-5.1" || model_id == "gpt-5.2" {+ return Some(400_000);+ }+ if model_id == "gpt-5.4" || model_id == "gpt-5.5" {+ return Some(1_050_000);+ }+ if model_id == "gpt-4" || model_id == "gpt-4o" || model_id == "gpt-4o-mini" {+ return Some(128_000);+ }++ if model_id.starts_with("gemini-2.5-") || model_id.starts_with("gemini-3-") {+ return Some(1_000_000);+ }+ if model_id.starts_with("tokamak-") {+ return Some(200_000);+ }++ None+}++/// Resolve the effective context window for `model_id`. `configured` (from+/// `[agent].context_window`) is authoritative when set; otherwise the built-in+/// catalog is consulted; an unknown model gets the conservative fallback.+pub(crate) fn resolve_context_window(+ model_id: &str,+ configured: Option<u64>,+) -> ResolvedContextWindow {+ if let Some(tokens) = configured {+ return ResolvedContextWindow {+ tokens,+ source: ContextWindowSource::Configured,+ };+ }+ let bare = strip_provider_qualifier(model_id);+ match catalog_window(bare) {+ Some(tokens) => ResolvedContextWindow {+ tokens,+ source: ContextWindowSource::Catalog,+ },+ None => ResolvedContextWindow {+ tokens: FALLBACK_CONTEXT_WINDOW,+ source: ContextWindowSource::Fallback,+ },+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn configured_window_overrides_every_catalog_entry() {+ let resolved = resolve_context_window("claude-sonnet-4-6", Some(333_000));+ assert_eq!(resolved.tokens, 333_000);+ assert_eq!(resolved.source, ContextWindowSource::Configured);+ }++ #[test]+ fn catalog_resolves_supported_model_families() {+ assert_eq!(+ resolve_context_window("claude-sonnet-4-5", None).tokens,+ 200_000+ );+ assert_eq!(+ resolve_context_window("claude-sonnet-4-6", None).tokens,+ 1_000_000+ );+ assert_eq!(resolve_context_window("gpt-5.2", None).tokens, 400_000);+ assert_eq!(+ resolve_context_window("gpt-5.2-codex", None).tokens,+ 272_000+ );+ assert_eq!(+ resolve_context_window("gemini-3-pro", None).tokens,+ 1_000_000+ );+ }++ #[test]+ fn provider_qualifier_does_not_change_resolution() {+ assert_eq!(+ resolve_context_window("anthropic/claude-sonnet-4-6", None),+ resolve_context_window("claude-sonnet-4-6", None),+ );+ }++ #[test]+ fn unknown_model_uses_conservative_fallback() {+ assert_eq!(+ resolve_context_window("private-gateway-model", None),+ ResolvedContextWindow {+ tokens: 128_000,+ source: ContextWindowSource::Fallback,+ },+ );+ }++ #[test]+ fn non_catalog_provider_qualifier_is_not_stripped() {+ // A provider Jan doesn't ship keeps its qualifier: the id must still+ // fall through to the conservative default rather than mis-matching.+ assert_eq!(+ resolve_context_window("azure/claude-sonnet-4-6", None).source,+ ContextWindowSource::Fallback,+ );+ }+}