Delegate code generation tasks from Claude Code to local models (LM Studio/Ollama) while maintaining quality through iterative feedback. Save 60-90% on API costs.
- π― Intelligent Delegation - Claude Code automatically delegates tasks to local models
- π Feedback Loop - Iterative review and improvement (max 3 iterations)
- π Real-Time Statistics - Track costs and savings on every delegation
- πΎ Persistent Tracking - Statistics saved across sessions
- βοΈ Configurable Pricing - Accurate cost calculations for any model
- π Global or Per-Project - Works everywhere or specific projects
User Request β Claude Code (Sonnet) β Delegates Simple/Medium Tasks
β
MCP Server
β
Local Model (Free)
β
Review & Iterate
β
Final Output β
Result: Claude handles architecture and complex decisions. Local model handles code generation. You save money.
# Install MCP SDK
pip install mcp
# Start a local model backend (choose one):
# Option A: LM Studio (recommended)
# 1. Download from lmstudio.ai
# 2. Load Qwen 2.5 Coder 7B
# 3. Start server (http://localhost:1234)
# Option B: Ollama
curl -fsSL https://ollama.com/install.sh | sh
ollama pull deepseek-coder:6.7b
ollama serve# Global (all projects)
python configure/configure_mcp_global.py
# Or project-specific
python configure/configure_mcp.pyThis adds the MCP server to ~/.claude.json.
# Copy template to your project
cp CLAUDE-template.md CLAUDE.md
# Or create global default
mkdir -p ~/.claude
cp CLAUDE-template.md ~/.claude/CLAUDE.mdImportant:
- MCP config = Makes tools available
- CLAUDE.md = Tells Claude when to use them
Both are needed for automatic delegation.
Close and reopen VS Code/Claude Code completely.
Best for most coding tasks. Local model generates β Claude reviews β iterates until approved.
{
"task_id": "add-auth-001",
"task_type": "backend",
"prompt": "Create user authentication endpoints",
"quality_criteria": ["Include error handling", "Add type hints"]
}For simple tasks that don't need review (boilerplate, formatting, docs).
{
"prompt": "Generate a Python dataclass for User with name, email, age"
}Claude uses this to provide specific feedback for iteration.
View improvement across iterations with statistics.
Get cumulative statistics across all delegation tasks.
{
"total_tasks": 47,
"cost_analysis": {
"total_savings_usd": 3.71,
"savings_percent": 86.9
}
}Backend:
export BACKEND_TYPE=lmstudio # or ollama, openai-compatible
export BACKEND_URL=http://localhost:1234/v1Pricing (for accurate cost tracking):
export REMOTE_INPUT_COST_PER_1K=0.003 # Claude Sonnet input
export REMOTE_OUTPUT_COST_PER_1K=0.015 # Claude Sonnet output
export LOCAL_COST_PER_1K=0.0 # Local models are freeEvery delegation returns comprehensive statistics:
{
"statistics": {
"local_model_usage": {
"tokens": {"sent": 1500, "received": 800},
"time_seconds": 2.34
},
"cost_analysis": {
"local_model_cost_usd": 0.00,
"estimated_cost_if_fully_remote_usd": 0.0165,
"actual_cost_usd": 0.0024,
"savings_usd": 0.0141,
"savings_percent": 85.5
}
}
}Statistics are automatically saved to .mcp_stats.json and persist across sessions.
Claude Code should delegate:
β To Local Model:
- Boilerplate and simple code generation
- Refactoring and formatting
- Test generation
- API endpoints and CRUD operations
- Documentation
π― Handle Yourself (Remote):
- Architecture and design decisions
- Security-critical code
- Complex algorithms
- Novel/ambiguous requirements
- Final integration and review
Target: 60-80% of tasks delegated for 60-90% cost savings.
- Use Feedback Loops - Better quality than quick delegation
- Be Specific in Feedback - "Add email validation" beats "improve validation"
- Check Statistics - Use
get_statistics_summaryto track savings - Iterate Up to 3 Times - If not fixed, handle it yourself
- Customize CLAUDE.md - Tailor delegation rules per project
"Connection error"
- Ensure LM Studio/Ollama is running
- Check model is loaded
- Verify server URL:
curl http://localhost:1234/v1/models
"Tools not available"
- Run config script:
python configure/configure_mcp_global.py - Restart Claude Code completely
- Check
~/.claude.jsoncontains "local-agents"
"Not delegating automatically"
- Create CLAUDE.md from template
- Restart Claude Code after adding CLAUDE.md
mcp-local-agent/
βββ local_agent_mcp_server.py # Main MCP server
βββ configure/
β βββ configure_mcp_global.py # Global setup
β βββ configure_mcp.py # Project setup
βββ CLAUDE-template.md # Template for project guidelines
βββ README.md # This file
βββ .mcp_stats.json # Statistics (auto-generated)
{
"system_prompt": "You are a senior Python developer. Follow PEP 8 strictly. Always include comprehensive error handling and type hints."
}- Global:
configure_mcp_global.pyβ Works everywhere - Project:
configure_mcp.pyβ Only current project - Both supported: Can override global with project settings
LM Studio:
- Qwen 2.5 Coder 7B (best balance)
- DeepSeek Coder V2 16B (highest quality)
- CodeLlama 13B (alternative)
Ollama:
deepseek-coder:6.7bcodellama:13b
CLAUDE-template.md- Project guidelines templatedocs/DELEGATION_STRATEGY.md- Decision frameworktest_hybrid_workflow.py- Example workflows
- Issues: GitHub Issues
- MCP Docs: MCP Documentation
Status: π’ Production Ready | Cost Savings: 60-90% | Quality: Maintained