Skip to content

feat: support openrouter usage in api #1055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2025

Conversation

unsync
Copy link
Contributor

@unsync unsync commented Apr 23, 2025

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)
  • 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
  • Manual Testing

Screenshots (if applicable)

N/A

Checklist

  • My code follows the style guidelines of this project
  • 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
  • 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

Copy link

Code Quality new feature

Summary By MatterAI MatterAI logo

🔄 What Changed

This PR adds support for OpenRouter's usage accounting feature by:

  • Adding a new usage parameter to the request configuration
  • Creating a detailed OpenrouterUsageDetails interface to handle the expanded usage information
  • Updating response handling to return the full usage details including cost information

🔍 Impact of the Change

This 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 Changed

1 file modified: src/providers/openrouter/chatComplete.ts with 23 additions and 15 deletions

🧪 Test Added

N/A - No tests were added in this PR

🔒Security Vulnerabilities

N/A - No security vulnerabilities identified

Type of Change

  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • 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
  • Manual Testing

Screenshots (if applicable)

N/A

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • 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
  • New and existing unit tests pass locally with my changes

Related Issues

Implements OpenRouter usage accounting feature as described in https://openrouter.ai/docs/use-cases/usage-accounting

Sequence Diagram

sequenceDiagram
    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
Loading

@VisargD VisargD merged commit 7fa8ce1 into Portkey-AI:main May 5, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants