Building Advanced Agents: Conversation History
Pass 2: Keeping a Conversation History
In pass 1 every question was a fresh start. The fix is structural: we keep a list of messages outside the loop, append the user's turn to it before each call, send the entire list to the model, and append the assistant's reply to the same list after we get it back. Every call now carries the full history of the conversation, and the model can refer back to anything in it.
Step 1: Create a List to Hold the Conversation
Before the loop starts, we create an empty list that will accumulate every message of the session:
messages: list[dict] = []
Each entry will be a dict with a role (user or assistant) and a content string. Same shape as the message we already sent in pass 1, just kept around instead of thrown away.
Step 2: Append the User's Message before Sending
Inside the loop, as soon as we read the user's input, we push it into the history:
messages.append({"role": "user", "content": user})
Step 3: Send the Whole History, Not Just the Last Message
The client.chat(...) call now takes the full list:
response = client.chat(
model=OLLAMA_MODEL,Local AI Engineering with Ollama
Run, understand, customize, fine-tune, and build agentic apps on your own hardwareEnroll now to unlock all content and receive all future updates for free.
