Sub topic 1
Why Processing a Long Prompt Is Expensive
Prerequisites
This chapter assumes you have:
- Called an LLM API at least once and seen a request and a response.
- A working idea of tokens (the pieces of text a model reads) and the context window (the maximum number of tokens a model can handle in one request).
- Enough Python to read a short snippet.
You do not need to know how transformers work internally. The chapter gives only the part you need.
Two phases of every request
When you send a prompt, the model does its work in two phases.
- Prefill. The model reads every input token and builds an internal representation of them. All input tokens are known up front, so this phase can process many of them at once on the GPU.
- Decoding. The model produces output tokens one at a time. Each new token depends on the one before it, so this phase cannot be parallelized across the output.
Prefill happens before the first output token appears. A long prompt therefore delays the first token. The time from sending the request until that first token arrives is called time to first token (TTFT).
Why prefill grows with prompt length
During prefill, the model uses a mechanism called attention. For each token, attention compares that token with the tokens before it, so the model can decide which earlier tokens matter. Two costs follow from this:
- Every token must pass through all the model's layers. Work grows at least in proportion to the number of tokens.
- Attention compares each token with earlier ones, so the comparison work grows faster than linearly as the prompt gets longer.
You do not need the exact formulas. The practical point is that a prompt of 50,000 tokens costs far more compute than one of 500 tokens, and the provider has to pay for that compute on every request.
Where the cost shows up
Prefill cost reaches you in two ways.
| Effect | What you see |
|---|---|
| Latency | A longer wait before the first token arrives |
| Price | Input tokens are billed on every request, including ones you have sent before |
The second row is the one that surprises people. Suppose you build a support assistant with a 20,000-token policy document at the start of every prompt. Each user question adds only about 50 tokens. Without caching, the model reprocesses the same 20,000 tokens on each call, and you pay for them each time.
A rough cost model
The code below estimates input cost for repeated requests with the same long prefix. The price is a placeholder. Use your provider's published price.
PRICE_PER_MILLION_INPUT_TOKENS = 3.00 # placeholder, in dollars
def input_cost(prefix_tokens, question_tokens, requests):
total_tokens = (prefix_tokens + question_tokens) * requests
return total_tokens / 1_000_000 * PRICE_PER_MILLION_INPUT_TOKENS
cost = input_cost(prefix_tokens=20_000, question_tokens=50, requests=10_000)
print(f"Input cost: ${cost:,.2f}")
repeated_share = 20_000 / (20_000 + 50)
print(f"Share of input tokens that are the same every time: {repeated_share:.1%}")With these numbers, the input cost is $601.50. About 99.8% of the input tokens are identical on every request. That repeated work is what prompt caching targets.
Common misreadings
- "A bigger context window means cheaper long prompts." The window sets what fits. It does not change the cost of processing what you put in it.
- "Only the new question costs anything." Without caching, the provider treats each request as independent. It processes the whole prompt again.
- "The model remembers the last request." The model keeps no memory between calls. Reuse has to be built deliberately, and the next chapter explains what the provider stores to make that possible.
Exercise
A request has a 30,000-token shared prefix and a 100-token question. Which phase is the main source of delay before the first output token, and why does sending the same prefix again cost the same as before when caching is not used?
Show answer
Prefill is the main source of delay, because the model must read all 30,100 input tokens before it can produce the first output token. Without caching, the provider processes each request independently, so it repeats the prefill work for the same 30,000 tokens and bills them again.
Exercise
Using the placeholder price of $3.00 per million input tokens, what is the input cost of 1,000 requests that each have a 10,000-token prefix and a 200-token question?
Show answer
Total tokens are (10,000 + 200) × 1,000 = 10,200,000. That is 10.2 million tokens, so the cost is 10.2 × $3.00 = $30.60.