Sub topic 13
Cost and latency trade-offs
This chapter assumes you can write basic Python, have called an LLM through an API, and know that models read and write tokens. It also assumes you have seen the three strategies from earlier chapters: single-pass, map-reduce and refine. Here you will estimate what each one costs and how long it takes, so you can choose before you build.
What you pay for
Most APIs charge per token, with a separate price for input tokens (what you send) and output tokens (what the model writes). Output tokens usually cost several times more than input tokens. Latency follows a similar pattern. The model reads the input quickly, but it writes the output one token at a time.
Two quantities drive almost everything:
- Total tokens processed: the sum of input and output tokens over all calls.
- Critical path length: the number of calls that must run one after another.
Cost follows the first quantity. Wall-clock time follows the second.
Estimating each strategy
Let a document have D tokens, and let each chunk have C tokens. The number of chunks is about D / C. Let each partial summary have S tokens.
| Strategy | Input tokens | Output tokens | Sequential calls |
|---|---|---|---|
| Single-pass | D | S | 1 |
| Map-reduce | D + (D/C) × S | (D/C) × S + S | 2 or more (map in parallel, then reduce) |
| Refine | D + (D/C) × S | (D/C) × S | D / C |
Map-reduce reads the document once, then reads all the partial summaries. Refine sends the running summary with every chunk, so it also reads the summary many times. Both cost somewhat more than single-pass, because each call repeats instructions and partial summaries. The big difference is time. Map calls are independent, so you can run them at the same time. Refine calls depend on each other, so they cannot.
def estimate(doc_tokens, chunk_tokens, summary_tokens, in_price, out_price):
"""Prices are per 1,000,000 tokens. Returns input tokens, output tokens, cost."""
n = -(-doc_tokens // chunk_tokens) # ceiling division
map_in = doc_tokens + n * 50 # 50 tokens of instructions per call
map_out = n * summary_tokens
reduce_in = map_out + 50
reduce_out = summary_tokens
total_in = map_in + reduce_in
total_out = map_out + reduce_out
cost = total_in / 1e6 * in_price + total_out / 1e6 * out_price
return total_in, total_out, cost
print(estimate(200_000, 3_000, 300, in_price=3.0, out_price=15.0))For a 200,000-token document, this prints about 220,000 input tokens, 20,000 output tokens and a cost of roughly $0.96 at the example prices. The prices are placeholders. Replace them with your provider's current rates.
Parallelism and its limits
Running map calls concurrently cuts latency from n × call time to roughly call time, plus the reduce step. Two limits apply:
- Rate limits. Providers cap requests and tokens per minute. Too many parallel calls trigger errors, so use a small worker pool and retry with backoff.
- Reduce depth. If the combined partial summaries still exceed the context window, you add another round. Each round adds one call's worth of latency.
Ways to reduce cost
- Use a cheaper model for the map step. Partial summaries are short and simple. Reserve the stronger model for the final combine.
- Cap output length. Set a word or token limit in the prompt and in the API call. Output tokens are the expensive ones.
- Use larger chunks. Fewer chunks mean fewer repeated instructions and fewer partial summaries. Do not exceed the length where quality drops, as covered in the long-context chapter.
- Cache results. Store each chunk summary keyed by a hash of the chunk text. When the document changes, only changed chunks are summarised again.
- Skip what you do not need. Remove boilerplate such as headers, footers and repeated legal text before chunking.
Choosing
- Document fits comfortably in the window: use single-pass.
- Latency matters and sections are fairly independent: use map-reduce with parallel map calls.
- Order and narrative matter, and you can wait: use refine.
- Very long input: use hierarchical summarisation and budget for the extra rounds.
Exercise
A 120,000-token report is split into 4,000-token chunks. You can run 10 calls in parallel, and each call takes about 8 seconds. Roughly how long does the map step take with map-reduce, and how long would refine take for the same chunks?
Show answer
There are 30 chunks. With 10 parallel calls, the map step runs in 3 batches, so about 24 seconds, plus the reduce call. Refine runs the 30 calls one after another, so about 240 seconds (4 minutes). Both strategies process a similar number of tokens, so cost is close. Parallelism accounts for the difference in time.