fix: guarantee usage rows for streamed /v1/responses#141
Conversation
📝 WalkthroughWalkthroughThis PR introduces context-based enforcement for returning usage data in streaming responses. It adds a context key to control usage data injection, implements stream options cloning to prevent mutation, and updates handler logic to conditionally inject IncludeUsage when enforcement is enabled. Changes span context management, provider adapters, server handlers, and usage logging. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Handler as Server Handler
participant Context as Context Ctx
participant Adapter as Provider Adapter
participant Provider as Chat Provider
Client->>Handler: Stream Chat Request
Handler->>Handler: Check shouldEnforceReturningUsageData()
alt Enforcement Enabled
Handler->>Handler: cloneChatRequestForStreamUsage(req)
Handler->>Handler: Set IncludeUsage: true in StreamOptions
Handler->>Context: WithEnforceReturningUsageData(ctx, true)
end
Handler->>Provider: Call with enforced context & cloned request
alt Responses Stream Path
Provider->>Adapter: ConvertResponsesRequestToChat()
Adapter->>Adapter: cloneStreamOptions()
Adapter->>Context: GetEnforceReturningUsageData()
alt Enforcement Active
Adapter->>Adapter: Set IncludeUsage: true
end
Adapter->>Provider: ChatRequest with usage enabled
end
Provider-->>Handler: Stream response with usage
Handler-->>Client: Usage data in stream
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
internal/providers/responses_adapter.go (1)
988-998:⚠️ Potential issue | 🟠 MajorGate
include_usageinjection to providers that actually support it; the unusedproviderNameparameter suggests this is incomplete.
StreamResponsesViaChatunconditionally forcesstream_options.include_usage = truewhen the context flag is on, but sends the request directly through each provider's nativeStreamChatCompletionwithout any field filtering. TheproviderNameparameter is available but never used—leaving no way to conditionally apply this field only to providers that support it. This violates the coding guideline to "be conservative in what you send to AI model providers."Check which of Groq, Ollama, and Gemini support
stream_options.include_usagein their upstream APIs, then either condition the injection or strip the field before sending to unsupported providers.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@internal/server/handlers_test.go`:
- Around line 2998-3016: The test after calling handler.Responses(c) currently
only verifies a usage entry was persisted but not that the HTTP response was
successful; update the test to assert the response status is 200 (OK)
immediately after the call to handler.Responses(c) by checking the response
recorder/status on the test context (the same context variable used for the
call, e.g., c or the response recorder tied to it) so the test fails if
/v1/responses returns an error status; keep the existing usageLog.entries and
entry checks (RequestID, Provider, Endpoint, and token counts) after this new
status assertion.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: b67a04f3-3322-4037-b434-78ece2926ffa
📒 Files selected for processing (10)
config/config.gointernal/core/context.gointernal/providers/responses_adapter.gointernal/providers/responses_adapter_test.gointernal/server/handlers.gointernal/server/handlers_test.gointernal/usage/factory.gointernal/usage/logger.gointernal/usage/usage.gointernal/usage/usage_test.go
|
@coderabbitai feedback: The review comment is based on incorrect premises. providerName is |
|
Tip For best results, initiate chat on the files or code changes.
✏️ Learnings added
|
Summary
/v1/responsesusage enforcement into the chat-backed responses adapterinclude_usage=truewhenENFORCE_RETURNING_USAGE_DATAis enabled without mutating the canonical requestValidation
stream_options.include_usage=true/v1/responsesusage row to SQLiteRefs GOM-89
Summary by CodeRabbit
New Features
Bug Fixes
Tests
Refactor