AI architecture

Your own LLM, on GPUs that bill by the hour

A helpdesk company's model API bill reaches US$540,000 a month, and someone proposes running an open-weight model on rented GPUs. The cost of one answer is worked out again at every step, from one GPU answering one request at a time to a shared fleet that is kept busy while the people who are waiting go first.

Jump to the final architecture

US$540,000. That is last month's total on the invoice from the model provider, for a company that sells helpdesk software to thousands of businesses and calls a language model about 2 million times a day on their behalf. Four teams built those calls into four features: the support team's reply drafter writes a first answer for an agent to edit, the ticketing team summarises long ticket threads, the help-centre team runs a search assistant that answers customers from the help articles, and the insights team tags every new ticket with its topic. The features worked, people used them, and the bill grew with them.

In the meeting about the invoice, someone proposes the obvious alternative. Open-weight models, whose weights anyone can download and run, are now good enough for drafting, summarising and tagging. Rent some GPUs, the chips with thousands of small arithmetic units that models run on, run one of those models, and stop paying by the token. A GPU is rented by the hour, so the question becomes: how many answers can one GPU write in an hour?

That question runs through this whole post. A GPU is expensive, it is billed whether it works or waits, and run the obvious way, one request at a time, it waits most of the time. Every section ends by working out one number again, the cost of one answer, and by saying what the GPU spent its hour doing. The list near the end puts all of those numbers side by side, so an engineer who already runs inference fleets can start there and read the fifth drawing.

What the bill is made of

Every number below is an assumption of the exercise, rounded so the arithmetic stays easy. None of them is a measurement of a real provider, GPU or model, and none is a price quote:

  • About 2 million model calls a day from the four features, about 83,000 an hour on average. The day has a shape, also assumed: about four busy hours at three times the average, about ten hours of the working day near the average, and about ten hours of night at a tenth of it.
  • An average call reads about 1,500 tokens and writes about 300. A token is the unit models read and bill by, roughly three quarters of an English word.
  • The managed API charges US$3 per million input tokens and US$15 per million output tokens, a round price for the exercise rather than any provider's list.
  • An open-weight model of about 30 billion parameters, the numbers the model learned in training. At 16 bits per parameter its weights take about 60 GB, so it fits on one GPU with 80 GB of memory, with about 20 GB to spare.
  • A GPU rents for US$2.50 an hour, working or not: about US$1,800 a month for one that runs all the time.

The day adds up: four hours at about 250,000 calls, ten at about 92,000 and ten at about 8,300 come to about 2 million. The bill, worked out: 2 million calls × 1,500 tokens is 3 billion input tokens a day, US$9,000; 2 million × 300 is 600 million output tokens, another US$9,000. About US$18,000 a day, US$540,000 a month, and about 0.9 cents per answer. That is the number to beat.

Two kinds of waiting matter, and they are very different. An agent using the drafter, or a customer using the search assistant, is watching the screen: the answer must start appearing within about a second. Summaries and tags can take minutes, and a job over old tickets can take a day without anyone minding.

One GPU, one request at a time

The first version is a pilot: the reply drafter, for one support team of forty agents. One rented GPU machine runs an open-source inference server, the program that loads a model's weights into GPU memory and answers requests over HTTP. vLLM is a common one, and it offers an API shaped like the managed providers', so the drafter's code barely changes. When the server first starts on a machine, it downloads the 60 GB of weights from a public model hub, the site where open-weight models are published (Hugging Face is the best known), and loads them into the GPU. The drafter's backend sends a prompt and waits; the server writes the whole answer and returns it in one piece.

Stage 1 of 5: One inference server, one request at a time
One inference server, one request at a timeProduct services send each prompt to one inference server, a GPU machine, along an edge labelled whole answer: the request goes along it and the answer comes back along it in one piece. Below the inference server, the model hub supplies its weights along an edge labelled weights.whole answerweightsProduct servicesRuns the business logicSERVICES & APISInference serverAccelerated computePLATFORM & RUNTIMEModel hubModel hub / inferenceEXTERNAL INTEGRATIONS
One inference server, one request at a timeProduct services send each prompt to one inference server, a GPU machine, along an edge labelled whole answer: the request goes along it and the answer comes back along it in one piece. Below the inference server, the model hub supplies its weights along an edge labelled weights.whole answerweightsProduct servicesRuns the business logicSERVICES & APISInference serverAccelerated computePLATFORM & RUNTIMEModel hubModel hub / inferenceEXTERNAL INTEGRATIONS

Every new machine that starts the server downloads the weights from the public hub.

Learn moreGPU & accelerated computeModel serving: inference, versions & safe rollout

For a pilot this is enough, and it answers the question that comes before all the others: is the open model good enough? The team replays a few thousand of last month's real drafting prompts through both models and has support leads compare the answers without knowing which model wrote which. Judging quality is a subject of its own (Evaluating LLM output: golden sets, judges & release gates); here, assume the open model passes for all four features. Check its licence too: open weights come with terms, and not every licence allows every commercial use.

Then the arithmetic, and it is bad. Assume this server, working on one request alone, writes about 40 tokens a second. A 300-token answer takes about 7.5 seconds, plus a fraction of a second to read the prompt: call it 8 seconds, or about 450 answers an hour if the GPU never stops. At US$2.50 an hour that is about 0.56 cents per answer, under two thirds of the API's 0.9 cents, and only if every second of every hour is used.

Now give it the whole company's traffic. The average of about 83,000 calls an hour needs 185 such GPUs; the busy hours, about 250,000, need 556. A fleet that can take the busy hours costs 556 × US$1,800, about US$1 million a month, for traffic the API serves for US$540,000: about 1.7 cents per answer, nearly twice the bill this was meant to cut.

Why is one GPU so slow? A model writes its answer one token at a time, and each token takes a pass through the whole model: all 60 GB of weights are read from GPU memory into the arithmetic units, used for a few multiplications each, and read again for the next token. Reading 60 GB forty times a second is 2.4 terabytes a second, in the range of what the memory of a large data-centre GPU can move. The memory is the limit. The arithmetic units, built for far more work than one sequence gives them, are mostly idle. Reading the prompt is different work: its 1,500 tokens go through the model together, which keeps the arithmetic busy, so it is quick. The two phases are called prefill (reading the prompt) and decode (writing the answer), and decode is where this GPU's time goes.

What the GPU does most of the time: during the pilot, nothing at all between requests; during a request, mostly wait for its own memory.

Eight seconds of an empty box

The pilot's first complaint is not about money. A support agent clicks Draft, looks at an empty box for eight seconds, decides it is broken and clicks again, which puts a second request in line behind the first on a server that does one at a time.

Nothing has to be generated faster to fix this. The server already produces the answer one token at a time; it was only holding the tokens until the end. So it sends each one as it is made. The inference server streams tokens to the drafter's backend, and the backend streams them on to the agent's browser, usually as server-sent events: one long HTTP response that carries a small event after another (Realtime transport: long-polling vs SSE vs WebSocket compares the options). When nothing is queued ahead, the first words appear as soon as the prompt has been read, well under a second, and the rest arrive at 40 tokens a second, faster than people read.

Stage 2 of 5: Stream each token as it is written
Stream each token as it is writtenThe edge from product services to the inference server is now labelled token stream: the request still goes along it, and the tokens come back along it as they are written. The whole answer edge is gone. The model hub still supplies the weights along the edge labelled weights.token streamweightsProduct servicesRuns the business logicSERVICES & APISInference serverAccelerated computePLATFORM & RUNTIMEModel hubModel hub / inferenceEXTERNAL INTEGRATIONS
Stream each token as it is writtenThe edge from product services to the inference server is now labelled token stream: the request still goes along it, and the tokens come back along it as they are written. The whole answer edge is gone. The model hub still supplies the weights along the edge labelled weights.token streamweightsProduct servicesRuns the business logicSERVICES & APISInference serverAccelerated computePLATFORM & RUNTIMEModel hubModel hub / inferenceEXTERNAL INTEGRATIONS

Same boxes, same work for the GPU; only the shape of the response changed.

Learn moreRealtime transport: long-polling vs SSE vs WebSocketServing LLMs: gateways, fallback & token budgets

The number users feel is now the time to first token, from clicking the button to the first word on screen. The whole answer still takes eight seconds, and the cost of one answer does not change at all: the GPU does exactly the same work, token for token. Still about 0.56 cents at full use, with the GPU still mostly waiting for its memory. Streaming is for the person, not for the bill.

It does change the design in three places:

  • Every answer holds a connection open for its whole length. Anything between the server and the browser that buffers responses, a proxy or a compression step, has to pass each event on as it arrives, or the stream reaches the browser in one lump at the end.
  • Timeouts measure gaps, not totals. A five-second limit on the whole request would cut every healthy answer at second five. The backend limits the time to the first token and the gap between two tokens instead, so a stalled stream is noticed within seconds while a long answer runs as long as it needs.
  • A stream can stop in the middle. If the server crashes after 120 tokens, the agent has half a draft on screen that looks like a short answer. So the backend treats an answer as finished only when the stream's last event says why generation stopped: the answer is complete, or it reached its length limit. A stream that ends without that event is incomplete, and the interface says so instead of leaving half a draft that looks finished.

Drafts wait in line behind summaries

Two weeks in, the ticketing team points the same support team's summaries at the pilot too, and requests start to queue. At about 450 answers an hour, each request waits for every request ahead of it to write all its tokens. An agent's draft can sit behind three summaries, and its first token arrives after about half a minute instead of half a second. The obvious fix is a second GPU, and the arithmetic above says where that road ends: 556 of them.

The better fix comes from the reason the GPU is slow. Each decode step reads all 60 GB of weights to write one token for one request. The same read can write one token for each of many requests: the weights come in once, and each request's numbers are multiplied by them together, which is exactly the arithmetic the GPU had to spare. That is a batch: many requests decoded together, one token each per step.

Language models make batching awkward, because requests do not finish together: a tag is a dozen tokens, a summary a few hundred. A server that waits for the whole batch to finish before starting the next leaves the short answers' places empty while the longest one writes on. Inference servers built for language models, vLLM among them, use continuous batching instead: after every step, finished requests leave the batch and waiting ones join it, so the batch stays full as long as there is work. It is a property of the inference server, not a new box.

What stops the batch from growing forever is memory again, and this time not the weights. To write its next token, the model needs something computed from every token before it in that request, the prompt included. Recomputing all of that at every step would be wasteful, so the server keeps it in GPU memory, one store per request that grows with every token: the KV cache, named after the keys and values the model's attention layers compute. Of the 20 GB left after the weights, assume the server's own working memory takes about 5, and that each request in flight needs about 300 MB of KV cache for its 1,800 tokens (the figure depends on the model's shape). That is room for about 50 requests at once. A request that does not fit waits.

Assume that with about 50 requests in the batch, the server writes about 1,500 tokens a second in total, about 30 a second for each request. Each answer is a little slower than alone, about 10 seconds for 300 tokens instead of 8, but the GPU now finishes about 5 answers a second, 18,000 an hour. At US$2.50 an hour that is about 0.014 cents per answer at full use: a fortieth of the cost of one request at a time, and about a sixty-fifth of the API's.

The price is paid in waiting, a little at a time. Each request's tokens come more slowly, because every step now reads 50 KV caches as well as the weights. A new request's prompt is read in the same steps as everyone else's tokens, so a long prompt joining the batch can slow those steps for all of them. And a request that arrives while the KV cache is full waits for a place, which users see as a longer time to first token. Where the batch limit sits is a trade-off between the cost of an answer and how long each person waits, and the right point moves with the traffic.

A trade-off that moves has to be watched, so the inference server now reports to a metrics system: time to first token, including its slow tail at the 95th percentile (Latency percentiles & tail latency), tokens per second for each request, requests in the batch, KV-cache use, requests waiting, and the GPU's busy percentage. That last one is the most quoted and the least useful on its own: it says that something ran on the GPU, not how much of the chip it used. A full KV cache with a growing line of waiting requests says the GPU is at its limit; a half-empty batch with a fast first token says there is room. These readings are for the engineers who tune the batch limit and the ones on call; nothing in the drawing acts on them automatically.

Stage 3 of 5: Batch the requests, watch the trade-off
Batch the requests, watch the trade-offA metrics box joins the drawing beside the inference server, which reports to it. Product services still receive a token stream from the inference server, and the model hub still supplies its weights.token streamweightsProduct servicesRuns the business logicSERVICES & APISInference serverAccelerated computePLATFORM & RUNTIMEMetricsGeneric metrics collectorGeneric metricscollectorPLATFORM & RUNTIMEModel hubModel hub / inferenceEXTERNAL INTEGRATIONS
Batch the requests, watch the trade-offA metrics box joins the drawing beside the inference server, which reports to it. Product services still receive a token stream from the inference server, and the model hub still supplies its weights.token streamweightsProduct servicesRuns the business logicSERVICES & APISInference serverAccelerated computePLATFORM & RUNTIMEMetricsGeneric metrics collectorGeneric metricscollectorPLATFORM & RUNTIMEModel hubModel hub / inferenceEXTERNAL INTEGRATIONS

Continuous batching happens inside the inference server; what is new in the drawing is the means to watch it.

Learn moreGPU & accelerated computeObservability: metrics, logs & alerting

The cost of one answer, at full use: about 0.014 cents. The catch is “at full use”. One GPU can now write 18,000 answers an hour; the company's average is about 83,000, so on paper five GPUs would carry everyone. What they would spend their time doing depends on the hour, and the next section is about the hour.

Ten in the morning, three times the traffic

All four features move to the open model, so the model now runs on several GPUs, each with its own inference server and its own copy of the weights in memory. These are replicas: any of them can answer any request, and something has to choose one for each request. The usual first answer, round robin, works badly here, because requests differ in size by a factor of a hundred: a tag reads a couple of hundred tokens and writes ten, a summary of a long thread can read 20,000. Round robin counts requests, so one replica can collect the long summaries while another sits nearly idle. The router sends each request to the replica with the least work outstanding, measured in tokens in flight or KV-cache use as each server reports it, not in requests. Load balancing & horizontal scaling covers the strategies; this is the least-loaded one, with load counted in tokens.

The first fleet is the five replicas the average asked for. At ten in the morning the busy hours arrive: about 250,000 calls an hour against room for 90,000. The line of waiting requests grows by about 160,000 an hour, and within ten minutes a draft's first token takes more than a quarter of an hour. At three the next morning, the same five replicas are asked for about 8,300 answers an hour and are more than 90% idle.

So size for the busy hours instead. A replica run at its full 18,000 answers an hour always has requests waiting for a place in its batch, and the wait climbs steeply as it nears full. So plan each one at about 60% of that, say 11,000 an hour. The busy hours then need 23 replicas; the ten hours near the average need 9; the night needs one by traffic, and gets two, so that a single failure at three in the morning does not leave nothing answering. A fixed fleet of 23 costs about US$1,380 a day, US$41,400 a month, about 0.07 cents per answer, and every night it is about 98% idle.

An autoscaler adds replicas when they are needed and removes them when they are not, between a minimum of two and a maximum of 30, a ceiling set by the budget. Its signal comes from the router, which sees every request: how many are waiting, and the time to first token it measures as the streams pass through. It acts on the waiting line first, which grows before anyone feels it; time to first token is already bad by the time it climbs. The replicas it starts are the box called Added replicas.

Following the day, 23 replicas for the four busy hours, 9 for the ten average ones and 2 at night, the fleet runs about 210 GPU-hours a day instead of 552. That includes starting each step on a schedule about half an hour before its traffic arrives, since the busy hours come at the same time every working day. About US$530 a day, US$16,000 a month, about 0.027 cents per answer: under twice a fully used GPU's 0.014 cents, the price of headroom, the schedule and the warm minimum, and about a thirty-fourth of the API's 0.9 cents. Against the fixed 23, the autoscaler saves about US$25,000 a month, about three fifths of the GPU bill. It also leaves room above the plan: a launch that adds half the busy-hour traffic, 375,000 calls an hour, would need 35 replicas at 60%; the ceiling of 30 carries it at about 70%, with slower first tokens, and beyond that requests wait.

It has one problem that most autoscalers do not: a new replica is useless for minutes, and every morning it has to add about fourteen of them at once before the busy hours. The cloud has to find a free GPU machine and start it, which takes minutes, and sometimes the region has none to give. The server then downloads 60 GB of weights; from the public hub, over the internet, that takes several minutes more, at a speed that depends on a site the company does not control. Then the weights load into GPU memory. GPU & accelerated compute covers scaling a pool whose machines take minutes to arrive. The schedule and acting early on the waiting line are part of the answer; here are the other three:

  • A copy of the weights next to the GPUs. The weights are copied once from the hub into object storage in the same region, the Weights copy box, and every replica loads them from there, in a minute or two. The hub is no longer on the path of starting a replica, and every replica loads exactly the version that was evaluated. Model serving: inference, versions & safe rollout covers pinning versions and rolling out a new one.
  • A warm minimum. Two replicas always run, so the night is carried by machines that are already warm. A surge nobody scheduled for still meets a growing line, and then first-token timeouts, for the minutes a new replica takes.
  • Draining before removing. When the autoscaler removes a replica, the router first stops sending it new requests, and the replica finishes the streams it holds before it shuts down. Removing it at once would cut up to 50 answers in the middle.
Stage 4 of 5: Replicas, a router and an autoscaler
Replicas, a router and an autoscalerProduct services now send their requests to a router, which spreads them between the inference server and the added replicas; the direct token stream edge from product services to the inference server is gone. The router reports to an autoscaler, which starts and stops the added replicas. Both the inference server and the added replicas report to metrics. The model hub now feeds a weights copy in object storage, which loads the weights into the inference server and the added replicas; the direct weights edge from the hub to the inference server is gone.Product servicesRuns the business logicSERVICES & APISRouterTraffic RouterPLATFORM & RUNTIMEAutoscalerScales instances on demandScales instances ondemandPLATFORM & RUNTIMEInference serverAccelerated computePLATFORM & RUNTIMEMetricsGeneric metrics collectorGeneric metricscollectorPLATFORM & RUNTIMEAdded replicasAccelerated computePLATFORM & RUNTIMEModel hubModel hub / inferenceEXTERNAL INTEGRATIONSWeights copyS3 / BlobPLATFORM & RUNTIME
Replicas, a router and an autoscalerProduct services now send their requests to a router, which spreads them between the inference server and the added replicas; the direct token stream edge from product services to the inference server is gone. The router reports to an autoscaler, which starts and stops the added replicas. Both the inference server and the added replicas report to metrics. The model hub now feeds a weights copy in object storage, which loads the weights into the inference server and the added replicas; the direct weights edge from the hub to the inference server is gone.Product servicesRuns the business logicSERVICES & APISRouterTraffic RouterPLATFORM & RUNTIMEAutoscalerScales instances on demandScales instances ondemandPLATFORM & RUNTIMEInference serverAccelerated computePLATFORM & RUNTIMEMetricsGeneric metrics collectorGeneric metricscollectorPLATFORM & RUNTIMEAdded replicasAccelerated computePLATFORM & RUNTIMEModel hubModel hub / inferenceEXTERNAL INTEGRATIONSWeights copyS3 / BlobPLATFORM & RUNTIME

Product services no longer talk to a GPU: every request goes through the router.

Learn moreLoad balancing & horizontal scalingGPU & accelerated computeModel serving: inference, versions & safe rollout

What happens when a replica dies in the middle of an answer? With batching it is never one answer: every stream that replica held, up to 50, stops at once, each after however many tokens it had written. Generation cannot simply carry on elsewhere, because the KV cache it was using died with the GPU; in this design the request runs again on another replica, from the beginning. If no token has reached the caller yet, the router does that itself, and the only sign is a slower first token. Once tokens have gone out, the router cannot start over quietly, because the caller would receive a new answer glued to the end of half an old one. So the stream ends without its final event, the product service throws the partial text away, and it asks again. Asking again is safe, because writing text changes nothing anywhere. It is not free: the prompt and every token written before the break are paid for twice, and 50 broken streams retried at the same moment land on the replicas that are left. So each request is retried once at most (Retry covers caps and backoff). Replacing the dead machine is the job of the platform's health checks, not of the autoscaler; until it is back, the other replicas carry its load.

And when the model hub is down or slow? Since the weights copy, a new replica does not notice. Before it, a scale-up during a hub outage would have started GPU machines that could not load a model, billed by the hour all the same. If the weights copy itself is unavailable, the running replicas keep serving from GPU memory; only new ones cannot start until it is back.

The router is now on every request, so it runs as at least two instances; when one dies, the streams passing through it break and are treated as incomplete, and new requests go through the other. If the autoscaler stops, the fleet simply stays the size it was. The metrics system is not on the request path: when it is down, answers carry on, and so does autoscaling, which reads the router. What the GPUs do most of the time now: through the day, writing about 60% of the answers they could, the rest kept as headroom; at night, two replicas run about a quarter full.

A million old tickets on a Monday morning

The insights team wants three years of trends, and for that every old ticket needs a short summary with topic tags, about the size of an average call: a million of them. At ten on a Monday morning, as the busy hours start, an engineer starts a script that sends them, keeping 10,000 requests open at a time. The router does what it was built for and spreads them over the replicas. The 23 replicas hold about 1,150 requests in their batches; the rest wait. A draft request joins the back of that line, and its first token arrives after more than a minute instead of under a second. The autoscaler sees the line and starts replicas up to its maximum of 30, minutes later; the draft still waits about a minute, because the script refills the line as fast as it drains. The company is now buying GPUs in the busiest hours so that a job nobody needed today can finish sooner.

More GPUs is not the fix; deciding whose work goes first is. Every call now goes through an LLM gateway in front of the router, the one way in for all four teams, and the product services no longer reach the router directly. The gateway knows which team sent each call, and whether a person is waiting for it: drafts and search answers are interactive, summaries and tags are batch. It does two things with that:

  • Quotas in tokens, per team. Each team gets a share of the fleet in tokens per minute, not in requests, because one request can be a hundred times the size of another; input and output tokens counted together are only a rough measure of GPU time, since writing a token costs far more than reading one. The gateway charges a call before sending it, its input tokens plus the most output it may write, and corrects the charge when the stream ends; Serving LLMs: gateways, fallback & token budgets covers how such a budget is kept. A team past its quota gets an immediate “slow down, retry later” answer, HTTP 429, instead of a place at the back of a line. That is the “within quota” on the gateway's edge to the router.
  • Interactive first, batch only into spare room. Interactive calls within quota go straight to the router. A batch call is admitted only while the router reports spare room, few requests waiting and free KV cache; otherwise the gateway refuses it with the same 429 and a time to retry. The gateway keeps no queue of its own: the backfill script holds its backlog, backs off and sends again. The router keeps a fifth of each replica's batch for interactive calls, never lent to batch work, and gives them the next free place first. That shortens how long a draft waits behind the backfill; how long it actually waits under load is the time to first token the metrics watch. Priority Queue covers the idea of letting urgent work overtake routine work. The scheduled levels, 23, 9 and 2, stay sized for all four features' daily traffic, so the cost and the spare room below still hold; only the reactive scale-up above them now counts interactive calls alone, so the backfill's backlog does not buy GPUs.

The same Monday, a change to the login page confuses customers, and in one hour the search assistant is asked some 30,000 times, in hundreds of wordings, how to reset a password. Every one of those questions takes a place in a batch to write an answer the fleet has already written. So the gateway asks a semantic cache first. It keeps each answer with the meaning of its question, as a vector, and returns it for a new question that is close enough, without a place in the fleet's batches. It is scoped per team and per customer business, never shared between them, because two businesses' help centres can give different answers to the same question; its key includes the model version and the version of that customer's help articles, so neither an upgrade nor an edited article serves an old answer. The drafter and the summaries gain almost nothing from it: their prompts contain a ticket, and tickets do not repeat. Two questions can be close in meaning and differ in exactly the word that matters; the RAG chatbot post linked at the end covers those false matches and stale answers, and this cache takes the same precautions.

Stage 5 of 5: A gateway decides whose work goes first
A gateway decides whose work goes firstThe final architecture. Product services now send every call to an LLM gateway, which checks a semantic cache and passes calls to the router along an edge labelled within quota; the direct edge from product services to the router is gone. The rest is unchanged: the router spreads requests between the inference server and the added replicas and reports to the autoscaler, both GPU boxes report to metrics, and the weights come from the weights copy, which the model hub fills.within quotaProduct servicesRuns the business logicSERVICES & APISLLM GatewayModel routing / fallbackSERVICES & APISSemantic CacheCache by meaningDATA STORESRouterTraffic RouterPLATFORM & RUNTIMEAutoscalerScales instances on demandScales instances ondemandPLATFORM & RUNTIMEInference serverAccelerated computePLATFORM & RUNTIMEMetricsGeneric metrics collectorGeneric metricscollectorPLATFORM & RUNTIMEAdded replicasAccelerated computePLATFORM & RUNTIMEModel hubModel hub / inferenceEXTERNAL INTEGRATIONSWeights copyS3 / BlobPLATFORM & RUNTIME
A gateway decides whose work goes firstThe final architecture. Product services now send every call to an LLM gateway, which checks a semantic cache and passes calls to the router along an edge labelled within quota; the direct edge from product services to the router is gone. The rest is unchanged: the router spreads requests between the inference server and the added replicas and reports to the autoscaler, both GPU boxes report to metrics, and the weights come from the weights copy, which the model hub fills.within quotaProduct servicesRuns the business logicSERVICES & APISLLM GatewayModel routing / fallbackSERVICES & APISSemantic CacheCache by meaningDATA STORESRouterTraffic RouterPLATFORM & RUNTIMEAutoscalerScales instances on demandScales instances ondemandPLATFORM & RUNTIMEInference serverAccelerated computePLATFORM & RUNTIMEMetricsGeneric metrics collectorGeneric metricscollectorPLATFORM & RUNTIMEAdded replicasAccelerated computePLATFORM & RUNTIMEModel hubModel hub / inferenceEXTERNAL INTEGRATIONSWeights copyS3 / BlobPLATFORM & RUNTIME

The final architecture.

Learn moreServing LLMs: gateways, fallback & token budgetsMulti-tenancyPriority Queue

Now the backfill runs in the room the fleet already has. The headroom kept through the day, the gap between 60% and a full batch, and the night's two warm replicas add up to room for about 1.6 million more answers a day on GPUs that are running anyway; the gateway lets batch work fill it, so the million tickets take about a day. On the API they would have cost about US$9,000, a million calls at 0.9 cents. Each of the day's 2 million calls still costs about 0.027 cents, and the same US$530 a day now writes the backfill as well. The interactive answers pay a little for it: a full batch writes each request's tokens at about 30 a second rather than faster.

The cache's share is smaller. Assume the search assistant sends about a quarter of the calls, 500,000 a day, and one question in three is a close rewording of one already answered: about 170,000 calls a day never reach a GPU, most of them in the busy hours. That is a modest saving next to what batching did, bought with a vector lookup on every search question and a small risk of a wrong match.

What happens when the autoscaler cannot get GPUs? On some mornings the region has none to rent. Say the morning ramp stalls at the 9 replicas of the average hours: at full batch they write about 162,000 answers an hour against 250,000 asked. The gateway decides who waits. Batch calls are refused first, so summaries and tags fall behind by hours, which they can afford. If interactive traffic alone still outgrows the fleet, the gateway answers the excess with a quick 503 and a time to retry rather than letting everyone wait a minute: a clear “busy, try again” in the drafter is better than a box that fills in sixty seconds.

The gateway is now on every call, so it runs as several instances, and their quotas have to agree: the counters live in a small shared store that every instance updates. If that store is unreachable, each instance keeps admitting interactive calls up to its own share of each quota, and refuses batch calls until the store is back. An instance that dies breaks the streams it carried, which are treated as incomplete, and new calls go through the others. When the cache is down, every call goes to the GPUs: slower in the busy hours, but no answer is wrong because of it. The AI agent post's gateway also sends calls to a second provider when the first fails; this one could do the same with the old managed API for interactive overflow, and that post covers what a fallback costs.

What the GPUs do most of the time now: through the day, interactive answers first, with batch work filling the headroom; at night, the backfill.

The cost of one answer, stage by stage

  • The managed API: about 0.9 cents.
  • One GPU, one request at a time: about 0.56 cents if it is never idle; about 1.7 cents for the 556 GPUs that could take the busy hours.
  • Streaming: unchanged. It shortens the wait, not the work.
  • Continuous batching: about 0.014 cents at full use.
  • Replicas, a router and an autoscaler: about 0.07 cents for a fixed fleet sized for the busy hours; about 0.027 cents when the fleet follows the day.
  • A gateway with quotas, priority and a cache: still about 0.027 cents for each of the day's 2 million calls, and a backfill of a million tickets runs in room already paid for.

None of these figures includes the people who run the fleet, the machines under the router, the gateway and the metrics, storage or network; with the API, running the model was inside the per-token price. At this traffic the gap, more than US$500,000 a month, leaves room for them. At a hundredth of the traffic, 20,000 calls a day, it would not be: the two warm replicas alone would cost about 0.6 cents per answer, close to the API's price before anyone is paid to run them. An idle GPU is the most expensive way to answer nothing.

Why each box is in the drawing

  • Product services: the four features. They mark each call interactive or batch, back off when the gateway says so, and treat a stream without its final event as unfinished.
  • LLM gateway: the one way in. Per-team quotas in tokens, interactive calls first, and batch calls admitted only into spare room.
  • Semantic cache: answers repeated questions without the fleet, scoped per team and per customer business, keyed by model version and help-article version.
  • Router: sends each request to the replica with the least work in tokens, retries elsewhere before the first token, drains replicas being removed, and reports the waiting line and time to first token.
  • Autoscaler: follows the day between two and 30 replicas, on a schedule for the known busy hours and early on the interactive waiting line, because a new replica takes minutes.
  • Inference server: one GPU running continuous batching, about 50 requests at a time, streaming each token as it is written.
  • Added replicas: the same, started and stopped by the autoscaler.
  • Metrics: time to first token, tokens per second per request, batch size, KV-cache use and the waiting line, read by the engineers who tune the batch limit and the ones on call.
  • Model hub: where the open-weight model comes from, visited once per model version.
  • Weights copy: object storage in the same region, where every replica loads the evaluated weights in a minute or two.

Outside this drawing

  • Quantization. Storing the weights at 8 or 4 bits instead of 16 halves or quarters the 60 GB, so each step reads less and more memory is left for KV cache and a bigger batch. It can lower the quality of the answers, which has to be measured with the same evaluation as the model itself.
  • Models too big for one GPU. A larger model is split across several GPUs that exchange data at every step (tensor or pipeline parallelism). A 30-billion-parameter model at 16 bits avoids that here.
  • Speculative decoding. A small model proposes several tokens and the large one checks them in one pass, which speeds up each request's tokens at the cost of a second model to run and tune.
  • A fallback to a managed API. Sending interactive overflow to the old provider would bring back the per-token bill only for the overflow; the AI agent post builds a fallback and its tests.
  • Several regions. One region is enough for this company's users and its latency budget.
  • Choosing, fine-tuning or upgrading the model. Picking the model and judging a new version is evaluation work; rolling a new version out across replicas is in Model serving: inference, versions & safe rollout.

Build it yourself

Now draw it yourself. The Self-Hosted LLM Inference Challenge asks for this platform, requirement by requirement: weights pulled from a model hub onto GPU inference servers, tokens streamed, requests batched, a GPU pool autoscaled on queue depth and latency that scales idle replicas down, a semantic cache, one gateway with per-tenant quotas (the teams, here), and dashboards for GPU utilisation, latency and throughput. Then put the Monday morning through your drawing: write on the gateway's edge what the quota counts and what happens to a batch call when there is no room, on the autoscaler its signal and its warm minimum, and on the weights' edge where a new replica loads them from and how long that takes. Its palette names some boxes differently: Hugging Face for the model hub, GPU Compute for each inference server, a load balancer for the router, and Prometheus with Grafana where this post has one metrics box, so add the dashboards this post leaves out as Grafana reading from Prometheus. There is no box for the product services, so the gateway is where traffic enters, and its quotas sit in front of every model, where this post serves one.