How to Keep a Multi-Turn Conversation with the Claude API in Python
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
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})
| Part | What it means |
|---|---|
messages | The 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
.envfile asANTHROPIC_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
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 second question only makes sense with the first turn in front of Claude.
Step 2: Run It
uv run --env-file .env conversation.py
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**!

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
| Mistake | What happens | Do this instead |
|---|---|---|
| Sending only the newest question | Claude has no memory of earlier turns | Send the whole messages list each time |
| Appending only the reply's text | Thinking blocks are dropped from the history | Append response.content unchanged |
| Editing or reordering earlier turns | Earlier thinking blocks no longer match the history, and the prompt cache misses | Only append; never change what's already in the list |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Claude "forgets" what the user said | The question was never appended, or a new list was started | Append 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
- Working with the Messages API — multi-turn conversations and the content of each turn.
