~/content/model-configuration-guide

Model Configuration Guide

Complete guide to configuring AI models in OpenCode - Claude, GPT-4, Gemini, and local models. Learn how to switch between models and optimize performance.

last_updated: "2025-01-19"

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.

ModelBest ForContext Window
claude-sonnet-4-20250514Daily coding tasks, balanced performance200K tokens
claude-opus-4-20250514Complex architecture, deep analysis200K tokens
claude-3-5-haikuQuick tasks, high speed200K tokens

Configure Claude:

bash
# 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.

ModelBest ForContext Window
gpt-4oComplex reasoning, multi-step tasks128K tokens
gpt-4o-miniFast responses, simple tasks128K tokens
o1Advanced reasoning, planning128K tokens

Configure GPT:

bash
# 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.

ModelBest ForContext Window
gemini-2.0-flashFast, efficient coding1M tokens
gemini-1.5-proLarge codebase analysis2M tokens

Configure Gemini:

bash
# 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:

bash
# 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

bash
# Use specific model for a session
opencode --model gpt-4o

# Use alias
opencode --model fast

During Session

Switch models without restarting:

bash
/model claude-opus-4-20250514

Or use the model picker:

bash
/model

Local Models

OpenCode also supports local models through Ollama:

bash
# 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
ModelSizeBest For
codellama:13b7GBCode completion, simple tasks
deepseek-coder:33b19GBComplex coding, analysis
qwen2.5-coder:32b18GBMulti-language support

Performance Tips

  1. Match model to task: Use faster models for simple tasks, powerful models for complex ones
  2. Use aliases: Set up shortcuts for frequently used models
  3. Monitor token usage: Larger contexts cost more with cloud models
  4. Consider latency: Local models eliminate network latency but require GPU resources

Cost Optimization

StrategyDescription
Start smallBegin with cheaper models, escalate when needed
Use cachingEnable response caching to reduce API calls
Batch requestsGroup related questions to maximize context usage
Local fallbackUse local models for simple tasks

Troubleshooting

Model Not Available

bash
Error: Model 'claude-opus-4' not found

Solution: Check the exact model name in provider documentation.

Rate Limiting

bash
Error: Rate limit exceeded

Solution: Configure rate limiting in config:

bash
rate_limits:
  anthropic:
    requests_per_minute: 50
    tokens_per_minute: 100000

Context Length Exceeded

bash
Error: Context length exceeded

Solution: Use a model with larger context window or enable automatic context management:

bash
context:
  auto_truncate: true
  max_tokens: 100000
Comments (Coming Soon)

Configure Giscus in environment variables to enable comments.