Fix hardcoded Opus defaults and improve provider flexibility#174
Open
inpassing wants to merge 21 commits intocloudflare:mainfrom
Open
Fix hardcoded Opus defaults and improve provider flexibility#174inpassing wants to merge 21 commits intocloudflare:mainfrom
inpassing wants to merge 21 commits intocloudflare:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request: Fix Hardcoded Opus Defaults and Improve Provider Flexibility
Problem
The current start-moltbot.sh script has several issues that force users into expensive configurations:
Issue 1: Hardcoded Opus Default
The script sets anthropic/claude-opus-4-5-20251101 as the fallback model when OPENROUTER_API_KEY is not detected:
javascriptif (!process.env.OPENROUTER_API_KEY) {
config.agents.defaults.model.primary = 'anthropic/claude-opus-4-5';
}
Impact: Users who set up OpenRouter configuration in R2 or use AI Gateway without the OpenRouter env var get forced into Opus ($15-75/1M tokens) instead of more cost-effective options like Sonnet ($3-15/1M tokens) or Kimi K2.5 via OpenRouter ($0.45-2.50/1M tokens).
Issue 2: Configuration Override on Every Startup
The script runs on EVERY container restart and overwrites whatever model is configured in R2, making it impossible to persist model choices across restarts.
Issue 3: No OpenRouter-First Logic
Even when OPENROUTER_API_KEY is set, the script doesn't default to cost-effective models like Kimi K2.5. Users must manually configure this or risk falling back to expensive providers.
Issue 4: Limited Provider Support
The script only checks for Anthropic and OpenRouter, but doesn't properly support other providers that moltbot/OpenClaw supports (like OpenAI, custom endpoints, etc.).
Solution
This PR updates start-moltbot.sh to:
Respect existing configuration: If a model is already configured in R2, don't override it
Smart provider detection: Check for API keys in priority order:
OpenRouter (most cost-effective) → Default to Kimi K2.5
AI Gateway or Direct Anthropic → Default to Sonnet 4.5 (NOT Opus)
Fallback safely to Sonnet if no keys detected
Make Opus opt-in: Only add Opus as an available model option, never as the default
Better logging: Show which provider is detected and which model is being used
Key Changes
Before
javascript// Old logic - always overrides to Opus if no OpenRouter key
if (!process.env.OPENROUTER_API_KEY) {
config.agents.defaults.model.primary = 'anthropic/claude-opus-4-5';
}
After
javascript// New logic - respects existing config, smart defaults
const hasExistingModel = config.agents.defaults.model.primary;
if (!hasExistingModel) {
if (hasOpenRouter) {
config.agents.defaults.model.primary = 'openrouter/moonshotai/kimi-k2.5';
} else if (hasAIGateway || hasAnthropic) {
config.agents.defaults.model.primary = 'anthropic/claude-sonnet-4-5-20250929';
}
}
Cost Impact
For a user with 100K messages/month at average 1K tokens each:
Before (forced Opus)
Input: 100M tokens × $15 = $1,500
Output: 100M tokens × $75 = $7,500
Total: $9,000/month
After (Sonnet default)
Input: 100M tokens × $3 = $300
Output: 100M tokens × $15 = $1,500
Total: $1,800/month (80% savings)
After (OpenRouter Kimi K2.5)
Input: 100M tokens × $0.45 = $45
Output: 100M tokens × $2.50 = $250
Total: $295/month (97% savings)
Testing
Tested with the following configurations:
✅ Fresh deployment with OPENROUTER_API_KEY → Defaults to Kimi K2.5
✅ Fresh deployment with ANTHROPIC_API_KEY → Defaults to Sonnet 4.5
✅ Fresh deployment with AI_GATEWAY_* → Defaults to Sonnet 4.5
✅ Existing config in R2 → Respects existing model choice
✅ Container restart → Preserves model configuration
✅ No API keys set → Falls back to Sonnet (safest option)
Breaking Changes
None. This is fully backward compatible:
Users with OPENROUTER_API_KEY will now get better defaults (Kimi K2.5 instead of first available)
Users with existing R2 configs will see no change
Users forced to Opus previously will now default to Sonnet (significant cost savings)
Additional Benefits
Clearer provider detection logging: Shows which provider is detected on startup
Better model options: Pre-configures sensible model choices for each provider
Configuration summary: Displays active model and available options on startup
Safer fallbacks: Never defaults to the most expensive option
Files Changed
start-moltbot.sh - Complete rewrite of model configuration logic
Related Issues
Fixes #50 (OpenRouter support)
Addresses cost concerns mentioned in multiple issues
Improves overall developer experience