Skip to content

Instantly share code, notes, and snippets.

@bdougie
Last active April 13, 2026 13:47
Show Gist options
  • Select an option

  • Save bdougie/e126ddb21d1ff54704e93960f3874125 to your computer and use it in GitHub Desktop.

Select an option

Save bdougie/e126ddb21d1ff54704e93960f3874125 to your computer and use it in GitHub Desktop.
Heuristic-based model routing for AI agents

Model Routing with OpenRouter Auto

Smart model routing reduces costs by matching request complexity to model capability. Instead of building your own heuristics, let OpenRouter handle it.

The Problem

Most AI agent setups use a single expensive model for everything:

  • Quick status checks → opus ($$$)
  • Complex reasoning → opus ($$$)
  • Heartbeat processing → opus ($$$)

You're paying premium prices for tasks that don't need premium intelligence.

The Solution: OpenRouter Auto

OpenRouter's Auto Model (openrouter/openrouter/auto) automatically selects the most cost-effective model based on your prompt. No client-side heuristics needed.

How It Works

[Your Request] → [OpenRouter Auto] → [Picks optimal model] → [Response]

OpenRouter analyzes your prompt and routes to the best model for the job:

  • Simple queries → cheap/fast models
  • Complex reasoning → capable models
  • Code generation → code-optimized models

OpenClaw Configuration

Step 1: Add OpenRouter Auth

openclaw onboard --auth-choice apiKey --token-provider openrouter --token "$OPENROUTER_API_KEY"

Or manually add to ~/.openclaw/openclaw.json:

{
  "auth": {
    "profiles": {
      "openrouter:default": {
        "provider": "openrouter",
        "mode": "api_key"
      }
    }
  }
}

Then store the key:

openclaw auth set openrouter:default --key "$OPENROUTER_API_KEY"

Step 2: Configure Auto Model

Update your agents.defaults.model section:

{
  "agents": {
    "defaults": {
      "model": {
        "primary": "openrouter/openrouter/auto",
        "fallbacks": [
          "openrouter/anthropic/claude-haiku-3.5"
        ]
      },
      "models": {
        "openrouter/openrouter/auto": {},
        "openrouter/anthropic/claude-haiku-3.5": {}
      }
    }
  }
}

Step 3: Restart Gateway

openclaw gateway restart

Cost Impact

Typical distribution after switching to auto routing:

Before:  100% opus           → $15.00/1M tokens (blended)
After:   Auto-routed mix     → ~$3-5/1M tokens (blended)
         ↓
         60-80% cost reduction

Hybrid Approach: Auto + Explicit Overrides

Use auto for most tasks, but pin specific workloads to specific models:

{
  "agents": {
    "defaults": {
      "model": {
        "primary": "openrouter/openrouter/auto"
      }
    }
  }
}

Then override per-spawn for critical tasks:

# Force opus for complex coding tasks
openclaw spawn --model anthropic/claude-opus-4-5 --task "Review this PR..."

Or in sessions_spawn:

{
  "model": "anthropic/claude-opus-4-5",
  "task": "Complex analysis requiring top-tier reasoning"
}

Monitoring

Track what's actually being used:

  1. OpenRouter Dashboard: openrouter.ai/activity

    • See which models auto is selecting
    • Cost breakdown by model
    • Request volume over time
  2. clawtel (planned): Heartbeat telemetry includes model field

    • Surface distribution on claw.tech dashboard
    • Identify further optimization opportunities

When to Override Auto

Auto is good for most tasks, but consider explicit models when:

Scenario Override To
Complex coding/debugging claude-opus-4-5
Long context (>100k tokens) gemini-pro-1.5
Cost-critical batch jobs claude-haiku-3.5
Reasoning chains deepseek-reasoner

Related


The best model for the job isn't always the biggest one — let the router figure it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment