merge_message_runs is a utility that merges consecutive messages from the same role into a single message, reducing token usage and simplifying conversation history when multiple messages of the same type appear back-to-back.
In some scenarios, you may end up with consecutive messages of the same type (e.g., two HumanMessage in a row, or multiple AIMessage without an intervening ToolMessage). This can happen during retries or due to certain agent flows. merge_message_runs combines these adjacent messages by concatenating their content. This reduces the number of messages sent to the LLM, saving tokens and making the history cleaner. You would use this when you want to minimize token consumption and you don't need to preserve the exact boundary between same-role messages.