SlidingWindowTransformer

class SlidingWindowTransformer(maxMessages: Int, preserveSystemMessages: Boolean = true) : ToolLoopTransformer

Transformer that maintains a sliding window of messages to manage context size.

Applies windowing in both transformBeforeLlmCall and transformAfterIteration to ensure bounded memory and context window usage.

Tool Call/Result Grouping

LLM APIs (OpenAI, Anthropic, Google, etc.) require that every ToolResultMessage must be preceded by an AssistantMessageWithToolCalls containing the corresponding tool call ID. This is a universal constraint across all major providers.

When truncation occurs, the sliding window might cut between an AssistantMessageWithToolCalls and its ToolResultMessages, leaving "orphaned" tool results that would cause API errors like:

"messages with role 'tool' must be a response to a preceding message with 'tool_calls'"

This transformer automatically drops any orphaned ToolResultMessages that appear at the start of the non-system message sequence after truncation.

Example

Before truncation (7 messages):

[System, User, Assistant+Calls(1,2), ToolResult(1), ToolResult(2), Assistant+Calls(3), ToolResult(3)]

After naive takeLast(4) with 1 system message (INVALID):

[System, ToolResult(2), Assistant+Calls(3), ToolResult(3)]
↑ ORPHAN - no parent Assistant+Calls for call_2

After fix (VALID):

[System, Assistant+Calls(3), ToolResult(3)]
↑ Valid start - parent exists for ToolResult(3)

Parameters

maxMessages

Maximum number of messages to retain

preserveSystemMessages

When true, preserves all SystemMessages regardless of position

Constructors

Link copied to clipboard
constructor(maxMessages: Int, preserveSystemMessages: Boolean = true)

Functions

Link copied to clipboard

Transform history after iteration completes. Return modified list.

Link copied to clipboard

Transform LLM response before adding to history. Return modified message.

Link copied to clipboard

Transform tool result before adding to history. Return modified string.

Link copied to clipboard

Transform history before sending to LLM. Return modified list.