Skip to main content

Overview

85% of agent traffic is simple queries hitting expensive models. The ModelRouter classifies incoming requests by complexity and routes them to the cheapest model that can handle them, achieving 40–85% cost savings.

Quick Start

import { ModelRouter, openai } from "@radaros/core";

const router = new ModelRouter({
  tiers: [
    { model: openai("gpt-4o-mini"), maxComplexity: 0.3 },
    { model: openai("gpt-4o"), maxComplexity: 0.7 },
    { model: openai("o3"), maxComplexity: 1.0 },
  ],
  outcomeTracking: true,
});

const agent = new Agent({
  name: "cost-efficient-agent",
  model: router,
  instructions: "You are a helpful assistant.",
});

Built-in Complexity Classifier

The built-in classifier runs in under 1ms with no LLM call. It scores on:
SignalWeightExample
Token count+0.05–0.15Long prompts → higher complexity
Code markers+0.15Code fences, function, class, import
Reasoning keywords+0.20”analyze”, “compare”, “step by step”
Complex instructions+0.15”build a system”, “design an API”
Tool count+0.08–0.15More tools → harder routing
Structured output+0.10JSON schema responses
Multi-turn depth+0.05–0.10Deep conversations

Custom Routing Rules

Override the classifier with explicit rules:
const router = new ModelRouter({
  tiers: [
    { model: openai("gpt-4o-mini"), maxComplexity: 0.3 },
    { model: openai("gpt-4o"), maxComplexity: 1.0 },
  ],
  rules: [
    {
      condition: (msgs, opts) => (opts?.tools?.length ?? 0) > 5,
      tier: 1, // Always use GPT-4o for many tools
    },
    {
      condition: (msgs) => msgs.some(m =>
        typeof m.content === "string" && m.content.includes("```")
      ),
      tier: 1, // Code tasks to GPT-4o
    },
  ],
});

Outcome Tracking

When outcomeTracking: true, the router logs success/failure per tier in a ring buffer:
const stats = router.getOutcomeStats();
// [{ tierIndex: 0, total: 145, successes: 140, rate: 0.97 },
//  { tierIndex: 1, total: 32, successes: 31, rate: 0.97 }]

Events

EventPayload
model.routed{ tier, complexity, modelId }