keepLast

fun keepLast(messages: Int): (List<ChatMessage>) -> List<ChatMessage>

Returns a function for shorten of toolLoop that leaves out the oldest messages of a conversation of more than messages messages, so that a long conversation stays within what a model can read:

val agent = toolAgent(model, tools = tools, shorten = keepLast(messages = 40))

The messages up to the first ChatMessage.User stay, because they usually say what the conversation is about, and after them a short message tells the model that older messages were left out. A cut never separates a tool call from its results, which a provider would refuse. These rules can leave more messages than asked for, when the last round has more results than messages.

The cut moves by half of messages at a time, so a model reads between half of messages and all of them. A cut that moved with every new message would change the start of every request, and a provider can only reuse a start that stays the same.

Parameters

messages

the most messages the model reads. At least 3.

Throws