How to Intermediate · 4 min read

How to implement agent memory with LangGraph

Quick answer
Use LangGraph to create a graph-based memory structure that stores agent interactions and context. Integrate this memory with your agent's reasoning loop to retrieve and update relevant information dynamically during conversations or tasks.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install langgraph openai

Setup

Install LangGraph and set your environment variables for the OpenAI API key.

bash
pip install langgraph openai

Step by step

This example demonstrates creating a simple agent memory graph, adding conversation nodes, and querying memory to inform agent responses.

python
import os
from langgraph import LangGraph
from openai import OpenAI

# Initialize OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Initialize LangGraph memory
memory = LangGraph()

# Add initial memory nodes
memory.add_node("user_intent", "User wants to book a flight.")
memory.add_node("previous_booking", "User booked a flight to NYC last month.")

# Function to query memory and generate response

def agent_response(user_input):
    # Retrieve relevant memory nodes
    context = memory.query(user_input, top_k=2)
    context_text = " ".join(node.content for node in context)

    # Compose prompt with memory context
    prompt = f"Context: {context_text}\nUser: {user_input}\nAgent:" 

    # Call OpenAI chat completion
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": prompt}]
    )
    answer = response.choices[0].message.content

    # Update memory with new interaction
    memory.add_node("conversation", f"User: {user_input} | Agent: {answer}")

    return answer

# Example usage
print(agent_response("Can you help me find a flight to Boston next week?"))
output
Can you please provide your preferred dates and airline? I can help you find the best options.

Common variations

  • Use asynchronous calls with asyncio for non-blocking memory queries and API calls.
  • Switch to different LLM models like claude-3-5-sonnet-20241022 by Anthropic for improved coding or reasoning.
  • Customize memory node types to store structured data like entities, intents, or user preferences.

Troubleshooting

  • If memory queries return irrelevant nodes, increase top_k or improve node content quality.
  • For API rate limits, implement exponential backoff or caching of frequent queries.
  • Ensure environment variables are correctly set to avoid authentication errors.

Key Takeaways

  • Use LangGraph to build a persistent, queryable memory graph for agents.
  • Integrate memory context dynamically into prompts to improve agent responses.
  • Customize memory nodes and retrieval parameters to fit your agent's domain.
  • Async calls and model switching enhance performance and capabilities.
  • Handle API limits and memory relevance with caching and tuning.
Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022
Verify ↗