Skip to content

Conversation

@VAIBHAVSING
Copy link

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

@VAIBHAVSING
Copy link
Author

@timothycarambat PTAL πŸ‘‹

@timothycarambat timothycarambat added the PR:needs review Needs review by core team label Oct 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR:needs review Needs review by core team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants