Skip to content

feat: export request TTFB, stream duration, TTFT and token throughput - #24

Merged
reonokiy merged 3 commits into
mainfrom
feat/inference-performance-metrics
Nov 7, 2026
Merged

reonokiy merged 3 commits into
mainfrom
feat/inference-performance-metrics

Conversation

@reonokiy

@reonokiy reonokiy commented Nov 7, 2026 •

Copy link
Copy Markdown
Owner

The existing OTLP pipeline reports token usage and inference lifetime but cannot distinguish HTTP ingress latency, full response stream lifetime, first generated output, or per-request throughput. Add five gateway-specific histograms and numeric per-request log/span fields using the existing OTLP configuration.

Metric Unit Meaning
codex_api.http.time_to_first_byte s Request middleware entry to response headers ready (application-observed TTFB)
codex_api.http.request.duration s Request middleware entry to response-body end, error, or early drop
codex_api.inference.time_to_first_token s Inference start to first non-empty text/refusal/visible-reasoning/tool-input delta
codex_api.inference.output_tokens_per_second token/s Upstream-reported output tokens divided by start-to-terminal inference duration
codex_api.inference.duration s Inference start to observed terminal event

HTTP request lifecycle

An outer middleware starts before authorization, request body reads/decompression, validation, concurrency waiting and upstream setup/retries. A response-body wrapper stops total duration at the final frame/EOF rather than when response headers return, preserving frames, trailers, size hints and backpressure. Normal exhaustion, body errors and early drops produce distinct completed, body_error and cancelled outcomes. Fully consumed HTTP error responses retain their actual status and have completed body outcome. Empty and HEAD responses complete at headers. HTTP 101 upgrades use upgraded and measure only the handshake, not socket lifetime.

All router requests, including rejected requests and non-inference endpoints, generate separate codex_api.http.request logs/server spans with ttfb_ms, total_duration_ms, method, matched route, status and outcome. Cancellation before headers has no TTFB or status. HTTP request IDs are independent of inference usage IDs; supplied W3C parent trace context is honored. Histogram dimensions are limited to bounded method/route/status/outcome values, excluding raw paths and identifiers.

TTFB means application response headers ready, not the first body chunk or actual network delivery. HTTP timers exclude connection acceptance, TLS and request header parsing before middleware dispatch. Stream duration ends at application-observed completion/drop; socket/proxy buffering and actual client receipt are outside this measurement.

Inference measurements

Responses HTTP/SSE, native aliases, compact inference and generating WebSocket turns produce usage logs with inference_duration_ms, plus ttft_ms and tps when available. Existing full-guard-lifetime duration metrics remain unchanged. HTTP SSE timestamps are captured before internal channel delivery and freeze on the first terminal event. WebSocket response attribution uses explicit response IDs, bounded item-ID mappings and unambiguous lanes; duplicate terminals and ambiguous/unknown-item deltas do not produce duplicate or misassigned TTFT samples.

TTFT measures gateway-observed first generated content, not server-internal prefill time. TPS includes initial wait and reported reasoning tokens; it is overall inference throughput, not decode-only speed, and never estimates tokens from text or chunks. Failed/incomplete/cancelled/missing-usage/zero-token/zero-duration responses omit TPS. Inference clocks exclude HTTP upload/validation and WebSocket handshake, which are separately covered by the HTTP request metrics. Standalone images/search, audio, Realtime and generic proxies receive HTTP lifecycle metrics but no synthetic TTFT/TPS. No prompts, generated content, tool arguments or credentials are exported, and identifiers remain outside metric dimensions.

Validation

  • cargo test --workspace --locked: 317 passed, 0 failed, 12 ignored. Optional real-account/client/source-audit cases were not run locally.
  • Timing/formula tests inspect actual in-memory histograms, JSON logs, numeric span/log attributes, missing usage, failure/cancellation, terminal deduplication and multiplexed WebSocket attribution.
  • HTTP lifecycle tests cover deferred stream completion, body errors, cancellation before/after headers, preserved data/trailers/size hints and exactly one record per body.
  • Local socket tests verify slow body upload is included, auth/validation/policy rejections and HEAD are recorded, raw fallback paths are excluded, and HTTP/SSE/WebSocket aliases export both lifecycle and inference records.
  • cargo fmt --all -- --check and git diff --check pass.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 7, 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-07T09:17:44.219537Z e4e9455 PR opened
ℹ️ 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.

@reonokiy reonokiy changed the title feat: export per-request TTFT and output token throughput feat: export request TTFB, stream duration, TTFT and token throughput Nov 7, 2026
@reonokiy
reonokiy merged commit 4566c1e into main Nov 7, 2026
3 checks passed
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