SeedlingPlanted Tended 6 min read

Magpie 的 Intent Routing 是怎么选模型的

每一轮开始时,一个小模型先判断这条消息是难还是易,再决定组里哪个模型先回答。

Magpie 是 yetone 做的开源工具,跑在你自己的电脑上。它给 Claude Code、Codex、Gemini CLI 这类 coding agent 提供一个统一的模型入口。你可以把多个模型放进同一个路由组,让 agent 选用整个组。Intent Routing 是组里的一种规则:每一轮开始时,一个小的分类模型会先判断这条消息是难还是易,再决定组里哪个模型先回答。

一轮是怎么选模型的

一个路由组里通常放一个强模型和一个便宜模型。新的一轮开始时,分类器会读用户的话,并判断这是困难任务还是简单任务。这个答案只决定谁先被问到,另一个模型仍然留在后面做故障转移。选定之后,这个选择会管住整一轮。因此,agent 把工具结果送回来时,请求会回到当初提出工具调用的那个模型,而不会重新分类。

User messagea new turn begins
user's words
Classifiersmall model, once a turn
answers 1 · hard
Strong modelasked first
if it fails
Cheap modelfailover
answers 2 · easy
Cheap modelasked first
if it fails
Strong modelfailover

The choice holds for the whole turn: tool results go back to the model that asked for them.

图 1 一轮开始时,分类器只决定谁先上场。工具结果也会回到同一个模型。

分类器并不是只看到最新这一句。源码说明写明,它拿到的是开启这一轮的用户原文,长消息会保留开头 3000 个字符和结尾 1000 个字符;同时还会拿到上一条用户消息和助手上一次回复,各最多 1000 个字符,并且被告知上一轮属于哪一类。因此,用户只回一句「go on」时,分类器可以按上一轮的类别来判断。官方文档则写分类器只看最新一条消息。就这一点而言,源码说明比文档写得更具体。

规则按什么顺序检查

Magpie 自己有一套模板,名字叫 Hard to strong, easy to cheap。示例组用的就是这套模板写出来的四条规则。网关从上往下检查,第一条命中的规则把它指定的成员排到第一。

你可以点选下面的一种情况。你点选其中一种之后,请求原文、规则按顺序检查的结果、分类器是否被问起,以及谁先被问到,都会跟着改变。

The request

what does the -race flag do?

1 · Rules, checked in order
  1. 1Compaction → cheap modelNo: not a compaction
  2. 2Carries an image → strong modelNo: no image
  3. 3Intent: a hard task → strong modelNo: the turn is easy
  4. 4Intent: an easy task → cheap modelMatches: the turn is easy
2 · Classifier
asked onceIt is sent:
Kinds:
1. a hard task: designing, debugging, a change across several files, or planning
2. an easy task: a quick question, a small edit, a lookup, or running a command

The user's message:
<message>
what does the -race flag do?
</message>

The number of the kind that fits it best (0 only if none does):
answers 2Easy.
3 · Who is asked
  1. Cheap modelasked first
  2. Strong modelfailover

Rule 4 decides. The classifier says easy, so rule 4 puts the cheap model first. The strong model stays behind it in case the cheap one fails.

这里的消息和分类器答案都是示例。真正的分类器本身是一个模型,因此它给出的回答可能不同。不过,网关对每一种回答的处理,与源码里的做法一致。

分类器只在「一条带意图的规则有可能成为第一条命中规则」时才会被问起。如果更前面的规则已经命中,比如请求里带了图片,这次调用就不会发生。除了意图、压缩和图片,一条规则还可以加上请求长度、推理强度、来自哪个 agent、开始时间,以及工作目录这些看得见的条件。源码说明强调,这些条件不会被猜测,同一条请求每次都会按同样的方式路由。

分类器失败时会怎样

同一道题在 10 分钟内再问一次,会直接用记忆里的答案。分类器失败之后会被搁置 30 秒,这样后面的每一轮都不必再把超时时间等完。

What happens on each request

  1. Is the agent compacting?

    When the agent asks for a summary of its conversation, the first rule that matches a compaction decides, by itself. This does not become the turn's decision, so the requests after it carry on as before. A model whose context window is known to be smaller than the conversation is passed over, because it would only refuse it.

  2. Is this the same turn?

    If the agent is sending tool results back, nothing is decided again. The request goes to what was chosen when the turn began. One thing can move it: the conversation growing past what that model can take. Then the rules are looked at again and it moves to a model known to take more.

  3. A new turn: read the facts

    The gateway reads what is visible in the request: its length in tokens, whether it carries an image (in this turn or an earlier one), the reasoning level the agent asked for, which agent sent it, its working folder, and the time. Rules on these are never guessed. The same request is always routed the same way.

  4. Ask the classifier, only if needed

    The classifier is asked only when a rule that could be the first to match is waiting on an intent. If an earlier rule already matches, for example the image rule, no call is made.

    It is given the user's own words from the message that began the turn, without the blocks agents add to it. Long messages keep their first 3,000 and last 1,000 characters. It also gets the previous user message and the assistant's last reply, up to 1,000 characters each, and is told what kind the turn before was, so a short follow-up like "go on" is judged with what it follows.

    The request is sent at temperature 0 with reasoning turned off where the model allows it. The answer is a single number: the kind that fits, or 0 for none.

  5. Handle a slow or failed classifier

    • The same question asked again within 10 minutes is answered from memory, with no call.
    • The classifier has a timeout, set in Settings. Its own failover counts against it.
    • After a failure it is left alone for 30 seconds, so every turn does not wait out the timeout.
    • If it cannot say and the turn before had an intent, that intent is used again. If there was none, no intent matches.
  6. The first rule that matches decides

    Its member goes first. Members named by later rules that also match come next, then the rest of the group. If no rule matches, the group's own order is used, so whichever member is listed first gets the turn. That is why the strong model is listed first: an unplaced turn stays on it.

  7. Remember it for the turn

    The decision is stored per group and conversation, along with the intent, and reused for every later request of the turn. The Routing view's trace shows which rule decided, what the classifier was asked, what it said, how long it took, and whether the answer came from memory.

图 3 每条请求按这个顺序处理。分类器失败或回答 0 时,会沿用上一轮的意图,或退回组的默认顺序。

它做不到的事

源码说明也列出了这项机制的短处。这些限制来自对源码的阅读,而不是线上评测。

What it costs and where it falls short

  • One extra call per new turn. The classifier runs before the turn goes anywhere, so a slow classifier delays the first token.
  • It judges the message, not the work. "Fix the flaky test" can look easy and turn out hard. The turn stays on the cheap model unless the conversation outgrows its context window.
  • Hard and easy are what you wrote. The classifier chooses among your wordings. Vague intents give vague routing.
  • No feedback. Nothing checks whether the answer was good, and nothing is learned from past turns.
  • Switching models loses the prompt cache. A conversation that alternates between the two re-reads its whole context uncached each time it moves, which eats into the saving.
  • An image pins the conversation. With the image rule, one screenshot keeps every later turn of that conversation on the strong model.
图 4 源码说明里列出的六条短处。

根据公开的 Claude Artifact Magpie Intent Routing 整理,对应开源项目 yetone/magpie 的 commit 873b498。产品介绍参考了 usemagpie.ai。