Skip to content

core: emit responses api call analytics#18030

Open
rhan-oai wants to merge 3 commits intomainfrom
pr18030
Open

core: emit responses api call analytics#18030
rhan-oai wants to merge 3 commits intomainfrom
pr18030

Conversation

@rhan-oai
Copy link
Copy Markdown
Collaborator

@rhan-oai rhan-oai commented Apr 16, 2026

Why

The analytics crate can now represent and reduce Responses API call events, but core still needs to observe actual sampling request lifecycles and submit those observations. This PR wires the event emission into the turn execution path without changing model behavior.

What changed

run_turn now owns a monotonically increasing turn_responses_call_index counter for the turn. run_sampling_request allocates an index for each actual Responses API attempt, including retry attempts, so emitted analytics can be ordered within the turn.

run_sampling_request creates a ResponsesApiCallAttempt after prompt construction. That attempt captures the prompt input items that are actually sent to the model, the call start time, completed output items observed from the stream, the Responses API response_id when available, and token usage when the stream completes.

To avoid cloning and retaining the full prompt for analytics, the attempt stores precomputed item metadata rather than raw ResponseItems. Input metadata is computed from borrowed prompt items before the API call, and output metadata is appended as each completed output item arrives.

try_run_sampling_request now receives the attempt state. It records completed output metadata on ResponseEvent::OutputItemDone, records responses_id and token usage on ResponseEvent::Completed, and otherwise leaves the existing stream handling behavior intact.

Terminal paths in run_sampling_request now submit the attempt through emit_responses_api_call_attempt. Calls that observe response.completed emit completed; context-window, usage-limit, exhausted-retry, non-retryable failures, and retryable failures before a retry/fallback emit failed; aborted turns and locally preempted streams emit interrupted. If cancellation wins before stream setup completes, the attempt is not emitted, avoiding phantom API-call analytics for requests that were never established. The emission remains gated by Feature::GeneralAnalytics.

The analytics client also truncates Responses API call errors to 1024 bytes before building the final fact, keeping analytics-specific payload constraints in the analytics crate instead of core.

Verification

Verified with cargo check -p codex-core. The stack was also exercised with cargo test -p codex-analytics for the analytics-side event handling.


Stack created with Sapling. Best reviewed with ReviewStack.

Copy link
Copy Markdown
Contributor

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

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: 569266739b

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

@rhan-oai rhan-oai force-pushed the pr18030 branch 6 times, most recently from b32fbad to b7bd713 Compare April 20, 2026 18:07
@rhan-oai rhan-oai requested a review from a team as a code owner April 20, 2026 18:07
@rhan-oai rhan-oai force-pushed the pr18030 branch 9 times, most recently from 070c807 to 4565f6d Compare April 21, 2026 00:52
@rhan-oai rhan-oai changed the base branch from main to pr18029 April 21, 2026 01:45
@rhan-oai rhan-oai changed the base branch from pr18029 to main April 21, 2026 05:16
@rhan-oai rhan-oai changed the base branch from main to pr18029 April 21, 2026 16:44
@rhan-oai rhan-oai changed the base branch from pr18029 to main April 21, 2026 16:57
@rhan-oai rhan-oai changed the base branch from main to pr18029 April 21, 2026 16:58
@rhan-oai rhan-oai changed the base branch from pr18029 to main April 21, 2026 17:08
@rhan-oai rhan-oai changed the base branch from main to pr18029 April 21, 2026 17:08
@rhan-oai rhan-oai changed the base branch from pr18029 to main April 21, 2026 17:15
@rhan-oai rhan-oai changed the base branch from main to pr18029 April 21, 2026 17:33
@rhan-oai rhan-oai changed the base branch from pr18029 to main April 21, 2026 19:14
@rhan-oai
Copy link
Copy Markdown
Collaborator Author

@codex

Copy link
Copy Markdown
Contributor

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

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

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

@rhan-oai
Copy link
Copy Markdown
Collaborator Author

@codex

@openai openai deleted a comment from chatgpt-codex-connector Bot Apr 22, 2026
Copy link
Copy Markdown
Contributor

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

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: 1d923c73d7

ℹ️ 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 codex-rs/core/src/session/turn.rs Outdated
Comment on lines +1200 to +1205
emit_responses_api_call_attempt(
sess.as_ref(),
turn_context.as_ref(),
responses_api_call_attempt,
CodexResponsesApiCallStatus::Completed,
/*error*/ None,
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.

P1 Badge Report preempted stream attempts as interrupted

run_sampling_request marks every Ok path as Completed, but try_run_sampling_request can break Ok(...) early when mailbox preemption fires before any ResponseEvent::Completed. In that case the stream is locally cut short and response_id/token usage may never be observed, so analytics misclassifies a partial attempt as a successful completed call.

Useful? React with 👍 / 👎.

Comment on lines +1244 to +1249
emit_responses_api_call_attempt(
sess.as_ref(),
turn_context.as_ref(),
responses_api_call_attempt,
status,
error,
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 Skip emitting attempts when cancellation preempts stream start

try_run_sampling_request maps cancellation to TurnAborted before stream setup completes, yet run_sampling_request always emits an Interrupted analytics attempt for that error. This records phantom API attempts when no request was actually sent, inflating per-turn call counts and interruption metrics.

Useful? React with 👍 / 👎.

Comment on lines +262 to +264
let Some(content_items) = output.content_items() else {
return (None, None);
};
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 Count text outputs in function-call output part metadata

function_call_output_part_counts returns (None, None) whenever output is the Text variant, even though that payload is one text part. As a result, text-only tool outputs are emitted with null part counts, undercounting modality statistics for Responses API item metadata.

Useful? React with 👍 / 👎.

@openai openai deleted a comment from chatgpt-codex-connector Bot Apr 22, 2026
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