if you want your own middleware, use RemoveMessage(id=REMOVE_ALL_MESSAGES) before the new messages to clear the existing state, then add the summary + recent messages:
return {
"messages": [
RemoveMessage(id=REMOVE_ALL_MESSAGES),
HumanMessage(content=f"Summary:\n\n{result.content}"),
*messages[-5:],
]
}
summarize all messages except the last 5