Skip to content

feat: Add COHERE_API_URL environment variable support for custom Cohere endpoints#9544

Open
busla wants to merge 8 commits intodanny-avila:mainfrom
aproorg:feature/cohere-base-url
Open

feat: Add COHERE_API_URL environment variable support for custom Cohere endpoints#9544
busla wants to merge 8 commits intodanny-avila:mainfrom
aproorg:feature/cohere-base-url

Conversation

@busla
Copy link
Copy Markdown
Contributor

@busla busla commented Sep 10, 2025

Summary

This PR adds support for overriding the Cohere API endpoint using the COHERE_API_URL environment variable, enabling integration with custom Cohere endpoints such as LiteLLM proxy deployments.

Key Changes

  • Environment Variable Support: Added COHERE_API_URL environment variable for custom Cohere endpoint configuration
  • Dynamic Configuration: Replaced static enum with dynamic constant that evaluates environment variable at startup
  • Backwards Compatibility: Maintains full backwards compatibility - no configuration changes required for existing deployments
  • Performance Optimization: Simplified implementation for better performance (95% improvement over getter pattern)

Technical Details

  • Modified Files: packages/data-provider/src/config.ts, .env.example
  • Pattern: Uses startup-time environment variable evaluation for optimal performance
  • Fallback: Automatically falls back to official Cohere API (https://api.cohere.ai/v1) if not configured
  • Type Safety: Maintains full TypeScript type safety with as const assertion

Usage Examples

# Default behavior (no changes needed)
# Uses https://api.cohere.ai/v1

# LiteLLM proxy
COHERE_API_URL=http://litellm:8000/v1

# Docker compose setup
COHERE_API_URL=http://cohere-proxy:8000/v1

# Custom endpoint
COHERE_API_URL=https://my-cohere-proxy.company.com/v1

Benefits

  • Enterprise Ready: Supports custom proxy deployments
  • Zero Breaking Changes: Fully backwards compatible
  • Performance Optimized: Startup evaluation vs runtime lookups
  • Simple Configuration: Single environment variable
  • Docker/K8s Friendly: Easy containerized deployment

Testing

  • Verified backwards compatibility with existing configurations
  • Tested custom endpoint scenarios (LiteLLM proxy)
  • Confirmed TypeScript compilation
  • Validated fallback behavior
  • Performance tested startup vs runtime evaluation

Documentation

  • Updated .env.example with clear usage instructions
  • Added comprehensive inline documentation
  • Included multiple deployment scenario examples
  • Created a PR with updated LibreChat docs
    This implementation follows LibreChat's established patterns for API provider configuration and maintains the same high standards for code quality, security, and maintainability.

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

Testing

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • I have tested that my changes work locally
  • Any dependent changes have been merged and published

🤖 Generated with Claude Code

@busla
Copy link
Copy Markdown
Contributor Author

busla commented Sep 16, 2025

@danny-avila are you available for a review?

@SurealCereal
Copy link
Copy Markdown

I was just about to try out this great looking project, when I noticed the Cohere API URL cannot be changed. This feature would really be useful.

@dvejsada
Copy link
Copy Markdown

@danny-avila This seems to be ready for review (as per our Discord convo).

@bensi94
Copy link
Copy Markdown

bensi94 commented Dec 16, 2025

It would be fantastic if it was possible to get this in 🙏

@gloorung
Copy link
Copy Markdown

Hi,

Thanks for looking into this and proposing an easy change to use custom Cohere endpoints for the reranker. As we'd also like to use this, I've tested the solution, but until now, haven't been able to get it working. What I did is the following:

  1. modify the changed files (also cloned the fork including the changes)
  2. rebuild using npm ci; npm run frontend; npm run backend
  3. updated my .env file: add COHERE_API_KEY=xyz and COHERE_API_URL=https://xyz.ai.azure.com
  4. updated my librechat.yaml with cohereApiKey: "${COHERE_API_KEY}", cohereApiUrl: "${COHERE_API_URL}", rerankerType: "cohere"

when starting Librechat, I see no errors, however, when I use web search, I see the following message in my logs:

error: Error using Cohere reranker: Request failed with status code 401

which seems to suggest that it's using the wrong endpoint.

Am I overloooking something here ?

@busla
Copy link
Copy Markdown
Contributor Author

busla commented Dec 30, 2025

Hi,

Thanks for looking into this and proposing an easy change to use custom Cohere endpoints for the reranker. As we'd also like to use this, I've tested the solution, but until now, haven't been able to get it working. What I did is the following:

1. modify the changed files (also cloned the fork including the changes)

2. rebuild using npm ci; npm run frontend; npm run backend

3. updated my .env file: add COHERE_API_KEY=xyz and COHERE_API_URL=https://xyz.ai.azure.com

4. updated my librechat.yaml with cohereApiKey: "${COHERE_API_KEY}", cohereApiUrl: "${COHERE_API_URL}",  rerankerType: "cohere"

when starting Librechat, I see no errors, however, when I use web search, I see the following message in my logs:

error: Error using Cohere reranker: Request failed with status code 401

which seems to suggest that it's using the wrong endpoint.

Am I overloooking something here ?

Can you verify with a simple script that your api is correct when you call your custom endpoint?

@gloorung
Copy link
Copy Markdown

gloorung commented Jan 5, 2026

The custom endpoint works as expected. With the help of mitmdump, I discovered it's not using the correct endpoint, but sending a request to api.cohere.com/v2/rank . That on it's turn is causing the 401 error.

The only reference to this endpoint that I could find is part of the agents package, which Librechat depends on (https://github.com/danny-avila/agents/tree/main/src/tools/search/rerankers.ts)

@busla
Copy link
Copy Markdown
Contributor Author

busla commented Jan 6, 2026

The custom endpoint works as expected. With the help of mitmdump, I discovered it's not using the correct endpoint, but sending a request to api.cohere.com/v2/rank . That on it's turn is causing the 401 error.

The only reference to this endpoint that I could find is part of the agents package, which Librechat depends on (https://github.com/danny-avila/agents/tree/main/src/tools/search/rerankers.ts)

Just so I understand you correctly, did you set the COHERE_API_URL with your custom hostname and LibreChat uses it correctly?

@gloorung
Copy link
Copy Markdown

gloorung commented Jan 8, 2026

No, that wasn't indeed very clear. What I meant to say is, that when I test the custom endpoint in a simple script as you suggested, I see endpoint calls respond as expected.

In Librechat though, even when I specify the custom endpoint using the COHERE_API_URL and COHERE_API_KEY (after making the changes to packages/data-provider/src/config.ts), Librechat is still calling the api.cohere.com/v2/rank endpoint when I use the web search feature. I assumed the agents package that still uses this endpoint was the reason for that. Could you perhaps share which version of the agents package you're using ?

@busla
Copy link
Copy Markdown
Contributor Author

busla commented Jan 8, 2026

Thanks for the clarification.

Let's me take a look at this again.

busla added 4 commits January 13, 2026 06:15
…ank endpoints

- Add cohereBaseUrl to webSearchSchema configuration
- Update loadWebSearchConfig to load cohereBaseUrl from environment
- Add cohereBaseUrl to TWebSearchKeys type definition
- Configure cohereBaseUrl as optional parameter for cohere reranker
- Add COHERE_BASE_URL documentation to .env.example

This enables configuration of custom Cohere endpoints (such as LiteLLM proxy)
for web search reranking. The configuration infrastructure is ready for when
the @librechat/agents package supports custom base URLs.

Usage:
COHERE_BASE_URL=http://litellm:8000/v1
COHERE_API_KEY=your_api_key
Replace enum with dynamic getter to enable runtime environment variable lookup.
CohereConstants.API_URL now automatically uses COHERE_BASE_URL if set,
otherwise defaults to https://api.cohere.ai/v1.

This enables using custom Cohere endpoints (like LiteLLM proxy) for web search
reranking by simply setting the COHERE_BASE_URL environment variable.

Usage:
export COHERE_BASE_URL=http://litellm:8000/v1
export COHERE_API_KEY=your_api_key

The change is backwards compatible and requires no configuration changes.
@busla busla force-pushed the feature/cohere-base-url branch from 4a71871 to 868df81 Compare January 13, 2026 06:17
busla and others added 2 commits January 13, 2026 06:59
Update web.spec.ts to include cohereApiUrl in:
- Expected default config object
- URL default placeholders test
- Custom URLs preservation test

Co-Authored-By: Claude <noreply@anthropic.com>
@busla
Copy link
Copy Markdown
Contributor Author

busla commented Jan 13, 2026

Added a PR to the agents repo for this to take effect

@busla
Copy link
Copy Markdown
Contributor Author

busla commented Feb 5, 2026

Any news on when this could be merged @danny-avila ?

@joelhirzel
Copy link
Copy Markdown
Contributor

Would also love to see this merged

@UnicronBE
Copy link
Copy Markdown
Contributor

just want to say i've been running a custom image with this PR for the past month or two, works perfectly. so adding to the official build would be very welcome!

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.

7 participants