Skip to content

Expose result.usage on regular and streamed runs - #6140

Merged
markstuart-oai merged 8 commits into
feat/simple-codexfrom
feat/runner-request-capture
Nov 24, 2026
Merged

markstuart-oai merged 8 commits into
feat/simple-codexfrom
feat/runner-request-capture

Conversation

@markstuart-oai

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

Copy link
Copy Markdown
Contributor

Summary

Reading run usage currently requires going through result.context_wrapper.usage. This change adds a typed, read-only usage property to RunResultBase for both regular and streamed results.

from agents import Runner

result = Runner.run_sync(agent, prompt)
print(result.usage.total_tokens, result.usage.requests)

The property returns the same Usage object as result.context_wrapper.usage, preserving its existing names and counts. During a stream, reading it shows usage accumulated so far. The object can continue to change as usage is reported. Read it after draining the stream for the final reported totals.

usage.requests can include retries. A model that reports only tokens can leave it at zero. RunBudget.max_model_calls counts calls separately. Use the budget to enforce a limit.

This revision supersedes the earlier RequestCapture proposal and needs a fresh review. Network counters and transport instrumentation are deferred. The checkpoint format stays the same.

Test plan

The sync, async and streaming tests check that both access paths return the same object. The stream test reads usage before and after consuming events. The checkpoint test verifies that the SDK still stores usage only with the run context.

138 focused tests passed. Repository verification passed formatting, lint, mypy, Pyright and the test suite with 13,942 passed, 53 skipped and 88 deselected.

Checks

  • Regression tests
  • Repository verification
  • Independent review of the replacement diff

@markstuart-oai
markstuart-oai requested review from a team, rm-openai and seratch as code owners November 21, 2026 14:38
@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:47:23.105510Z a18eb98 New commits
🔒 Security Review ✅ Completed 2026-11-24T06:52:35.537384Z a18eb98 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.

@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: 79564c8739

ℹ️ 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/models/_run_context.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 existing P2 guardrail accounting finding. In streamed runs, model_run_context_stream enters _model_submission before get_model_stream checks the input-guardrail tripwire. If the guardrail trips during awaited preparation, _StreamAttemptSkipped prevents model.stream_response from being called, yet the capture records one submission. Move registration after that gate while retaining one logical submission across retries, and add a regression for a guardrail tripping during input-filter or on_llm_start preparation. Referencing the existing comment rather than adding a duplicate inline finding.

Completed comprehensive static review with two independent reviews covering capture ownership, attached-child isolation, retries, automatic compaction, HTTP authentication/redirects and trace gaps, WebSocket send/reply boundaries, frozen snapshots, and disabled-capture behavior. No additional actionable findings.

Hosted CI snapshot: {"SKIPPED": 2, "SUCCESS": 17, "IN_PROGRESS": 7}. Remaining checks stay pending. Static review only; 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 static re-review and an independent second review. The guardrail accounting finding is fixed: streamed submissions are registered only after the input-guardrail skip gate, in the task that actually opens the model stream. The per-stream flag preserves one logical submission across retries, and the capture scope restores the enclosing context. Reviewed timed and untimed retry paths, nonstreaming scope preservation, and the enabled/disabled capture regression matrix. No remaining actionable findings in the reviewed changes.

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

Hosted CI after the base merge at 95924303df849a364be0e32fcac74b3bb3d422e3: all 33 checks passed, with no pending or failed checks. The merge integration was statically rechecked; approval remains unchanged. 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.

Re-reviewed current commit 6f5bb569cd1728cea29ef2208791d2d4573809bb, including the integration changes since the prior approval. The HTTP capture scope now correctly wraps create_with_retry_observation, and failed-request retry accounting recognizes the SDK-owned _ObservedOpenAI client without broadening support to arbitrary subclasses. Checked request ownership, context cleanup on cancellation, streamed guardrail ordering, session/approval resume, child attribution, and native/borrowed transport coverage; no actionable regression found.

Hosted CI: {'SUCCESS': 18, 'IN_PROGRESS': 6}. Unfinished checks remain pending follow-up. Static review only; no candidate code or local tests executed.

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

Update feat/runner-request-capture 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 of 6b8ae2f9ea24e154d20f44660ecd217b694f2d03. Capture now includes multiple direct runs using balanced active-root accounting. Child runs retain their ownership, independent nested calls stay excluded, and exiting with work active freezes a partial snapshot. Opaque-only routes now correctly report unknown observed totals rather than zero. Reviewed the changed behavior and base integration; independent review found no actionable issue.

Hosted CI at publication: {'SUCCESS': 9, 'IN_PROGRESS': 15}. Pending checks are tracked. No candidate code or tests were executed locally.

Update feat/runner-request-capture 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 of 5d34bfbde4f6ba345229fdfb03a886b1982873ef. The refreshed request-capture integration preserves redaction during model iteration, error interpretation and stream closure. The AsyncExitStack retains model and capture scopes across retries and cleanup; automatic compaction remains inside its diagnostic boundary. Core capture behavior is unchanged. Independent review found no actionable regression.

Hosted CI: {"success":20,"in_progress":4}. Remaining checks are tracked. 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-request-capture 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 an independent second review. The feature change clarifies root/child route ownership and per-route coverage/counts. Production changes and regression tests match the prior approved feature after accounting for the refreshed base. Rechecked capture ownership, HTTP/WebSocket observation, retries, automatic compaction, and streamed cancellation/closure against the new approved-output and final-output-policy integration. Submission counting still follows the guardrail skip gate, and capture scopes remain active through stream cleanup alongside redaction. No actionable findings.

Hosted CI at review: 16 checks passed and 8 running, with no failures. Remaining checks require follow-up. Static review only; no candidate code or local tests 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.

Update feat/runner-request-capture against feat/simple-codex.
@markstuart-oai markstuart-oai changed the title feat: capture Runner submissions and provider request attempts Expose result.usage on regular and streamed runs 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 a fresh static review and independent review of the replacement diff. RunResultBase.usage exposes the existing live context usage object for both regular and streamed results, without adding serialized fields or changing accounting. Reviewed sync/async/stream identity and checkpoint-shape coverage. The earlier RequestCapture instrumentation is removed; the new base includes the reviewed budget implementation. No actionable findings.

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

Hosted CI follow-up: 31 successful, 3 skipped, 1 failed. macOS ARM64/Python 3.14 verification failed in test_worker_exit_joins_native_close with assert workers (empty list): https://github.com/openai/openai-agents-python/actions/runs/45342937369/job/138278646559. The test, native bridge and Python provider are unchanged from the base; this matches the previously observed fast-completion callback-observation failure. Static inspection found no connection to this PR's usage accessor change, so the code approval stands, but CI is not green. No local tests executed.

Hosted CI follow-up: all currently reported checks completed with 33 successful, 4 skipped, no failures. The same-head rerun cleared the earlier native-close observation failure.

@markstuart-oai
markstuart-oai merged commit adaa6e1 into feat/simple-codex Nov 24, 2026
52 of 53 checks passed
@markstuart-oai
markstuart-oai deleted the feat/runner-request-capture branch November 24, 2026 16:39
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.

2 participants