feat: enhance token usage details for upstream OpenRouter - #1120
Conversation
|
""" WalkthroughThe changes introduce new fields to OpenAI request and response data structures for enhanced usage and cost tracking, update request adaptation logic for OpenRouter channels, refine usage statistics handling in Claude conversion, and add a function for recalculating cached creation tokens based on cost in quota management. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Adaptor
participant Service
participant QuotaManager
Client->>Adaptor: Send OpenAI request (OpenRouter channel)
Adaptor->>Adaptor: Set Usage = {"include": true}
Adaptor->>Service: Forward adapted request
Service->>Service: Process response, update usage stats
Service->>QuotaManager: Call PostClaudeConsumeQuota with usage/cost info
QuotaManager->>QuotaManager: If OpenRouter, recalculate cached tokens using cost
Poem
""" 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 3
🧹 Nitpick comments (2)
dto/openai_response.go (1)
181-183: Expose negative-cost guard / doc comment?
Costmay be zero or even negative when upstream returns unexpected data. Down-stream math (e.g.CalcOpenRouterCacheCreateTokens) currently assumes a non-negative value.
Recommend clamping to>=0or documenting the expected range at the struct site.relay/channel/openai/adaptor.go (1)
162-164: Minor readability: use raw string literal.Back-ticks avoid the noisy escape sequences.
- request.Usage = json.RawMessage("{\"include\": true}") + request.Usage = json.RawMessage(`{"include":true}`)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
dto/openai_request.go(1 hunks)dto/openai_response.go(1 hunks)relay/channel/openai/adaptor.go(1 hunks)service/convert.go(1 hunks)service/quota.go(3 hunks)
🔇 Additional comments (2)
dto/openai_request.go (1)
59-60: Looks good – new field integrates cleanly.No functional concerns. The new
Usagefield is correctly typed asjson.RawMessageand keeps the struct backward-compatible viaomitempty.service/convert.go (1)
279-288: Nil-safety handled – good update.The extra guard around
oaiUsageprevents a latent nil-pointer panic and the new cache fields align withdto.Usage.
LGTM.
use review's suggestion Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
feat: enhance token usage details for upstream OpenRouter
Enhanced OpenRouter token usage details.
Reference
Summary by CodeRabbit