Automatic AI model selection for OpenCode based on task complexity and context.
Save 60-70% on API costs by intelligently routing tasks to the most appropriate model. Uses cheap models for simple work, premium models for complex tasks.
- 🎯 Agent-Activated: Only runs with specific agents, no interference with normal workflow
- 🤖 Context-Aware: Analyzes task complexity, detects plans, tracks context size
- 💰 Cost-Optimized: Route simple tasks to free/cheap models (GLM 4.6)
- ⚡ Performance Mode: Use premium models for quality-critical work
- 📊 Multi-Dimensional: Strategy × Task Type × Complexity selection
- 🔄 Fallback Support: Per-level fallback chains for reliability
- 🎨 Highly Configurable: Customize every aspect of model selection
# Create directories
mkdir -p ~/.config/opencode/plugin
mkdir -p ~/.config/opencode/agent
# Copy plugin and agents
cp orchestrator.plugin.ts ~/.config/opencode/plugin/
cp agents/*.md ~/.config/opencode/agent/
# Copy configuration
cp orchestrator.config.md ~/.config/opencode/
# Install dependencies
npm install yamlEdit ~/.config/opencode/orchestrator.config.md:
strategies:
cost-optimized:
coding-simple:
simple: your-free-model # e.g., glm-4.6
medium: your-free-model
complex:
- your-premium-model # e.g., claude-sonnet
- your-free-model # Fallbackopencode
# Press Tab to switch agents
# Select: auto-optimized (cost mode) or auto-performance (quality mode)The orchestrator only runs with specific agents to prevent interference:
auto-optimized: Cost-efficient mode (uses cheap models when possible)auto-performance: Quality mode (uses premium models for best results)- Other agents (build, plan): Orchestrator stays inactive
Selects models using Strategy × Task Type × Complexity:
Model = strategies[agent_strategy][task_type][complexity]
7 Task Types:
coding-simple: Implementation with existing plancoding-complex: Architectural/critical workplanning: Design and strategydebugging: Error analysisreview: Code reviewdocumentation: Docs and commentsgeneral: Questions and exploration
4 Complexity Levels:
simple: Quick fixes, questionsmedium: Standard featurescomplex: System changes, migrationsadvanced: Full rewrites, architecture
Automatically adjusts complexity based on context:
✓ Detailed plan exists in context
→ Reduce complexity by 1 level
→ medium → simple → use cheaper model✓ Prompt contains "implement step", "from plan"
→ Reduce complexity (implementation is simpler with plan)< 50K tokens → Reduce complexity (focused task)
50-100K → Normal (no adjustment)
> 100K tokens → Raise complexity (multi-faceted task)Phase 1: Planning (auto-performance agent)
Prompt: "Plan how to migrate invoice admin to unified system"
Analysis:
Task: planning
Complexity: complex → medium (context < 50K)
Model: claude-sonnet-4-5 (performance mode)
Result:
- Detailed 4-step plan created
- Context grows to 50K tokens
Phase 2: Implementation (auto-optimized agent)
Prompt: "Implement step 1 from the plan"
Analysis:
Task: coding-simple ("step 1", "from plan")
Base Complexity: medium
Context Adjustments:
✓ Plan detected (4 steps): medium → simple
✓ Subtask detected: confirmed simple
✓ Context 50K: normal (no change)
Final: simple
Model: glm-4.6 (FREE)
Result:
- Successful implementation
- 0 errors
- $0 cost
Savings: 60-70% compared to using premium models for everything
Configure models for each strategy, task type, and complexity:
strategies:
cost-optimized:
coding-simple:
simple: glm-4.6 # Free
medium: glm-4.6 # Free
complex: # Fallback array
- claude-sonnet-4-5
- glm-4.6
advanced: # Multi-tier fallback
- gpt-5-codex
- claude-sonnet-4-5
- glm-4.6
performance-optimized:
coding-simple:
simple: claude-haiku # Fast
medium: claude-haiku
complex:
- claude-sonnet-4-5
- claude-haiku
advanced:
- claude-sonnet-4-5detection:
contextAware:
planAwareness:
enabled: true
planIndicators: ["step 1", "- [ ]", "1."]
minStepsForReduction: 3
subtaskDetection:
enabled: true
subtaskIndicators: ["implement step", "from plan"]
contextSize:
enabled: true
smallContextThreshold: 50000 # <50K: reduce
largeContextThreshold: 100000 # >100K: raiseagentStrategies:
auto-optimized: cost-optimized
auto-performance: performance-optimizedUse auto-optimized agent:
✓ Simple questions → GLM (free)
✓ Medium coding with plan → GLM (free)
✓ Complex features → Sonnet ($$$) → GLM fallback
✓ Documentation → GLM (free)
Estimated savings: 60-70%
Use auto-performance agent:
✓ Simple tasks → Haiku (fast)
✓ Medium coding → Sonnet (quality)
✓ Complex features → Sonnet (premium)
✓ Planning → o1 (best reasoning)
Focus: Speed + Quality over cost
# Start with planning (quality mode)
Tab → auto-performance
> "Design the authentication system"
→ Uses o1 or Sonnet for planning
# Switch to implementation (cost mode)
Tab → auto-optimized
> "Implement step 1 from the plan"
→ Uses GLM (free) because plan exists
# Switch back for manual control
Tab → build
→ Orchestrator inactive, pick model manuallyAlways use specific models for critical files:
filePatternOverrides:
- pattern: "**/security/**"
model: claude-sonnet-4-5
reason: "Security code is critical"
- pattern: "**/*.sql"
model: claude-sonnet-4-5
reason: "Database queries require attention"
- pattern: "**/*.test.*"
taskTypeOverride: coding-simple
reason: "Tests can use cheaper models"Check what model would be selected:
// In OpenCode
{
"tool": "checkComplexity",
"args": {
"prompt": "Implement user authentication",
"strategy": "cost-optimized"
}
}
// Returns:
{
"taskType": "coding-simple",
"baseComplexity": "medium",
"finalComplexity": "simple",
"primaryModel": "glm-4.6",
"contextAdjustments": ["Plan detected: medium → simple"]
}Goal: Minimize costs while maintaining quality
Best for:
- Exploration and prototyping
- Implementation with existing plans
- Documentation
- Simple features
Model preferences:
- Free/cheap models for simple/medium tasks
- Premium models only for complex/advanced
Goal: Maximize quality and speed
Best for:
- Production code
- Critical features
- Complex planning
- Quality-sensitive work
Model preferences:
- Fast models (Haiku) for simple tasks
- Premium models (Sonnet, o1) for quality
./install.shThe installer will:
- Ask for global or project installation
- Copy plugin and agent files
- Install dependencies
# Global installation
mkdir -p ~/.config/opencode/plugin ~/.config/opencode/agent
cp orchestrator.plugin.ts ~/.config/opencode/plugin/
cp agents/*.md ~/.config/opencode/agent/
cp orchestrator.config.md ~/.config/opencode/
# Install dependencies
npm install yaml
# Customize configuration
nano ~/.config/opencode/orchestrator.config.mdmkdir -p .opencode/plugin .opencode/agent
cp orchestrator.plugin.ts .opencode/plugin/
cp agents/*.md .opencode/agent/
cp orchestrator.config.md .opencode/The orchestrator provides transparent logging:
[Orchestrator V2.1] Task Analysis:
Strategy: cost-optimized
Task Type: coding-simple
Base Complexity: medium
Context Adjustments:
- Plan detected: medium → simple
- Subtask detected: confirmed simple
- Normal context (50K tokens), no adjustment
Final Complexity: simple
Model: glm-4.6
Fallbacks: (none)
Reasoning:
- Task type: coding-simple
- Base complexity: medium
- Detailed plan exists in context
- Implementing subtask from plan
- Selected from cost-optimized.coding-simple.simple
Check you're using an orchestrator agent:
# In OpenCode, press Tab
# You should see: auto-optimized, auto-performance
# If not, verify agent files exist:
ls ~/.config/opencode/agent/auto-*.mdEnable verbose logging:
logLevel: verboseReview the analysis output to see reasoning.
Lower the threshold:
planAwareness:
minStepsForReduction: 2 # Was 3Or add custom indicators:
planAwareness:
planIndicators:
- "step 1"
- "task 1"
- "your custom indicator"- OpenCode
- Node.js or Bun
yamlpackage (npm install yaml)- At least one AI provider configured
MIT License - see LICENSE
Contributions welcome! Areas for improvement:
- Additional task type detection patterns
- Model performance benchmarking
- Cost tracking integration
- Custom scoring algorithms
- UI for configuration management
See CHANGELOG.md for version history.
- Issues: GitHub Issues
- Documentation: See
orchestrator.config.mdfor full configuration reference
Save money. Maintain quality. Let the orchestrator choose.