Sub topic 7
Why the model only knows what you send each time
This chapter assumes you can read Python lists and dictionaries, know what a chat message with a role and content looks like, and have called an HTTP API or SDK before. It covers why chat APIs are stateless and what that means for the code you write.
The API has no memory
A chat completion endpoint is stateless. Each request is processed on its own. When the response comes back, the server keeps nothing about your conversation that the next request can use.
The model only sees the messages list in the request in front of it. If you send "How do I add an item to it?" alone, the word "it" has nothing to point to. The model will guess or ask you to clarify.
Chat apps feel like they remember because the client resends the earlier turns each time. The memory is in your program, not in the model.
What you must send
For a follow-up question to work, the request has to contain every earlier message you want the model to take into account:
- The system message, if you use one.
- Each earlier user message.
- Each earlier assistant reply.
- The new user message.
The assistant messages in your list are text you stored. The API does not check that the model really wrote them. You can edit, remove, or invent assistant turns. Later chapters use this for few-shot examples.
A loop that keeps history
The pattern is to keep one list, append the user's text, call the API, then append the reply.
from openai import OpenAI
client = OpenAI()
history = [
{"role": "system", "content": "You are a helpful assistant. Keep answers under three sentences."}
]
def chat(user_text):
history.append({"role": "user", "content": user_text})
response = client.chat.completions.create(
model="MODEL_NAME", # replace with a chat model available to you
messages=history,
)
reply = response.choices[0].message.content
history.append({"role": "assistant", "content": reply})
return reply
print(chat("What is a list in Python?"))
print(chat("How do I add an item to it?"))On the second call, history holds four messages: system, user, assistant, user. The model can resolve "it" to "a list in Python" because the first exchange is in the request.
If you skip the line that appends the assistant reply, the model sees two user questions in a row and never sees its own earlier answer. Follow-ups then drift or contradict what it said before.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The model asks "what do you mean by it?" | Only the latest user message was sent | Send the whole `history` list |
| The model forgets its earlier answer | The assistant reply was never appended | Append the reply after every call |
| Two users share one conversation | A global `history` list is reused across users | Keep one list per conversation |
| Cost and latency rise with each turn | Every turn resends all earlier turns | Trim or summarize history (see the history management chapter) |
Consequences of statelessness
- You pay for history each time. Input tokens are counted on every request, including the old turns. A long chat costs more per turn than a short one.
- The context window is a hard limit. The history plus the reply must fit within the model's context window. Past that point the request fails or you must cut history.
- You control the memory. You can save a conversation to a database and resume it days later. You can also drop, rewrite, or summarize turns.
- Some provider features add server-side state. Certain APIs can store conversations for you, but the underlying model still reads a full sequence of messages each time. Check your provider's documentation for details.
Exercise
A developer's chatbot answers the first question well. For the second question, "Can you shorten that?", the model replies "Shorten what?". The code calls the API with messages=[{"role": "user", "content": user_text}] on every turn. What is wrong, and what should the code change?
Show answer
The API is stateless, so each request contains only the latest user message and the model has no access to the earlier answer. The code should keep a history list that starts with the system message, append each user message before the call, and append each assistant reply after it, then send the whole list as messages every time.
Exercise
A conversation has a system message and three completed question-and-answer exchanges. The user now asks a fourth question. How many messages are in the messages list for that request, and which roles do they have?
Show answer
There are eight messages: one system, then three user and three assistant messages in alternating order, then the new fourth user message. That is 1 + 6 + 1 = 8, with four user messages, three assistant messages, and one system message.