Skip to content

LangGraph TS engine: make room in long conversations instead of outgrowing the model's window - #2184

Merged
davidmckayv merged 1 commit into
mainfrom
fix/langgraph-ts-context-room
Nov 25, 2026
Merged

davidmckayv merged 1 commit into
mainfrom
fix/langgraph-ts-context-room

Conversation

@davidmckayv

Copy link
Copy Markdown
Contributor

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.

  1. Claude: every request carries context_management with clear_tool_uses_20250919 (trigger 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, with the whole history. Sent through @langchain/anthropic 1.6.5's own contextManagement and betas fields: betas routes the call to client.beta.messages.create, which sets the anthropic-beta header. 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 naming context_management) is remembered for the process, and that call and every later one get rule 2.
  2. Every other provider: the same rule runs here before each model call. Past an estimated 100,000 tokens (characters / 2.3, images about 1,600), every tool result but the last 10 is replaced in what is sent by a note naming its call id, if that frees at least 20,000 tokens. search_tools results 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.
  3. Any provider, reactive: when a provider still refuses as too long (Anthropic "prompt is too long", OpenAI 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 carrying OPENBOT_MODEL_MESSAGE_TOO_LONG and the provider's words.
  4. 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 by call_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 in shared/recall-earlier-results.ts, and shared/tool-name.ts gives 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.ts drives 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.

  • OpenAI past its window keeps going: 48 results cleared, the last 10 and both search_tools results whole, every call paired, the snapshot whole.
  • Claude sends context_management and the beta, with the whole history, and finishes.
  • A proxy that refuses context editing: cleared here instead, and the next run never sends the field.
  • A smaller window: refused once, retried at keep 3, and the next step starts there.
  • A latest message too long even alone: four tries, each harder, then OPENBOT_MODEL_MESSAGE_TOO_LONG with "prompt is too long: 1008027 tokens > 1000000 maximum".
  • Recall by call_id returns the result whole; by words, the three most recent matches, cut at 20,000.
  • A Bot with no tools is not offered the recall tool.

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 typecheck and npm test in 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.

…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
davidmckayv merged commit 3544cc1 into main Nov 25, 2026
36 checks passed
@davidmckayv
davidmckayv deleted the fix/langgraph-ts-context-room branch November 25, 2026 06:58
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant