Topic 5 of 5 · 5 min read
Capping output length and growing history
Goal: Find the biggest cost driver in a usage log and cut it with output caps, effort settings or trimmed history.
Most of an AI bill comes from tokens you did not mean to send or receive: answers longer than needed and history that grows every turn. Find the biggest line in the usage log, then cap it.
Check yourself · 1 of 3
Why does a 30-turn chat cost far more per turn at the end than at the start?
Why it matters
Output tokens cost more than input tokens, so a rambling answer is expensive. History is worse: a chat or agent resends every earlier message on every turn, so total input grows roughly with the square of the number of turns. A 20-turn agent run can send the first message 20 times.
How to do it
- Read the usage log by feature and find the largest cost line: output, history, or a big retrieved document. Fix that one first.
- Cap output with
max_tokens(or the provider's equivalent) at a value above a normal answer. This is a safety net: a capped answer is cut off mid-sentence, so also ask for brevity in the prompt ("answer in at most 3 sentences", "return only the JSON"). - Ask for a compact format: JSON with short keys, a label instead of an explanation, a number instead of a paragraph.
- Set the effort or thinking budget where the model supports it. Lower effort means fewer reasoning tokens, which are billed as output. Use high effort only for tasks that fail without it.
- Trim history: keep the last few turns verbatim, replace older turns with a short summary, and drop tool results once they have been used. In an agent loop, clear old tool outputs.
- Combine with caching: a stable history prefix can be cached, but trimming still reduces what you write to the cache.
Before and after
Weak: a support bot sends the full 30-message history and replies in 400-token essays to yes/no questions. Average cost per turn doubles every ten turns.
Fixed: the last 6 messages plus a 150-token summary of earlier ones, max_tokens of 250, and a prompt line asking for short answers. Cost per turn stays flat as the conversation grows.
What goes wrong
- Setting
max_tokensvery low as the only control, so answers end mid-sentence and users retry, costing more. - Summarising history with a large model, so the saving is eaten by the summary call.
- Dropping history that the next turn needed, such as the user's earlier constraint.
- Leaving high reasoning effort on for every request, including trivial ones.
Examples
Reading a usage log for the biggest driver
[
{ "turn": 1, "input_tokens": 900, "output_tokens": 420 },
{ "turn": 10, "input_tokens": 6100, "output_tokens": 410 },
{ "turn": 20, "input_tokens": 12300, "output_tokens": 430 }
]
Output is flat near 420 tokens, but input has grown more than ten times. History is the driver, so trimming it beats shortening the answers.
Capping output and trimming history
const recent = history.slice(-6);
const res = await client.messages.create({
model,
max_tokens: 250,
system: "Answer in at most three sentences.",
messages: [{ role: "user", content: `Earlier summary: ${summary}` }, ...recent],
});
The summary stands in for older turns. The prompt asks for brevity, and max_tokens is only a backstop.
Key points
- Read the usage log first and fix the largest cost line, not the one that is easiest to change.
- A max_tokens cap is a safety net: pair it with a prompt that asks for a short answer, or replies get cut off mid-sentence.
- Conversation and agent history is resent every turn, so input cost grows quickly with the number of turns.
- Lower reasoning effort cuts hidden reasoning tokens, which are billed as output.
- Trim history by keeping recent turns, summarising older ones and clearing used tool results.
Common mistakes
- Using max_tokens as the only control, so answers are truncated and users retry.
- Sending the entire conversation or agent trace on every turn.
- Summarising history with an expensive model on every turn.
- Keeping maximum reasoning effort on for simple requests.
Check yourself · 2 of 3
You set max_tokens to 50 to cut cost, and users now see answers that stop mid-sentence. What is the better fix?
Check yourself · 3 of 3
An agent run sends 40 tool results back to the model on every step, and only the latest two are still needed. What is the best way to cut cost?
Practice items, not counted towards your mastery.
Start here
- Claude API pricing (opens in a new tab)DocsFree
Per-million-token prices, the cache write and read multipliers and the batch discount in one table.
- Prompt caching (Claude docs) (opens in a new tab)DocsFree
Cache breakpoints, the 5-minute and 1-hour lifetimes and the usage fields that report writes and reads.
- Batch processing (Claude) (opens in a new tab)DocsFree
How the Message Batches API works and what it costs compared with standard calls.
More for this topic
- Token counting (Claude docs) (opens in a new tab)DocsFree
Counting the input tokens of a request before sending it.