agentleFS
alinaqialinaqi/claude-bootstrap/skills/model-routing/SKILL.md

model-routing

Skill708 starsChanged 5 months agoMITView on GitHub

9-tier model routing system with cascading classifier fallback and result auto-evaluation

  • Reads credentials

30 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use ecosystem-reference, fractal-delegation and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • naman10parikhSkill

    ecosystem-reference

    Quick-access directory of Claude Code ecosystem repos, skills, plugins, MCPs, and patterns. Use when improving the harness.

    Also in 5 projects that use this

  • naman10parikhSkill

    fractal-delegation

    Also in 5 projects that use this

  • KirchliveSkill

    skill-routing

    Decides which of the installed skills a task needs and loads them before the work starts. This skill should be used at the beginning of any multi-step task — build, fix, refactor, migrate, review, plan, release, research, write, design, video — and again at every phase change, after context compaction, and when work moves into a different part of the codebase. Use it for requests like "let's build X", "fix this bug", "refactor Y", "plan the migration", "review this", and also when the user asks which skill applies, why a skill did not fire, or asks to check what is available.

    Also in 5 projects that use this

  • sublimotionSkill

    agent-runtime

    Launch and manage detached agent runs on EKS via the agent-runner CLI. Use when the user says "launch an agent run", "agent runtime", "agent-runner", "run this spec on the cluster", "check run status", "agent run logs", or "stop a run". Do NOT use for writing the agent-runner code itself (edit the sibling ../agent-runner repo), Terraform of the foundation (use terraform-automation), or GPU serving deployments (use infra-deployer).

    Also in 5 projects that use this

  • GoZippySkill

    repo-scout

    Also in 5 projects that use this

  • BenjaminGSkill

    harness-review

    Turn one PR's review comments into harness fixes — classify each cause by the most solid place it can live (codebase, lint, rules, skill), then draft the fixes in a separate PR.

    Also in 5 projects that use this

  • +6 more from naman10parikh

The file

We split this skill into the rules it gives an agent

5 rules. 0 are common, 3 are distinctive, and 2 are very specific to this project.

  • 0

    Common

    Most of the 28 edited versions we read keep these.

  • 3

    Distinctive

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Every user prompt goes through a 9-tier classification pipeline before any AI model processes it.

    line 13
  2. Distinctive:

    When the hook says "delegate to X", run the matching command and return its output:

    line 54
  3. Very specific:

    Every ~/bin/ script follows the same pattern:

    line 85
  4. Very specific:

    Before the hook emits a delegation instruction it checks that the tier's wrapper exists (the resolved flash/pro binary, ~/bin/gemini-api, …

    line 120
  5. Distinctive:

    When Claude's built-in tools fail, external backends take over:

    line 129

“Common” is measured: kept by at least 6 in 10 of the 28 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Model Routing System

How Routing Decisions Are Made

Every user prompt goes through a 9-tier classification pipeline before any AI model processes it. The system answers three questions:

  1. Which model should handle this? — 9-tier cost/complexity classification
  2. Is the classifier itself working? — Cascading fallback (qwen3 → kimi → deepseek → cache)
  3. Can we verify the result? — Tool-level fallback + auto-evaluation

The Pipeline

User types prompt
    ↓
UserPromptSubmit hook fires (~/.claude/hooks/route-task-hook)
    ↓
Classifier: qwen3 (local, free) classifies into tier
    ↓  (fails?)
Classifier: kimi (local, free) retries
    ↓  (fails?)
Classifier: deepseek-flash (~$0.0001) retries
    ↓  (fails?)
Classifier: cached tier from last success
    ↓
Hook injects routing decision into Claude's context
    ↓
Claude delegates to the right model or handles directly

9-Tier Routing Table

TierModelInput (per M)Output (per M)Handles
1Gemini 2.5 Flash-Lite$0.10$0.40Bulk extraction, classification, CIG pipelines
2DeepSeek V4 Flash$0.14$0.28Simple code, CRUD, test writing, small fixes
3DeepSeek V4 Pro$0.44$0.87Multi-file features, refactors, debugging (~80% of work)
4Gemini 2.5 Flash$0.15$0.60Multimodal (images, video, audio), brand analysis
5Kimi K2.6$0.60$2.50Code review, commit messages, diff summaries
6Gemini 3.1 Pro + Search$1.25$10.00Deep research, Google grounding, 2M context
7CodexvariesvariesBulk generation, code review
8Claude Sonnet/Opus$3-5$15-25Architecture, security, quality-critical

Delegation Commands

When the hook says "delegate to X", run the matching command and return its output:


# Tier 1 — Gemini Flash-Lite
~/bin/gemini --flash-lite "prompt"

# Tier 2 — DeepSeek Flash
~/bin/deepseek --flash "prompt"

# Tier 3 — DeepSeek Pro
~/bin/deepseek --pro "prompt"

# Tier 4 — Gemini Flash
~/bin/gemini --flash "prompt"

# Tier 5 — Kimi
~/bin/kimi --quiet -p "prompt"

# Tier 6 — Gemini Pro Search
~/bin/gemini --pro-search "prompt"

# Tier 7 — Codex
codex exec "prompt"

# Tier 8 — Claude
# Handle directly (no delegation)

Delegation Script Contract

Every ~/bin/ script follows the same pattern:

  1. Accepts prompt as argument: script "what is 2+2"
  2. Model flags: --flash, --pro, --flash-lite, --pro-search
  3. Quiet mode: --quiet (where applicable)
  4. Output: writes response to stdout, errors to stderr
  5. Exit codes: 0 on success, non-zero on failure

Available Scripts

~/bin/
├── qwen3       # Shell: curl to local Ollama API
├── kimi        # Shell: execs Kimi CLI binary
├── deepseek    # Python: httpx to DeepSeek Anthropic-compat API
├── gemini      # Python: httpx to Gemini OpenAI-compat API
├── research    # Python: multi-backend research with auto-evaluation
└── route-task  # Shell: qwen3-powered task classification

Classifier Fallback Chain

The classifier itself can fail. When it does, cascading fallback kicks in:

LevelClassifierCostThreshold
1qwen3 (Ollama)$02s connect, 8s classify
2kimi CLI$0Local process
3deepseek-flash~$0.0001API call
4Cached tier$0From ~/.claude/routing-cache.json

The cache (~/.claude/routing-cache.json) saves the last successful tier and timestamp. After compaction, when Ollama may be briefly unreachable, the cache ensures routing continues without dropping to CLAUDE by default.

Missing Wrapper Fallback

Before the hook emits a delegation instruction it checks that the tier's wrapper exists (the resolved flash/pro binary, ~/bin/gemini-api, ~/bin/gemini-cli, ~/bin/kimi, ~/bin/grok, or codex on PATH). If it is missing, the decision becomes ROUTING DECISION: CLAUDE (fallback from <tier>) and Claude handles the task directly, without the PLAN FIRST step. A cached tier the hook no longer knows is discarded the same way. There is no qwen3 delegation tier any more.

Tool Fallback Protocol

When Claude's built-in tools fail, external backends take over:

Failed ToolFallback 1Fallback 2
WebSearch / WebFetch~/bin/research "query"~/bin/deepseek --pro "query"
Read / file accesscat via Bash—
Grepgrep -r via Bash—

Research Tool (~/bin/research)

Multi-backend research with auto-evaluation:

  • Tries deepseek-flash → deepseek-pro in sequence
  • Scores results 0-10 on content quality, structure, length
  • Auto-adjusts preferred backend based on evaluation scores
  • View stats: ~/bin/research --eval
  • Score log: ~/.claude/research-eval.jsonl

Maggy Integration

Maggy's model_router.py mirrors the same 9-tier structure in DEFAULT_TIERS. The PiAdapter uses the same delegation scripts for execution. Task type overrides in routing_rules_defaults.py ensure:

  • research, competitor → Gemini Pro Search (Google grounding)
  • bulk → Gemini Flash-Lite (cheapest)
  • security, architecture, planning → Claude (quality-critical)
  • docs, tests → DeepSeek Pro (cost-efficient)
  • review → Claude (security + architecture depth)

Environment

# Required for delegation scripts (in ~/.zshrc)
export DEEPSEEK_API_KEY="sk-..."
export GEMINI_API_KEY="..."       # For gemini delegator
export OPENAI_API_KEY="sk-..."    # For codex CLI

# Ollama must be running locally for qwen3
ollama serve  # or launch at startup

Observability

  • Routing log: ~/.claude/routing-log.jsonl — every classification with tier, classifier used, tokens saved
  • Routing cache: ~/.claude/routing-cache.json — last tier for post-compact recovery
  • Research eval: ~/.claude/research-eval.jsonl — per-query backend scoring
  • Maggy routing heatmap: Dashboard → Models tab → per-model reward scores

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.