Repository navigation
LangGraph TS engine: make room in long conversations instead of outgrowing the model's window - #2184
Merged
Merged
Conversation
…owing the model's window A Bot on a long task stopped after a few hundred steps when its thread grew past the model's window and the provider refused it as too long. Its instructions and tools are about ten thousand tokens; nearly all the rest is old tool output, and nothing trimmed it. The engine now follows the rule Claude uses: - On Claude, every request carries Anthropic's context editing (clear_tool_uses_20250919: past 100,000 input tokens, keep the last 10 tool uses, clear at least 20,000, never search_tools) and the context-management-2025-06-27 beta, through @langchain/anthropic's own contextManagement and betas fields. A proxy that refuses the field is remembered for the process and gets the local rule. - On every other provider the same rule runs before each model call: every tool result but the last 10 is replaced in what is sent by a note naming its call. Calls keep their results, and the system prompt, the person's messages and the Bot's words are kept. - When a provider still refuses as too long, the call is retried cleared harder, up to three times, and the stage that worked is kept for the rest of the run. If all fail, the run ends with OPENBOT_MODEL_MESSAGE_TOO_LONG and the provider's words. - A new tool, recall_earlier_results, reads a cleared result back from the whole thread the run holds, by call_id or by words. Its constant lives in shared/, and shared/tool-name.ts names it. Only what is sent is cut: the thread the run hands back stays whole.
davidmckayv
requested review from
MikeRyanDev,
dustywusty,
guidovizoso,
mxmzb,
samjulien and
tylerslaton
as code owners
November 25, 2026 06:17
davidmckayv
added a commit
that referenced
this pull request
Nov 25, 2026
Unreleased gains, from the code of each change since rc.5 (OpenBot main a9ba3fe to 25d50a1, and the server since rc.5's): a Bot's work folded with a shimmer on the running step (#2201), long tasks that keep going instead of stopping as too long (#2184), a push to your phone when a Bot finishes while you're away and a conversation marked read only while it's seen (#2192), an invited address with no account signing up (#2187), the version on screen on OpenBot Cloud (#2197), Setup's model row as the reader's AI (#2185), Admin's managed-updates and favicon requests (#2186), the screen guide's Open apps and Every Bot, the pre-upgrade backup of every database with a restore command, and the API's invitationSignUp and setup checklist wording. release-policy.json: fromVersions adds v1.2.0-rc.5 and database.from adds its last migration, 0181_organization_ai_sign_in_credentials.
davidmckayv
added a commit
that referenced
this pull request
Nov 25, 2026
Unreleased gains, from the code of each change since rc.5 (OpenBot main a9ba3fe to 25d50a1, and the server since rc.5's): a Bot's work folded with a shimmer on the running step (#2201), long tasks that keep going instead of stopping as too long (#2184), a push to your phone when a Bot finishes while you're away and a conversation marked read only while it's seen (#2192), an invited address with no account signing up (#2187), the version on screen on OpenBot Cloud (#2197), Setup's model row as the reader's AI (#2185), Admin's managed-updates and favicon requests (#2186), the screen guide's Open apps and Every Bot, the pre-upgrade backup of every database with a restore command, and the API's invitationSignUp and setup checklist wording. release-policy.json: fromVersions adds v1.2.0-rc.5 and database.from adds its last migration, 0181_organization_ai_sign_in_credentials.
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.
Why
A Bot given a long task stopped after a few hundred steps with "this conversation is too long for the model". Its instructions and tools are about ten thousand tokens; nearly everything else a late step sends is old tool output, and the TypeScript LangGraph engine, which new Bots run on by default, never trimmed it.
What changes
The engine now follows the rule Claude itself uses (Anthropic's context editing). New module:
agent-langgraph-ts/src/context-room.ts.context_managementwithclear_tool_uses_20250919(trigger 100,000 input tokens, keep the last 10 tool uses, clear at least 20,000, neversearch_tools) and thecontext-management-2025-06-27beta, with the whole history. Sent through @langchain/anthropic 1.6.5's owncontextManagementandbetasfields:betasroutes the call toclient.beta.messages.create, which sets theanthropic-betaheader. Keep is 10, not 3, because a Bot batches about five reads and must see its whole latest batch. A proxy that refuses the field (a 4xx namingcontext_management) is remembered for the process, and that call and every later one get rule 2.search_toolsresults are never cleared. Each call keeps its result, so no provider sees an orphan. The system prompt, the person's messages and the Bot's words are kept.context_length_exceeded/ "maximum context length", Gemini's token count, a 413 or "request too large"), the same call is retried cleared harder, up to 3 times: keep 3; then keep 1 plus earlier messages cut to 2,000 characters, old reasoning dropped and long earlier call arguments emptied; then keep 0. The stage that worked is kept for the rest of the run. If all fail, the run ends with a RUN_ERROR carryingOPENBOT_MODEL_MESSAGE_TOO_LONGand the provider's words.recall_earlier_results: a new tool, offered to any Bot that has tools, answered by the engine itself from the whole thread it holds: one result bycall_id, or the 3 most recent whose tool name, arguments or output hold every given word, capped at 20,000 characters and cut visibly. Its name lives inshared/recall-earlier-results.ts, andshared/tool-name.tsgives it words for its chat line.Only what is sent is cut. The thread the run hands back in its snapshot stays whole.
Server-side summarisation (Anthropic
compact_20260112, OpenAI's compaction item) is left out on purpose: its block would have to be carried in the stored thread, which does not keep it, so every step would summarise again.Tests
agent-langgraph-ts/test/context-room.test.tsdrives the real AG-UI endpoint against a fake provider that speaks each provider's real protocol behind a window, answering any request over it with that provider's exact refusal body. Its Anthropic side applies context editing as Anthropic does when the request carries it and its beta. The thread is 60 earlier tool calls with 8,000-character results.search_toolsresults whole, every call paired, the snapshot whole.context_managementand the beta, with the whole history, and finishes.OPENBOT_MODEL_MESSAGE_TOO_LONGwith "prompt is too long: 1008027 tokens > 1000000 maximum".On origin/main's engine code, 6 of these 7 fail (runs end in RUN_ERROR; the too-long one ends with OPENBOT_MODEL_REFUSED). With this change all pass. The existing tests that list a run's tools now include
recall_earlier_results.npm run typecheckandnpm testin agent-langgraph-ts,bun run lint,bun run format:check, the shared package's typecheck, and the app and mobile tool-name tests are green.