Skip to main content

How to Keep a Multi-Turn Conversation with the Claude API in Python

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Each call to the Claude API stands alone: Claude doesn't remember the previous one. A chat works because your code keeps the conversation in a list and sends all of it every time. This guide builds a two-turn conversation in which Claude recalls something from the first turn, with real output from anthropic 2.14.0.

Syntax​

Syntax
messages = []

messages.append({"role": "user", "content": <question>})
response = client.messages.create(model=<model ID>, max_tokens=<limit>, messages=messages)
messages.append({"role": "assistant", "content": response.content})
PartWhat it means
messagesThe whole conversation, oldest first, alternating user and assistant
{"role": "user", ...}Your next question, added before the call
{"role": "assistant", "content": response.content}Claude's reply, added unchanged after the call

Anthropic's documentation: "By default, the Messages API is stateless, so you send the full conversation history with each request. To continue a conversation, add the response's content to messages as an assistant message. Keep it unchanged."

What It Costs​

The Claude API is paid: Claude Opus 5.5 costs $4 per million input tokens and $20 per million output tokens. Each call resends the whole conversation as input, so long chats cost more per turn.

Prerequisites​

  • A Claude API key, in a .env file as ANTHROPIC_API_KEY=....
  • The Anthropic Python SDK in a uv project. This guide uses anthropic 2.14.0, added with uv add anthropic, on Python 3.15.2.

Step 1: Write the Script​

conversation.py
import anthropic

client = anthropic.Anthropic()
messages = []

for question in [
"My favourite colour is green. Suggest a one-word name for my new bike.",
"What is my favourite colour?",
]:
messages.append({"role": "user", "content": question})
response = client.messages.create(model="claude-opus-5-5", max_tokens=1024, messages=messages)
messages.append({"role": "assistant", "content": response.content})
print("You:", question)
print("Claude:", response.content[-1].text)

The conversation.py script in a macOS-style code window: a messages list grows by one user turn and one assistant turn per question, with the whole list sent to claude-opus-5-5 each time.

The second question only makes sense with the first turn in front of Claude.

Step 2: Run It​

Terminal
uv run --env-file .env conversation.py
Output
You: My favourite colour is green. Suggest a one-word name for my new bike.
Claude: **Jade**: sleek, smooth, and a little bit precious, just like a new bike should be.

A few other green-inspired options:
- **Clover**, if you like luck on your rides
- **Basil**, if you want something fresh and a bit cheeky
- **Gecko**, if it's quick and nimble
- **Fern**, if you ride mostly forest trails
- **Emerald**, if it's your pride and joy
You: What is my favourite colour?
Claude: Your favourite colour is **green**!

The output in a terminal: the first answer suggests Jade and five other green names, and the answer to What is my favourite colour? is green.

Claude answered green because the second call carried the first question and its answer. The ** marks are Markdown bold: Claude formats replies unless you ask for plain text.

Common Mistakes​

MistakeWhat happensDo this instead
Sending only the newest questionClaude has no memory of earlier turnsSend the whole messages list each time
Appending only the reply's textThinking blocks are dropped from the historyAppend response.content unchanged
Editing or reordering earlier turnsEarlier thinking blocks no longer match the history, and the prompt cache missesOnly append; never change what's already in the list

Troubleshooting​

SymptomLikely causeFix
Claude "forgets" what the user saidThe question was never appended, or a new list was startedAppend before each call and keep one list per conversation

FAQ​

How do I start a new conversation?​

Start a new, empty list.

Doesn't the history get expensive?​

Yes, every turn resends it. Prompt caching makes the repeated part much cheaper.

Conclusion​

Appending each question, then each response.content unchanged, gave Claude the whole conversation on every call, so it answered green to the second question.

Additional Resources​