feat: Add opt-in metrics collection for chat requests #102
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.