gateway
gateway copied to clipboard
feat: support openrouter usage in api
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:
- Accept
usageparameter in the request body (enable cost report) - Add a new type for the OpenRouter
usageformat (described here) - Return the full
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:
- Added the
usageparameter to the request configuration - Created a new interface
OpenrouterUsageDetailsto properly type the usage data format - Updated the response handling to return the full usage information in non-streaming responses
๐ 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
- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] Documentation update
- [ ] Refactoring (no functional changes)
How Has This Been Tested?
- [ ] Unit Tests
- [ ] Integration Tests
- [x] Manual Testing
Screenshots (if applicable)
N/A
Checklist
- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my feature works
- [x] New and existing unit tests pass locally with my changes
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