Agent Skills › alinaqi/maggy › model-routing

model-routing

GitHub

实现9层模型路由系统,通过级联分类器将提示词分配至不同成本/复杂度模型。包含故障降级机制、自动结果评估及基于可用性的回退策略,优化AI调用效率与成本。

skills/model-routing/SKILL.md alinaqi/maggy

触发场景

需要智能分发LLM请求以优化成本或性能时 配置多模型代理或自动化工作流时

安装

npx skills add alinaqi/maggy --skill model-routing -g -y
更多选项

不安装直接使用

npx skills use alinaqi/maggy@model-routing

指定 Agent (Claude Code)

npx skills add alinaqi/maggy --skill model-routing -a claude-code -g -y

安装 repo 全部 skill

npx skills add alinaqi/maggy --all -g -y

预览 repo 内 skill

npx skills add alinaqi/maggy --list

SKILL.md

Frontmatter
{
    "name": "model-routing",
    "effort": "medium",
    "description": "9-tier model routing system with cascading classifier fallback and result auto-evaluation",
    "when-to-use": "When configuring or debugging how prompts are classified and routed to the cheapest capable model",
    "user-invocable": false
}

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
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:


# 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.

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 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

# 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

版本历史

  • 55aabe2 当前 2026-10-19 09:28

    移除Qwen3路由层级,当目标模型包装器缺失时直接回退至Claude处理,并新增钩子测试用例。

  • 2a98228 2026-09-09 08:48

同 Skill 集合

skills/aeo-optimization/SKILL.md
skills/agent-teams/SKILL.md
skills/agentic-development/SKILL.md
skills/ai-models/SKILL.md
skills/android-java/SKILL.md
skills/android-kotlin/SKILL.md
skills/autonomous-testing/SKILL.md
skills/aws-aurora/SKILL.md
skills/aws-dynamodb/SKILL.md
skills/azure-cosmosdb/SKILL.md
skills/base/SKILL.md
skills/build-in-public/SKILL.md
skills/cloudflare-d1/SKILL.md
skills/code-deduplication/SKILL.md
skills/code-graph/SKILL.md
skills/codex-review/SKILL.md
skills/commit-hygiene/SKILL.md
skills/context-shunt/SKILL.md
skills/council-review/SKILL.md
skills/cpg-analysis/SKILL.md
skills/cross-agent-delegation/SKILL.md
skills/database-schema/SKILL.md
skills/dataforseo/SKILL.md
skills/demo-video/SKILL.md
skills/existing-repo/SKILL.md
skills/external-model-delegation/SKILL.md
skills/firebase/SKILL.md
skills/flutter/SKILL.md
skills/gemini-review/SKILL.md
skills/icpg/SKILL.md
skills/iterative-development/SKILL.md
skills/klaviyo/SKILL.md
skills/llm-patterns/SKILL.md
skills/maggy/SKILL.md
skills/medusa/SKILL.md
skills/mnemos/SKILL.md
skills/ms-teams-apps/SKILL.md
skills/nodejs-backend/SKILL.md
skills/playwright-testing/SKILL.md
skills/polyphony/SKILL.md
skills/project-tooling/SKILL.md
skills/pwa-development/SKILL.md
skills/python/SKILL.md
skills/react-native/SKILL.md
skills/react-web/SKILL.md
skills/reddit-api/SKILL.md
skills/security-audit/SKILL.md
skills/session-management/SKILL.md
skills/shopify-apps/SKILL.md

元信息

文件数
0
版本
55aabe2
Hash
fd9ebdd9
收录时间
2026-09-09 08:48

首页 - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-10-19 16:43
浙ICP备14020137号-1