Skip to content

Clear a long run's old results late, close to the model's own window - #2963

Merged
davidmckayv merged 1 commit into
mainfrom
parity/s5-window-aware-clearing
Dec 4, 2026
Merged

davidmckayv merged 1 commit into
mainfrom
parity/s5-window-aware-clearing

Conversation

@davidmckayv

Copy link
Copy Markdown
Contributor

What this changes

Long runs clear their old tool results late, close to the model's own window. Every engine used to clear past 100,000 input tokens, whatever the model. On a model with a 1M window that threw away what a Bot had read long before it had to: Claude Code on the same model keeps everything until about 367,000.

shared/context-window.ts holds one rule for every engine, the way Claude Code sets its compaction point:

Window Clears at Why
1,000,000 or 400,000 367,000 Claude Code's 400K auto window, which leaves 33K for the turn. It stays as a cost guard: every step sends the whole conversation.
200,000 167,000 Near the limit, as Claude Code compacts a 200K model
128,000 95,000
32,768 16,384 Never past half the window
unknown 100,000 As before

How each place uses it:

  • Telling the engines. The server tells each engine the window of the model it chose for the run in forwardedProps.openbotContextWindow. A stock AG-UI agent ignores it.
  • LangGraph engine. It clears from that trigger on every provider, holds its boundary at 60% of the trigger (the same share as before), and gives Anthropic's context editing the same trigger.
  • Claude Agent SDK engine. It replays more of the thread on a larger window:
    • 100,000 estimated tokens on a 200K or unknown window, as before;
    • about 238,000 on a 1M window, sized so that even at the worst characters-per-token ratio measured the replay stays below where Claude Code itself would compact.
  • Connected Bots. A connected Bot can declare its model's window in its AG-UI capabilities, under AG-UI's extension point for applications: custom["openbot.contextWindow"] = { "tokens": N }. It is then cleared at the same point as OpenBot's own engines. docs/agent-frameworks.md says how.

Nothing here is specific to one model or provider. The window comes from the provider's own listing or from models.dev, and the rule is the same for every engine.

How Claude does it (searched 2026-12-04)

  • Compaction thresholds. "Claude Code compacts when the conversation reaches the model's context limit." On current 1M models the auto window is 400K, "tuned per model for the best cost and performance": "On auto at 400K, compaction runs at about 367K tokens". At a full 1M it is about 967K. Model configuration, Default auto-compact thresholds
  • Context editing API. Defaults are trigger 100K and keep 3 tool uses. Context editing
  • Capability declarations. AG-UI keeps AgentCapabilities.custom for integration-specific declarations; OpenBot already reads openbot.runGuidance there.

Where it runs

  • New state that outlives a request? None. The window rides on each run's forwardedProps.
  • What happens on the second replica? The same: each run carries its own window.
  • Anything serialised? No.
  • Anything fanned out to a browser? No.
  • New listener, port, or schedule? No.

Boundary and audit

  • Every acting call still goes through the gateway.
  • No new refusals or failures.
  • Nothing new is trusted from the client. The window is decided by the server; a value a request carries is not passed on.

API and database

  • This changes nothing under api/, no app's requests, no database change and no release-policy.json.

Changelog

Bots.

  • Changed: long jobs keep more of what they read. Old tool results are cleared close to the model's own context window, at about 367,000 tokens on a 1M model, instead of at 100,000 for every model.
  • Added: a connected Bot can declare its model's window. custom["openbot.contextWindow"] = { "tokens": N } in its AG-UI capabilities, and OpenBot clears what it sends it at the same point.

Proof

Each test was red before the change and green after.

  • agent-langgraph-ts/test/context-room.test.ts, "A model with a 1M window": a real AG-UI run against a fake provider with a 1M window. OpenAI gets all 60 results whole, and Claude's context editing trigger is 367,000.
    • Red before: 38 results cleared.
  • agent-claude-sdk/tests/test_long_turn_replay.py::test_a_model_with_a_1m_window_replays_far_more_whole: through the adapter, a 45-result thread replays every result whole on a 1M window, and is cut on an unknown window.
    • Red before: result 0 cut.
  • shared/tests/context-window.test.ts pins the rule against real windows.
  • Full suites:
    • bun test shared: 706 passed.
    • LangGraph engine: 127 passed, 3 skipped (live).
    • Claude SDK harness: 181 passed.
    • tsc and Biome: clean.

Every engine cleared old tool results past 100,000 input tokens, whatever
the model. On a model with a 1M window that cleared what a Bot had read
long before it had to: Claude Code on the same model keeps everything
until about 367,000.

shared/context-window.ts holds one rule: clear close to the window of the
model the run is on, as Claude Code compacts. 1M and 400K windows clear at
367,000 (Claude Code's 400K auto window, kept as a cost guard), a 200K
window at 167,000, a 128K window at 95,000, a small one at half its window,
and an unknown window at 100,000 as before. The server says the window of
the model it chose in forwardedProps.openbotContextWindow; a stock AG-UI
agent ignores it.

The LangGraph engine clears from that trigger on every provider, holds its
boundary at 60% of it, and tells Anthropic's context editing the same
trigger. The Claude Agent SDK engine replays more of the thread on a larger
window: 100,000 estimated tokens on a 200K or unknown window as before,
about 238,000 on a 1M window, never past where Claude Code itself would
compact. A connected Bot can declare its model's window in its AG-UI
capabilities, custom["openbot.contextWindow"] = { "tokens": N }, and is
cleared at the same point.
@davidmckayv
davidmckayv added this pull request to the merge queue Dec 4, 2026
Merged via the queue into main with commit 0ba58d2 Dec 4, 2026
41 checks passed
@davidmckayv
davidmckayv deleted the parity/s5-window-aware-clearing branch December 4, 2026 22:00
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