Skip to content

Limit model calls and reported tokens per Python Runner run - #6129

Merged
markstuart-oai merged 10 commits into
feat/simple-codexfrom
feat/runner-observed-budgets
Nov 24, 2026
Merged

markstuart-oai merged 10 commits into
feat/simple-codexfrom
feat/runner-observed-budgets

Conversation

@markstuart-oai

@markstuart-oai markstuart-oai commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A Python run can pause for approval, resume on another worker, or hand off to another agent. RunBudget keeps the same model call and token limits throughout that run.

from agents import RunBudget, RunConfig, Runner

config = RunConfig(budget=RunBudget(max_model_calls=3, max_tokens=4000))
result = await Runner.run(agent, question, run_config=config)

Set either limit or both. Zero prevents the first call. A completed response can exceed the token threshold. The Runner can return the answer or run the requested tools, but it blocks the next model call. Missing token usage also blocks that call unless you set missing_usage="continue".

  • max_model_calls counts regular model calls, including follow-ups. Retries, approval replies and compaction add no calls. Reported compaction tokens count toward max_tokens.
  • max_turns still takes precedence when the limits coincide. Use the turn limit for a fallback handler. To let a finite call budget control the stop, pass max_turns=None on the initial run.
  • A budget stop raises RunBudgetError. Its code identifies the reason. For a reached limit, limit is "model_calls" or "tokens". Text redaction can replace the error with a generic one.
  • An agent used as a tool has a separate budget. Native Codex sampling and hosted multi-agent runs reject budgets. The limits cannot cap HTTP attempts or billed spend.

Resume with the same budget and missing-usage policy. RunState 1.25 preserves earlier checkpoint formats and keeps the request counter keys in saved budgets. This change includes no prices, dollar limit or cost estimator.

Test plan

Exercised both Runner modes with scripted responses, approval resumes, handoffs, child agents, retries, missing usage, zero limits and redacted runs. Checked that incompatible restores fail before tools execute. Resumed an approval from the earlier 1.25 writer with one tool execution. The updated branch passed 795 focused tests, formatting, lint, type checks and 13,941 SDK tests, with 53 skipped.

Checks

  • Regression tests
  • Formatting, lint, mypy, Pyright and repository test suite
  • Independent implementation review

@markstuart-oai
markstuart-oai requested review from a team, rm-openai and seratch as code owners November 21, 2026 06:02
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 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-24T06:43:28.355754Z 035f942 New commits
🔒 Security Review ✅ Completed 2026-11-24T06:14:45.619148Z 035f942 New commits
ℹ️ 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.

@jbeckwith-oai jbeckwith-oai 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.

Approved after comprehensive static review and an independent second review. Reviewed admission counting outside retry loops, completed-response and compaction usage, missing versus measured-zero totals, estimator validation, root/child isolation, approval resume, policy validation before durable claims, checkpoint schema checks, and pending child binding identity. The documented limits apply to root admissions and observed usage; they do not cap physical sends, child usage, or billing. No actionable findings.

Hosted CI is complete: 33 passed and 4 skipped; no failures. Static review only; no candidate code or local tests executed.

Preserve native approval receipts and optional run budgets in the same checkpoint.

@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: 908cfd244e

ℹ️ 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".

@jbeckwith-oai jbeckwith-oai 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.

Requesting changes after static re-review and independent validation at 908cfd2. The existing P2 finding is confirmed: #6129 (comment)

When a streamed ResumableSession run hits its budget before the next request, finish_budget_stop persists completed work but bypasses the checkpoint publication callback. Message and subsequent semantic events remain buffered. Publishing alone is insufficient: stream_events also surfaces RunBudgetError before draining queued events. Please publish the committed events and drain them before raising the budget error, with regression coverage for an assistant message plus completed tool work followed by budget exhaustion. Referencing the existing inline finding to avoid duplication.

Static review only; no candidate code or local tests executed. Hosted CI follow-up remains tracked separately.

@jbeckwith-oai jbeckwith-oai 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.

Approved after static re-review and independent validation of the streaming budget-stop fix. The streamed admission path now checkpoints through the publication callback, then marks the original error so committed semantic events drain before it is raised. A failed checkpoint neither publishes nor marks the error for draining. The regression covers blocked, successful, and failed checkpoint publication. This addresses #6129 (comment) and my previous request-changes decision. No additional actionable findings.

Hosted CI snapshot: {"skipped": 2, "in_progress": 7, "success": 17}. Unfinished checks remain tracked. Static review only; no candidate code or local tests executed.

Hosted CI complete at e2a1bc4b50e0eae822b9cb8c981e6370e68d995a: 33 checks passed, 4 skipped. No pending or failed checks. Static review only; no local tests executed.

Refresh follow-up (2026-11-22 21:45 UTC): rechecked current commit 9c7fbadf43571c3880f3f7cce516ff56c4bd1cee against the prior reviewed patch and new base. Added/deleted patch lines are unchanged; surrounding integration context was reviewed with no actionable regression found. GitHub already associates this approval with the current commit; no duplicate review submitted. Earlier CI results above describe the prior commit. Current hosted CI: 7 in_progress, 17 success. Unfinished checks remain pending follow-up. Static review only; no candidate code or local tests executed.

Hosted CI follow-up (2026-11-22 22:20 UTC): current commit 9c7fbadf43571c3880f3f7cce516ff56c4bd1cee completed with 33 successful checks and 2 skipped checks. No failures. Static review only; no local tests executed.

@jbeckwith-oai jbeckwith-oai 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.

Approved after static re-review of 72a3164488973aec6643cba00515cfb7f564eb85. Budget construction now rejects inconsistent cost/estimator combinations, and admission errors identify the reached ceiling with documented precedence. Zero-cost ceilings still block before dispatch; nested/resumed policy checks use the validated configuration. Reviewed the base integration and relevant existing paths; independent review found no actionable regression.

Hosted CI at publication: {'SUCCESS': 18, 'IN_PROGRESS': 6}. Pending checks remain tracked. No candidate code or tests were executed locally.

Update feat/runner-observed-budgets against feat/simple-codex.

@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: e63270dbd1

ℹ️ 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 src/agents/run_budget.py Resolved

@jbeckwith-oai jbeckwith-oai 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.

Request changes: independently confirmed the P2 ordinary-stream budget-stop issue in the existing finding.

_admit marks budget errors for queued-event draining only inside the ResumableRun branch. In an ordinary Runner.run_streamed run, max_requests=1 can allow a response and tool execution, then fail the next admission. A slow consumer sees the unmarked stored exception, exits stream_events before draining, and loses queued message/tool events for work that already completed.

Mark ordinary streamed budget-stop errors for draining as well, including the replacement redacted exception in protected runs. Add a slow-consumer regression without ResumableSession that verifies the completed semantic events are delivered before the budget error, both with and without redact_text. Keep failed persistence from publishing uncommitted events.

Reviewed the refreshed pricing and redaction integration, including USD accumulation, model selection, compaction accounting, and resume constraints. No additional confirmed findings. Hosted CI: 21 passed, 2 skipped, and 3 running at the latest observation. Static review with independent validation; no candidate code or local tests executed.

@jbeckwith-oai jbeckwith-oai 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.

Approved after independent static re-review. The ordinary-stream budget-stop finding is addressed: streamed admission failures now settle the parallel input-guardrail verdict before marking either the ordinary or redacted budget error for event draining. Rejected, cancelled, or failed input checks do not authorize publication, and a failed durable checkpoint still prevents draining uncommitted work. Ordinary sessions correctly use the no-op checkpoint path.

Reviewed the slow-consumer coverage across no session, ordinary session, durable session, and failed persistence, with and without redaction, plus parallel accept/reject/cancel cases. No new actionable findings.

Hosted CI remains pending (17 passed and 7 running at the latest observation); no failures observed. Static review only; no candidate code or local tests executed.

CI follow-up (November 23): all hosted checks at this reviewed commit have completed: 33 passed and 2 skipped, including all eight wheel verifiers. The pending CI follow-up above is resolved. No candidate code or local tests executed.

Update feat/runner-observed-budgets against feat/simple-codex.

@jbeckwith-oai jbeckwith-oai 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.

Approved after static re-review and independent validation. Schema 1.25 prevents older readers from silently dropping saved budgets. Recursive validation retains required nested snapshots until restoration succeeds, rejects missing or ambiguous agent-tool bindings, and checks current-context enablement before durable session claims or approved sibling execution in both run modes. Failed or skipped required-child restoration remains fail-closed. The previous checkpoint publication and queued-event draining fixes remain intact. No new actionable findings.

Hosted CI: 17 checks passed and 7 are running. Static review only; no candidate code or tests were executed.

Hosted CI update (2026-11-23 23:41 UTC): all 33 executed checks passed and 2 were skipped at this reviewed commit. No local tests were run.

@seratch

seratch commented Nov 23, 2026

Copy link
Copy Markdown
Member

This feature addition looks independent from the codex integration, so we may want to review the changes toward main branch. Once everything is fine, we can ship it earlier than the codex integration.

markstuart-oai added a commit that referenced this pull request Nov 24, 2026
Keep the budget reader from #6129 in the next approval checkpoint version.
Update feat/runner-observed-budgets against feat/simple-codex.
markstuart-oai added a commit that referenced this pull request Nov 24, 2026
Keep the budget reader from #6129 in the next approval checkpoint version.

@jbeckwith-oai jbeckwith-oai 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.

Re-reviewed 68e8a88 against the previously reviewed revision, with an independent static review. The estimator example now distinguishes synthetic input/output rates, validates token counts, and explicitly documents per-request overshoot. Rechecked budget admission against the merged continuation and output-preview paths. No new actionable findings.

Hosted CI at the latest check: 8 successful, 16 in progress; completion remains pending. Static review only; no candidate code or local tests were executed.

Hosted CI follow-up (2026-11-24 03:32 UTC): all checks at this reviewed commit are complete: 33 passed and 2 skipped, with no failures. The pending CI follow-up is resolved. Static review only; no candidate code or local tests executed.

@markstuart-oai markstuart-oai changed the title feat: add per-run request, token and estimated cost limits Limit model calls and reported tokens per Python Runner run Nov 24, 2026

@jbeckwith-oai jbeckwith-oai 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.

Approved after static re-review and independent review of 035f942. The simplified model-call/token budget removes pricing callbacks consistently across buffered responses, streams, retries and compaction. The public max_model_calls rename preserves schema 1.25 checkpoint keys and same-policy resume checks. Explicit zero usage remains distinct from missing usage in ScriptedModel. Prior checkpoint publication, guardrail ordering and queued-event draining fixes remain intact. No new actionable findings.

Hosted CI at the latest observation: 11 passed and 13 running; completion remains pending. Static review only; no candidate code or local tests were executed.

Hosted CI follow-up (2026-11-24 06:37 UTC): all checks at this reviewed commit are complete: 33 passed and 2 skipped, with no failures. The pending CI follow-up is resolved. Static review only; no candidate code or local tests executed.

@markstuart-oai
markstuart-oai merged commit 536bfb6 into feat/simple-codex Nov 24, 2026
26 checks passed
@markstuart-oai
markstuart-oai deleted the feat/runner-observed-budgets branch November 24, 2026 06:20
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.

3 participants