Skip to main content

Module 10: Multi-Turn Conversations

Building conversational applications with context preservation.

What You’ll Learn

  • Maintaining context across multiple exchanges
  • The query/response pattern for conversations
  • How Claude remembers previous messages in a session

Single-Turn vs Multi-Turn

Both clients support multi-turn conversations with full context preservation. Choose based on your programming paradigm.

The Conversation Loop Pattern

The pattern differs by paradigm: blocking uses connectText()/queryText(), reactive uses connect().text()/query().text() with flatMap chaining. Context is preserved across all turns in both.

How It Works

Full Message Access

When you need metadata, tool use details, or cost information:
Use connectAndReceive() and queryAndReceive() when you need access to all message types.

Key Points

  • Both ClaudeSyncClient and ClaudeAsyncClient support multi-turn conversations
  • connectText(prompt) starts the session and returns the text response
  • queryText(prompt) sends follow-ups and returns the text response
  • connectAndReceive()/queryAndReceive() for full message access
  • Context persists until the client is closed

When to Use Multi-Turn

Tradeoffs

  • Memory growth: Context accumulates with each turn. Long conversations consume more tokens and increase latency as Claude re-processes the full history.
  • No partial context: You cannot selectively forget messages. The entire conversation history persists until the session closes.
  • Session coupling: The client must remain open for the conversation to continue. Network interruptions or process crashes lose the session.
  • Cost implications: Each turn includes all prior messages in the API call. A 20-turn conversation sends the full history 20 times.
Avoid multi-turn for simple, independent queries. Use Query.text() when context is unnecessary.

Source Code

View on GitHub

Running the Example

Next Module

Module 11: Session Resume - Continuing conversations across restarts.