Skip to main content
Glama

Model Router — コスト意識型マルチLLMルーティング(無料優先フォールバックチェーン)

すべてのタスクを最適なモデルに自動ルーティング — 無料プロバイダを優先、有料プロバイダをフォールバックとして利用。

License: MIT Python 3.8+ Tests CI

🎯 Model Router の理由

ほとんどのLLM統合コードは、単一のモデルをハードコードするか、単一のAPIプロバイダを使用します。つまり:

  • 高コスト: すべてのリクエスト(単純なものでも)が有料のフロンティアモデルに送られる

  • 脆弱性: 1つのプロバイダの障害 = 完全な障害

  • 柔軟性の欠如: モデルの能力をタスクの難易度に合わせる方法がない

Model Router は、シンプルで依存関係の少ない設計でこれらすべてを解決します:

  • タスク難易度分類: キーワード + コンテンツ長のヒューリスティック(LLM不要、コストゼロ)による5段階(vision / long / complex / medium / simple

  • 無料優先フォールバックチェーン: 各ルートレベルで順序付けられた候補チェーンを定義 — 無料プロバイダ(Zhipu、SiliconFlow、OpenRouter無料モデル)を最初に試行し、失敗した場合は次の候補(最終的には有料のDeepSeek/Qwen/GLM/Kimi)が自動的に試行される

  • サードパーティ依存関係ゼロ: コアライブラリは requests のみ必要。MCPサーバーは純粋なstdlib(JSON-RPC 2.0 over stdio)

  • 複数のインターフェース: Python API · CLI · MCPサーバー(任意のMCPクライアント:Claude、Qoder、Cursor…) · ファイル監視デーモン

🏗 アーキテクチャ

                ┌─────────────────────────────────────────────┐
                │              router_core.py                 │
                │                                             │
   task_desc ──▶│  classify_task()  →  5 difficulty levels   │
   content   ──▶│  _get_candidates() → ordered provider chain│
   image ──────▶│  route_and_call()  → free-first, fallback  │
                │                                             │
                └──────────────┬──────────────────────────────┘
                               │
              ┌────────────────┼───────────────────┐
              ▼                ▼                   ▼
        auto_router.py    mcp_server.py       Python API
        (CLI + daemon)    (MCP tools)         (import router_core)

ルーティングレベル

レベル

トリガー

代表的なモデル

vision

画像/スクリーンショット/OCR

Qwen-VL、GLM-4V

long

コンテンツ > 2000文字、全文書

128Kコンテキストモデル

complex

分析/コード/データ/統計/推論

DeepSeek、Qwen3-32B

medium

執筆/翻訳/推敲

GLM、Qwen

simple

日常会話 / クイッククエリ

小さな無料モデル

フォールバックの意味: 候補は順に試行され、レスポンスは完全な観測可能性のために tier(無料/有料)、attemptsfallback_used、およびプロバイダごとの errors を報告します。

🚀 クイックスタート

# 1. Install (only requests is required)
pip install requests

# 2. Configure
cp config.example.json config.json
#    → fill in your API keys

# 3. CLI — analyze only (zero cost, no model call)
python router_core.py analyze "分析这份气象数据"

# 4. CLI — route and call
python router_core.py call "翻译以下段落" --content "Hello world" --system "你是专业翻译"

# 5. Python API
from router_core import route_and_call, analyze_task
result = analyze_task("写一份论文摘要", content_len=300)
print(result["primary"])           # first candidate
print(result["candidates"])        # full fallback chain
text = route_and_call("总结要点", "long text...")["content"]

🖥 MCPサーバー(任意のMCPクライアント向け)

python mcp_server.py

ツール

説明

smart_call

自動ルーティング + 呼び出し(無料優先、失敗時フォールバック)

route_analyze

難易度分析 + 候補チェーンを返す(コストゼロ)

list_models

設定されたすべてのプロバイダ、モデル、チェーンを一覧表示

MCPクライアント(例:Claude Desktop claude_desktop_config.json)に登録:

{
  "mcpServers": {
    "model-router": {
      "command": "python",
      "args": ["/path/to/model-router/mcp_server.py"],
      "env": {"PYTHONIOENCODING": "utf-8"}
    }
  }
}

⏱ タスクトリガーデーモン(ファイル監視モード)

# One-shot
python auto_router.py "任务描述" -c "内容" --image photo.png

# Daemon: drop task files into inbox/, results appear in outbox/
python auto_router.py --watch --dir ./tasks

🔧 設定

config.json の構造(config.example.json を参照):

  • providers: tierfree / paid)とオプションの enabled: false を持つOpenAI互換エンドポイント

  • routing: レベルごとの順序付き candidates チェーン — 無料を優先、有料をセーフティネットとして

  • levels: レベルごとの人間が読める説明

自由にプロバイダを追加・削除可能 — ルーターは完全にデータ駆動型です。

📄 ライセンス

MIT — 個人利用および商用利用は無料。 LICENSE を参照。


実世界のコスト最適化に着想を得て: 日常のタスクの約90%は無料階層のモデルで対応可能であり、困難なタスクでも自動フォールバックによりフロンティアモデルの品質を確保します。

-
license - not tested
-
quality - not tested
B
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Connectors

  • Hosted MCP server for LLM cost estimation, model comparison, and budget-aware routing.

  • Agent Cost Allocator MCP — multi-tenant LLM cost attribution for chargeback billing. Companion to

  • MCP server providing access to the Scorecard API to evaluate and optimize LLM systems.

View all MCP Connectors

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/yaowanxiang/model-router'

If you have feedback or need assistance with the MCP directory API, please join our Discord server