This guide helps you migrate your workflows from Claude Code CLI to OpenRouter, giving you access to 400+ AI models from multiple providers.
Benefits of Migration:
- 🌐 Access to 400+ models from providers like OpenAI, Anthropic, Google, Meta, Mistral
- 💰 Cost optimization with automatic model routing and fallbacks
- 🔑 Single API key instead of managing multiple provider keys
- 🚀 Better performance with direct API calls vs CLI subprocess overhead
- 🎯 Model flexibility - switch models without changing your code
pip install openai>=1.0.0 # OpenRouter uses OpenAI SDK- Sign up at openrouter.ai
- Get your API key from the dashboard
- Set environment variable:
export OPENROUTER_API_KEY="your-api-key-here"# Research workflow
python workflows/research/research_openrouter.py "your research topic"
# Code review workflow
python workflows/code_review/review_openrouter.py 123 --repository owner/repo
# JIRA task workflow
python workflows/jira_task/enhanced_workflow_openrouter.py PROJ-123 "implement feature"Use the OpenRouterClient class to replace subprocess.run(["claude", ...]) calls:
from openrouter_client import OpenRouterClient, WorkflowType
# Replace Claude Code CLI calls
client = OpenRouterClient(workflow_type=WorkflowType.RESEARCH)
result = client.execute_prompt("Your prompt here")- Primary:
perplexity/llama-3.1-sonar-large-128k-online($0.001/1K tokens)- Excellent for research with web access
- Alternative:
anthropic/claude-3-haiku($0.00025/1K tokens)- Fast and cost-effective
- Primary:
anthropic/claude-3.5-sonnet($0.003/1K tokens)- Best for code analysis and complex reasoning
- Alternative:
deepseek/deepseek-coder($0.00014/1K tokens)- Specialized for code review tasks
- Primary:
anthropic/claude-3.5-sonnet($0.003/1K tokens)- Excellent for development tasks
- Alternative:
openai/gpt-4-turbo($0.01/1K tokens)- Great for comprehensive analysis
import subprocess
command = ["claude", "-p", "--verbose", "--model", "sonnet", "--allowedTools", "bash,webSearch"]
result = subprocess.run(command, input=prompt, capture_output=True, text=True, timeout=1800)
response = result.stdout.strip()from openrouter_client import OpenRouterClient, WorkflowType
client = OpenRouterClient(workflow_type=WorkflowType.RESEARCH)
result = client.execute_prompt(prompt, timeout=1800)
response = result["content"]Old Command:
python workflows/research/research.py "AI trends 2024"New Command:
python workflows/research/research_openrouter.py "AI trends 2024"Key Differences:
- ✅ Direct API calls (faster)
- ✅ Better error handling
- ✅ Cost estimation
- ✅ Multiple model options
- ✅ Usage tracking
Old Command:
python workflows/code_review/review.py 123 --repository owner/repoNew Command:
python workflows/code_review/review_openrouter.py 123 --repository owner/repoEnhanced Features:
--list-models: See recommended models--estimate-cost: Estimate review cost before running--model: Specify exact model to use
Old Command:
python workflows/jira_task/enhanced_workflow.pyNew Command:
python workflows/jira_task/enhanced_workflow_openrouter.py PROJ-123 "implement user authentication"Enhanced Features:
- Context preservation across interactions
- Dynamic model switching
- Better error recovery
- Cost tracking
# Required
export OPENROUTER_API_KEY="your-api-key"
# Optional (for OpenRouter leaderboards)
export OPENROUTER_SITE_URL="https://github.com/yourusername/yourproject"
export OPENROUTER_SITE_NAME="Your Project Name"# List available models for a workflow type
python -c "
from openrouter_client import OpenRouterModels, WorkflowType
models = OpenRouterModels.get_recommended_models(WorkflowType.RESEARCH)
for model in models:
print(f'{model.name}: {model.description}')
"| Provider | Model | Cost per 1K tokens | Best for |
|---|---|---|---|
| Claude Code | claude-3.5-sonnet | $0.003 | All workflows |
| OpenRouter | anthropic/claude-3.5-sonnet | $0.003 | Same model, more flexibility |
| OpenRouter | anthropic/claude-3-haiku | $0.00025 | Cost-effective tasks |
| OpenRouter | deepseek/deepseek-coder | $0.00014 | Code-focused tasks |
| OpenRouter | perplexity/sonar-large | $0.001 | Research with web access |
export OPENROUTER_API_KEY="your-key-here"
# Or pass directly:
python script.py --api-key "your-key-here"# List available models
python script.py --list-modelspip install openai>=1.0.0- Check model availability: Use
--list-modelsflag - Estimate costs: Use
--estimate-costflag - Enable verbose logging: Use
--verboseflag - Test with simple prompts: Start with basic requests
from openrouter_client import OpenRouterClient
client = OpenRouterClient()
# Use specific model
result = client.execute_prompt(
prompt="your prompt",
model="anthropic/claude-3.5-sonnet",
max_tokens=4096,
temperature=0.7
)cost_info = client.estimate_cost("your prompt", model="anthropic/claude-3.5-sonnet")
print(f"Estimated cost: ${cost_info['estimated_cost']:.4f}")# Use different models for different tasks
research_client = OpenRouterClient(workflow_type=WorkflowType.RESEARCH)
code_client = OpenRouterClient(workflow_type=WorkflowType.CODE_REVIEW)
# Each optimized for their respective tasks
research_result = research_client.execute_prompt("research prompt")
code_result = code_client.execute_prompt("code review prompt")- Start with one workflow: Migrate your most-used workflow first
- Test with small tasks: Verify functionality before large projects
- Monitor costs: Use the cost estimation features
- Experiment with models: Try different models for different use cases
- Optimize for your needs: Adjust models based on quality vs cost preferences
- OpenRouter Documentation: openrouter.ai/docs
- Model Information: openrouter.ai/models
- GitHub Issues: Report problems in your project repository
Ready to migrate? Start with the research workflow using:
python workflows/research/research_openrouter.py "test migration" --list-models