Skip to content

feat: prototype token-budget context windows for Coder Agents (do not merge) - #31191

Draft
ThomasK33 wants to merge 12 commits into
mainfrom
tk/token-budget-prototype
Draft

ThomasK33 wants to merge 12 commits into
mainfrom
tk/token-budget-prototype

Conversation

@ThomasK33

@ThomasK33 ThomasK33 commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Warning

Do not merge. This is a throwaway prototype for an RFC about token-budget context windows in Coder Agents. The code will be rewritten.

Behind the chat-token-budget experiment, Coder Agents start fresh context windows instead of summarizing. Past the model's compaction threshold, the agent gets two hidden nudges to save a checkpoint with a new chat-scoped notes tool and then call new_context. The new window holds the system prompt, a lead-in that lists the notes, and a copy of the current user message. Earlier windows stay in the database, and the agent reads them with chat_history (list_windows, list_items, search, read_item). If the agent ignores both nudges, a deployment setting picks the fallback at the forced point (90% by default): today's summary compaction, or a new window with no summary.

The design copies the token-budget modes of the Codex CLI and Xum. A window boundary reuses the existing compaction rows, so the prompt query does not change. The user sees the summary card with the label "New context window" and the checkpoint inside.

Main changes:

  • coderd/x/chatd/contextbudget.go: the token points, the once-per-window nudges, and the boundary rows.
  • coderd/x/chatd/chattool/{notes,newcontext,chathistory}.go: the three tools.
  • generation.go: the boundary commits with the new_context tool result. The new_window fallback skips the summary model call.
  • Migration 000633_chat_notes and the agents_context_window_config site config, behind GET/PUT /api/experimental/chats/config/context-windows.
  • site/: the "Context window fallback" admin setting and the card labels.

NOTE: This branch adds migration 000633_chat_notes.

To try it: open the preview eph-pr-31191 (https://3000--dev--eph-pr-31191--tk--apps.dogfood.cdr.dev, members of the dogfood coder organization, develop.sh admin login) and chat with the model "Sonnet 5.5 (64k budget test)". Its 64,000-token limit at threshold 70 gives nudges at 44,800 and 51,200 and the fallback at 57,600. Locally, ./scripts/develop.sh on this branch turns the experiment on by default (a prototype-only change to scripts/develop.sh). The fallback setting is under Admin settings, Coder Agents, Deployment settings.

Dogfood results and evidence: #31191 (comment)


Generated with xum • Model: anthropic:claude-opus-5-5 • Thinking: xhigh

@ThomasK33

ThomasK33 commented Oct 23, 2026 •

Copy link
Copy Markdown
Member Author

Dogfood results (local ./scripts/develop.sh at 9e28faad39, claude-sonnet-5-5 through the Coder AI gateway, context limit 64,000, threshold 70, so the points are 44,800 / 51,200 / 57,600):

Passed:

  • Handoff and final nudges fire once per window, also in the second window.
  • The agent saves checkpoint.md with notes, calls new_context alone, and continues in window w:1 (54,524 tokens before, 15,365 after).
  • chat_history search and read_item find a user message in w:0 by item ID.
  • Fallback "Summarize the conversation" runs at 90% when the agent does not call new_context.
  • Fallback "Start a new context window", set on the admin page, starts w:1 with no summary model call (source context_limit).
  • With the experiment off, the same task compacts at 70% as before.

Findings:

  • After the handoff nudge, the agent saved notes but did not call new_context. It called it only after the final nudge.
  • A user message of about 9,600 tokens moved usage past the final and the forced point in one step (2 of 3 runs), because the decision uses the previous step's usage.
  • On the 16-part task, the experiment used about 3.1x the cache-read tokens of the baseline (41 vs 17 steps), mostly because the agent updated its notes after almost every part.

Not tested: GPT models, sub-agents, plan mode, workspace tasks, the 1M-token model config.

Preview: eph-pr-31191 (full mode). The model "Sonnet 5.5 (64k budget test)" there shows the nudges after a few messages.

Run 1, summary fallback after the notes write:

Run 1: notes write, then the summary at 90%

Run 2, new_context and the new window card with the checkpoint:

Run 2: new_context and the New context window card

Run 2, chat_history in the new window:

Run 2: chat_history search and read_item

Admin setting, saved as "Start a new context window":

Context window fallback admin setting

Run 3, the new window that the fallback started:

Run 3: New context window (limit) card

Run 3 video (real run, built from screenshots taken every 1.5 s, played at 4x):

run3-new-window-fallback.mp4

Generated with xum • Model: anthropic:claude-opus-5-5 • Thinking: xhigh

@linear-code

linear-code Bot commented Oct 26, 2026

Copy link
Copy Markdown

CODAGT-1467

# Conflicts:
#	coderd/x/chatd/chatd_test.go

This branch has not been deployed

No deployments
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