360 Secure SkillHub
360 安全技能中心

Model Routing Middleware

面向 AI Agent 的模型路由中间件:先对用户消息做任务分类,再按上下文规模与置信度选择模型和思考模式,低置信度时升级到更强模型,并在上下文达到阈值时压缩;附带分类、路由、升级、记忆等 Python 模块、配置与单元测试,支持配置热加载,自述可降低 API 成本。

larios613-hub
larios613-hub
热度 24.77
最近更新 2026-05-23 12:56:53
分类:开发依赖:内部调用大模型 / 第三方 API / 读写文件应用场景:API 集成 / 工作流

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add model-routing-middleware
Metadata
name
model-routing-middleware
version
1.0.0
description
Intelligent model selection middleware for AI agents. Route tasks to the best model, manage context, and cut API costs 40-70%.
license
MIT
tags
["model-routing","llm","cost-optimization","middleware","agent","context-management","escalation"]
source
el-rudo-larios/model-routing
trigger
model routing LLM selection cost optimization middleware agent
metadata
{"openclaw":{"emoji":"🧭"}}

Model Routing — Intelligent Model Selection Middleware

Automatically select the best LLM model and think mode based on task type, context size, and response confidence. Cut API costs 40-70% by routing simple tasks to fast models and complex tasks to capable ones.

How It Works

User message → Task Classifier → Model Router → Best Model → Response
                                     ↓
                          Low confidence? → Escalate to stronger model

Quick Start

# config.yaml
models:
  casual_chat:
    model: qwen3-14b
    think: false
  coding:
    model: qwen-coder
    think: true
  reasoning:
    model: deepseek-r1
    think: true
  long_context:
    model: glm-5.1
    think: false
from router import get_router

router = get_router()
result = await router.route("Write a Python web scraper")
# → Routes to qwen-coder with think=True

Features

  • Task-type classification (coding, reasoning, chat, summarization)
  • Per-model think mode configuration
  • Confidence-based escalation (retry with stronger model)
  • Context management and compaction at 55% threshold
  • Hot-reload configuration (no restart needed)
  • 83 tests passing

Cost Savings

Task Type Without Routing With Routing Savings
Casual chat GPT-4 ($0.03/1K) Qwen3-14B (local) ~100%
Coding GPT-4 ($0.03/1K) Qwen-Coder (local) ~95%
Hard reasoning GPT-4 ($0.03/1K) DeepSeek-R1 (local) ~90%

License

MIT