Skip to content

Smart Model Routing ​

The SmartModelRouter automatically selects the optimal LLM model based on task complexity, saving costs by routing simple tasks to cheaper models.

How It Works ​

  1. Analyze the prompt to classify complexity (trivial/simple/moderate/complex/expert)
  2. Route to the appropriate model tier
  3. Fall back to a higher tier if the selected model fails
"fix typo" ──► trivial ──► gpt-5.4-mini ($0.15/M)
"add button" ──► simple ──► gpt-5.4-mini ($0.15/M)
"refactor auth" ──► complex ──► claude-sonnet-4-6 ($3/M)
"design system" ──► expert ──► claude-opus-4-6 ($15/M)

Usage ​

typescript
import { SmartModelRouter } from '@opensin/sdk'

const router = new SmartModelRouter({
  models: {
    trivial: 'gpt-5.4-mini',
    simple: 'gpt-5.4-mini',
    moderate: 'gpt-5.4',
    complex: 'claude-sonnet-4-6',
    expert: 'claude-opus-4-6',
  },
  fallbackOnError: true,
})

// Automatic selection
const model = router.selectModel('Fix the typo in README.md')
// Returns: 'gpt-5.4-mini'

const model2 = router.selectModel('Refactor the entire authentication system to use JWT')
// Returns: 'claude-sonnet-4-6'

Complexity Classification ​

The router uses keyword analysis and prompt structure to classify tasks:

LevelIndicatorsExamples
Trivialtypo, rename, comment"Fix typo in line 5"
Simpleadd, update, format"Add a loading spinner"
Moderateimplement, create, build"Implement pagination"
Complexrefactor, redesign, migrate"Refactor auth to OAuth2"
Expertarchitect, design system, security audit"Design the plugin system"

Fallback Chain ​

When a model fails (rate limit, error, insufficient output), the router automatically escalates:

gpt-5.4-mini → gpt-5.4 → claude-sonnet-4-6 → claude-opus-4-6
typescript
const router = new SmartModelRouter({
  models: { ... },
  fallbackOnError: true,
  maxRetries: 2,
  fallbackChain: ['gpt-5.4-mini', 'gpt-5.4', 'claude-sonnet-4-6', 'claude-opus-4-6'],
})

Cost Savings ​

Typical cost reduction with smart routing vs. always using the best model:

WorkloadWithout RoutingWith RoutingSavings
100 tasks (mixed)$4.20$0.9577%
Code review (simple)$1.50$0.1590%
Architecture (expert)$7.50$7.500%

Smart routing saves the most when your workload has a mix of simple and complex tasks, which is the typical case for coding agents.

Integration with Agent Loop ​

typescript
const agent = new AgentLoop({
  model: router,  // pass router instead of model string
  tools: toolRegistry,
})

The agent loop calls router.selectModel(prompt) on each turn, so the model can change mid-conversation as task complexity evolves.


Relevante Mandate ​

MandatPriorityRegel
Bun-Only-1.5bun install / bun run statt npm
Annahmen-Verbot-5.0KEINE Diagnose ohne Beweis
Test-Beweis-Pflicht0.0KEIN "Done" ohne echten Test-Lauf

→ Alle Mandate

Guides and concepts for the OpenSIN API.