# How Do You Budget Tokens for Chat History?

> How to fit a growing chat into a model's fixed token window: what to count, what to trim and what to keep, for your AI interview.

**In short:** Budget the window before the chat grows: take out the fixed parts and room for the answer, then fit the history into what is left. Trim old tool output and older turns first, and count tokens before each request.

AI and LLM interview question Q105, Prompt and context engineering. Intermediate, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/how-do-you-budget-tokens-for-chat-history

## Say it in 30 seconds

Every model has a fixed context window, the most text it can handle in one request. Each turn, the whole chat goes back in, so it fills up. I budget it first: I take out the system prompt, the tool definitions and room for the answer, and the rest is for history. When history goes over, I clear old tool output and summarise the older turns, but keep the recent ones word for word. Then I count tokens before each request.

## Say it in 2 minutes

The context window is the total text a model can handle in one request, including its answer. Everything counts: the system prompt, the tool definitions, every earlier message and every tool result. Most chat setups send the history again on every turn, so the history is the part that grows and eats the budget.

So I budget from the fixed parts outward. I start with the system prompt and tool definitions, then reserve room for the answer, plus a safety margin. What is left is what the history can use. I count with the provider's token counting tool before sending, so the budget is checked before the request goes out.

When history goes over budget, I trim in a set order. Old tool output goes first, since a raw lookup from twenty turns back is rarely needed again. Older turns get summarised, with the facts that matter kept: the claim number, the agreed payout, the open complaint. The recent turns stay word for word. A summary that cuts too hard loses details that matter later, so I test it.

Finally, caching lowers what reused tokens cost. A cached prefix is cheaper to re-read, but it still takes up the window. My test is a long chat where the bot must still know the claim number from the first turn. If it forgets, the summary is too thin.

## What is a token budget for chat history, in plain words?

A model reads text in tokens, small pieces that are often part of a word. The context window is the most text it can handle in one request, and the answer it writes counts too.

Most chat setups send the whole conversation again on every turn. So the history grows, and at some point it cannot all fit. A token budget is the plan for what gets room and what gets cut first.

Think of a shift nurse's handover board. Only so much fits on it. The allergies and today's medication stay, and yesterday's routine notes come off first. In a claims chat, the claim number from the first turn is the allergy: it must survive the cuts.

- **Token:** The small unit a model reads, bills and limits. Often part of a word.
- **Context window:** The most text one request can hold, the answer included.
- **Reserved output:** Room kept back at the end, so the reply is not cut short.
- **Chat history:** Every earlier message and tool result that goes back in with each turn.

## How do you work out the budget, and where do the tokens go?

Start from the window and take away what is fixed. Everything in a request counts, so the system prompt and the tool definitions come off first. Then keep room for the answer and any thinking, because a reply cut short is a failure too. What is left is the history budget.

*Diagram, What goes into each request: Four parts go into each request, and the model reads them all at once. Only the chat history grows from turn to turn, so it needs the budget. The answer then joins the history for the next turn.*
1. The fixed rules go in with every request.
2. The list of tools goes in too, every time.
3. The whole history goes in again, so each turn is bigger than the last.
4. The new message joins the request for this turn only.
5. The answer is added to the history for the next turn.

1. **Start from the window:** The most text one request can hold, the answer included.
2. **Take off the fixed parts:** The system prompt and the tool definitions go in on every turn.
3. **Keep back room for the answer:** Otherwise a long reply, or the model's thinking, is cut short, which is a failure too.
4. **Fill the rest with history:** Add the newest turns first, then stop when the budget is full.

Here is a made-up claims assistant at an insurer. It has 7,000 tokens left for history after the fixed parts and the answer room. Its five turns come to 14,800 tokens, so something has to go. The code keeps the newest turns that fit and drops the rest.

```python
# A call-centre assistant with a made-up budget of 7,000 tokens for chat history.
# Each turn is a (name, tokens) pair. Walk back from the newest turn and keep turns until the budget is full.
BUDGET = 7_000
turns = [
    ("turn 1 (claim number)", 3_800),
    ("turn 2", 2_900),
    ("turn 3", 4_100),
    ("turn 4", 1_600),
    ("turn 5", 2_400),
]

def fit(turns, budget):
    kept, used = [], 0
    for name, tokens in reversed(turns):
        if used + tokens > budget:
            break
        kept.insert(0, name)
        used += tokens
    return kept, used

kept, used = fit(turns, BUDGET)
print("Budget:", BUDGET, "tokens")
print("Kept:", ", ".join(kept))
print("Used:", used, "tokens")
print("Dropped:", len(turns) - len(kept), "oldest turns")
```

Output:

```text
Budget: 7000 tokens
Kept: turn 4, turn 5
Used: 4000 tokens
Dropped: 3 oldest turns
```

Turn one held the claim number, and the code drops it first. The bot would then ask the customer for it again. That is the hard part: the newest turns fit, but a fact from turn one is needed later.

## What do you cut when the history is too long?

Cut in a fixed order, so the cheapest losses go first and the facts stay. The model needs the recent turns most, and the old raw tool output least.

*Diagram, Trimming a chat that is over budget: Trim until the whole request fits, then send it. Old raw tool output goes first, then older turns are summarised, and the recent turns always stay. The count runs before each request, so a fit is checked before it is sent.*
1. The whole chat is too big, so trimming starts.
2. After trimming, the smaller chat is counted again.
3. If it fits, the request goes to the model.

- **Drop the oldest turns: Quick, but lossy.** The claim number from turn one is gone, and the bot asks for it again.
- **Summarise and keep: Keeps the facts.** The summary holds the claim number, the agreed payout and the open complaint.

Anthropic says it plainly:

> "Everything in the request counts toward the context window: the system prompt, every message in messages (including tool results, images, and documents), and your tool definitions." (Anthropic, context window docs)
>
> "Context, therefore, must be treated as a finite resource with diminishing marginal returns." (Anthropic engineering)

Summaries are a model call too, so they cost tokens and can lose details. Anthropic's engineers warn that cutting too hard loses context that matters later, so I test each summary against the facts it has to keep.

Anthropic's compaction feature, still in beta in 2026, writes the summary on its server, so you need no summarising code of your own. It summarises the earlier parts of the chat. Keeping the last few turns word for word is an option you switch on. It saves the code, but you still decide which facts must survive.

> **Tip:** Asked how you would keep a long chat in budget? Name the order: clear old tool output, summarise older turns with the key facts kept, keep recent turns word for word, then count tokens before every request.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] The window is fixed, and everything in a request counts toward it.
- [ ] Work out the budget: window, minus fixed parts, minus room for the answer.
- [ ] History grows every turn, so it is the part to budget.
- [ ] Count tokens before sending.
- [ ] Trim in order: old tool output, then summaries with key facts kept, recent turns intact.
- [ ] Caching cuts cost, but cached tokens still take up the window.

**Common traps**

- Trap: "Just send the whole chat, the window is big enough." Better: Big windows still fill up, and recall drops as they fill. Budget first.
- Trap: "Keep the last twenty messages and we are done." Better: Twenty short messages and twenty long tool results are very different sizes, so count tokens.
- Trap: "Cut the oldest turns, nothing else matters." Better: The oldest turn can hold the one fact the customer needs, like a claim number. Keep or summarise it before you drop it.

## Can you spot the budget mistake?

**Spot the budget mistake:** A developer wrote this to keep a claims chat inside budget. Which line is the safe plan?

A. history = all_turns  # the window is big, so send everything
B. history = all_turns[-20:]  # keep 20 messages, no token count
C. if count_tokens(history) > budget: history = summarise(history[:-6]) + history[-6:]
D. history = all_turns[-6:]  # the last six messages are always enough

**Answer:** C. Only the third counts the real size and summarises the old part while keeping the recent turns. The first sends everything, the second counts messages rather than tokens, and the fourth throws away the claim number from the first turn.

**Quick check:** A call-centre assistant is 3,000 tokens over budget. Which change keeps the most useful facts?

A. Delete the oldest half of the chat without reading it
B. Summarise older turns, keep the claim number, keep recent turns word for word
C. Raise the reserved answer space to half the window
D. Add more rules to the system prompt

**Answer:** B. Summarising keeps the facts a later turn needs, and recent turns keep their exact wording. The other options either lose facts blindly or make the budget tighter.

## Can you answer these from memory?

**Flash cards**

- **What is the context window?** The most text one request can hold, the answer included.
- **What counts toward it?** The system prompt, tool definitions, every message, every tool result, and the model's output and thinking.
- **What is the history budget?** The window, minus the fixed parts, minus room for the answer.
- **Why clear old tool output first?** Raw lookups from many turns back are rarely needed again.
- **Does caching make a chat smaller?** No. It lowers what reused tokens cost, but they still fill the window.
- **What is context rot?** Recall drops as the token count in the window grows.

## Likely follow-up questions

- Q008 What is a context window? https://designervenkat.online/interview/ai/what-is-a-context-window
- Q040 How do you estimate the token count of a text? https://designervenkat.online/interview/ai/how-do-you-estimate-the-token-count-of-a-text
- Q083 What is "context rot"? https://designervenkat.online/interview/ai/what-is-context-rot
- Q096 What is compaction (summarising history)? https://designervenkat.online/interview/ai/what-is-compaction-summarising-history
- Q163 Short-term vs long-term agent memory? https://designervenkat.online/interview/ai/short-term-vs-long-term-agent-memory
- Q324 How do you estimate token cost for a feature? https://designervenkat.online/interview/ai/how-to-estimate-token-cost

## Learn it in depth

- [Context Window](https://designervenkat.online/learn/ai-engineering/context-window)
- [Context Compaction](https://designervenkat.online/learn/ai-engineering/context-compaction)
- [Prompt Caching](https://designervenkat.online/learn/ai-engineering/prompt-caching)
