Implement sliding window memory by using a custom reducer or a node that trims the message list to the last N messages, while always preserving the first system message (if present). You can also use LangChain's trim_messages utility inside a node.
To keep only the last N messages while preserving the system prompt, you can write a custom reducer function or a dedicated node that runs after each update. The reducer approach is more efficient because it runs during state updates. The logic is: filter the message list to keep the system message (if any) plus the last N messages from the end, ensuring the system message is never dropped even if it falls outside the window.