Skip to main content

Multi-turn conversations

📖 Lesson content

Scheduled maintenance: Anthropic Academy will be unavailable on Saturday, May 2 from 5:30–8:30 PM PDT.×

Summary

When working with the Anthropic API and Claude, there's a crucial concept you need to understand: Claude doesn't store any of your conversation history. Each request you make is completely independent, with no memory of previous exchanges.

This means if you want to have a multi-turn conversation where Claude remembers context from earlier messages, you need to handle the conversation state yourself.

The Problem with Stateless Conversations

Let's say you ask Claude "What is quantum computing?" and get a good response. Then you follow up with "Write another sentence" - Claude has no idea what you're referring to. It will write a sentence about something completely random because it has no memory of the quantum computing discussion.

How Multi-Turn Conversations Work

To maintain conversation context, you need to do two things:

  • Manually maintain a list of all messages in your code
  • Send the complete message history with every request

Here's the flow that actually works:

  1. Send your initial user message to Claude
  2. Take Claude's response and add it to your message list as an assistant message
  3. Add your follow-up question as another user message
  4. Send the entire conversation history to Claude

iterate

1. Initial user message

2. Send messages list → Vertex Claude

3. Append assistant response to list

4. Append next user message

Full context retained

Building Helper Functions

To make conversation management easier, you can create three helper functions:

def add_user_message(messages, text):
    user_message = {"role": "user", "content": text}
    messages.append(user_message)

def add_assistant_message(messages, text):
    assistant_message = {"role": "assistant", "content": text}
    messages.append(assistant_message)

def chat(messages):
    message = client.messages.create(
        model=model,
        max_tokens=1000,
        messages=messages,
    )
    return message.content[0].text

Putting It All Together

Here's how you use these functions to maintain a conversation:


messages = []

add_user_message(messages, "Define quantum computing in one sentence")

answer = chat(messages)

add_assistant_message(messages, answer)

add_user_message(messages, "Write another sentence")

final_answer = chat(messages)

Now Claude will understand that "Write another sentence" refers to expanding on the quantum computing definition, because you've provided the complete conversation context.

Takeaways

Remember that every API call to Claude is independent. If you want conversational context, you must:

  • Store all messages locally in your application
  • Send the complete message history with each request
  • Properly format messages with "user" and "assistant" roles

These helper functions will be essential throughout your work with Claude, making it much easier to build applications that feel like natural conversations rather than isolated question-and-answer exchanges.

🔁 Related lessons

📚 Source & attribution

Was this lesson helpful?

Feedback / ReportSpotted an issue or have an improvement idea?