Skip to content

Latest commit

 

History

History
73 lines (50 loc) · 1.98 KB

File metadata and controls

73 lines (50 loc) · 1.98 KB

LangGraph Memory Integration

LangGraph provides first-class support for persistent memory across graph executions. This guide covers how to integrate agent-memory-kit patterns with LangGraph's built-in persistence layer.

Overview

LangGraph uses a Checkpointer interface for persistence. Memory is stored between graph runs, enabling stateful agents, conversation history, and cross-session recall.

Basic Setup

from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import StateGraph

# In-memory checkpointer
checkpointer = MemorySaver()

# Build graph with persistence
graph = StateGraph(MyState)
graph.set_checkpointer(checkpointer)

Pattern Integrations

Sliding Window + LangGraph

from agent_memory_kit.patterns import SlidingWindowMemory

class SlidingWindowCheckpointer:
    def __init__(self, max_messages=20):
        self.memory = SlidingWindowMemory(max_messages=max_messages)

    def get(self, config):
        return self.memory.get_context()

    def put(self, config, checkpoint):
        self.memory.add(checkpoint)

Summary Compression

Use LangGraph's interrupt/resume to trigger summarization when context exceeds a threshold:

def should_summarize(state):
    return len(state["messages"]) > 10

def summarize_messages(state):
    summary = llm.invoke(f"Summarize: {state['messages']}")
    state["summary"] = summary
    state["messages"] = state["messages"][-2:]  # keep last 2
    return state

Postgres Persistence (Production)

pip install langgraph-checkpoint-postgres
from langgraph.checkpoint.postgres import PostgresSaver

conn_string = "postgresql://user:pass@localhost:5432/memory"
checkpointer = PostgresSaver.from_conn_string(conn_string)

Key Considerations

  • Use graph.update_state() to inject memory into running graphs
  • Combine with cache-aware-design strategy for token optimization
  • Episodic memory works naturally with LangGraph's thread-level persistence