[PR #4482] [CLOSED] Openrouter user #4641

Closed
opened 2026-02-22 18:36:14 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Mintplex-Labs/anything-llm/pull/4482
Author: @VAIBHAVSING
Created: 10/3/2025
Status: Closed

Base: masterHead: openrouter-user


📝 Commits (2)

  • 831236a feat[openrouter]: add openrouter user tracking
  • 6d4cc6c feat : refractor code

📊 Changes

3 files changed (+12 additions, -2 deletions)

View changed files

📝 server/utils/AiProviders/openRouter/index.js (+7 -2)
📝 server/utils/chats/apiChatHandler.js (+3 -0)
📝 server/utils/chats/stream.js (+2 -0)

📄 Description

Pull Request Type

  • feat
  • 🐛 fix
  • ♻️ refactor
  • 💄 style
  • 🔨 chore
  • 📝 docs

Relevant Issues

resolves #xxx

What is in this change?

This PR implements OpenRouter user tracking support for AnythingLLM, enabling multi-user systems to track usage per user when using OpenRouter as the LLM provider.

Key Changes:

  1. OpenRouter LLM Provider (server/utils/AiProviders/openRouter/index.js):

    • Updated getChatCompletion and streamGetChatCompletion methods to accept an optional user parameter
    • Added logic to include user: String(user.id) in OpenRouter API requests when user information is available
    • Maintains backward compatibility - the user parameter is optional
  2. Stream Chat Handler (server/utils/chats/stream.js):

    • Modified calls to LLM completion methods to pass the user object in options
    • Ensures user context flows through workspace chat streams
  3. API Chat Handler (server/utils/chats/apiChatHandler.js):

    • Updated chatSync and streamChat functions to pass user context to LLM methods
    • Enables user tracking for both synchronous and streaming API endpoints

Benefits:

  • Enables OpenRouter's user tracking features for multi-user AnythingLLM deployments
  • Supports usage attribution, cost tracking, and analytics per user
  • Maintains full backward compatibility with existing single-user setups
  • No impact on other LLM providers

Technical Details:

  • User ID is converted to string as required by OpenRouter's API
  • Graceful degradation when user information is not available (anonymous chats, embed widgets)
  • Covers main chat flows (workspace chat, API chat) - agent chats noted for future enhancement

Additional Information

This implementation addresses the need for user-level tracking in multi-tenant LLM services using OpenRouter. The feature enables businesses to properly attribute API costs, implement per-user rate limits, and gain insights into usage patterns.

OpenRouter Documentation Reference: https://openrouter.ai/docs/use-cases/user-tracking

Future Enhancements (not included in this PR):

  • Support for agent-based chat user tracking
  • Configuration option to enable/disable user tracking
  • Additional user metadata support (username, role) if OpenRouter adds support

Developer Validations

  • I ran yarn lint from the root of the repo & committed changes
  • Relevant documentation has been updated
  • I have tested my code functionality
  • Docker build succeeds locally

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Mintplex-Labs/anything-llm/pull/4482 **Author:** [@VAIBHAVSING](https://github.com/VAIBHAVSING) **Created:** 10/3/2025 **Status:** ❌ Closed **Base:** `master` ← **Head:** `openrouter-user` --- ### 📝 Commits (2) - [`831236a`](https://github.com/Mintplex-Labs/anything-llm/commit/831236a563445ba3d74b890b84093551b7f2a96b) feat[openrouter]: add openrouter user tracking - [`6d4cc6c`](https://github.com/Mintplex-Labs/anything-llm/commit/6d4cc6c7394cfef5b4183f37db7d29db352246ae) feat : refractor code ### 📊 Changes **3 files changed** (+12 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `server/utils/AiProviders/openRouter/index.js` (+7 -2) 📝 `server/utils/chats/apiChatHandler.js` (+3 -0) 📝 `server/utils/chats/stream.js` (+2 -0) </details> ### 📄 Description ### Pull Request Type <!-- For change type, change [ ] to [x]. --> - [x] ✨ feat - [ ] 🐛 fix - [ ] ♻️ refactor - [ ] 💄 style - [ ] 🔨 chore - [ ] 📝 docs ### Relevant Issues <!-- Use "resolves #xxx" to auto resolve on merge. Otherwise, please use "connect #xxx" --> resolves #xxx ### What is in this change? <!-- Describe the changes in this PR that are impactful to the repo. --> This PR implements OpenRouter user tracking support for AnythingLLM, enabling multi-user systems to track usage per user when using OpenRouter as the LLM provider. **Key Changes:** 1. **OpenRouter LLM Provider** (`server/utils/AiProviders/openRouter/index.js`): - Updated `getChatCompletion` and `streamGetChatCompletion` methods to accept an optional `user` parameter - Added logic to include `user: String(user.id)` in OpenRouter API requests when user information is available - Maintains backward compatibility - the `user` parameter is optional 2. **Stream Chat Handler** (`server/utils/chats/stream.js`): - Modified calls to LLM completion methods to pass the `user` object in options - Ensures user context flows through workspace chat streams 3. **API Chat Handler** (`server/utils/chats/apiChatHandler.js`): - Updated `chatSync` and `streamChat` functions to pass user context to LLM methods - Enables user tracking for both synchronous and streaming API endpoints **Benefits:** - Enables OpenRouter's user tracking features for multi-user AnythingLLM deployments - Supports usage attribution, cost tracking, and analytics per user - Maintains full backward compatibility with existing single-user setups - No impact on other LLM providers **Technical Details:** - User ID is converted to string as required by OpenRouter's API - Graceful degradation when user information is not available (anonymous chats, embed widgets) - Covers main chat flows (workspace chat, API chat) - agent chats noted for future enhancement ### Additional Information <!-- Add any other context about the Pull Request here that was not captured above. --> This implementation addresses the need for user-level tracking in multi-tenant LLM services using OpenRouter. The feature enables businesses to properly attribute API costs, implement per-user rate limits, and gain insights into usage patterns. **OpenRouter Documentation Reference:** https://openrouter.ai/docs/use-cases/user-tracking **Future Enhancements (not included in this PR):** - Support for agent-based chat user tracking - Configuration option to enable/disable user tracking - Additional user metadata support (username, role) if OpenRouter adds support ### Developer Validations <!-- All of the applicable items should be checked. --> - [ ] I ran `yarn lint` from the root of the repo & committed changes - [ ] Relevant documentation has been updated - [ ] I have tested my code functionality - [ ] Docker build succeeds locally --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-22 18:36:14 -05:00
yindo closed this issue 2026-02-22 18:36:14 -05:00
yindo changed title from [PR #4482] Openrouter user to [PR #4482] [CLOSED] Openrouter user 2026-06-05 15:19:36 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mintplex-Labs/anything-llm#4641