feat: Add opt-in metrics collection for chat requests#102
feat: Add opt-in metrics collection for chat requests#102nazq wants to merge 1 commit intograniet:mainfrom
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ 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".
src/chat/tracked.rs
Outdated
| // Record first chunk time | ||
| if this.first_chunk_time.is_none() { | ||
| *this.first_chunk_time = Some(Instant::now()); | ||
| } |
There was a problem hiding this comment.
Record TTFT only when a text token is seen
Here first_chunk_time is set on the very first streamed item, even if that item is a tool-use event or an empty/usage-only chunk. For tool-only responses or streams that send ToolUseStart/Done before any text, time_to_first_token becomes Some(...) even though no token was emitted, which makes the metric inaccurate. Consider setting first_chunk_time only when extract_text() returns Some (and ideally non-empty), so TTFT reflects the first actual text token.
Useful? React with 👍 / 👎.
4073506 to
871b0b2
Compare
871b0b2 to
b0b056b
Compare
Summary
ChatMetricsstruct for tracking token usage and timingTracked<S>stream wrapper for metrics-aware streamingMetricsProvidertrait withchat_with_metrics()andchat_stream_with_metrics().enable_metrics(bool)builder methodStreamChunk::Usagevariant for token trackingMotivation
I realized I was building most of these features into multiple App engines, so thought I'd push down and share. Users need visibility into LLM request performance and costs. This feature provides:
Approach
Opt-in design: Metrics collection is disabled by default. Users must explicitly enable it via
.enable_metrics(true)on the builder. This ensures zero overhead for users who don't need metrics.Non-breaking: All new APIs are additive. Existing
chat()andchat_stream()methods work unchanged.Usage
Enable Metrics
Non-Streaming
Streaming
Changes
New Files
src/metrics.rsChatMetricsstruct andMetricsProvidertraitsrc/chat/tracked.rsTracked<S>stream wrapperModified Files
src/lib.rsmetricsmodulesrc/chat/mod.rsTrackedsrc/builder.rsenable_metricsfield and methodTest Plan
cargo checkpassescargo clippypassescargo testpassescargo build --releasepassesDependencies
This feature integrates with PR #96 (
stream_usage) which addsStreamChunk::Usagevariant. The metrics collection leverages this to capture token counts during streaming.