gateway icon indicating copy to clipboard operation
gateway copied to clipboard

feat: support openrouter usage in api

Open unsync opened this issue 7 months ago โ€ข 0 comments

Code Quality new feature

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 usage parameter in the request body (enable cost report)
  • Add a new type for the OpenRouter usage format (described here)
  • Return the full usage in 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 usage parameter to the request configuration
  • Created a new interface OpenrouterUsageDetails to 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

  1. Add unit tests to verify the new usage parameter functionality

  2. Add validation for the usage parameter to ensure it's a boolean value

  3. Add documentation comments for the new OpenrouterUsageDetails interface

  4. Consider adding examples of how to use the new usage parameter in the code comments

unsync avatar Apr 23 '25 09:04 unsync