agentleFS
anoopnair-aipmanoopnair-aipm/frugal-claude/skill/SKILL.md

model-router

Skill1 starsChanged 7 months agoMITView on GitHub

Dynamically route Claude API calls between expensive and cheap models based on task complexity. Use this skill whenever the user wants to optimize API costs, reduce latency, build a smart model-switching system, or automatically select between Claude Haiku and Claude Sonnet/Opus based on what the task actually needs. Trigger this skill for any mention of model routing, cost optimization, tiered model usage, dynamic model selection, or smart API dispatching — even if the user just says "I want to save on API costs" or "route tasks to the right model."

  • Sends data out

25 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

2 rules. 0 are common, 2 are distinctive, and 0 are very specific to this project.

  • 0

    Common

    Most of the 24 edited versions we read keep these.

  • 2

    Distinctive

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    If 70% of your traffic is simple tasks routed to Haiku instead of Sonnet, you reduce costs by ~85% on those calls — with classifier …

    line 119
  2. Distinctive:

    Run these prompts and verify correct routing:

    line 147

“Common” is measured: kept by at least 6 in 10 of the 24 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Model Router Skill

What This Skill Does

This skill teaches Claude to build a two-stage routing system that:

  1. Uses a cheap classifier (Haiku) to assess task complexity
  2. Routes to the appropriate model based on that assessment

The result: maximum quality where it matters, minimum cost everywhere else.


Model Tier Map

TierModelUse When
Fastclaude-haiku-4-5-20251001Simple Q&A, formatting, translation, summarization
Smartclaude-sonnet-4-6Reasoning, code, analysis, multi-step tasks
Powerclaude-opus-4-6Architecture decisions, deep synthesis, highest stakes

Core Routing Logic

Stage 1 — Classify (always use Haiku, it's cheap)

Send the user's prompt to Haiku with this system prompt:

You are a task complexity classifier. Analyze the user's prompt and return ONLY valid JSON.
No preamble, no markdown fences.

Return this structure:
{
  "complexity": "simple" | "medium" | "complex",
  "reasoning": "one sentence explanation",
  "estimated_tokens": <number>
}

Classification rules:
- simple: factual Q&A, yes/no, formatting, translation, basic summarization, single-step tasks
- medium: code generation, multi-step reasoning, content creation, structured analysis
- complex: architecture decisions, deep research synthesis, cross-domain reasoning, debugging complex systems

Stage 2 — Route

simple  → claude-haiku-4-5-20251001
medium  → claude-sonnet-4-6
complex → claude-opus-4-6

Implementation Pattern (JavaScript / React)

async function routeAndRun(userPrompt) {
  // Stage 1: Classify
  const classifyRes = await fetch("https://api.anthropic.com/v1/messages", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      model: "claude-haiku-4-5-20251001",
      max_tokens: 200,
      system: CLASSIFIER_SYSTEM_PROMPT,
      messages: [{ role: "user", content: userPrompt }]
    })
  });
  const classifyData = await classifyRes.json();
  const { complexity, reasoning } = JSON.parse(classifyData.content[0].text);

  // Stage 2: Route
  const modelMap = {
    simple:  "claude-haiku-4-5-20251001",
    medium:  "claude-sonnet-4-6",
    complex: "claude-opus-4-6"
  };
  const selectedModel = modelMap[complexity];

  // Stage 3: Execute
  const response = await fetch("https://api.anthropic.com/v1/messages", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      model: selectedModel,
      max_tokens: 1000,
      messages: [{ role: "user", content: userPrompt }]
    })
  });
  const data = await response.json();

  return {
    answer: data.content[0].text,
    model: selectedModel,
    complexity,
    reasoning
  };
}

Cost Insight

The classifier call costs ~$0.0002 per call (Haiku, ~200 tokens). If 70% of your traffic is simple tasks routed to Haiku instead of Sonnet, you reduce costs by ~85% on those calls — with classifier overhead under 1%.


Key Design Decisions

  • Always classify with Haiku — never use Sonnet/Opus just to decide which model to use
  • Keep classifier prompt tight — the output must be parseable JSON, no fluff
  • Add a fallback — if JSON parsing fails, default to medium (Sonnet)
  • Log routing decisions — track which tasks land where; use this to tune thresholds over time
  • Never route user-facing trust-critical tasks to Haiku — legal, medical, financial decisions should default to medium/complex

Edge Cases to Handle

ScenarioRecommended Handling
Classifier returns invalid JSONDefault to medium, log the failure
API error on selected modelRetry once, then fall back to Sonnet
Very long prompts (>4k tokens)Override to medium or complex regardless
User override requestedHonor it — expose a forceModel param

Testing This Skill

Run these prompts and verify correct routing:

Should → Haiku (simple)

  • "What is the capital of France?"
  • "Translate 'good morning' to Spanish"
  • "Format this list alphabetically: banana, apple, cherry"

Should → Sonnet (medium)

  • "Write a Python script to parse a CSV and compute weekly averages"
  • "Explain the tradeoffs between REST and GraphQL APIs"
  • "Draft a 3-paragraph product update for our stakeholders"

Should → Opus (complex)

  • "Design a multi-tenant SaaS architecture for a fintech platform handling PII"
  • "Synthesize these 5 research papers and identify contradictions"
  • "Debug why our distributed system has intermittent data inconsistency under load"

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.