본문 바로가기
Q&A해결2026년 4월 23일1분 읽기

LangChain → LangGraph 마이그레이션 시 메모리 추적이 안 됩니다

L
LLM초보
조회 598댓글 1

LangChain의 ConversationChain + ConversationBufferMemory로 멀티턴 챗봇을 운영 중입니다. LangGraph로 마이그레이션하려는데 기존 메모리 객체를 그대로 가져갈 수 없네요.

LangGraph의 checkpointer가 비슷한 역할인 것 같은데, 기존 사용자 대화 히스토리를 어떻게 옮길지, 마이그레이션 중 끊김 없이 가능한지 알려주세요.

댓글 1

김김영삼7개월 전

LangChain → LangGraph 마이그레이션은 단순한 라이브러리 교체가 아니라 "체인" 사고에서 "그래프" 사고로의 전환입니다. 메모리 처리는 그 중 가장 큰 차이입니다.

개념 매핑

LangChainLangGraph 대응ConversationChainStateGraphConversationBufferMemoryState + CheckpointerLLMChainNode 함수SequentialChainedge로 노드 연결memory.save_contextstate['messages'].append

1) State 정의로 메모리 표현

from typing import Annotated
from typing_extensions import TypedDict
from langgraph.graph.message import add_messages

class ChatState(TypedDict):
messages: Annotated[list, add_messages]
user_id: str

# add_messages는 자동으로 history 누적

2) Checkpointer로 영속화

from langgraph.checkpoint.postgres import PostgresSaver

# DB 기반 — production 권장
checkpointer = PostgresSaver.from_conn_string(DATABASE_URL)
checkpointer.setup() # 최초 1회

graph = builder.compile(checkpointer=checkpointer)

# 사용자별 thread_id로 분리
config = {"configurable": {"thread_id": user_id}}
result = graph.invoke({"messages": [...]}, config=config)

3) 기존 LangChain 히스토리 마이그레이션

import asyncio
from langgraph.checkpoint.postgres import PostgresSaver

async def migrate(user_id: str, old_messages: list):
cp = PostgresSaver.from_conn_string(DATABASE_URL)

# LangChain BaseMessage → LangGraph 형식
converted = [
("user", m.content) if m.type == "human"
else ("assistant", m.content)
for m in old_messages
]

config = {"configurable": {"thread_id": user_id}}
state = {"messages": converted, "user_id": user_id}

# 새 thread 첫 체크포인트로 저장
await graph.aupdate_state(config, state)

4) 무중단 점진적 마이그레이션

전체를 한 번에 옮기지 말고 dual-write 단계 두기:

async def hybrid_chat(user_id, message):
# 신규 thread는 LangGraph
if await is_migrated(user_id):
return await graph_chat(user_id, message)
# 미마이그레이션 thread는 LangChain
else:
result = await langchain_chat(user_id, message)
# 백그라운드로 점진적 이전
if random() < 0.05:
asyncio.create_task(migrate(user_id, get_history(user_id)))
return result

실무 체크리스트

  1. checkpointer는 PostgresSaver 권장 (Sqlite는 단일 노드 한정)
  2. thread_id 키 설계 — user_id + session_id 조합 추천
  3. 대용량 history → MessagesPlaceholder + 자동 trimming (trim_messages)
  4. 마이그레이션 후 LangChain memory 테이블 30일 보존 후 삭제
  5. 관측성 — LangSmith trace로 노드별 latency·token 사용량 추적

주의 — LangGraph 함정

  • Checkpoint가 매 노드마다 저장되어 DB write 폭증 가능 → batching 옵션 검토
  • Streaming 출력은 graph.astream(...) 별도 API — invoke와 다름
  • Cycle 그래프(반복 호출) 시 max_iterations 명시 필수 — 무한루프 방지

마지막 권장: LangGraph의 그래프 구조는 분기·검증·human-in-the-loop이 필요한 워크플로에서 진가를 발휘합니다. 단순 챗봇이라면 LangChain의 새 LCEL(RunnableWithMessageHistory)도 충분히 좋습니다 — 마이그레이션 자체가 목적이 되지 않게 주의하세요.

Ctrl+Enter로 등록