From GPT-2 to a real model: one interface, three providers

Until now every model in this series was one I could run in numpy on my laptop: GPT-2 small, built
from scratch. This week I started Phase II: one real application, an assistant that answers questions about distributed-db, the Go database
I built in my earlier distributed-systems series.
This week was the foundation: one interface that hides whose model is on the other end, prices
every call, and decides which failures deserve a second try.

What I built

One Provider contract and three adapters behind it: Anthropic, Gemini and a local Ollama model.

eval / CLI / agent
      │
call_with_retries ── 3 attempts, 0.5 s → 1 s backoff, transient errors only
      │
Provider: complete · stream · structured · tool_turn
      │
 Anthropic │ Gemini │ Ollama      ← each one only translates
      │
Completion{text, model, stop_reason, usage, cost_usd, latency_s}
  or one of: Timeout · RateLimited · Unavailable · Refused · InvalidRequest · InvalidOutput

On top of that: a price table that turns tokens into dollars, a 50-question golden set about
distributed-db, and an eval runner with a spend cap.

The concepts that made it work

  • Callers never see an SDK. Each provider puts the system prompt somewhere different, returns
    text in a different shape and fails in its own way. The adapter absorbs all of that. Code above the
    contract sees a Completion or a ProviderError, and nothing else.
  • The error taxonomy is the design. Timeouts, 429s and 5xx errors are worth retrying. Refusals,
    bad requests and bad keys are not, because the same input gets the same answer. Retries live in
    exactly one place. I turned the SDKs’ own retries off, because two layers multiply: 3 x 3 = 9 calls
    for one question.
  • Cost is part of the response. Every Completion carries its dollar cost. An unknown model
    raises an error instead of costing $0, because a silent zero makes an expensive model look free in
    every report after it.
  • Measure before you improve. The first number is a no-retrieval baseline. Everything next has
    to beat it.

The thing that bit me

Three things. The first turned out to be the best argument for the design.

The plan was Anthropic. The key failed authentication on every call. My eval runner happily sent
20 doomed requests and wrote a report full of errors. I fixed the runner first: a 401 is now an
AuthFailed error that stops the run at the first call. Then I switched the default provider to
Gemini, in an afternoon. The swap was one new adapter file, a registry entry, pricing rows and a
--provider flag. Nothing that consumes a Completion changed. That is what an interface is for.

Thinking ate the answer. I asked Gemini for JSON with a 256-token budget. It spent 243 of those
tokens thinking and returned the words “Here is the JSON requested:”, then stopped. Gemini counts
thinking tokens against the output limit, and bills them as output. Now the budget a caller passes
means the answer budget, and the adapter adds a 1,024-token reserve for thinking. Speed was a
surprise too: gemini-3.8-flash took 30-45 seconds per call on the free tier, so the default is
gemini-3.5-flash-lite, at about 1 second.

The baseline lied. My first no-retrieval score was 0.455, which looked suspiciously good for a
model that has never seen the repo. The system prompt mentioned a <context> block even though there
was no context. So in 37 of 50 answers the model invented one, complete with fake repository code.
Those invented answers scored higher (0.50 vs 0.32), because my scorer checks whether key facts
appear in the answer, and made-up numbers matched by luck. With an honest prompt:

No retrieval, gemini-3.5-flash-lite, 50 questions Result
Mean key-fact score 0.385
By type: how / where / failure 0.49 / 0.33 / 0.29
Full marks / zero 6 / 17
Latency p50 / p95 1.27 s / 2.09 s
Tokens 9,113
Cost for all 50 $0.019

0.385 is still not zero. My guess: distributed-db follows the naming of a well-known course
(MIT 6.5840), so the model can probably guess names like lastSeq and applyCh. I haven’t tested that. That is the floor the next layer has to beat.

Tradeoffs & the road not taken

  • No gateway product. LiteLLM or Portkey would give me this contract for free. I wrote it by hand
    to learn where providers really differ. They differ in errors and token accounting, not in the API
    call.
  • A crude scorer. Substring matching can’t tell “it uses lastSeq” from “it does not use
    lastSeq”, and it rewarded invented code. I kept it on purpose for now; a model-as-judge comes later
    in the phase, with a check of how often the two agree.
  • Client-side limits only. A requests-per-minute pacer (10/min keeps a run under the free tier,
    about 5 minutes for 50 questions) and a spend cap per run. No per-team quotas, no shared counters,
    no jitter in the backoff yet.
  • The cheap model. At $0.30 / $2.50 per million tokens, flash-lite is over 10x cheaper than
    claude-opus-5-5. I haven’t compared answer quality between them; that wasn’t measured this week.

Where this sits in the system

Phase I built every layer of an AI stack from scratch on a toy model. This week the toy model was
swapped for real ones, behind an interface that already survived its first provider change. Next
week: a code index over distributed-db, so the <context> block holds real code instead of
invented code, and the score has to go up honestly.


Phase II of a series on AI engineering: one real application on real models, measured from day one.