feat: add native OpenRouter provider#4208
feat: add native OpenRouter provider#4208subtleGradient wants to merge 6 commits intocrewAIInc:mainfrom
Conversation
- Add OpenRouterCompletion class extending OpenAICompletion - Use OpenRouter API endpoint (https://openrouter.ai/api/v1) - Support OPENROUTER_API_KEY environment variable - Support OpenRouter attribution headers (HTTP-Referer, X-Title) - Register provider in SUPPORTED_NATIVE_PROVIDERS
- Add 24 unit tests covering initialization, client params, context window, header injection, and provider integration - Fix bug: pass provider='openrouter' to parent constructor - Improve error message with link to openrouter.ai/keys - Add usage examples to class docstring - Document context window default rationale Tests cover: - API key handling (explicit + env var fallback) - Site URL/name for attribution - Base URL override behavior - Header injection logic - Error handling for missing API key
- Remove unused MagicMock import from tests - Add Returns section to _get_client_params docstring - Add Returns section to get_context_window_size docstring - Add Raises section to _get_client_params docstring
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
There was a problem hiding this comment.
Pull request overview
This pull request adds OpenRouter as a first-class native provider in CrewAI, enabling direct access to 300+ models through a unified OpenAI-compatible API. The implementation extends the existing OpenAICompletion class and adds proper support for OpenRouter-specific features including API key management and attribution headers.
Key changes:
- Introduces
OpenRouterCompletionclass that extendsOpenAICompletionwith OpenRouter-specific configuration - Registers "openrouter" in the supported native providers list and adds routing logic in the LLM factory
- Adds comprehensive test coverage for initialization, API key handling, header injection, and context window size
Reviewed changes
Copilot reviewed 4 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| lib/crewai/src/crewai/llm.py | Registers "openrouter" in SUPPORTED_NATIVE_PROVIDERS and adds provider mapping and routing logic |
| lib/crewai/src/crewai/llms/providers/openrouter/completion.py | Implements OpenRouterCompletion class with OpenRouter-specific API endpoint, environment variables, and attribution headers |
| lib/crewai/src/crewai/llms/providers/openrouter/init.py | Empty module initialization file for the openrouter provider package |
| lib/crewai/tests/llms/providers/openrouter/test_completion.py | Comprehensive test suite covering initialization, API key handling, site attribution, header injection, and integration scenarios |
| lib/crewai/tests/llms/providers/openrouter/init.py | Empty test module initialization file |
| lib/crewai/tests/llms/providers/init.py | Empty test module initialization file |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
When LLM class passes provider='openrouter' explicitly, it was being duplicated since OpenRouterCompletion also hardcodes provider='openrouter' in super().__init__(). Now we pop 'provider' from kwargs before calling super.
There was a problem hiding this comment.
This PR is being reviewed by Cursor Bugbot
Details
Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.
To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.
- Add openrouter case to _validate_model_in_constants (returns True for all models since OpenRouter is a proxy) - Fix context window docstring to accurately note that 128K default may exceed some models' actual limits
- Store resolved OpenRouter API key before calling super().__init__ - Override self.api_key after super to ensure OPENAI_API_KEY is never used - Add test to verify OPENAI_API_KEY is never used even when set Fixes cursor[bot] feedback about wrong API key being used when OPENAI_API_KEY is set but OPENROUTER_API_KEY is not.
|
This PR is stale because it has been open for 45 days with no activity. |
Summary
Adds OpenRouter as a first-class native provider in CrewAI, enabling direct access to 300+ models through a unified API.
Changes
"openrouter"inSUPPORTED_NATIVE_PROVIDERSLLMclassOpenRouterCompletionclass extendingOpenAICompletionUsage
Verified Working Example
This exact script was tested and produces the output shown below:
Actual Output (verified):
Why
While OpenRouter works today via LiteLLM's
openrouter/prefix, a native provider offers:OPENROUTER_API_KEYenvironment variable support (notOPENAI_API_KEY)HTTP-Referer,X-Title) for attributionImplementation Details
OpenRouterCompletionextendsOpenAICompletion(since OpenRouter is OpenAI-compatible) and:base_urltohttps://openrouter.ai/api/v1OPENROUTER_API_KEYinstead ofOPENAI_API_KEYTruein model validation (like Azure) since OpenRouter proxies 300+ modelsTesting
anthropic/claude-4.5-sonnetmodel