Skip to main content

Multi-Turn Conversations

Maintain context across multiple exchanges by keeping a message history.

Message Roles

Basic Multi-Turn Pattern

Conversation History Format

After a few turns, your messages array looks like:

With Tool Calls

Tool calls add more message types:

Managing Context Length

As conversations grow, you may hit token limits. Strategies:

1. Sliding Window

Keep only recent messages:

2. Summarization

Periodically summarize older context:

Complete Example

See Also