model-routing
# Model Routing System
What this skill does
# 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
| Tier | Model | Input (per M) | Output (per M) | Handles |
|------|-------|---------------|----------------|---------|
| 0 | **Qwen3** (local) | $0 | $0 | grep, find, shell, syntax, log reading |
| 1 | **Gemini 2.5 Flash-Lite** | $0.10 | $0.40 | Bulk extraction, classification, CIG pipelines |
| 2 | **DeepSeek V4 Flash** | $0.14 | $0.28 | Simple code, CRUD, test writing, small fixes |
| 3 | **DeepSeek V4 Pro** | $0.44 | $0.87 | Multi-file features, refactors, debugging (~80% of work) |
| 4 | **Gemini 2.5 Flash** | $0.15 | $0.60 | Multimodal (images, video, audio), brand analysis |
| 5 | **Kimi K2.6** | $0.60 | $2.50 | Code review, commit messages, diff summaries |
| 6 | **Gemini 3.1 Pro + Search** | $1.25 | $10.00 | Deep research, Google grounding, 2M context |
| 7 | **Codex** | varies | varies | Bulk generation, code review |
| 8 | **Claude Sonnet/Opus** | $3-5 | $15-25 | Architecture, security, quality-critical |
## Delegation Commands
When the hook says "delegate to X", run the matching command and return its output:
```bash
# Tier 0 — Qwen3
~/bin/qwen3 "prompt"
# 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:
| Level | Classifier | Cost | Threshold |
|-------|-----------|------|-----------|
| 1 | **qwen3** (Ollama) | $0 | 2s connect, 8s classify |
| 2 | **kimi** CLI | $0 | Local process |
| 3 | **deepseek-flash** | ~$0.0001 | API call |
| 4 | **Cached tier** | $0 | From `~/.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.
## Tool Fallback Protocol
When Claude's built-in tools fail, external backends take over:
| Failed Tool | Fallback 1 | Fallback 2 |
|-------------|------------|------------|
| **WebSearch** / **WebFetch** | `~/bin/research "query"` | `~/bin/deepseek --pro "query"` |
| **Read** / file access | `cat` via Bash | — |
| **Grep** | `grep -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
```bash
# 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
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.