liveBook · Manning

Dialogue

As explained in Build an AI Agent (From Scratch)

Conversation history is the accumulated sequence of prior messages retained for continuing an interaction with a language model.

Language-model API calls are stateless: each call sees only the messages sent with it. Conversation continuity therefore depends on retaining prior messages and resending them with each request. section 2.2.4

For a basic exchange, append user messages and assistant replies to a messages list, then send the accumulated list on the next call. Agents may also need to retain tool results, search results, and intermediate reasoning. As history grows, token use, cost, and response time grow too. section 2.2.4 section 2.2.4

Across separate agent runs, store history in a session and retrieve it using the same session ID. A session can also preserve execution state; a session manager loads or creates it before a run and saves it afterward. section 6.3.3 section 6.3

Listing 2.6 Managing conversation history
from litellm import completion

messages = []

# First exchange
messages.append({"role": "user", "content": "My name is Jungjun."})
response1 = completion(model="gpt-5-mini", messages=messages)
assistant_message1 = response1.choices[0].message.content
messages.append({"role": "assistant", "content": assistant_message1})
print(assistant_message1)

# Second exchange - includes previous conversation history
messages.append({"role": "user", "content": "What is my name?"})
response2 = completion(model="gpt-5-mini", messages=messages)
assistant_message2 = response2.choices[0].message.content
print(assistant_message2)

Trace how user and assistant messages accumulate and are resent to maintain continuity. section 2.2.4

# Maintaining conversation continuity with the same session_id
result1 = await agent.run("My name is Alice", session_id="session_1")  
# → "Nice to meet you, Alice!"

result2 = await agent.run("What's my name?", session_id="session_1")   
# → "Your name is Alice."

See how reusing a session ID carries a name from one agent run into the next. section 6.3

How Build an AI Agent (From Scratch) covers it

How deep it goes
Implements message accumulation and session persistence, while identifying token growth as a cost and deferring efficient context management.
Examples
Demonstrates retaining a name across API calls and agent runs using a message list or session ID.
What you can do after
Apply continuity by resending prior messages or reusing a session ID across agent runs.
How it connects
Connects conversation history to stateless APIs, context management, sessions, and agent execution state.

Also explained in

Compare how 10 books explain dialogue →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build an AI Agent (From Scratch). The section links point to the parts of the book it draws on.