Multi-turn Conversation

Maintain context across conversations so AI agents generate more relevant and continuous responses.

Overview

Multi-turn conversations allow your agent to remember previous exchanges, enabling more natural, context-aware interactions. This is essential for building chatbots, assistants, and any application requiring back-and-forth dialogue.

Basic Example

Pass conversation history via the messages array:

multi-turn-basic.ts

Building Chat History

Here's a pattern for maintaining an ongoing conversation:

chat-history.ts

Message Roles

Mosaic supports three message roles:

system

Sets the behavior and personality of the assistant. Usually the first message in the conversation.

user

Represents messages from the user or the application requesting a response.

assistant

Represents previous responses from the AI. Include these to maintain context.

Managing Context Length

Warning

AI models have token limits. For long conversations, you may need to truncate or summarize older messages to stay within limits.
context-management.ts

Next Steps