feat: adapt agent_input_token_budget to the model context window (#1170) (#1230)

The agent soft-trims input context to `agent_input_token_budget` (default 6000).
The old computation `min(context_length or budget, budget)` made the 6000 default
a hard ceiling for every model, so 128K/1M context models were silently capped at
6000 input tokens — now that num_ctx is sent correctly (#1056), this was the last
barrier to actually using a long context window.

This derives the default budget from the model's discovered context window
(~85%, capped at a generous hard max) while honouring an explicit user setting
exactly (clamped to the window). When the window is unknown it falls back to the
previous value, so behaviour is unchanged for that case.

- src/context_budget.py: pure `compute_input_token_budget()` (unit-testable)
- src/settings.py: `is_setting_overridden()` to tell an explicit user value from
  the merged default (load_settings merges DEFAULT_SETTINGS, so equality alone
  can't distinguish them)
- src/agent_loop.py: use the helper in the soft-trim path

Covered by tests/test_context_budget.py (6 cases).

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
lekt8
2026-06-02 23:13:53 +08:00
committed by GitHub
parent 1fda906407
commit 8c376d2b0e
4 changed files with 126 additions and 1 deletions
+15
View File
@@ -195,6 +195,21 @@ def get_setting(key: str, default: Any = None) -> Any:
return load_settings().get(key, default)
def is_setting_overridden(key: str) -> bool:
"""True if ``key`` is explicitly present in the saved settings file.
``load_settings`` merges DEFAULT_SETTINGS with the saved file, so a value
equal to its default is indistinguishable from "never set" via get_setting.
Callers that need to treat an explicit user choice differently from the
default (e.g. adaptive budgets) use this to read the raw saved file.
"""
try:
with open(SETTINGS_FILE, "r", encoding="utf-8") as f:
return key in json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
return False
# Per-user settings (user prefs override the global admin default). Used for
# keys that a user is allowed to choose individually — currently the vision
# model + image-generation model. The owner argument is the authed username