Repository navigation
Compact the conversation when it nears the model's context window - #28
Merged
Merged
Conversation
10 tasks
vicgupta
force-pushed
the
issue-24-compact-context
branch
from
October 24, 2026 20:33
7c952ea to
931a8e6
Compare
This was referenced Oct 24, 2026
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.
Closes #24
What changed
prompt is too long,context_length_exceededormaximum context length, it compacts and retries that step once; a second overflow ends the run asError(context still exceeds the window after compaction: ...).model.ContextWindowstable, longest prefix wins (gpt-4o128K,gpt-4.1*1,047,576,claude-*200K), also matching nested names likeopenrouter/anthropic/claude-*. New config keycontext_windowoverrides it. With neither, only the reactive path runs.internal/agent/compact.go):[output elided: N chars], outside a protected zone: system prompt, task, and the recent turns (~25% of the window, never fewer than the last 2 assistant turns with their results). Stops if the estimate drops under 50% of the window (under half the previous estimate when the window is unknown).Error. No loops.Trajectory.Messagesstays the full original history.compactions(omitted when empty), one record per compaction: step, reason, stage, tokens before/after, elided and summarized message indexes, summary text, summary cost, andview(the message indexes the model is sent next, with-1for the summary).--cost-limitis checked right after compaction; it doesn't count as a step.compacted context: ~17,020 → ~9,048 tokens (elided 4 tool outputs).--no-compact(in the Limits help group) turns it all off; requests are then exactly the full history, as before.context_windowin the config example, and a new "Long runs" section (80% threshold, stages, lossy summaries, put must-hold rules in AGENTS.md).Testing
gofmt -lclean,go build ./...,go vet ./...clean,go test -race -count=1 ./...passes in every package (Go 1.27.3).internal/agent/compact_test.go, using a fake model and realread_filecalls:context_window(overriding the 128K table value forgpt-4o), the next request has steps 1–4 elided, steps 5–6 verbatim, system and task unchanged, no summary call; trajectory keeps original outputs and records the compaction; stderr line checked.-oJSON has the compaction record.--cost-limit.Errorafter exactly one retry.--no-compact: every request equals the full history, no stderr output, overflow ends the run asError.internal/model/context_test.gocovers the window table and overflow detection.