Skip to main content

MessageTokenLimiter

Truncates messages to meet token limits for efficient processing and response generation.
This transformation applies two levels of truncation to the conversation history:
1. Truncates each individual message to the maximum number of tokens specified by max_tokens_per_message.
2. Truncates the overall conversation history to the maximum number of tokens specified by max_tokens.
NOTE: Tokens are counted using the encoder for the specified model. Different models may yield different token counts for the same text.
NOTE: For multimodal LLMs, the token count may be inaccurate as it does not account for the non-text input (e.g images).
The truncation process follows these steps in order:
1. The minimum tokens threshold (min_tokens) is checked (0 by default). If the total number of tokens in messages is less than this threshold, then the messages are returned as is. In other case, the following process is applied.
2. Messages are processed in reverse order (newest to oldest).
3. Individual messages are truncated based on max_tokens_per_message. For multimodal messages containing both text and other types of content, only the text content is truncated.
4. The overall conversation history is truncated based on the max_tokens limit. Once the accumulated token count exceeds this limit, the current message being processed get truncated to meet the total token count and any remaining messages get discarded.
5. The truncated conversation history is reconstructed by prepending the messages to a new list to preserve the original message order.
Parameters:

Instance Methods

apply_transform

Applies token truncation to the conversation history.
Parameters: Returns:

get_logs

Parameters: