feat(memory): implement token-aware sliding window memory management - #28
Merged
Conversation
Nithwin
force-pushed
the
feat/memory-manager
branch
from
July 26, 2026 06:26
ffd2a7c to
a29daa2
Compare
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.
Description
This PR fundamentally changes how the WindMist agent handles conversation history. It replaces the naive 8-message hard cap with a dynamic, token-aware sliding window memory system, preventing the agent from forgetting long-term goals while protecting against token limit crashes (e.g.,
400 Token Limit Exceededwhen reading large files).Key Changes
agent/messages.go: IntroducedestimateTokensandestimateMessageTokensto roughly calculate context size (using the standardlen/4heuristic).agent/messages.go: Re-wrotepruneMessagesto use a token budget. It strictly pins theMessage[0](the original user goal) and aggressively keeps the most recent tool calls and thoughts, dynamically discarding intermediate steps from the middle of the history when the 15,000 token limit is reached.agent/limits.go&agent/agent.go: IntroducedDefaultMaxContextTokensand wired it into the Agent configuration struct.Why this is necessary
If the agent reads a 3,000-line file, the old system would keep that massive payload in context for the next 8 turns, burning thousands of API tokens on every request and quickly crashing smaller models. The new system acts like LangChain's
ConversationTokenBufferMemory, keeping API costs low and multi-step reasoning focused.How to Test
go run ./cmd/windmist