Translation, chat answers, suggested follow-ups, memory compression — different features, written at different times. They all end up in the same place: llm.service.chat(). I checked the codebase: the only call to a provider's chat() anywhere sits inside that one function. Everything else calls the funnel, and the funnel calls the provider.
Four callers, one door
The translation orchestrator calls it. The chat orchestrator calls it, streaming. The suggestion code calls it. Memory compression calls it. The word-lookup endpoint calls it. Each caller passes a call_type — translate, analyze, or chat — plus a character count and a session id, so the audit row knows what the call was for. The provider never sees any of that. It gets messages and a config dict, and returns text and a usage count.
The point of routing everything through one function is that the guarantees only have to be written once. A new feature that needs the model doesn't negotiate its own billing or audit behavior. It passes its call_type and gets the same treatment as everything else.
The same steps, in the same order
Every call runs the same sequence. First the provider is resolved from the user's configuration. Then the config gets an SSRF check, before any network leaves the box. Then, only on the system track, a quota check — a read-only check before the call, not a reservation. A user spending their own key never meets that check at all. Then the provider is called. Only after it returns successfully does billing happen, and only for the tokens actually used.
The quota check being read-only is a choice with a visible edge: a user who has already burned the month's allowance is stopped at the next call, not the current one. The service can overshoot by at most one call. That was accepted rather than building a reserve-and-settle flow, because the reserve would have to be unwound on every failure, and failures are the common case for a quota system to get wrong.
A call that fails costs nothing
Billing runs on the usage the provider reports. If the call throws, there is no usage, no audit row, and no billing — as far as the ledger is concerned, the call never happened. If the call returns but reports no usage, the audit row is still written, with zeros, and billing is skipped. The attempt is recorded; the spend is not.
The counter and the ledger are fed from the same number, total_tokens, so they can't drift apart from each other. They can only drift from reality, when a call leaves no row at all — which is what the next two sections are about.
The receipt has no return value to ride on
For a normal call, the funnel hands the caller the ledger row id through an on_call_id callback, along with a snapshot of which provider served the call — provider id and source — so the caller can freeze that on the spot instead of joining the ledger later. The chat orchestrator needs it to tie "which turn" to "how much it cost and who served it."
For a streaming call there is no return value, only a generator. The ledger row id is created inside that generator, after the last chunk is consumed. The caller can't get it from a return value because there isn't one. The callback is the only way out. That asymmetry is why the callback exists at all: the sync path could have returned the id, but the streaming path couldn't, so both go through the same callback and the caller writes one piece of code.
If the callback itself throws, the funnel logs a warning and carries on. A bookkeeping failure is not allowed to fail a call that already succeeded.
Close the tab and the call never happened
The streaming wrapper writes its audit row and bills only after the generator is fully consumed — the usage figures arrive in the final done block. If the reader disconnects halfway, Python raises GeneratorExit inside the wrapper and the tail never runs. No audit row, no billing, for tokens that were really generated and really sent.
The code doesn't try to bill a half-consumed stream. There is no number to bill from — the usage block never arrived — and guessing from the text streamed so far would mean charging for tokens the provider hasn't confirmed. So those tokens are simply never billed. That is the part the reconciliation can't fix: it recomputes counters from the stored call records, and a call that left no record has nothing to recompute from. The loss is accepted, and it is bounded in one direction — the counter can only under-count on this path, never over-count — which is the side a quota system would rather err on.
The funnel never looks the user up
The caller passes a full user dict into chat(). Since a decision in September 2026, the funnel does not query the database for the user — cost attribution never triggers a hidden lookup. Whoever calls the funnel is responsible for knowing who the user is, and the funnel takes their word for it.
The same instinct shows in capabilities(): if the provider can't even be resolved, it returns an empty dict instead of throwing, so "cannot tell" is treated as "not probed" and no caller fails because a capability check failed. The funnel answers the questions it can and stays quiet about the ones it can't, and the caller carries on with what it already knew.
One function, a fixed order, bills only what succeeded, forgets what was cut off, and never asks the database who the user is. Everything else in the service just calls it.
About the author
- Writing about ebook technology.
Comments
Join the discussionNo comments yet. Be the first to comment.