Skip to content

< all problems61 · Level 01, LLM APIs

Carry the Conversation Yourself

easy · debug · LLM Fundamentals

A user tells your chatbot their name. One message later they ask what it is, and the bot has no idea.

The model did nothing wrong. A model API is stateless: each request is answered from what that request contains and nothing else. There is no session on the other end. If the model is to know what was said two turns ago, you have to send it again, every time. The Chat class below was written by someone who assumed otherwise.

Fix Chat so that:

  1. Every request carries the whole conversation so far, oldest first, ending with the new user message.
  2. The model's reply is recorded as a message with role "assistant", so the next request includes it. A history with only the user's half reads to the model as someone talking to themselves.
  3. The system prompt travels in the system argument on every call. It is not part of any message.
  4. A failed call leaves no trace. If the request raises, the conversation must be exactly what it was before send was called, and the error must still reach the caller. Otherwise a retry sends two user messages in a row.

chat.messages is the history: a list of {"role": ..., "content": ...} dicts, roles alternating user, assistant, user, assistant. send(text) returns the reply's text.

The model is scripted. The tests read what your class sent it.