Skip to content

fix(agent): include tools schema in post-compression token estimate (#14695)#15433

Closed
Tranquil-Flow wants to merge 1 commit into
NousResearch:mainfrom
Tranquil-Flow:fix/14695-compression-token-estimate
Closed

fix(agent): include tools schema in post-compression token estimate (#14695)#15433
Tranquil-Flow wants to merge 1 commit into
NousResearch:mainfrom
Tranquil-Flow:fix/14695-compression-token-estimate

Conversation

@Tranquil-Flow

@Tranquil-Flow Tranquil-Flow commented Apr 25, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

After context compression, last_prompt_tokens is set using estimate_tokens_rough(system_prompt) + estimate_messages_tokens_rough(compressed), which counts only the system prompt and message content. Tools schema tokens are excluded, causing the system to underestimate context usage and delay the next compression cycle.

The codebase already has estimate_request_tokens_rough() which accepts a tools parameter — it just wasn't being used in the post-compression path. This PR routes the post-compression estimate through that function so the tools schema overhead is included.

Related Issue

Fixes #14695

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 🔒 Security fix
  • 📝 Documentation update
  • ✅ Tests (adding or improving test coverage)
  • ♻️ Refactor (no behavior change)
  • 🎯 New skill (bundled or hub)

Changes Made

  • run_agent.py: Replace the two-function estimate with a single call to estimate_request_tokens_rough(compressed, system_prompt=new_system_prompt, tools=self.tools), which includes tools schema overhead in the token count.

How to Test

  1. Run the new regression suite:
    pytest tests/run_agent/test_compression_token_estimate_tools.py -v
    3 new tests:
    • estimate_request_tokens_rough with 30 tools produces a substantially larger estimate than without (2000+ tokens overhead).
    • _compress_context() end-to-end: stored last_prompt_tokens includes tools overhead.
    • No crash when self.tools is None.

Tested on macOS (Python 3.11).

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/ -q and all tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: macOS 15 (Darwin 24.6.0)

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — or N/A
  • I've updated tool descriptions/schemas if I changed tool behavior — or N/A

Screenshots / Logs

N/A — see commit description and PR diff.

…ousResearch#14695)

After compression, the token estimate was computed using only the
system prompt and compressed messages, ignoring the tools schema
entirely. With 50+ tools this can add 20-30K tokens — a significant
blind spot that caused pressure heuristics to under-report context
usage and trigger premature re-compression.

Switch from the two-function estimate (estimate_tokens_rough +
estimate_messages_tokens_rough) to estimate_request_tokens_rough()
which already accepts a tools parameter, matching what the API
call actually sends.
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/agent Core agent loop, run_agent.py, prompt builder labels Apr 25, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #14882 — both fix the same issue (#14695): post-compression token estimate excluding tools schema. Also overlaps with omnibus PR #14696.

@Tranquil-Flow

Copy link
Copy Markdown
Contributor Author

Closing — the fix is now on main.

On current origin/main:

Same diagnosis, same fix. No further action needed on this PR. Thanks for the original write-up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent loop, run_agent.py, prompt builder 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.

BUG: Post-compression token estimate excludes tools schema, delaying next compression cycle

2 participants