part II · chapter 9 of 17

Model routing and caching

Sending each request to the right model, and not paying twice for the same answer.

why it matters

Cuts cost and latency sharply, using the Redis and performance skills a backend engineer already has.

before this: chapter 8, Guardrails and AI security

being written

This chapter is being written. Here is what it will cover; until then, the full topic list is in our notes.

  • rule, complexity and semantic routing
  • fallback, cascading and failover
  • exact, semantic and prompt caches
  • streaming, batching, backoff
the topic list on GitHub (opens in a new tab)

you reached the end of the chapter

build this

A router (easy to a small model, hard to a strong one, failure to a fallback) with Redis caching; measure the savings.

the maths underneath

  • maths note 6

    Softmax and sampling

    pi=ezi/T∑jezj/Tp_i = \frac{e^{z_i / T}}{\sum_j e^{z_j / T}}

    Temperature, top-k and top-p, every generation setting an API takes, are this one function.

  • maths note 10

    Production numbers: percentiles, throughput, cost

    E[c]=h⋅chit+(1−h)⋅cmiss\mathbb{E}[c] = h \cdot c_{hit} + (1 - h) \cdot c_{miss}

    Dashboards, budgets, routing and caching decisions are all these numbers.

check yourself

Tick what you could explain to someone else, out loud, without notes.