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

# Message History

> Configure conversation context and memory

Message history controls how much conversation context is included when your bot calls the AI integration. This directly affects the bot's ability to maintain coherent, contextual conversations.

## Overview

Message history (also called context window) is the number of recent messages sent to your AI backend with each request:

```json theme={null}
{
  "messages": [
    {"role": "user", "content": "What's the weather?"},
    {"role": "assistant", "content": "I'll check for you."},
    {"role": "user", "content": "Thanks"}
  ]
}
```

The AI receives this history to understand context and generate relevant responses.

## Context Window Size

Choose how many messages to include:

### Small (5 messages)

**Best for**:

* Simple Q\&A bots
* Transactional interactions

**Pros**:

* Faster processing
* Less API overhead

**Cons**:

* Limited context
* May forget earlier conversation

### Medium (10 messages - default)

**Best for**:

* General-purpose bots
* Customer support
* Balanced applications

**Pros**:

* Good context retention
* Handles most conversations well

**Cons**:

* May lose very early context

### Large (20+ messages)

**Best for**:

* Complex conversations
* Technical support
* Detailed interactions

**Pros**:

* Excellent context retention
* Coherent long conversations
* Better understanding

**Cons**:

* Slower processing

## Configuration

<Steps>
  <Step title="Go to Bot Settings">
    Navigate to your bot's settings page.
  </Step>

  <Step title="Find Context Settings">
    Look for **Message History** or **Context Window** setting.
  </Step>

  <Step title="Set Message Count">
    Enter the number of messages (e.g., 10).
  </Step>

  <Step title="Save Changes">
    Click **Save** to apply the new setting.
  </Step>
</Steps>

Changes apply to new messages immediately. Existing conversations keep their current context.

## How It Works

### Message Retrieval

When a user sends a message:

1. **Identify Conversation**: Use message detection to find conversation
2. **Fetch History**: Retrieve last N messages from database
3. **Format Messages**: Convert to AI backend format
4. **Send Request**: Include history in integration request
5. **Generate Response**: AI uses full context
6. **Save Response**: Add bot response to history

### Message Format

Messages are formatted as an array:

```json theme={null}
[
  {"role": "user", "content": "First message"},
  {"role": "assistant", "content": "Bot response"},
  {"role": "user", "content": "Follow up"},
  {"role": "assistant", "content": "Another response"},
  {"role": "user", "content": "Latest message"}
]
```

This follows OpenAI/Anthropic standards for easy integration.

## Message Pruning

When conversations exceed context window:

### Sliding Window

Oldest messages are dropped:

```
Context window: 5 messages
Messages: [1, 2, 3, 4, 5, 6, 7]
Sent to AI: [3, 4, 5, 6, 7]
```

Messages 1 and 2 are not included but remain in database.

### System Messages

System messages (if configured) are always included:

```
Messages sent:
1. System: "You are a helpful assistant"
2-6. Last 5 conversation messages
```

System message doesn't count toward context window.

## Best Practices

### Start Small, Increase if Needed

Begin with default (10 messages):

* Check if context is lost
* Increase if bot seems forgetful

### Consider Use Case

**Support Bots**: Medium to large (10-20)

* Need full problem context
* Multiple back-and-forth exchanges

**FAQ Bots**: Small (5)

* Simple questions and answers
* No long conversations

**Sales Bots**: Medium (10-15)

* Build relationship over conversation
* Remember user preferences

## Troubleshooting

### Bot Forgets Earlier Context

**Symptoms**:

* Repeats questions
* Loses conversation thread
* Contradicts earlier responses

**Solutions**:

* Increase context window
* Check message detection is working
* Verify conversation grouping is correct

## Next Steps

<CardGroup cols={2}>
  <Card title="Should Respond" icon="robot" href="/bots/behaviors/should-respond">
    Configure when bot responds
  </Card>

  <Card title="View Conversations" icon="messages" href="/bots/conversations/index">
    See conversation history
  </Card>
</CardGroup>
