Conversation.set_context_window()

Limit how many recent messages get_context_messages() returns.

Usage

Source

Conversation.set_context_window(max_length)

When working with LLMs that have limited context, setting a window ensures only the most recent messages are sent, keeping token usage under control while preserving conversational continuity.

Parameters

max_length: int
The maximum number of messages to include in the context window.

Examples

from talk_box.conversation import Conversation

convo = Conversation()
for i in range(10):
    convo.add_user_message(f"Message {i}")
convo.set_context_window(3)
len(convo.get_context_messages())
3