5 min read

One Door Per Vendor


Every vendor a program depends on should come in through one module. Call sites import a name from that module and never learn which company is behind it. I’ve paid for this rule up front in two apps and collected on it twice in the same week, so here is what it buys and the two things it does not.

The first case is a model provider. My recipe planner’s backend made eleven LLM calls in production in August. Thirteen cents. Moving it off OpenAI had nothing to do with price at that volume; my Anthropic account carries monthly credits I wasn’t using and my OpenAI account bills a card. The move took an afternoon because the vendor had one door:

export const defaultModel = anthropic(config.aiModelDefault);
export const fastModel = anthropic(config.aiModelFast);
export const premiumModel = anthropic(config.aiModelPremium);

Those three lines used to say openai.responses(...). Seventy-three files in that backend use the Vercel AI SDK, fifty-two of them tool definitions for the agent loop, the rest structured-generation calls and the loop itself. None of them names a vendor. They take defaultModel and pass it along, and the SDK normalizes tool calling and structured output across providers, so the swap was the factory, three model ids in config, and one package. 1,943 tests, green before and after. I had left a comment on that module months earlier saying a provider switch would need the matching package here, which is the whole design: the comment and the module are the same size.

Two things the seam could not hide.

Provider-specific options. Twelve call sites passed an OpenAI prompt-cache key through provider options. Anthropic caches by request prefix with a marker instead, so the helper that builds those options was rewritten and every call site changed one identifier. A sed, but one I had to understand before running. A seam hides the vendor’s name; it cannot hide the vendor’s knobs, because the knobs are usually why you chose the vendor. What you can do is make the knobs come through the same door. The helper lives in the provider module, not at the call sites, which is why the next swap is a sed again instead of twelve small arguments with myself.

Pricing tables. Usage events record token counts and a model id, and two tables turn those into money: micro-dollars for the per-user cap, credits for billing. Both needed rows for the new models. The old rows stayed so August’s eleven events still price, and the fallback row for an unknown model moved to the most expensive one, so a typo in a model id can only over-charge. A price is a fact about a vendor that has to live outside the vendor’s door, because the thing that reads it is the billing domain and the billing domain should not import an LLM client. Nobody thinks of pricing as part of a provider migration until the first event bills at the fallback.

The second case is a database, and I built this door on purpose the week after, knowing what it was for. A feed of SEC insider trades started on a laptop with a sqlite file. Putting it on Cloud Run meant Postgres on Neon, because Cloud Run instances are disposable and a file store dies with them. The door is one interface:

export interface Driver {
  readonly dialect: Dialect;
  all<T>(sql: string, params?: unknown[]): Promise<T[]>;
  run(sql: string, params?: unknown[]): Promise<void>;
  close(): Promise<void>;
}

Two implementations, sixty-six lines between them. One wraps bun:sqlite. The other wraps a pg pool and rewrites ? placeholders to $1, $2 before each query, which turns out to be the entire dialect gap once the store sticks to TEXT, INTEGER, REAL and ON CONFLICT upserts. DATABASE_URL set means Postgres; unset means the sqlite file; :memory: in tests. The store writes each statement once and never asks which driver it got.

The honest part: the callers did change. Every store method went from sync to async, because the Postgres driver is, so the scheduler and three other files grew an await in front of each call. That was all of it. No SQL moved, no file outside db.ts knows which database it is talking to, and the same sixty-five tests run against sqlite locally and three of them again against a real Postgres when CI provides one. Async is this seam’s version of the cache key: the one property of the new vendor the door could not absorb, and it cost a diff of awaits rather than a rewrite.

I don’t know whether the Driver interface survives the first query that wants something Postgres has and sqlite doesn’t, a JSONB index say. When that comes, the choice is to grow the interface or admit the sqlite path exists for tests and the laptop and nothing else, and I’d probably admit it. A door is worth keeping only while both sides still fit through it.