MongoDB Atlas를 이용한 단기 기억
MongoDB Atlas를 이용한 단기 기억 (Short-term memory with MongoDB Atlas)
MongoDBSaver와 MongoDB Atlas로 에이전트의 단기 기억을 영속화하는 방법을 설명할게요. 단기 기억은 에이전트가 단일 스레드나 대화 안에서 이전 상호작용을 기억하게 해주고, 이 가이드에서는 영속 체크포인터 백엔드로 MongoDB Atlas를 쓰는 법을 보여줘요. MongoDB는 대화 상태를 컬렉션의 문서로 저장하므로, 프로세스 재시작과 배포를 넘나들며 스레드를 이어갈 수 있어요.
대화를 넘어서 정보를 기억해야 한다면, 서로 다른 스레드와 세션에 걸쳐 데이터를 저장·조회하는 long-term memory with MongoDB Atlas 문서를 확인해 보세요.
출처: 공식문서
설정 (Setup)
설치 (Installation)
pip install -U langgraph-checkpoint-mongodb pymongo
uv를 쓴다면:
uv add langgraph-checkpoint-mongodb pymongo
자격증명 (Credentials)
Atlas 연결 문자열을 설정해요.
import getpass
import os
if not os.environ.get("MONGODB_ATLAS_URI"):
os.environ["MONGODB_ATLAS_URI"] = getpass.getpass("Enter your MongoDB Atlas URI: ")
모델 호출을 자동 추적하고 싶다면 LangSmith API 키를 설정해요.
if not os.environ.get("LANGSMITH_API_KEY"):
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
os.environ["LANGSMITH_TRACING"] = "true"
사용 (Usage)
from_conn_string으로 MongoDBSaver를 만들고 create_agent에 전달해요. 컨텍스트 매니저가 열릴 때 필요한 컬렉션과 인덱스를 만들어요. Postgres 체크포인터와 달리 별도의 setup() 메서드를 호출하지 않아요.
import os
from langchain.agents import create_agent
from langgraph.checkpoint.mongodb import MongoDBSaver
def get_user_info() -> str:
"""Look up information about the current user."""
return "No user profile on file."
MONGODB_ATLAS_URI = os.environ["MONGODB_ATLAS_URI"]
with MongoDBSaver.from_conn_string(MONGODB_ATLAS_URI) as checkpointer:
agent = create_agent(
"anthropic:claude-sonnet-4-6",
tools=[get_user_info],
checkpointer=checkpointer,
)
thread_config = {"configurable": {"thread_id": "1"}}
response = agent.invoke(
{"messages": [{"role": "user", "content": "Hi! My name is Bob."}]},
thread_config,
)["messages"][-1].content
print(response)
response = agent.invoke(
{"messages": [{"role": "user", "content": "What's my name?"}]},
thread_config,
)["messages"][-1].content
print(response)
MongoDBSaver는 async 그래프에서 사용할 aget, aput 같은 async 메서드도 제공해요. 별도의 AsyncMongoDBSaver 클래스는 없어요.
다음 단계 (Next steps)
- short-term memory 가이드에서 에이전트 상태 커스터마이징, 기록 정리·요약, 도구에서 메모리 접근을 배워요. 같은
MongoDBSaver를checkpointer로 전달하면 돼요. - 다른 백엔드는 checkpointer integrations 목록을 확인해요.
- 스레드를 넘나드는 영속성이 필요하다면 long-term memory with MongoDB Atlas를 보세요.
더 알아보기 (Learn more)
- MongoDB Atlas 장기 기억 (Long-term memory with MongoDB Atlas) — 스레드 간 정보 저장
- 단기 기억 가이드 (Short-term memory) — 상태 커스터마이징과 기록 정리
- 체크포인터 통합 목록 — 다른 백엔드