Overview
Concrete tool for multi-provider LLM access via OpenRouter's unified API, with prompt caching opt-in and native tool-calling support.
Description
The OpenRouterLLM class wraps OpenRouter's OpenAI-compatible endpoint, enabling access to models from Anthropic, Google, Meta, and other providers through a single API key. It inherits from BaseLLM and is registered via `@register_model` with OpenRouterConfig. Key features include prompt caching opt-in for providers that bill cache writes at a premium (Anthropic, Gemini, Qwen), and full native tool-calling support.
Usage
- Auto workflow generation: Use as the LLM for `WorkFlowGenerator` to power task planning and agent generation across providers.
- Sequential workflow build: Power sequential multi-agent workflows with a single API key across multiple model providers.
- Multi-model routing: Access models like `anthropic/claude-3.5-sonnet` or `google/gemini-2.0-flash` without provider-specific code.
Code Reference
Source Location
- Repository: EvoAgentX
- Config File: evoagentx/models/model_configs.py (L178-210)
- Model File: evoagentx/models/openrouter_model.py (L27-355)
Signature
class OpenRouterConfig(LLMConfig):
llm_type: str = "OpenRouterLLM"
model: str
openrouter_key: Optional[str] = None
openrouter_base: str = "https://openrouter.ai/api/v1"
openrouter_model_base: str = "https://openrouter.ai/api/v1/models"
temperature: Optional[float] = None
top_p: Optional[float] = None
top_k: Optional[int] = None
frequency_penalty: Optional[float] = None
presence_penalty: Optional[float] = None
repetition_penalty: Optional[float] = None
min_p: Optional[float] = None
top_a: Optional[float] = None
seed: Optional[int] = None
max_tokens: Optional[int] = None
stop: Optional[List[str]] = None
tools: Optional[List] = None
tool_choice: Optional[Union[str, dict]] = None
stream: Optional[bool] = None
enable_prompt_caching: Optional[bool] = False
class OpenRouterLLM(BaseLLM):
def init_model(self) -> None
def supports_native_tool_calling(self) -> bool
def prepare_request(self, messages: List[dict], params: dict) -> Tuple[List[dict], dict]
def single_generate(self, messages: List[dict], **kwargs) -> str
async def single_generate_async(self, messages: List[dict], **kwargs) -> str
Import
from evoagentx.models.openrouter_model import OpenRouterLLM
from evoagentx.models.model_configs import OpenRouterConfig
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| config |
OpenRouterConfig |
Yes |
Config with model id and OpenRouter API key
|
| messages |
List[dict] |
Yes (for generate) |
Chat messages in OpenAI format
|
| enable_prompt_caching |
bool |
No |
Per-call override for prompt caching
|
Outputs
| Name |
Type |
Description
|
| single_generate returns |
str |
LLM completion text
|
| cost |
Cost |
Tracked via cost_manager
|
Usage Examples
Workflow Generation with OpenRouter
from evoagentx.models.openrouter_model import OpenRouterLLM
from evoagentx.models.model_configs import OpenRouterConfig
from evoagentx.workflow.workflow_generator import WorkFlowGenerator
# 1. Configure OpenRouter with a Claude model
llm_config = OpenRouterConfig(
model="anthropic/claude-3.5-sonnet",
openrouter_key="sk-or-v1-...",
enable_prompt_caching=True,
)
llm = OpenRouterLLM(config=llm_config)
# 2. Generate a workflow from a natural language goal
generator = WorkFlowGenerator(llm=llm)
graph = generator.generate_workflow(
goal="Search arxiv for papers on LLM agents and write a summary report"
)
Related Pages
Implements Principle
Requires Environment
Uses Heuristic
Page Connections
Double-click a node to navigate. Hold to expand connections.