Skip to content

[codex] Add token budget context feature - #27438

Merged
pakrym-oai merged 4 commits into
mainfrom
pakrym/token-budget
Jun 11, 2026
Merged

[codex] Add token budget context feature#27438
pakrym-oai merged 4 commits into
mainfrom
pakrym/token-budget

Conversation

@pakrym-oai

Copy link
Copy Markdown
Contributor

Why

The model should be able to see bounded context-window budget metadata when the token_budget feature is enabled. The full-window message is only injected with full context, while normal turns get a smaller follow-up only when reported usage first crosses a budget threshold.

What changed

  • Added the TokenBudget feature flag.
  • Added <token_budget> developer fragments for full context-window metadata and current-window remaining tokens.
  • Inserted the threshold message during normal turn handling by comparing token usage before and after sampling, avoiding persistent threshold bookkeeping.
  • Added core integration coverage for full-context-only metadata and 25/50/75 percent threshold messages.

Verification

  • just test -p codex-core token_budget
  • git diff --check

@pakrym-oai
pakrym-oai marked this pull request as ready for review June 10, 2026 19:09
@pakrym-oai
pakrym-oai requested a review from a team as a code owner June 10, 2026 19:09

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

Copy link
Copy Markdown
Contributor

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: a19ec71e8d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread codex-rs/core/src/session/mod.rs
let turn_metadata_header = turn_context
.turn_metadata_state
.current_header_value_for_model_request(&window_id);
let tokens_before_sampling = sess.get_total_token_usage().await;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Badge Detect budget thresholds crossed by queued input

Because this snapshot is taken after run_hooks_and_record_inputs has already appended the current user turn and any pending input, a large input can move the thread from below a budget threshold to above it before this comparison starts. For example, going from 24% used to 26% used via the user's message makes tokens_before_sampling already above 25%, so maybe_record_token_budget_remaining_context never emits the 25% reminder on this or any later turn unless another threshold is crossed.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok for the initial version.

Comment thread codex-rs/core/src/session/mod.rs

@bolinfest bolinfest left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Small suggestions: feel free to take or leave.

assert_eq!(requests.len(), 2);

let expected = vec![format!(
"<token_budget>\nCurrent context window 0.\nYou have {EFFECTIVE_CONTEXT_WINDOW} tokens left in this context window.\n</token_budget>"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

prefer raw string to make this more readable?

Comment thread codex-rs/core/tests/suite/token_budget.rs Outdated
Comment thread codex-rs/core/tests/suite/token_budget.rs Outdated
@pakrym-oai
pakrym-oai merged commit 658af93 into main Jun 11, 2026
31 checks passed
@pakrym-oai
pakrym-oai deleted the pakrym/token-budget branch June 11, 2026 03:07
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 11, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants