AI / Claude Models Basics Interview Questions
What is multi-turn conversation handling in Claude and how do you implement it?
Claude's Messages API is stateless — each API call is independent and Claude has no memory of previous calls unless you include the conversation history explicitly. Multi-turn conversation is implemented by appending each exchange to the messages array.
# Building a multi-turn conversation manually messages = [] # Turn 1 messages.append({"role": "user", "content": "What is the capital of France?"}) response = client.messages.create( model="claude-opus-4-8", max_tokens=256, messages=messages ) assistant_reply = response.content[0].text messages.append({"role": "assistant", "content": assistant_reply}) # Turn 2 â Claude now has context of the previous exchange messages.append({"role": "user", "content": "What is its population?"}) response2 = client.messages.create( model="claude-opus-4-8", max_tokens=256, messages=messages # full history included ) print(response2.content[0].text) # Claude knows "its" refers to Paris from the previous turn # Important: as conversation grows, context window fills up # Common strategies when context limit approaches: # 1. Summarise older turns and replace them with the summary # 2. Use prompt caching on stable early context # 3. Truncate oldest messages (may lose important context)
Key implementation notes:
- Messages must alternate: user → assistant → user → assistant (etc.)
- You cannot have two consecutive user or assistant messages
- The entire conversation history is sent on every request — this grows your token count over time
- Prompt caching can significantly reduce costs for long conversations with stable early context
More Related questions...