Sub topic 1
Why Every Request Carries the Whole Conversation
Prerequisites
This chapter assumes you have:
- Basic Python or comparable programming experience.
- Familiarity with chat-style LLM APIs and the message roles
system,user, andassistant. - A general idea of what tokens are.
The model has no memory between requests
A chat model does not remember your last request. Each API call is independent. The model reads the messages you send, writes a reply, and keeps nothing. The server does not store the conversation for you.
The feeling of a continuous chat comes from your application. After each reply, your code appends the new messages to a list. On the next turn, it sends the full list again.
A worked example
The loop below keeps the history in a Python list and sends all of it on each call. The call_model function is a stand-in for a real API client, so the example runs without a network. It reports how many messages it received.
def call_model(messages):
# Stand-in for a real chat API call.
# A real client would return the model's generated text.
return f"(reply based on {len(messages)} messages)"
history = [{"role": "system", "content": "You are a helpful assistant."}]
for question in ["My name is Priya.", "What is my name?", "Spell it backwards."]:
history.append({"role": "user", "content": question})
reply = call_model(history)
history.append({"role": "assistant", "content": reply})
print(f"Sent {len(history) - 1} messages -> {reply}")Running it prints:
Sent 1 messages -> (reply based on 2 messages)
Sent 3 messages -> (reply based on 4 messages)
Sent 5 messages -> (reply based on 6 messages)The count of messages the model receives goes 2, 4, 6. The first number in each line is the history length after the reply is appended, so it is one higher than what was sent. The point is that the second question, "What is my name?", is only answerable because the first message travels with it.
Why this matters
Resending history has three consequences.
- Cost grows with every turn. You pay for input tokens on each call. A conversation of N equal-sized turns sends about N(N+1)/2 turns' worth of text in total, so the total grows roughly with the square of the number of turns.
- Latency grows. A longer input takes longer to process before the first output token appears.
- The input has a hard limit. The model accepts only a fixed number of tokens per request. This is the context window, covered in the next chapter. Once your history passes it, the request fails or the provider must drop content.
What you control
Because your code builds the message list, your code decides what the model sees. That gives you three options when history gets long:
- Drop older messages (truncation).
- Replace older messages with a shorter description (summarization).
- Do both: a summary of the old turns plus the most recent messages verbatim.
The rest of this tutorial builds each option, starting with how to measure the problem.
Exercise
A conversation has 10 turns, and each turn adds about 200 tokens (the user message and the reply together). Ignoring the system prompt, roughly how many input tokens are sent on the 10th request if the model's reply to turn 10 is not yet included, and why is the total across all 10 requests larger than 2,000?
Show answer
Before the 10th request, 9 full turns exist (9 × 200 = 1,800 tokens), plus the new user message, so about 1,800 tokens plus a part of the 10th turn. The total across all requests is larger than 2,000 because each request resends every earlier turn. Summing 0, 200, 400, ... up to 1,800 for the history portions gives 9,000 tokens, plus the new user messages, so the total is far above 2,000.