The add_messages reducer handles updates by matching on message IDs: if an incoming message has an ID that already exists in the state, it replaces the existing message; otherwise, it appends. You can also delete messages by using a special RemoveMessage object that targets a specific ID.
LangGraph's add_messages reducer is designed to work with a list of BaseMessage objects where each message has a unique id field (automatically generated by LangChain if not provided). When you add a new message to the state, the reducer iterates over the existing list. If it finds a message with the same ID, it replaces that message in-place. This allows you to update a tool's response or correct an earlier message. For deletions, you can pass a RemoveMessage object (which contains the id of the message to remove) and the reducer will filter it out. This ID-based approach is what enables append, replace, and delete operations without requiring the entire message list to be recreated.