This document details the cost-effective Chat Model architecture replacing Claude Sonnet 4.6 with Qwen 3.5 Plus, routed through OpenRouter and a local LiteLLM server for optimal performance and cost savings.
Architecture Flow:
Agent Zero → LiteLLM Server (local) → OpenRouter API → Qwen 3.5 Plus
| Model | Input Cost (/1M) | Output Cost (/1M) | Savings |
|---|---|---|---|
| Claude Sonnet 4.6 | ~$3.00 | ~$15.00 | baseline |
| Qwen 3.5 Plus | ~$0.30 | ~$0.60 | ~90% reduction |
Qwen 3.5 Plus strengths:
- ✅ Code generation and refactoring (near-parity with Sonnet)
- ✅ Tool calling and function execution (excellent)
- ✅ Long context understanding (256K tokens)
- ✅ Multi-step reasoning (strong)
⚠️ Creative writing (good, but different style)⚠️ Nuanced instruction following (95% of Sonnet)
OpenRouter provides unified access to 100+ LLM providers with:
- Single API interface
- Automatic fallbacks
- Usage tracking and cost optimization
- No vendor lock-in
Base URL: https://openrouter.ai/api/v1
LiteLLM acts as a local OpenAI-compatible proxy:
- Standardizes API format for Agent Zero
- Handles authentication and rate limiting
- Provides caching and retry logic
- Enables model switching without code changes
Local Endpoint: http://localhost:4000
Agent Zero connects to LiteLLM using standard OpenAI SDK:
- No framework modifications required
- Seamless model swapping
- Consistent tool calling interface
model_list:
- model_name: chat-model
litellm_params:
model: openrouter/qwen/qwen-3.5-plus-02-15
api_key: os.environ/OPENROUTER_API_KEY
api_base: https://openrouter.ai/api/v1
timeout: 120
max_retries: 3
- model_name: chat-model-fallback
litellm_params:
model: openrouter/anthropic/claude-3-5-sonnet
api_key: os.environ/OPENROUTER_API_KEY
api_base: https://openrouter.ai/api/v1
timeout: 120
litellm_settings:
drop_params: true
set_verbose: false
cache: true
cache_params:
type: redis
host: localhost
port: 6379# LiteLLM Server Configuration
LITELLM_SERVER_URL=http://localhost:4000
LITELLM_API_KEY=sk-1234 # Can be any value for local
# OpenRouter Configuration
OPENROUTER_API_KEY=sk-or-v1-xxxxxxxxxxxxx
OPENROUTER_BASE_URL=https://openrouter.ai/api/v1
# Model Configuration
CHAT_MODEL=chat-model
CHAT_MODEL_TEMPERATURE=0.7
CHAT_MODEL_MAX_TOKENS=4096
CHAT_MODEL_CONTEXT_WINDOW=256000{
"chat": {
"provider": "openai-compatible",
"model": "chat-model",
"base_url": "http://localhost:4000/v1",
"api_key": "sk-1234",
"temperature": 0.7,
"max_tokens": 4096,
"context_window": 256000,
"top_p": 0.9,
"frequency_penalty": 0.0,
"presence_penalty": 0.0,
"stop_sequences": [],
"tool_choice": "auto",
"parallel_tool_calls": true
}
}{
"temperature": 0.3,
"top_p": 0.9,
"max_tokens": 8192,
"frequency_penalty": 0.0,
"presence_penalty": 0.0
}Rationale:
- Lower temperature (0.3) for deterministic code output
- High max_tokens for complex file operations
- No penalty settings to allow necessary repetition in code
{
"temperature": 0.7,
"top_p": 0.95,
"max_tokens": 4096,
"frequency_penalty": 0.1,
"presence_penalty": 0.1
}Rationale:
- Moderate temperature for balanced creativity/coherence
- Slight penalties to reduce repetition
- Standard context window for most conversations
{
"temperature": 0.1,
"top_p": 0.5,
"max_tokens": 2048,
"tool_choice": "auto",
"parallel_tool_calls": true
}Rationale:
- Very low temperature for precise tool selection
- Low top_p to focus on most likely tool calls
- Parallel execution for efficiency
pip install litellm[proxy]
pip install redis # Optional: for cachinglitellm --config config.yaml --port 4000curl http://localhost:4000/v1/models -H "Authorization: Bearer sk-1234"Update Agent Zero's model configuration to point to LiteLLM:
- Set
base_urltohttp://localhost:4000/v1 - Use any API key (LiteLLM handles actual auth)
- Set model name to match LiteLLM config (
chat-model)
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:4000/v1",
api_key="sk-1234"
)
response = client.chat.completions.create(
model="chat-model",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)Access at http://localhost:4000/ui:
- Real-time request tracking
- Cost analytics
- Error rates and latency
- Model usage breakdown
| Metric | Target | Alert Threshold |
|---|---|---|
| Latency (p95) | <2s | >5s |
| Error Rate | <1% | >5% |
| Cost/Day | <$5 | >$10 |
| Cache Hit Rate | >40% | <20% |
1. Connection Refused
# Check LiteLLM is running
ps aux | grep litellm
# Restart if needed
litellm --config config.yaml --port 4000 --detailed_debug2. Model Not Found
# Verify model name in LiteLLM config
curl http://localhost:4000/v1/models -H "Authorization: Bearer sk-1234"3. Rate Limiting
# Add retry logic to LiteLLM config
litellm_settings:
num_retries: 3
request_timeout: 120
fallbacks: [{"chat-model": ["chat-model-fallback"]}]4. High Latency
# Enable caching
litellm_settings:
cache: true
cache_params:
type: redis
host: localhost
port: 6379| Usage Tier | Sonnet 4.6 | Qwen 3.5 Plus | Savings |
|---|---|---|---|
| Light (100K tokens/day) | ~$54/mo | ~$5/mo | $49 |
| Medium (500K tokens/day) | ~$270/mo | ~$27/mo | $243 |
| Heavy (2M tokens/day) | ~$1,080/mo | ~$108/mo | $972 |
Track usage at: https://openrouter.ai/activity
- Real-time token consumption
- Cost breakdown by model
- API call history
-
API Key Management
- Store OpenRouter key in environment variables
- Never commit keys to version control
- Rotate keys quarterly
-
Local Network Security
- Bind LiteLLM to localhost only (default)
- Use firewall rules to restrict access
- Enable authentication for production
-
Data Privacy
- LiteLLM doesn't log request content by default
- Disable verbose logging in production
- Review OpenRouter's data policy
-
Multi-Model Routing
- Route simple queries to cheaper models (Qwen 2.5)
- Escalate complex tasks to Qwen 3.5 Plus
- Automatic fallback to Sonnet if needed
-
Advanced Caching
- Semantic caching with embeddings
- Cross-session cache persistence
- Cache invalidation strategies
-
Load Balancing
- Multiple LiteLLM instances
- Round-robin model routing
- Health check monitoring