Nous.Router behaviour (nous v0.21.1)

Copy Markdown View Source

Pre-dispatch model routing: pick the chat model for a request before it is built.

A router is configured on the agent as {module, opts}, next to :fallback, because both are model-selection concerns:

agent =
  Nous.new("anthropic:claude-sonnet-4-5",
    fallback: ["openai:gpt-4o"],
    router:
      {Nous.Router.DecisionModel,
       model: Nous.DecisionModel.parse("workers_ai:clef-flash"),
       tiers: [
         simple: [model: "groq:llama-3.1-8b-instant", description: "small talk, lookups, arithmetic"],
         complex: [model: "anthropic:claude-sonnet-4-5", description: "multi-step reasoning, code, proofs"]
       ]}
  )

Nous.run/3 also accepts router: as a per-run override (router: nil turns routing off for that run). Nous.Router.DecisionModel is the shipped implementation; any module implementing this behaviour works.

When the router is asked

The :granularity option (read from the router's opts, for every router) sets the routing boundaries:

  • :per_turn (default) - the first request of every turn. A turn is one Nous.run/3 / Nous.AgentRunner.run_with_context/3 call, plus one per structured-output validation retry.
  • :once - the first request of the run only.
  • :per_iteration - every request, including the ones that follow tool results. Most decision calls and most prompt-cache misses.
  • :escalate_only - every request, but the model only ever moves up the router's ordered tiers (tiers/1, cheapest first, required for this mode). The run starts on the cheapest tier; a decision for a lower or equal tier, :default or an error keeps the current one, and once the top tier is reached the router is no longer asked.

The decision is runtime-only: it is never written to result.deps or to a persisted context, so every run (and every Nous.AgentServer turn) routes afresh.

Fallback interplay

A routed request uses the chain [routed | agent.fallback] (deduplicated), with tool schemas and structured-output settings built for the routed model's provider. [:nous, :agent, :fallback, :used] fires only when a model other than the routed one answered. A fallback that answered stays sticky until the next routing boundary, where the new decision clears it.

:default (or {:default, meta}), an {:error, reason}, an invalid return value or a raise all mean "no route": the request goes to agent.model (except under :escalate_only, which keeps its current tier). Under :escalate_only, a tiers/1 that raises or returns anything but a non-empty list of %Nous.Model{} fails new/1; if it does so later, during a run, that request is not routed (current tier, or agent.model before the first decision). Routing fails open by design; a router is a cost/latency control, not a security control.

Not routed

Nous.run_stream/3 (Nous.AgentRunner.run_stream/3) is a single request whose consumer owns the stream and is never routed: it always uses agent.model and its fallback chain. Use Nous.run/3 with stream: true for a routed, streamed run.

Telemetry

  • [:nous, :router, :route] - once per router consultation
    • Measurements: %{duration: native_time} (the router call's latency)
    • Metadata: %{agent_name, router, granularity, turn, iteration, cause, tier, probabilities, confidence, reason, model_provider, model_name}. cause is :routed, :held (:escalate_only kept its tier), :default, :error, or the :cause a router put in its meta (e.g. :low_confidence). model_provider / model_name name the model the requests now go to. Plus the run correlation keys (see Nous.Telemetry).

Writing a router

defmodule MyApp.LengthRouter do
  @behaviour Nous.Router

  @impl true
  def init(opts), do: Keyword.update!(opts, :long, &Nous.Model.parse/1)

  @impl true
  def route(_agent, %{text: text}, opts) do
    if String.length(text) > 2_000,
      do: {:ok, opts[:long], %{tier: :long}},
      else: :default
  end
end

Nous.new("openai:gpt-4o-mini", router: {MyApp.LengthRouter, long: "openai:gpt-4o"})

init/1 runs once when the agent is built (and for a per-run router: override) and must be idempotent: a spec that already went through it may be passed through it again.

Summary

Types

When the router is consulted. See the module docs.

What the router sees for the request being built

Optional decision details, surfaced in [:nous, :router, :route]: :tier, :probabilities, :confidence, and for {:default, meta} a :cause and :reason.

t()

A router spec: the module and its (normalised) options.

Callbacks

Validates and normalises the options once, when the agent is built. Must be idempotent. Raise ArgumentError for invalid options.

Chooses the model for the request described by info.

The router's models in order, cheapest first. Required by granularity: :escalate_only.

Functions

The configured granularity (default :per_turn).

Validates a router spec and runs the module's init/1.

Types

decision()

@type decision() ::
  {:ok, Nous.Model.t()}
  | {:ok, Nous.Model.t(), meta()}
  | :default
  | {:default, meta()}
  | {:error, term()}

granularity()

@type granularity() :: :once | :per_turn | :per_iteration | :escalate_only

When the router is consulted. See the module docs.

info()

@type info() :: %{
  text: String.t(),
  messages: [Nous.Message.t()],
  iteration: non_neg_integer(),
  turn: pos_integer(),
  granularity: granularity(),
  current_model: Nous.Model.t() | nil
}

What the router sees for the request being built:

  • :text - the latest user message's text ("" when there is none)
  • :messages - the conversation so far
  • :iteration - the run's iteration counter (0 for its first request)
  • :turn - the turn index in the session log (1-based)
  • :granularity - the configured granularity
  • :current_model - the routed model in effect, or nil before the first decision of the run (or after a :default)

meta()

@type meta() :: map()

Optional decision details, surfaced in [:nous, :router, :route]: :tier, :probabilities, :confidence, and for {:default, meta} a :cause and :reason.

t()

@type t() :: {module(), keyword()}

A router spec: the module and its (normalised) options.

Callbacks

init(opts)

(optional)
@callback init(opts :: keyword()) :: keyword()

Validates and normalises the options once, when the agent is built. Must be idempotent. Raise ArgumentError for invalid options.

route(agent, info, opts)

@callback route(agent :: Nous.Agent.t(), info :: info(), opts :: keyword()) :: decision()

Chooses the model for the request described by info.

tiers(opts)

(optional)
@callback tiers(opts :: keyword()) :: [Nous.Model.t()]

The router's models in order, cheapest first. Required by granularity: :escalate_only.

Functions

granularity(opts)

@spec granularity(keyword()) :: granularity() | term()

The configured granularity (default :per_turn).

new(module)

@spec new(t() | module() | nil) :: t() | nil

Validates a router spec and runs the module's init/1.

Accepts nil, a module, or {module, opts}. Raises ArgumentError when the module does not implement route/3, the granularity is unknown, or :escalate_only is asked of a router without tiers/1 or whose tiers/1 does not return a non-empty list of %Nous.Model{}.