> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cfosilvia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 05 multi turn

# Multi-Turn Conversations

Maintain context across multiple exchanges by keeping a message history.

## Message Roles

| Role | Description |
| - | - |
| `system` | Instructions for the model (optional, goes first) |
| `user` | User input |
| `assistant` | Model responses |
| `tool` | Results from tool calls |

## Basic Multi-Turn Pattern

```python theme={null}
messages = [
    {"role": "system", "content": "You are a helpful math tutor."}
]

# Turn 1
messages.append({"role": "user", "content": "What is 5 + 3?"})
response = chat(messages)
messages.append({"role": "assistant", "content": response})

# Turn 2 - references previous context
messages.append({"role": "user", "content": "Multiply that by 2"})
response = chat(messages)
messages.append({"role": "assistant", "content": response})
```

## Conversation History Format

After a few turns, your messages array looks like:

```json theme={null}
[
  {"role": "system", "content": "You are a helpful math tutor."},
  {"role": "user", "content": "What is 5 + 3?"},
  {"role": "assistant", "content": "5 + 3 = 8"},
  {"role": "user", "content": "Multiply that by 2"},
  {"role": "assistant", "content": "8 × 2 = 16"},
  {"role": "user", "content": "Divide by 4"},
  {"role": "assistant", "content": "16 ÷ 4 = 4"}
]
```

## With Tool Calls

Tool calls add more message types:

```json theme={null}
[
  {"role": "user", "content": "What's the weather in Tokyo?"},
  {
    "role": "assistant",
    "content": "",
    "tool_calls": [{"id": "call_123", "function": {"name": "get_weather", "arguments": "{\"location\":\"Tokyo\"}"}}]
  },
  {
    "role": "tool",
    "tool_call_id": "call_123",
    "name": "get_weather",
    "content": "{\"temp\": 15, \"condition\": \"sunny\"}"
  },
  {"role": "assistant", "content": "It's 15°C and sunny in Tokyo."},
  {"role": "user", "content": "What about London?"},
  // ... continues
]
```

## Managing Context Length

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

### 1. Sliding Window

Keep only recent messages:

```python theme={null}
MAX_MESSAGES = 20
if len(messages) > MAX_MESSAGES:
    # Keep system message + recent messages
    messages = messages[:1] + messages[-(MAX_MESSAGES-1):]
```

### 2. Summarization

Periodically summarize older context:

```python theme={null}
if len(messages) > 30:
    summary = summarize(messages[1:20])
    messages = [
        messages[0],  # System
        {"role": "assistant", "content": f"[Previous context: {summary}]"},
        *messages[20:]
    ]
```

## Complete Example

```python theme={null}
import httpx

ENDPOINT = "https://hetty.cfosilvia.com/v1/chat/completions"

def chat(messages):
    response = httpx.post(ENDPOINT, json={"messages": messages}, timeout=60.0)
    return response.json()["choices"][0]["message"]["content"]

def main():
    messages = [{"role": "system", "content": "You are a concise assistant."}]
    
    while True:
        user_input = input("You: ")
        if user_input.lower() in ["quit", "exit"]:
            break
        
        messages.append({"role": "user", "content": user_input})
        response = chat(messages)
        messages.append({"role": "assistant", "content": response})
        
        print(f"Assistant: {response}\n")

if __name__ == "__main__":
    main()
```

## See Also

* [examples/04\_multi\_turn.py](../examples/04_multi_turn.py) - Basic multi-turn
* [examples/05\_multi\_turn\_tools.py](../examples/05_multi_turn_tools.py) - With tools


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.