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
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
