Cost and latency — study guide
The concept's fragments, read in order.
For your capstone An agent that does a task you currently do by hand
Cost and latency decide whether the pipeline beats doing the job yourself. Every request carries a token count, a cost and a latency per step, and because the draft step loops, resending its whole transcript each turn, its share of the bill grows faster than its number of steps.
The sort step reads 1,200 input tokens and writes 20, routine work, so it runs on a smaller model, kept only after it held up on your evaluation set. The draft step's instructions, examples and tool definitions sit unchanged at the top of every call, with the request id moved to the end so the cached prefix survives. The night's requests have nobody waiting on their labels, so they go to the provider's batch interface as one job, collected in the morning.
Every call has a meter
A program that calls a language model pays for every call twice: once in money and once in waiting time. Both bills are counted in the same unit, the token, which is the word piece a model reads and writes. A call that sends more tokens or asks for more tokens back costs more and, in most cases, takes longer. One call is cheap and quick enough that nobody notices either bill. A pipeline that makes thousands of calls a day, or an agent that makes dozens of calls for one task, is where both bills become numbers someone has to plan around.
The economics come down to a handful of facts. Tokens going in and tokens coming out are priced and timed differently. An agent that resends its whole transcript on every turn pays for the same tokens again and again, so its cost climbs faster than its step count. The wait before the first token appears is a different number from the time the whole answer takes. And providers cap how many requests and tokens an account can push through per minute.
Against those sit a few levers: caching the part of the input that repeats, batching work that nobody is waiting for, choosing a smaller or larger model for each step, and a budget the code itself enforces. None of them needs exotic engineering. They need the arithmetic done before the pipeline ships. Every price, speed and limit used to work that arithmetic in this concept is illustrative, because real ones differ by provider and change often; the relationships between them are what carry over.
Tokens in, tokens out
A model call is metered in tokens, the word pieces a model reads and writes. For ordinary English prose, one widely repeated rule of thumb from one provider's tokenizer is about four characters, or three quarters of a word, per token, so 100 tokens is roughly 75 words. Code, long numbers and other languages can need more tokens for the same content, and every model family splits text its own way, so an exact count comes only from that model's tokenizer. Prices are usually quoted per million tokens.
Every call has two token counts, and they are billed separately. Input tokens are everything sent: instructions, examples, the conversation so far, tool results. Output tokens are what the model writes back. Output tokens are generally the more expensive kind, commonly several times the input price, roughly 2 to 8 times in published lists, so check the real ratio for the model you use rather than assuming one.
The price gap partly follows a difference in the work. A model reads its whole input together in one parallel pass, often called prefill. It writes output one token at a time, often called decode: each new token is fed back in before the next one can be produced, and those small sequential steps are commonly limited by how fast the model's weights can be moved through memory rather than by arithmetic. So the same split shows up in time as in money: a longer prompt mostly delays the start of the answer, while every extra output token adds another step to the end.
One illustrative call puts numbers on it. The prices used throughout this concept, $3 per million input tokens and $12 per million output tokens, are made-up round numbers chosen for easy arithmetic, not any provider's price. At those prices, a call that sends 3,000 tokens and gets 500 back costs 3,000 x 3 / 1,000,000 = $0.009 for input and 500 x 12 / 1,000,000 = $0.006 for output, $0.015 in all. The output is a seventh of the tokens and two fifths of the bill.
Why the bill outgrows the steps
A model keeps no memory between calls. An agent loop gets around that by sending the whole transcript every turn: the instructions and task, then every earlier plan, tool call and tool result, then the new request. So each turn's input is the previous turn's input plus whatever the last turn added, like a storyteller who, before adding each new sentence, has to recite the whole story so far from the beginning: by the tenth sentence, nearly all the effort goes into the retelling.
Take an illustrative run. The starting prompt is 2,000 tokens, and each turn adds 500 more: the model's own output plus the tool result that comes back. Turn 1 sends 2,000 input tokens, turn 2 sends 2,500, turn 3 sends 3,000, and turn 10 sends 2,000 plus 9 times 500, or 6,500. In general turn k sends P + a(k - 1) tokens, where P is the starting prompt and a is what each turn adds. Summing over n turns gives the total input: n times P, plus a times n(n - 1)/2. For ten turns that is 10 x 2,000 + 500 x 45 = 20,000 + 22,500 = 42,500 input tokens, and adding the ten turns one by one gives the same: 2,000 + 2,500 + 3,000 + 3,500 + 4,000 + 4,500 + 5,000 + 5,500 + 6,000 + 6,500 = 42,500.
The n(n - 1)/2 term is what makes the bill outgrow the steps. Double the run to twenty turns and the input total is 20 x 2,000 + 500 x 190 = 40,000 + 95,000 = 135,000 tokens: twice the steps, a little over three times the input (135,000 / 42,500 = 3.18). The new work per turn stayed the same; the resending grew. How close doubling the turns gets to four times the input depends on how large the starting prompt is compared with what each turn adds: the larger the start, the smaller the multiple.
Prices turn that into money. At the illustrative $3 per million input tokens and $12 per million output tokens, with 200 of each turn's 500 added tokens being model output, the ten-turn run costs 42,500 x 3 / 1,000,000 = $0.1275 for input plus 2,000 x 12 / 1,000,000 = $0.024 for output, $0.1515 in all. The twenty-turn run costs 135,000 x 3 / 1,000,000 = $0.405 plus 4,000 x 12 / 1,000,000 = $0.048, or $0.453, nearly three times as much. Output is the pricier token, but in a long loop the resent input is most of the bill.
First token, last token
A model call has two latencies, not one. Time to first token is how long the caller waits before anything comes back: the network trip, any wait in the provider's queue, and the prefill pass that reads the whole input, so it grows as the input grows. After that, the remaining tokens arrive at the model's generation speed, one decode step each. The total time is the first-token wait plus the remaining tokens divided by the generation speed, like a meal served in courses: the first plate can arrive quickly while the kitchen is still cooking the rest, but the meal as a whole takes as long as it takes. These are the same two dimensions as latency and bandwidth on a network link: how soon the first piece shows up, and how fast the rest follows, each with its own causes.
With illustrative numbers, a call that waits 0.5 seconds for its first token and then produces 400 more tokens at 50 tokens per second takes 0.5 + 400 / 50 = 0.5 + 8 = 8.5 seconds. Here the long answer, not the input, is what makes the call slow: halve the output and the call takes 0.5 + 200 / 50 = 4.5 seconds.
Streaming changes when a person can start reading, not when the call ends. A streamed response sends each token as it is produced, so someone watching sees text from 0.5 seconds onward instead of staring at nothing for 8.5. A program usually cannot use half an answer, though. An agent step that has to parse a complete tool call or a complete block of structured output still waits the full 8.5 seconds, so streaming makes an interface feel faster without making a pipeline finish sooner.
In an agent loop the steps run one after another, so their times add. Ten steps like this one take 10 x 8.5 = 85 seconds of model time before any tool has run, and because the resent transcript grows every turn, each later step's first-token wait tends to grow with it. Cutting output length, and cutting the number of steps, are the levers that move the total.
Throughput and rate limits
Latency is how long one run takes; throughput is how many runs a pipeline finishes per minute. Running more of them side by side raises throughput, but only up to the ceiling the provider sets. Model APIs commonly enforce several limits at once, counted in requests per minute and tokens per minute (and often per day as well), sometimes with input and output tokens counted separately, and the limits vary by model and account. A request that would exceed any one of them is rejected.
The rejection is an HTTP 429 Too Many Requests. Providers typically add a Retry-After header saying how long to wait, and headers reporting the limit, what remains of it and when it resets, though the header names differ from one provider to the next. Read the error body as well: a 429 can also mean an account spending limit was reached, which waiting will not fix. How a pipeline waits, retries and queues work around these rejections belongs to the infrastructure that runs it; what matters here is that the ceiling exists and can be computed.
The ceiling is a division. Take an illustrative limit of 400,000 input tokens per minute and an agent run that sends 42,500 input tokens over ten turns. The account can sustain 400,000 / 42,500 = 9.41 such runs a minute, so nine, however many workers are started. If the runs grow to twenty turns and 135,000 input tokens each, the same limit sustains 400,000 / 135,000 = 2.96, fewer than three a minute. The resent transcript that makes long runs expensive also makes them use up the rate limit faster.
So a pipeline's throughput is set by its token appetite as much as by its code. Shrinking the tokens each run sends raises the number of runs the same limit allows, and a limit hit in the busiest hour looks, to whoever is waiting on the pipeline, exactly like an outage. That is why the ceiling is worth computing before the busy hour arrives.
Paying once for what repeats
Most of what an agent sends on a given turn, it already sent on the turn before. Prompt caching, offered in some form by several major model APIs, lets a provider keep the work it did reading a prompt's opening section and reuse it when a later request starts with exactly the same tokens. Reused tokens are billed as cached input at a reduced rate, often a small fraction of the normal input price, though the discount varies by provider and model.
The catch is the word exactly. The cache matches a prefix: the request has to begin with the identical tokens, in the identical order, and the first difference ends the match, like reprinting a long document in which only the last page changed: reuse the pages that are exactly the same and print only the new one, which works only if nothing earlier in the stack was touched. So the stable parts go first and the changing parts go last: instructions, examples and tool definitions at the top, then the transcript in the order it happened, then the new request. A timestamp or request id near the top of the prompt changes every call and quietly defeats the cache for everything after it. Providers also set a minimum cacheable length, at the time of writing (2026) from a few hundred to a few thousand tokens depending on provider and model, and entries expire: at the time of writing, some providers keep them for minutes and others for hours or up to a day, with each use often resetting the clock. A loop that pauses longer than that pays full price to start again.
Applied to the illustrative ten-turn run, which sends 42,500 input tokens, and assuming its 2,000-token opening is above the provider's minimum, the arithmetic is exact. Turn 1 sends its 2,000 tokens at the full rate. Every later turn finds the previous turn's whole input already cached and pays the full rate only for the 500 tokens added since: 2,000 + 9 x 500 = 6,500 full-rate tokens, and 2,000 + 2,500 + ... + 6,000 = 36,000 cached tokens, which together make the same 42,500. At the illustrative $3 per million input tokens, with cached reads at a quarter of that, $0.75 per million, input costs 6,500 x 3 / 1,000,000 = $0.0195 plus 36,000 x 0.75 / 1,000,000 = $0.027, or $0.0465 instead of $0.1275, about 64 percent less.
Two cautions keep that number honest. Some providers charge a premium to write a prefix into the cache, while others charge nothing extra or bill for storage instead, so the real saving depends on how many times each cached prefix is read back. And caching makes resent tokens cheaper without making them fewer: the transcript still grows every turn, and only trimming or summarizing it slows that growth.
Trading a wait for a discount
Not every model call has someone waiting on it. Scoring a test set overnight, labeling a backlog of documents, or summarizing yesterday's records can all finish hours later without anyone noticing. For that kind of work, several providers offer a batch interface: you submit many independent requests as one job, the provider works through them in the background, and you collect the results together later, like sending a parcel by standard post instead of by courier: it arrives later, it costs less, and it is the right choice whenever nobody is standing at the door waiting for it. At the time of writing (2026), some providers promised results within up to a day, often sooner, at a substantial discount, often around half the normal price.
The arithmetic is simple, with illustrative numbers. A nightly job sends 10,000 documents, each with 1,500 input tokens and 200 output tokens. That is 15 million input tokens and 2 million output tokens. At the illustrative $3 per million input tokens and $12 per million output tokens, it costs 15 x $3 = $45 for input plus 2 x $12 = $24 for output, $69 a night sent one request at a time, and $34.50 at an illustrative half-price batch rate. The batch route does not suit an agent's steps, though. Each step needs the previous step's answer before it can start, and a step that might take a day to return is not a step.
A second kind of batching happens inside one request: packing several small items into a single call so they share one copy of the instructions. Classifying 100 short items with 1,000 tokens of instructions and 50 tokens per item sends 100 x 1,050 = 105,000 input tokens one item at a time, but only 10 x (1,000 + 10 x 50) = 15,000 at ten items per call. Whether answers stay as good with ten items to a call is something to measure, not assume.
Providers batch on their side too. A model server combines the requests of many callers into each decode step, which keeps its hardware busy and raises total throughput until it levels off, while each individual request can wait longer to start and for its tokens. That trade, more work done per second at the price of some delay per request, is the same one the batch interface sells, just at a smaller scale.
The right model for each step
A pipeline does not have to use one model for everything. Providers sell models in several sizes, and the smaller ones are priced far lower per token, often an order of magnitude or more below a provider's largest. Because generating each token means moving the model's weights through memory, a smaller model is also often quicker per token. The question for each step is whether it needs the larger model's ability or only its price.
Many steps in a pipeline are routine: pulling a field out of a tool result, classifying a request into one of a few categories, condensing a long result before it joins the transcript. Others carry the hard part: deciding what to do next, writing the code, judging whether the task is done. Routing the routine steps to a small model and keeping the large one for the hard steps cuts cost without touching the steps where quality matters most. With illustrative numbers, a classification step run on 10,000 items of 800 input and 20 output tokens sends 8 million input tokens and gets back 200,000 output tokens. That costs 8 x $3 + 0.2 x $12 = $24 + $2.40 = $26.40 on a model priced at the illustrative $3 and $12 per million, and $2.64 on one priced at a tenth of that.
Output length is the other dial inside every step. A maximum-output setting caps how many tokens one call can produce, and since every output token is both the dearer kind and one more decode step, asking for a short, structured answer instead of an essay saves money and time together. Set the cap with room to spare, though: an answer cut off at the limit is often useless, and the retry costs more than the tokens the cap saved.
None of these swaps can be judged by intuition. A smaller model that is wrong one time in twenty on a step that used to be right can cost more in failed runs than it saves in tokens. So a swap is checked the same way any change is: run the same fixed set of examples through both versions, compare the results, and keep the cheaper one only if the results hold.
A budget the loop enforces
An agent loop decides for itself how many more calls to make, so its cost has no natural ceiling. A task that confuses it can circle for fifty turns, and because the transcript grows every turn, the late turns are the most expensive ones. A cap on output tokens bounds a single call; it does nothing about the number of calls. What bounds the run is a budget the loop itself checks, in tokens, money or both, with a plain limit on the number of turns as a backstop.
The bookkeeping is cheap because the provider does the counting. Every response carries a usage record with its input and output token counts, often with cached tokens broken out, though the field names differ between providers. The loop adds each call's usage to a running total for the run, prices it, and before every new call asks one question: could this call push the run past its budget? It answers by estimating the call's input and reserving its worst case output, the maximum-output cap, against what remains. Retries, summarizing calls and calls to other models count toward the same total, because they cost the same money.
Take the illustrative run that adds 500 tokens a turn to a 2,000-token start, at $3 per million input tokens and $12 per million output tokens, with 200 output tokens a turn, a run budget of $0.28 and a cap of 1,000 output tokens per call, so every check reserves 1,000 x 12 / 1,000,000 = $0.012 for output. After 13 turns the run has sent 13 x 2,000 + 500 x (13 x 12 / 2) = 26,000 + 39,000 = 65,000 input tokens, costing $0.195, and received 2,600 output tokens, costing $0.0312, so it has spent $0.2262. Turn 14 would send 2,000 + 13 x 500 = 8,500 input tokens, costing $0.0255, so its worst case is $0.2262 + $0.0255 + $0.012 = $0.2637, inside the budget, and the call goes ahead. After 14 turns it has sent 14 x 2,000 + 500 x (14 x 13 / 2) = 28,000 + 45,500 = 73,500 input tokens, costing $0.2205, and received 2,800 output tokens, costing $0.0336, so it has spent $0.2541. Turn 15 would send 2,000 + 14 x 500 = 9,000 input tokens, costing $0.027, and could write up to 1,000 output tokens, costing $0.012. The worst case is $0.2541 + $0.027 + $0.012 = $0.2931, over the $0.28 budget, so the loop stops before making the call.
Stopping is a designed outcome, not a crash. The loop returns what it has: the partial result, the reason it stopped, and the spend so far, so a person or a supervising process can decide whether the task deserves more. Spending limits set on the provider account are worth keeping as a separate backstop, but they act on the whole account, not on the run that is about to make its next call, and a budget alert on the account is a warning, not a stop.
Economics you can see coming
Anything that calls a model in production has these economics, whether it is a multi-step agent working through tasks or a single feature inside an app that sends one prompt per user action. The difference is only how many calls stand behind each unit of work, and in an agent that number is decided at run time by the model itself, which is why its cost and latency need watching more closely than a fixed request path does.
Most of the arithmetic can be done before anything ships. Count the tokens a typical run sends and receives, apply the transcript sum if the run loops, price it, and compare the result with what the work is worth. Add up the first-token waits and generation times of the steps that run in sequence and compare that with how long anyone will wait. Divide the provider's token limit by the run's appetite and compare that with the busiest hour's demand. Each comparison either passes or points at a lever: cache the stable prefix, batch what nobody waits for, give routine steps a smaller model, cap output, and put a budget in the loop.
Once it runs, record the usage of every call against the run it belongs to, and chart cost per run and time per run next to the pipeline's error rate. The model bill is metered in tokens and moves with what the model is asked to do, separately from the bill for the machines the pipeline runs on, so both need reading. A cost per run that creeps upward after a prompt change, or a run time that doubles when a step's output grows, is the same kind of signal as a latency regression, and it deserves the same attention.
Next on your path to An agent that does a task you currently do by hand: Orchestration infrastructure ▸