esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

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.

10 min readIntermediateCommon · estimateFacts checked 26 November 2026

Say it out loud

Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.

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.

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.

Key words

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.

What goes into each request

Sent with every request

System prompt

fixed rules, sent every turn

Tool definitions

names and inputs of each tool

Chat history

grows with every turn

New message

the only part typed this turn

Model

reads it all, then answers

Answer

joins the history for next turn

  1. 1The fixed rules go in with every request.
  2. 2The list of tools goes in too, every time.
  3. 3The whole history goes in again, so each turn is bigger than the last.
  4. 4The new message joins the request for this turn only.
  5. 5The answer is added to the history for the next turn.
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. 1

    Start from the window

    The most text one request can hold, the answer included.

  2. 2

    Take off the fixed parts

    The system prompt and the tool definitions go in on every turn.

  3. 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. 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

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.

Trimming a chat that is over budget

Whole chat so far

more than the budget allows

Make it smaller

Clear old tool output

raw results rarely needed again

Summarise older turns

facts kept, chatter dropped

Keep recent turns

last turns stay word for word

Count the tokens

before each request is sent

Fits the budget

sent to the model

  1. 1The whole chat is too big, so trimming starts.
  2. 2After trimming, the smaller chat is counted again.
  3. 3If it fits, the request goes to the model.
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.
  • 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."

    BetterBig windows still fill up, and recall drops as they fill. Budget first.

  • Trap"Keep the last twenty messages and we are done."

    BetterTwenty short messages and twenty long tool results are very different sizes, so count tokens.

  • Trap"Cut the oldest turns, nothing else matters."

    BetterThe 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?

Quick check

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

Can you answer these from memory?

Flash cards · tap a card to flip it

1 / 6

What will they ask next?

  1. Q008What is a context window?11 min
  2. Q040How do you estimate the token count of a text?10 min
  3. Q083What is "context rot"?10 min
  4. Q096What is compaction (summarising history)?11 min
  5. Q163Short-term vs long-term agent memory?12 min
  6. Q324How do you estimate token cost for a feature?11 min

Learn it in depth

Last updated on 26 November 2026

Building a chatbot that has to remember a long conversation?

I design and build AI products and agents, the screens and the system behind them, including how their memory and context are managed. Book a short call and say what your chat needs to remember.