Goal

Set up a model chain that optimizes for quality first, then falls back to free models when paid models are unavailable.

Strategy: Paid First, Free Fallback

import { StyrRouter } from '@carloscortezcloud/styrr-llm';

const router = new StyrRouter({
  apiKey: process.env.OPENROUTER_API_KEY,
  models: [
    // Tier 1: Best quality (costs money)
    { id: 'openai/gpt-4o' },

    // Tier 2: Good quality (cheaper)
    { id: 'anthropic/claude-sonnet-4' },

    // Tier 3: Free fallback
    { id: 'nvidia/nemotron-3-ultra-550b:free' },
    { id: 'google/gemma-4-31b-it:free' },
  ],
  onFallback: (model, err, next) => {
    console.log(`${model} failed (${err.status}) → switching to ${next}`);
  },
});

Strategy: Free Only (Zero Cost)

const router = new StyrRouter({
  apiKey: process.env.OPENROUTER_API_KEY,
  models: [
    { id: 'nvidia/nemotron-3-ultra-550b:free' },
    { id: 'google/gemma-4-31b-it:free' },
    { id: 'meta-llama/llama-3.3-70b-instruct:free' },
  ],
});

Strategy: Budget-Aware (with Sayay)

Use Sayay’s degrade action to switch models when budget is low:

const guard = new SayayGuard({
  storage: new MemoryStorage(),
  budget: { dailyUsd: 5.0 },
  thresholds: { warn: 0.80, degrade: 0.95 },
});

// In your agent loop:
const decision = await guard.check(userId, estimatedCost);

const models = decision.action === 'degrade'
  ? [{ id: 'nvidia/nemotron-3-ultra-550b:free' }]          // Free only
  : decision.action === 'warn'
  ? [{ id: 'anthropic/claude-sonnet-4' }, { id: 'google/gemma-4-31b-it:free' }]  // Mixed
  : [{ id: 'openai/gpt-4o' }, { id: 'anthropic/claude-sonnet-4' }, { id: 'nvidia/nemotron-3-ultra-550b:free' }]; // Full chain

Strategy: By User Tier

function getModelsForUser(userTier: 'free' | 'pro' | 'enterprise') {
  switch (userTier) {
    case 'free':
      return [{ id: 'nvidia/nemotron-3-ultra-550b:free' }];
    case 'pro':
      return [
        { id: 'anthropic/claude-sonnet-4' },
        { id: 'nvidia/nemotron-3-ultra-550b:free' },
      ];
    case 'enterprise':
      return [
        { id: 'openai/gpt-4o' },
        { id: 'anthropic/claude-sonnet-4' },
        { id: 'nvidia/nemotron-3-ultra-550b:free' },
      ];
  }
}

Monitoring Fallbacks

Track fallback rates with Qhaway:

const router = new StyrRouter({
  apiKey: process.env.OPENROUTER_API_KEY,
  models: [paidModel, freeModel1, freeModel2],
  onFallback: (model, err, next) => {
    trace.wrap(fn, {
      model: model,
      metadata: { fallback: true, nextModel: next, reason: err.message },
    });
  },
});