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

# autogen.agentchat.contrib.capabilities.transforms.MessageHistoryLimiter

<h2 id="autogen.agentchat.contrib.capabilities.transforms.MessageHistoryLimiter" class="doc doc-heading">
  <code class="doc-symbol doc-symbol-heading doc-symbol-class" />

  <span class="doc doc-object-name doc-class-name">MessageHistoryLimiter</span>
</h2>

```python theme={null}
MessageHistoryLimiter(max_messages: int | None = None, keep_first_message: bool = False)
```

Limits the number of messages considered by an agent for response generation.<br />This transform keeps only the most recent messages up to the specified maximum number of messages (max\_messages).<br />It trims the conversation history by removing older messages, retaining only the most recent messages.<br />

<b>Parameters:</b>

| Name                 | Description                                        |
| -------------------- | -------------------------------------------------- |
| `max_messages`       | **Type:** int \| None<br /><br />**Default:** None |
| `keep_first_message` | **Type:** bool<br /><br />**Default:** False       |

### Instance Methods

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### apply\_transform

```python theme={null}
apply_transform(self, messages: list[dict[str, Any]]) -> list[dict[str, Any]]
```

Truncates the conversation history to the specified maximum number of messages.<br />This method returns a new list containing the most recent messages up to the specified
maximum number of messages (max\_messages). If max\_messages is None, it returns the
original list of messages unmodified.<br />

<b>Parameters:</b>

| Name       | Description                                                                                                    |
| ---------- | -------------------------------------------------------------------------------------------------------------- |
| `messages` | The list of messages representing the conversation history.<br /><br />**Type:** list\[dict\[str, typing.Any]] |

<b>Returns:</b>

| Type                          | Description                                                                              |
| ----------------------------- | ---------------------------------------------------------------------------------------- |
| list\[dict\[str, typing.Any]] | List\[Dict]: A new list containing the most recent messages up to the specified maximum. |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### get\_logs

```python theme={null}
get_logs(
    self,
    pre_transform_messages: list[dict[str, Any]],
    post_transform_messages: list[dict[str, Any]]
) -> tuple[str, bool]
```

<b>Parameters:</b>

| Name                      | Description                             |
| ------------------------- | --------------------------------------- |
| `pre_transform_messages`  | **Type:** list\[dict\[str, typing.Any]] |
| `post_transform_messages` | **Type:** list\[dict\[str, typing.Any]] |

<br />
