> For the complete documentation index, see [llms.txt](https://organization-bht.gitbook.io/it/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://organization-bht.gitbook.io/it/ai/5-langgraph-agent/reducer.md). Agents can also search and read this documentation through its [MCP server](https://organization-bht.gitbook.io/it/~gitbook/mcp).

# Reducer

## Reducer

* 노드가 상태를 업데이트 할 때, 기존값을 덮어쓸 것인가? 아니면 새롭게 추가할것인가를 결정하는 함수.

**Reducer종류**

1. 기본 리듀서 :

* 이전 기록을 지우고 새 기록으로 교체하는 역할의 리듀서.
* 리듀서를 지정하지 않은 경우 자동으로 설정되는 리듀서이다.

2. add\_messages 리듀서

* 이전 기록을 유지하고 새 기록을 추가하는 역할의 리듀서

3. operator.add 리듀서

* 숫자는 더하고 리스트는 합치는 리듀서.
* \[1,2] +\[3] => \[1,2,3]

<figure><img src="https://3572862491-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUON3CGxVeoUR2YdtAhxl%2Fuploads%2FXzI2XG5sk5oCN9HT550u%2Fimage.png?alt=media&amp;token=a11ea592-390d-4245-86ed-66455cc0e3c5" alt=""><figcaption></figcaption></figure>

### 기본 Reducer

* State에 별도의 리듀서를 지정하지 않으면, 자동으로 설정되는 기본 Reducer로, 새로운 노드에서 반환한 값이 **기존 값을 완전히 대체**한다.
* 즉, `기존값 = 새로운값` 으로 덮어쓰기(**Overwrite**)하는 리듀서

### add Reducer

* 대화 기록(message)처럼 리스트형태의 상태값을 관리할때 자주 사용되는 리듀서로, 새로운 상태값이 들어오는 경우 기존 상태값에 누적하여 추가하는 방식으로 동작한다.
* 즉, 상태값을 지속적으로 **누적/추가(Append)** 하는 리듀서
* `Annotated[list, operator.add]`

### ior Reducer

* 상태가 여러 키를 가진 딕셔너리형태일 때 사용되는 리듀서로, 특정 키의 값만 수정하거나 추가하고자 할 때 사용한다.
* 즉,복잡한 상태값을 일부분 `변경(update)`하는 리듀서
* `Annotated[dict, operator.ior]`

### Custom reducer

* 개발자가 **직접 상태변경 로직을 정의**하고자 할때 사용하는 리듀서
* 특정 조건에 따라 값을 합치거나 필터링 해야하는 경우 사용한다.
