Mirage Academy · LLM Gateways, Cost Management & Model Routing

LLM gateway course: routing, budgets and fallbacks behind one API

Aimed at software engineers and technical product people who build AI features, this advanced course covers many models behind one gateway. Eight chapters and forty lessons lead to a capstone: a gateway with three providers and per-user budgets.

Open the courseFollow one request

One door

Without a gateway each service meets each provider alone

Start with the course video. Under it, the same three services and three providers drawn twice: once without a gateway and once with one.

Still from the intro video for the course LLM Gateways, Cost Management & Model Routing
Intro video, 1 min 10 s. Transcript below.
Read the transcript

Your app calls more than one model, and each provider has its own API, its own limits and its own bill. This course is for the engineer who has to keep that under control. Without a gateway, each service calls each provider directly, and spend shows up late. With a gateway, one API hides the providers, budgets stop spend early, fallbacks cover an outage, and an audit log records every call. Each request follows one path. The gateway checks rate limits and quotas. It serves from cache when it can. Otherwise it routes by cost, quality and latency, and falls back to another model. Then it counts the tokens and charges them to a user. In the capstone you build a gateway with three providers and per-user budgets. If you run models in production and want one place to control them, start with the first chapter.

Without a gateway

ServicesProviders

Each service calls each provider directly, and spend shows up late.

With a gateway

ServicesGatewayProviders

One API hides the providers, budgets stop spend early, fallbacks cover an outage, and an audit log records the calls.

Two diagrams. On the left, three services each joined by a dotted line to each of three providers, nine lines crossing. On the right, the three services join one gateway and the gateway joins the three providers, six lines.

One request

Each request follows one path through the gateway

The picture shows the stages the course video names. Chapter 1 teaches the full nine-step request path in reference order.

  1. Limits and quotas

    The gateway checks rate limits and quotas first.

    Chapter 4
  2. Cache

    It serves from cache when it can.

    Chapter 5
  3. Route

    Otherwise it routes by cost, quality and latency.

    Chapter 3
  4. Fallback

    When the chosen model fails, it falls back to another model.

    Chapter 5
  5. Count and charge

    Then it counts the tokens and charges them to a user.

    Chapter 6

Audit log

An audit log records the calls.

Chapter 7
A request enters on the left and passes five stations in a row: limits and quotas, cache, route, fallback, and count and charge. Under all five runs a bar for the audit log.

Providers

Providers speak different dialects, so the gateway keeps one request shape

Chapter 2 covers the layer between your services and the providers.

  1. Adapters and the canonical request

    One request shape for services, and an adapter per provider.

  2. Model catalogue fields

    The fields a model catalogue keeps for each model.

  3. Aliases, pinning and deprecation

    Aliases, pinning and deprecation of models.

  4. Parameters, limits and what not to translate

    Parameters and limits across providers, and what not to translate.

  5. Contract tests with recorded replies

    Contract tests with recorded replies for an adapter.

Three rows. In each, the same canonical request goes through its own adapter and arrives at one provider in that provider's dialect.

LLM engineering course: routing inside one application →

Routing

Route by cost, quality and latency, and work out what a cascade costs

Chapter 3 covers routing policies, cascades and how to evaluate a router. The calculator below uses made-up cost units.

Cascade cost calculator

Example, made-up units
How much dearer is the strong model?

  • Cascade3.00 units per request

    Per 1,000 requests: 3,000
  • Strong model only10.00 units per request

    Per 1,000 requests: 10,000

The cascade costs less than sending everything to the strong model. (-70%)

The cascade starts to pay off above 10%.

The rule: each request pays for the cheap call, and the requests whose answer is not accepted pay for the strong call as well. Expected cost per request = 1 + (1 minus the accepted share) x the price ratio. Sending everything to the strong model costs the ratio itself. So the cascade is cheaper exactly when the accepted share is above 100 divided by the ratio, in percent. This covers cost only. Whether the cheap answers are good enough is what evaluating a router measures.

The cascade costs less than sending everything to the strong model. Cascade: 3.00. Strong model only: 10.00.

A calculator with two choices: how many times dearer the strong model is than the cheap one, and the share of requests the cheap answer is accepted for. Two bars compare the cascade with sending everything to the strong model.

Limits

Rate limits, budgets and quotas: a token bucket fills at a steady rate

Chapter 4 starts with the limits providers enforce and builds your own on top.

Example run, made-up numbersThe bucket holds up to 5 tokens. It refills 1 token each second.

  1. Second 0: asks for 2allowed
  2. Second 0: asks for 2allowed
  3. Second 1: asks for 2allowed
  4. Second 1: asks for 2refused (429)
  5. Second 2: asks for 1allowed
  6. Second 6: asks for 3allowed
  7. Second 6: asks for 3refused (429)

The rule: the bucket starts full and is topped up for the seconds since the last request, capped at its capacity. A request takes its cost when that much is in the bucket. Otherwise it is refused with HTTP 429 and the bucket stays as it was.

  1. Provider limits and 429

    A provider refuses requests over its limit and answers with HTTP 429.

  2. Token buckets

    Token buckets for rate limits.

  3. Budgets and reservation

    Budgets and reservation before a call.

  4. Quotas and fair sharing

    Quotas, and sharing capacity fairly between users.

  5. Alerts and enforcement

    Alerts and enforcement of limits.

One run of a token bucket in seven rows. Each row shows the second, what the request asks for, the bucket's dots before it, and whether it was allowed or refused.

Fallbacks

Serve from cache when you can, and move down a chain when a provider fails

Chapter 5 covers what the gateway does before a call, and what it does when the call fails.

  1. Exact-match caching

    A request that repeats word for word can be answered from the cache.

  2. Semantic caching and its risk

    Semantic caching, and the risk that comes with it.

  3. What not to cache

    Answers that should not be served from a cache.

  4. Retries and fallback chains

    Retries and fallback chains of other models.

  5. Timeouts, hedging and circuit breakers

    Timeouts, hedging and circuit breakers for a failing provider.

A request meets the cache first. A hit ends there with an answer. A miss goes down a chain of three providers: the first fails, the second is skipped by a circuit breaker, the third answers.

Accounting

Count the tokens and charge them to someone

Chapter 6 turns token counts into numbers a team can use. The figure uses made-up numbers.

Spend by tagExample, made-up numbers

  • support assistant5,000 thousand tokens, 50%
  • search3,000 thousand tokens, 30%
  • internal tools2,000 thousand tokens, 20%

Gateway count: 10,000Provider count: 10,100Gap: +100 (+1%)

  1. Counting and reconciling

    The gateway counts tokens itself and compares its count with the provider's.

  2. Cached and reasoning tokens

    Cached and reasoning tokens in the count.

  3. Tags and allocation

    Tags and allocation of spend.

  4. Showback, chargeback and unit cost

    Showback, chargeback and unit cost as ways to report spend.

  5. Forecasts and anomalies

    Forecasts and anomalies in spend.

One bar split into three tagged parts with their shares in percent, and under it two totals: the gateway's count and the provider's count, with the gap between them.

Audit and filtering

Log the calls, redact before the provider, filter what comes back

Chapter 7 is about what the gateway records and what it lets through.

  1. What to log

    Which fields of a call go into the audit log.

  2. Traceability

    Tracing a response back to its request, route and user.

  3. Redaction before the provider

    Sensitive values are masked before the request leaves the gateway.

  4. Output filtering

    Answers pass a filter on the way back to the service.

  5. Access and retention

    Who may read the log, and how long it is kept.

A pipe from the request to the provider and from the provider to the response. A redaction gate sits on the way out, a filter gate on the way back, and a log tape underneath marks each stop.

Capstone

The capstone: a gateway with three providers and per-user budgets

You end the course by building the gateway, in five steps from the requirements to the review.

  1. Step 1: Requirements and design record

    Write the requirements and the design record.

  2. Step 2: Catalogue and route table

    List the models of the three providers and the table that routes requests to them.

  3. Step 3: Failure scripts

    Script the failures the gateway has to survive, then run them.

  4. Step 4: Accounting report

    Report who spent what, per user.

  5. Step 5: Go or no-go review

    Decide whether the gateway is ready to run, and say why.

Three providers behind one gateway and three users with a budget meter each; one meter is full. Below, five steps from the requirements to the go or no-go review.

Start

How the Academy teaches it

Course language: English.

Level and audience

  • Advanced
  • People who build AI features: engineers and technical product roles
Open the course: LLM Gateways, Cost Management & Model Routing

In the Academy

  1. Lessons

    Each lesson combines a short visual explainer, questions, a task and a quiz.

  2. Chapter checks

    Passing the chapter assessments leads to your certificate.

  3. AI coach

    In a voice session the coach has you explain the material in your own words and asks again where the answer is thin. The coach does not decide the certificate.

  4. Certificate

    The course comes with a certificate that fixed rules grant. It has its own verification code, which anyone can use to check it online. It is Mirage's own statement and not a state-recognized degree.

A box with the level and audience, above four stations: lessons, chapter checks, the AI coach and the certificate.

Prompt and Context Engineering course →Agentic Engineering course →

Questions about the LLM gateway course

  • What is the LLM Gateways, Cost Management & Model Routing course?

    The catalogue lists it as advanced, with eight chapters and forty lessons. It teaches how to run many models behind one gateway, with routing, budgets, fallbacks and accounting.

  • Who is this LLM gateway course for?

    Its catalog audience is engineers and technical product people building AI features, at advanced level.

  • What is an LLM gateway?

    In this course it is the one place between your services and the model providers. The course video describes one API that hides the providers, budgets that stop spend early, fallbacks for an outage and an audit log of the calls.

  • What does the gateway capstone build?

    A gateway with three providers and per-user budgets. You write the requirements and design record, the catalogue and route table, the failure scripts and the accounting report, and then hold a go or no-go review.

  • How is it different from the LLM engineering course?

    The engineering course builds one application: structured outputs, tools and routing inside it. This course treats routing, limits and accounting as shared infrastructure for many services and providers.

  • Does it teach prompting or building agents?

    No. This course covers the layer around the model calls. The Prompt & Context Engineering course covers prompting, and the Agentic Engineering course covers agents.

  • Do I get a certificate?

    Yes, a certificate comes with the course. Fixed rules on your chapter checks decide it, the AI coach does not, and others can check it online with its verification code.