Repository navigation
Conversation
…ter and trigger (#545) - Size the summarizer's input budget from the summarization model's own context window (85% minus the prompt) instead of a fixed 24k cap, so it reads everything LangChain removes; keep the old cap only when that window is unknown. If history still overflows, log it and show a Notice. - Count ToolMessage bodies in the context meter, switch it to chars/4, and pass the same counter to summarizationMiddleware; lower the trigger by the system prompt so it fires at the total the meter shows. - Treat an empty summary and LangChain's "too long to summarize" stub as failures, and show a Notice after 3 consecutive failures. Co-Authored-By: Claude <noreply@anthropic.com>
Contributor
|
The summarization runnable is cached per agent config and shared by every chat, so one counter mixed chats: failures in two chats could add up to a warning, and a success elsewhere hid one. Key the streak by thread_id. Co-Authored-By: Claude <noreply@anthropic.com>
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.
What
Fixes three verified problems from #545 with chat summarization. Thanks to the reporter for an unusually thorough analysis; it made each of these easy to confirm against the source.
summarizationMiddlewaretrims the messages it is about to replace totrimTokensToSummarize(keeping the newest), then removes all of them. Our budget wasmin(trigger/2, 24k), so at a 128k window about 78k tokens of older history disappeared without being summarized. The budget now comes from the summarization model's own context window (85% of it, minus the prompt), so the summarizer reads everything it replaces. The old conservative cap is kept only when a dedicated summary model has no configured window. If history still overflows (a small dedicated summary model), it is logged and a Notice says roughly how much was left out.ToolMessagebodies, while the middleware counted chars/4 including tool results. The meter now uses chars/4 (LangChain's ratio), counts tool results under "Tool results", and has realistic per-message overheads. The same function (countMessageTokens) is passed to the middleware astokenCounter, and the trigger is lowered by the system prompt's size, because the middleware never sees it. The meter,shouldPredictSummarizationand the actual trigger now agree.guardSummarizationFailurenow also rejects an empty summary and LangChain's"Previous conversation was too long to summarize."stub. After 3 consecutive failures it shows a Notice, once per streak, because by then the full history keeps growing toward the provider limit. The log line now includes the failed summary text.The reporter's fourth claim, that a failed summary gets persisted as the thread's memory, was already handled by #435 (
guardSummarizationFailureaborts the trim).Fixes #545
How I tested it
Unit tests: estimator rework (tool-result counting, parity with
countTokensApproximatelywithin 2%), trim budget sizing, middleware config (shared counter, system-prompt-adjusted trigger, dedicated-summarizer bound), and guard behaviour against the realsummarizationMiddleware(empty/too-long stubs rejected, failure streak reported once and reset by success, dropped history reported with exact token figures).bun run check,format,lintandtestpass. Not yet tried in a live vault; a dev build is in slot vaultS2B WT3for the maintainer's live test.AI assistance: Claude Code (Opus) wrote the change and tests from the maintainer's brief for #545, after verifying each claim against langchain 1.5.10's middleware source; the maintainer reviews and live-tests before merging.
Checklist
bun run check,bun run format,bun run lint, andbun run testpass locallymanifest.json: I noted that the docs site needs updating (see "Documentation" in CONTRIBUTING.md)