Sub topic 8
Building conversational assistants
This chapter assumes you can write basic Python, know what a chat-style API call looks like, and have read the chapter on prompting. It covers how an assistant keeps track of a conversation and where that tracking breaks.
The model has no memory
A language model does not remember earlier turns. Each request is independent. An assistant feels continuous because your code resends the conversation every time. The request holds three parts:
- A system message: fixed instructions about role, tone, and limits.
- The history: earlier user and assistant messages, in order.
- The new user message.
A minimal turn function
The complete argument stands in for whichever model API you use. The fake version below lets you run the code without a key.
SYSTEM = "You are a support assistant for Acme. Answer briefly. If unsure, say so."
def trim(history, max_messages=6):
return history[-max_messages:]
def chat_turn(history, user_text, complete):
history.append({"role": "user", "content": user_text})
messages = [{"role": "system", "content": SYSTEM}] + trim(history)
reply = complete(messages)
history.append({"role": "assistant", "content": reply})
return reply
def fake_complete(messages):
return f"I can see {len(messages)} messages."
history = []
print(chat_turn(history, "Hi", fake_complete))
print(chat_turn(history, "Where is my order?", fake_complete))The program prints I can see 2 messages. and then I can see 4 messages.. The history is stored outside the model call, so your application owns it.
Managing context
The context window is a fixed limit on how much text one request can hold. Long conversations eventually exceed it, and you pay for every resent token. Common strategies:
| Strategy | How it works | Main risk |
|---|---|---|
| Sliding window | Keep the last N messages | Forgets early facts |
| Summarization | Replace old turns with a model-written summary | Summary may drop or distort details |
| Pinned facts | Store key details (name, order ID) separately and add them to every request | You must decide what to pin |
| Retrieval | Fetch relevant past or document content per turn | Needs a search step (see the RAG chapter) |
Most production assistants combine a sliding window with pinned facts.
Common errors
- Dropping the system message when trimming. Trim the history, not the instructions.
- Instructions that drift. In long chats, models follow early instructions less reliably. Repeat critical rules near the end of the request if needed.
- Confident answers outside scope. Tell the assistant what to do when it lacks information, such as "say you don't know and offer a human handoff."
- Mixing users. Keep one history per conversation. A shared list leaks one user's data into another's chat.
Streaming and feel
Responses can take seconds. Most APIs support streaming, which sends tokens as they are generated so the user sees text immediately. Streaming does not make the full answer faster. It makes the wait feel shorter.
Exercise
A user tells your assistant their order number in turn 1. After 20 turns with a sliding window of 6 messages, the assistant asks for the order number again. Name two fixes.
Show answer
Pin the order number as a stored fact and include it in every request, or summarize older turns into a short summary that is kept alongside the window. A larger window also helps but raises cost and only delays the problem.