Skip to content

feat(coderd/x/chatd): add organization token budget setting - #330

Merged
ibetitsmike merged 2 commits into
mainfrom
mike/issue-321-token-budget-setting
Nov 14, 2026
Merged

ibetitsmike merged 2 commits into
mainfrom
mike/issue-321-token-budget-setting

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Part of #321. This is the API and chatd half. The settings UI follows in a stacked PR that closes the issue.

What

An organization setting picks how chats compact their context when fork-token-budget is on.

API: GET and PUT /api/v2/chats/fork/organizations/{organization}/token-budget, behind httpmw.RequireExperiment(..., ExperimentForkTokenBudget). It follows ForkSubagentLimits: codersdk types and client methods, dbauthz on rbac.ResourceChatModelConfig.InOrg, read returns 404 when not readable, update returns 403 when readable but not updatable, 400 on unknown values. With no row the API returns the defaults.

Storage: a chatdmig node creates chat_organization_token_budget (org PK, CHECK constraints on both columns). Queries live in forkdb. No numbered migration.

Why organization scope

Chats belong to an organization, and the neighbouring settings (subagent limits, wake-up caps) are organization settings on the same page. A per-org row also keeps the read keyed by the chat's own organization.

chatd behavior

  • summary strategy equals the main path. No new_context tool, no <context_budget> handoff or final requests, and upstream summary compaction runs. The tools that stay are notes and chat_history, since they are not tied to the budget.
  • token_budget strategy keeps the feat(coderd/x/chatd): start token-budget context windows behind fork-token-budget #314 behavior: new_context, the handoff and final requests, and the forced rollover.
  • token_budget with summary fallback runs upstream summary compaction (chatloop.GenerateCompaction) at the forced point instead of opening a new window. A new_context call still rolls over without a summary. The compaction model override's trigger still applies with this fallback, so an override whose trigger comes before the usable limit summarizes there first, as it does before the threshold upstream.

Snapshot: the new_context tool holds the fallback it was built with. turnTokenBudgetFallback reads it from the step's tools, so tokenBudgetTriggers and forkGenerateCompaction follow the same setting within a step, even if an admin changes it mid-step. Only the server's own new_context counts, through an unexported interface that secretAnswerTool forwards, so a dynamic or MCP tool named new_context does not make a turn eligible.

Reads: appendForkTools reads the setting once per step in tool assembly. tokenBudgetMessage reads it again only when a request is due. Compaction reads nothing.

Code is in fork files. Upstream files get no new hook lines; fork_tools.go calls the new tokenBudgetTools helper.

How tested

  • go test ./coderd/x/chatd/... -count=1: pass.
  • TestTokenBudgetWindows: Cooperative and ForcedRollover set token_budget/new_window and pass (the feat(coderd/x/chatd): start token-budget context windows behind fork-token-budget #314 e2e path). SummaryFallback checks one summary compaction at the usable limit and no new window. SummaryFallbackNewContext checks a new_context rollover with no summary and a w:2 new_context boundary. SummaryStrategy ramps past the handoff point and checks no new_context, no <context_budget in any request or stored message, notes and chat_history present, and summary compaction at the threshold. Removing the strategy gate in tokenBudgetMessage makes SummaryStrategy fail. PlanModeSummarizes now runs with token_budget.
  • TestTokenBudgetTriggers: the summary fallback binds to a smaller override, keeps an override that comes later as the other trigger, and the new_window fallback drops it. TestTokenBudgetEligibility: a foreign new_context, wrapped or not, is not eligible, and the secret answer wrapper forwards the fallback.
  • TestChatNotes, TestNewContextTool, TestContextWindow, TestChatHistory, TestChatTranscript: pass.
  • go test ./coderd -run 'TestForkChatOrganizationTokenBudget|TestForkChatOrganizationWakeupCaps': pass. Covers defaults when unset, round trip, 400 on bad values, 404 and 403 for members without read or update, and 403 from RequireExperiment with the experiment off.
  • go test ./coderd/database/dbauthz -run TestForkQuerier, chatdmig and forkdb packages: pass.
  • go test ./coderd/coderdtest -run 'EndpointsDocumented|TestSwagger', .github/fork/check-apidoc.sh, go run ./scripts/check-scopes: pass.
  • golangci-lint run --fix=false on the touched packages reports nothing in changed lines, and the new-code config reports 0 issues. lint/markdown, lint/emdash, lint/architecture, lint/migrations, lint/agents, lint/check-experiment-keys: pass.

Docs: docs/fork/pstack-adapter.md and coderd/x/chatd/ARCHITECTURE.md.

Generated files: make gen regenerated coderd/apidoc, site/src/api/typesGenerated.ts and the API reference. I kept only the token-budget hunks in docs/reference/api/*.md; the generator also reorders unrelated enum values and fills older stale sections, which I left out.

Not verified: a live deployment run, and an end-to-end run with a compaction model override configured (covered by the unit test of tokenBudgetTriggers). The UI is in the follow-up PR.

The fork-token-budget experiment now reads a per-organization setting.
The summary strategy, the default, keeps main's summary compaction with
notes and chat_history. The token_budget strategy keeps the windows of
#314, and its fallback picks a new window or a summary at the usable
limit.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-14T15:41:36.361974Z 3047c90 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8930e21831

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/fork_token_budget.go Resolved
Comment thread coderd/x/chatd/fork_token_budget.go Resolved
Comment thread coderd/x/chatd/fork_token_budget.go Resolved
…ools

The new_context tool holds the fallback of the step that built it, so triggers and compaction follow one setting and a foreign new_context tool does not make a turn eligible. With the summary fallback, the compaction model override's trigger still applies.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3047c90be5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/fork_token_budget.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3047c90be5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/fork_token_budget.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 3047c90be5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike
ibetitsmike merged commit 9308bf6 into main Nov 14, 2026
10 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-321-token-budget-setting branch November 14, 2026 16:02
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged as 9308bf6 (squash). Part of #321; the UI in #331 closes it.

Gates on head 3047c90be5181fc25bc59ed7c3655c013f8ff8bf:

Tests I ran on the exact head, in a detached worktree:

  • go test -count=1 -v -run '(TestTokenBudget|TestNewContextTool|TestContextWindow|TestWithContextWindowResult)' ./coderd/x/chatd/: pass, including TestTokenBudgetWindows/SummaryStrategy, SummaryFallback, SummaryFallbackNewContext, ForcedRollover, Cooperative and ExperimentOff.
  • go test -count=1 -v -run TestForkChatOrganizationTokenBudget ./coderd/: pass (Enabled, ExperimentOff).
  • Negative control: with the strategy gate removed (new_context and requests always on), TestTokenBudgetWindows/SummaryStrategy fails. Reverted.

Not exercised: a live deployment with fork-token-budget on (it is off on the daily server), and an end-to-end run with a compaction model override. With no saved setting an organization uses summary compaction, so turning the experiment on changes nothing until an admin picks "Token budget".

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