TrimHistory Method  
 

The TrimHistory method removes older messages from the conversation history.

Syntax

object.TrimHistory( MessageCount, [MaxTokens] )

Parameters

MessageCount
The number of messages to remove from the beginning of the conversation history. If this value is zero, messages are only removed if required by the MaxTokens parameter. If this value is equal to or greater than the number of messages in the history, the entire history is cleared.
MaxTokens
An optional Integer parameter which specifies the maximum number of input tokens that should remain in the conversation history. If this parameter is omitted or the value is zero, token usage is not checked. If the current history exceeds this value, additional older messages may be deleted until the total input token count is below the specified limit.

Return Value

An Integer value which specifies the number of messages that were removed from the history. A value of -1 indicates an error has occurred and the LastError property will contain the error code. If there are no messages in the conversation history, or the history has been disabled, the method will return zero.

Remarks

The TrimHistory method removes messages from the beginning of the conversation history. This can be used to reduce memory usage, discard older context, or limit how much previous conversation text is included with future requests.

The MessageCount parameter specifies the minimum number of older messages to remove. If MaxTokens is greater than zero, the method also checks the total number of input tokens currently stored in the message history. If the token count exceeds the specified limit, additional older messages will be removed until the total input token count is below the specified threshold.

When both parameters are specified, the method may remove more messages than requested by MessageCount to reduce the token count below the limit specified by MaxTokens. The return value always indicates the actual number of messages removed.

If the number of messages to remove is equal to or greater than the number of messages currently stored, the entire conversation history is cleared. This is equivalent to calling the ClearHistory method. If you remove any messages from the history method while enumerating the messages using the GetFirstMessage and GetNextMessage methods, you must call GetFirstMessage again to begin a new enumeration.

This method only removes the local message and response history maintained by the client session. It does not change the current provider, model, system prompt, connection settings or cached provider metadata.

See Also

HistorySize Property, MessageCount Property, ClearHistory Method, GetFirstMessage Method, GetNextMessage Method