Sub topic 1
Why the Model Forgets: Stateless APIs
Prerequisites
This chapter assumes you can read basic Python (functions, lists, dictionaries, exceptions) and run programs in a terminal. You should also know the general shape of a chat API: you send messages in and get a reply back. No provider account is needed here. The code uses a stand-in function so you can run it as shown.
The working model: each request stands alone
A chat model API is stateless. The server keeps no record of your earlier requests. When you send a request, the model reads only what is in that request, produces a reply, and moves on. The next request starts from nothing.
The chat interfaces you have used feel continuous because the client program does the remembering. On every turn it collects the whole conversation so far and sends all of it again.
This has a direct consequence for the bot you will build: the conversation lives in your program's memory, not in the model. If your program does not store it, it is gone.
Seeing the problem with a fake model
You can observe statelessness without any real API. The function below pretends to be a model. It can only use the messages it receives, just like the real thing.
def fake_model(messages):
"""Answer a name question using only the messages passed in."""
for m in reversed(messages):
if m["role"] == "user" and m["content"].startswith("My name is "):
name = m["content"].removeprefix("My name is ")
return f"Your name is {name}."
return "I don't know your name."
# Attempt 1: send only the latest message.
print(fake_model([{"role": "user", "content": "What is my name?"}]))
# Attempt 2: resend the earlier turn along with the new one.
messages = [
{"role": "user", "content": "My name is Ana"},
{"role": "assistant", "content": "Nice to meet you, Ana."},
{"role": "user", "content": "What is my name?"},
]
print(fake_model(messages))Running it prints:
I don't know your name.
Your name is Ana.The only difference between the two calls is the list you pass. Memory is a property of the input, not of the model.
What "resend everything" means in practice
Each message is a dictionary with a role and content. The common roles are system (instructions for behavior), user (what the person typed), and assistant (what the model replied). Your program appends to one list and sends the whole list on every call. Later chapters build this out. For now, note the pattern:
- Append the user's message to the list.
- Send the whole list.
- Append the reply to the same list.
Consequences you will meet later
Resending history has costs. These explain why the rest of the tutorial exists.
| Consequence | Why it happens | Where it is handled |
|---|---|---|
| Requests grow with every turn | The full history goes out each time | Context window and trimming chapters |
| Cost and latency rise | More input to process per request | Trimming and summarization |
| Restarting loses everything | The list lives only in program memory | Saving history to JSON |
| A failed request can leave a broken list | A user message may be stored with no reply | Error handling chapter |
Common errors
- Sending only the newest message. The bot answers each line as if it were the first. This is the most frequent beginner bug.
- Forgetting to append the reply. The model sees your questions but never its own answers, so follow-ups like "explain that again" make no sense to it.
- Assuming the provider remembers you. Some services offer server-side conversation features, but the basic chat endpoint does not. Check your provider's documentation before relying on any such feature.
Exercise
A user tells your bot "I prefer metric units," then asks "How tall is Everest?" in a second request. Your code sends only the second message. Why might the answer use feet, and what is the fix?
Show answer
The API is stateless, so the model never saw the first message. Keep every message in one list, append the user's turn and the model's reply each time, and send the whole list with every request.