Skip to content

fix(agent): scale tool-output budget to the model context window (#23767) - #50136

Merged
kshitijk4poor merged 1 commit into
mainfrom
fix/context-aware-tool-budget
Jun 21, 2026
Merged

fix(agent): scale tool-output budget to the model context window (#23767)#50136
kshitijk4poor merged 1 commit into
mainfrom
fix/context-aware-tool-budget

Conversation

@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Summary

Tool-result size budgets now scale to the active model's context window. Previously they were fixed constants (100K chars per result, 200K per turn) regardless of the model — so on a small-context model a single large tool result, or one tool-heavy turn, could by itself approach or exceed the window and force a request the provider rejects as "Prompt too long" (part of #23767).

Root cause

tools/budget_config.py capped tool output at fixed char counts blind to the model. The reporter switched a large session onto a 65,536-token local model; a 279,549-char search result (and the 200K-char per-turn budget ≈ 50K tokens — ~77% of that window) overflowed it.

Changes

  • tools/budget_config.py: new budget_for_context_window(context_length) scales per-result (15%) and per-turn (30%) char caps to the window, clamped to the historical 100K/200K defaults (large models unchanged) and floored (small models stay usable). resolve_threshold() now caps the per-tool registry value at default_result_size so tools registering a fixed 100K (web/terminal/x_search) don't re-inflate a scaled-down budget — a no-op for the default budget.
  • agent/tool_executor.py: _budget_for_agent() wires the agent's live context_length (recomputed on model switch) into all four persist / turn-budget call sites.

Validation

Result
tests/tools/test_budget_config.py (+13 new) 30 passed
tests/tools/test_tool_result_storage.py + budget (CI-parity runner) 79 passed
tests/run_agent/test_tool_executor_contextvar_propagation.py 54 passed
ruff + ty (diff vs main) clean, no new diagnostics
E2E (real imports, temp HERMES_HOME) 279K-char result → 1.6K preview on a 65K model; 200K model byte-identical to today; read_file stays inf-pinned (no persist loop)

Part of #23767 (does not close it on its own — sibling fixes tracked separately).

Infographic

Image generation is unavailable in this environment (FAL_KEY unset, no managed-provider credits), so the infographic could not be generated here. To be attached once image generation is available.

)

The tool-result persistence budget was a fixed 100K chars/result and 200K
chars/turn regardless of the active model. On a small-context model (e.g. a
65K-token local model switched into mid-session) a single large tool result
(reporter: a 279K-char search result) or a full 200K-char turn (~50K tokens)
could by itself approach or exceed the window, forcing an oversized request
that the provider rejects as "Prompt too long".

- budget_config.budget_for_context_window() scales per-result/per-turn char
  caps to a fraction of the model window, clamped to the historical 100K/200K
  defaults (large models unchanged) and floored so small models stay usable.
- resolve_threshold() now caps the per-tool registry value at default_result_size
  so tools that register a fixed 100K cap (web/terminal/x_search) don't re-inflate
  a scaled-down budget. No-op for the default budget (both 100K).
- tool_executor wires the agent's live context_length (recomputed on model
  switch) into all four persist/turn-budget call sites.

read_file stays inf-pinned (no persist loop). Verified E2E: a 279K-char result
against a 65K model collapses to a ~1.6K preview; a 200K model is byte-identical
to today.
@github-actions

Copy link
Copy Markdown
Contributor

🔎 Lint report: fix/context-aware-tool-budget vs origin/main

ruff

Total: 0 on HEAD, 0 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 0 pre-existing issues carried over.

ty (type checker)

Total: 11297 on HEAD, 11297 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 5920 pre-existing issues carried over.

Diagnostics are surfaced as warnings — this check never fails the build.

@alt-glitch alt-glitch added type/bug Something isn't working comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P2 Medium — degraded but workaround exists labels Jun 21, 2026
@kshitijk4poor
kshitijk4poor merged commit ed8f789 into main Jun 21, 2026
35 checks passed
@kshitijk4poor
kshitijk4poor deleted the fix/context-aware-tool-budget branch June 21, 2026 14:31
waefrebeorn pushed a commit to waefrebeorn/slermes that referenced this pull request Jul 2, 2026
…ware-tool-budget

fix(agent): scale tool-output budget to the model context window (NousResearch#23767)
habarmc1223-sudo pushed a commit to habarmc1223-sudo/hermes-agent-fluxmem that referenced this pull request Jul 8, 2026
…ware-tool-budget

fix(agent): scale tool-output budget to the model context window (NousResearch#23767)
santhreal pushed a commit to santhreal/hermes-agent that referenced this pull request Jul 13, 2026
…ware-tool-budget

fix(agent): scale tool-output budget to the model context window (NousResearch#23767)
Gravezzz pushed a commit to Gravezzz/hermes-agent that referenced this pull request Jul 21, 2026
…ware-tool-budget

fix(agent): scale tool-output budget to the model context window (NousResearch#23767)
leewenjie pushed a commit to leewenjie/hermes-agent that referenced this pull request Aug 7, 2026
…ware-tool-budget

fix(agent): scale tool-output budget to the model context window (NousResearch#23767)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants