feat: support openrouter usage in api#1055
Merged
Merged
Conversation
VisargD
approved these changes
Apr 25, 2025
Israel-Laguan
approved these changes
Apr 25, 2025
Contributor
Summary By MatterAI
🔄 What ChangedThis PR adds support for OpenRouter's usage accounting feature by:
🔍 Impact of the ChangeThis change allows clients to request and receive detailed usage information from OpenRouter, including the cost of each request. This is valuable for usage accounting and cost tracking purposes. 📁 Total Files Changed1 file modified: 🧪 Test AddedN/A - No tests were added in this PR 🔒Security VulnerabilitiesN/A - No security vulnerabilities identified Type of Change
How Has This Been Tested?
Screenshots (if applicable)N/A Checklist
Related IssuesImplements OpenRouter usage accounting feature as described in https://openrouter.ai/docs/use-cases/usage-accounting Sequence DiagramsequenceDiagram
participant Client
participant Gateway
participant OpenRouter
Client->>Gateway: POST /v1/chat/completions
Note over Client,Gateway: Request includes 'usage' parameter
Gateway->>OpenRouter: Forward request with 'usage' parameter
Note over Gateway,OpenRouter: OpenRouter processes request and calculates usage
OpenRouter-->>Gateway: Response with detailed usage information
Note over OpenRouter,Gateway: Usage includes cost and token details
Gateway-->>Client: Return response with full usage details
Note over Gateway,Client: Response includes prompt_tokens, completion_tokens, total_tokens, cost
alt Streaming Response
Client->>Gateway: POST /v1/chat/completions with stream=true
Gateway->>OpenRouter: Forward streaming request
loop For each chunk
OpenRouter-->>Gateway: Stream chunk with usage details
Gateway-->>Client: Forward stream chunk with full usage details
end
end
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Author Description
OpenRouter recently added an api parameter to get the cost of the request in the responses:
https://openrouter.ai/docs/use-cases/usage-accounting
This PR introduces the following changes:
usageparameter in the request body (enable cost report)usageformat (described here)usagein the non-streaming response (we already do that in the streaming responses)Description
🔄 What Changed
This PR adds support for OpenRouter's usage accounting feature by implementing the following changes:
usageparameter to the request configurationOpenrouterUsageDetailsto properly type the usage data format🔍 Impact of the Change
This enhancement allows users to receive detailed cost and token usage information from OpenRouter API calls, including cost reporting and detailed token usage breakdowns.
📁 Total Files Changed
1 file modified:
src/providers/openrouter/chatComplete.ts(+23, -15)🧪 Test Added
N/A - No tests were added in this PR
🔒 Security Vulnerabilities
No security vulnerabilities identified
Motivation
OpenRouter recently added an API parameter to get the cost of the request in the responses, as documented in their usage accounting documentation. This PR implements support for this feature to provide users with detailed usage information.
Type of Change
How Has This Been Tested?
Screenshots (if applicable)
N/A
Checklist
Related Issues
Related to OpenRouter's usage accounting feature: https://openrouter.ai/docs/use-cases/usage-accounting
Quality Recommendations
Add unit tests to verify the new usage parameter functionality
Add validation for the usage parameter to ensure it's a boolean value
Add documentation comments for the new OpenrouterUsageDetails interface
Consider adding examples of how to use the new usage parameter in the code comments