perf: trim history by token budget, not message count - #115
Draft
aryansk wants to merge 1 commit into
Draft
Conversation
Fixes shauryagangrade#50 trim_history kept at most 30 messages plus system message. Message count is a poor proxy for context: a 2000-line read_file or huge grep output can fill the window in one turn, while long short-message conversations get cut prematurely. Estimate tokens per message via len//4 heuristic (tiktoken if available, including tool_calls) and trim to 12000 token budget plus the 30-message cap. Keep at least one recent turn, drop orphaned ToolMessages, and cap oversized grep output at the source (8000 chars / 200 lines, same as read_file line truncation) so a single tool call can't blow the window. Validation: py_compile passes, git diff --check clean; budget trims oldest messages while preserving system message and avoiding orphaned tool results; grep truncation covered.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #50
Problem
trim_historykeeps at mostMAX_HISTORY = 30messages (plus system). Message count is a poor proxy for context: a 2000-lineread_fileresult or huge grep output can fill the window in one turn, while long conversations of short messages get cut prematurely. Issue #50 proposes estimating token usage per message and trimming to a budget, and capping oversized tool results at the source.Change
gcode/agent.py:MAX_HISTORY_TOKENS = 12000and_estimate_tokens(msg)(len//4 heuristic, tiktokencl100k_baseif available, includestool_calls)trim_historyto keep history within bothMAX_HISTORY(30) andMAX_HISTORY_TOKENS(12000): preserve system message, keep most recent within count, then drop oldest while over token budget (keep at least one recent turn, drop orphanedToolMessages)gcode/tools.py:grepoutput at 8000 chars / 200 lines (both externalgrepand pure-Python fallback) with truncation note, matchingread_fileline truncation, so a single tool call can't blow the windowWhy this approach
Token budget (≈30 * 400 tokens) matches the existing message-count window but adapts to large tool outputs. Heuristic
len//4is the standard estimate when tiktoken is absent; tiktoken is used when installed for accuracy. Capping at the source prevents one huge grep from dominating the budget before trimming even runs.Testing
Documentation and release impact
Review notes