C.W.K.
Stream
Lesson 03 of 06 · published

Multi-Turn Conversations and Memory

~14 min · multi-turn, memory, history

Level 0Observer
0 XP0/64 lessons0/13 achievements
0/150 XP to next level150 XP to go0% complete

The API has no memory; you do

Each messages.create() call is stateless. To continue a conversation, you re-send the entire history (every prior user and assistant turn) on every request. The model has no recollection between calls — your application is the memory layer.

Three ways to manage growing history

For short chats, send everything verbatim. For medium chats, summarize older turns into a condensed assistant note and keep recent turns verbatim. For long-running agents, store turns externally (database, JSONL) and rebuild the relevant slice on each call. cwkPippa uses option three: every turn lives in a JSONL file keyed by conversation_id, and the Agent SDK replays just enough internally to keep coherence.

Append-only is your friend

Treat conversation history as append-only. Editing past turns to 'fix' the model's response confuses Claude (the appended text now contradicts what was supposedly already said). If you need to redo a turn, start a new branch, do not rewrite history in place.

Principle: The application owns memory. The API is the reasoner over the slice you choose to send.

Code

Stateless multi-turn loop·python
history = []  # list of {role, content}

def chat(user_text: str) -> str:
    history.append({"role": "user", "content": user_text})
    response = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        system="You are a helpful assistant.",
        messages=history,  # full history every call
    )
    text = response.content[0].text
    history.append({"role": "assistant", "content": text})
    return text

chat("My favorite color is forest green.")
chat("What was my favorite color?")  # works because history was resent
Summarize-old-turns pattern·python
MAX_VERBATIM = 10

def trimmed_history(full_history: list[dict]) -> list[dict]:
    if len(full_history) <= MAX_VERBATIM:
        return full_history
    old, recent = full_history[:-MAX_VERBATIM], full_history[-MAX_VERBATIM:]
    summary = client.messages.create(
        model="claude-haiku-4-5-20251001",
        max_tokens=512,
        system="Summarize the following conversation in one paragraph, preserving facts the assistant might need.",
        messages=[{"role": "user", "content": str(old)}],
    ).content[0].text
    return [{"role": "user", "content": f"<earlier_summary>{summary}</earlier_summary>"}] + recent

External links

Exercise

Implement a chat helper that supports two modes: 'verbatim' (full history resent) and 'summarized' (older turns folded into one summary). Run a 20-turn conversation through both and compare total input tokens used.
Hint
Use the token counting endpoint to compare without paying for completions during the test.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.