##Supported AI Models
OpenCode supports multiple AI providers and models, giving you flexibility to choose the best model for your coding tasks. Each model has unique strengths for different use cases.
###Anthropic Claude Models
Claude models excel at code understanding, complex reasoning, and following detailed instructions.
| Model | Best For | Context Window | |-------|----------|----------------| | claude-sonnet-4-20250514 | Daily coding tasks, balanced performance | 200K tokens | | claude-opus-4-20250514 | Complex architecture, deep analysis | 200K tokens | | claude-3-5-haiku | Quick tasks, high speed | 200K tokens |
Configure Claude:
# Set API key
export ANTHROPIC_API_KEY="sk-ant-..."
# Use in OpenCode
opencode --model claude-sonnet-4-20250514
###OpenAI GPT Models
GPT models are widely used and offer strong general-purpose coding capabilities.
| Model | Best For | Context Window | |-------|----------|----------------| | gpt-4o | Complex reasoning, multi-step tasks | 128K tokens | | gpt-4o-mini | Fast responses, simple tasks | 128K tokens | | o1 | Advanced reasoning, planning | 128K tokens |
Configure GPT:
# Set API key
export OPENAI_API_KEY="sk-..."
# Use in OpenCode
opencode --model gpt-4o
###Google Gemini Models
Gemini models offer excellent multimodal capabilities and large context windows.
| Model | Best For | Context Window | |-------|----------|----------------| | gemini-2.0-flash | Fast, efficient coding | 1M tokens | | gemini-1.5-pro | Large codebase analysis | 2M tokens |
Configure Gemini:
# Set API key
export GOOGLE_API_KEY="AIza..."
# Use in OpenCode
opencode --model gemini-2.0-flash
##Choosing the Right Model
###For Daily Development
Recommended: Claude Sonnet or GPT-4o-mini
These models offer the best balance of speed, cost, and capability for everyday coding tasks like:
- Writing new functions
- Debugging code
- Code reviews
- Documentation
###For Complex Projects
Recommended: Claude Opus or GPT-4o
Use these powerful models when working on:
- System architecture design
- Large-scale refactoring
- Complex algorithm implementation
- Security audits
###For Large Codebases
Recommended: Gemini 1.5 Pro
With its massive 2M token context window, Gemini excels at:
- Analyzing entire repositories
- Understanding complex dependencies
- Cross-file refactoring
##Configuration File
Set your default model in ~/.config/opencode/config.yaml:
# Default model configuration
model: claude-sonnet-4-20250514
# Provider-specific settings
providers:
anthropic:
api_key: ${ANTHROPIC_API_KEY}
default_model: claude-sonnet-4-20250514
openai:
api_key: ${OPENAI_API_KEY}
default_model: gpt-4o
google:
api_key: ${GOOGLE_API_KEY}
default_model: gemini-2.0-flash
# Model aliases for quick switching
aliases:
fast: claude-3-5-haiku
smart: claude-opus-4-20250514
large: gemini-1.5-pro
##Switching Models
###Command Line
# Use specific model for a session
opencode --model gpt-4o
# Use alias
opencode --model fast
###During Session
Switch models without restarting:
/model claude-opus-4-20250514
Or use the model picker:
/model
##Local Models
OpenCode also supports local models through Ollama:
# Install Ollama
curl -fsSL https://ollama.com/install.sh | sh
# Pull a model
ollama pull codellama:13b
# Configure in OpenCode
opencode --model ollama:codellama:13b
###Recommended Local Models
| Model | Size | Best For | |-------|------|----------| | codellama:13b | 7GB | Code completion, simple tasks | | deepseek-coder:33b | 19GB | Complex coding, analysis | | qwen2.5-coder:32b | 18GB | Multi-language support |
##Performance Tips
- Match model to task: Use faster models for simple tasks, powerful models for complex ones
- Use aliases: Set up shortcuts for frequently used models
- Monitor token usage: Larger contexts cost more with cloud models
- Consider latency: Local models eliminate network latency but require GPU resources
##Cost Optimization
| Strategy | Description | |----------|-------------| | Start small | Begin with cheaper models, escalate when needed | | Use caching | Enable response caching to reduce API calls | | Batch requests | Group related questions to maximize context usage | | Local fallback | Use local models for simple tasks |
##Troubleshooting
###Model Not Available
Error: Model 'claude-opus-4' not found
Solution: Check the exact model name in provider documentation.
###Rate Limiting
Error: Rate limit exceeded
Solution: Configure rate limiting in config:
rate_limits:
anthropic:
requests_per_minute: 50
tokens_per_minute: 100000
###Context Length Exceeded
Error: Context length exceeded
Solution: Use a model with larger context window or enable automatic context management:
context:
auto_truncate: true
max_tokens: 100000