insideout Get Pro
Learn
AI engineering · Module 11

Production AI

Run AI features people can depend on.

Start here · Lesson 01

Routing requests between models

Send each request to the cheapest model that can handle it.

Start lesson

LessonsAll lessons

  1. 01 Routing requests between modelsSend each request to the cheapest model that can handle it. Free
  2. 02 When the main model is downFallbacks, timeouts, and degrading gracefully. Free
  3. 03 Spending limits per user and per teamToken budgets, rate limits and quotas. Free
  4. 04 Exact and semantic cachesReuse answers safely, and know when not to. Pro
  5. 05 Logs, metrics and tracesWhat to record so you can see what the system is doing. Pro
  6. 06 When to page someoneAlerts worth waking up for, and how to run an incident. Pro
  7. 07 Drift and model monitoringNotice when inputs or quality shift after launch. Pro
  8. 08 How prompt injection worksText in the input that overrides your instructions. Pro
  9. 09 Poisoned documents and web pagesInjection through retrieved text, email and tool output. Pro
  10. 10 Defending against injectionLayered defences, and why no single one is enough. Pro
  11. 11 Input and output filtersBlock what shouldn't go in or come out. Pro
  12. 12 Canary, shadow and rollbackRelease a change to a few users first, and undo it fast. Pro
  13. 13 Cheap decisions before expensive modelsA small classifier decides first, and the large model only writes. Pro
  14. 14 Capstone: a service with routing, fallbacks and monitoringRun an AI service that stays up and within budget. Pro

Build it yourself · on your own machine

A model service with a fallback

A small HTTP service in front of two local models that routes, caches, limits spending, logs every request and keeps answering when its main model goes down.
  1. Run two model servers on different ports (llama-server or Ollama), a 3B model as the main one and a 1B model as the cheap one, and put a FastAPI endpoint in front of them.
  2. Route short, simple requests to the cheap model, and write one JSON log line per request: id, user, route, model, tokens in and out, time to first token, total time and status.
  3. Add a timeout that falls back to the other model, a token bucket per user, and an exact cache keyed on the normalised prompt.
  4. Replay 300 instructions from databricks-dolly-15k on Hugging Face with a load script, and stop the main server for one minute in the middle.
  5. From the logs alone, chart p95 latency and error rate per minute, and write the alert rule that should have fired.
  6. Put a canary string in the system prompt, attack the service with the injection rows of deepset/prompt-injections, then add an output check that blocks any reply holding the canary.

How you'll know it worksAt least 99% of requests succeed during the outage minute, the canary reaches no user once your check is in, and your per-minute chart comes from your own logs alone.

Python with FastAPI and httpx, llama.cpp or Ollama for the two models, and free datasets from Hugging Face. It all runs on a laptop, with no API key. Uses lessons 01 to 14.