{
  "schema_version": 2,
  "id": "integrate/langgraph",
  "title": "Redis with LangGraph",
  "url": "https://redis.io/docs/latest/integrate/langgraph/",
  "summary": "Persist LangGraph agent state, long-term memory, and cached model calls in Redis.",
  "tags": [
    "docs",
    "integrate",
    "oss",
    "rs",
    "rc"
  ],
  "last_updated": "2026-11-18T14:45:43Z",
  "children": [],
  "page_type": "content",
  "content_hash": "3fc4adb2aade22682b7125e30eee1d93331e245d3a43d228faa09cb6da988280",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "[LangGraph](https://docs.langchain.com/oss/python/langgraph/overview) is a Python framework for building stateful AI agents as graphs. The [`langgraph-checkpoint-redis`](https://github.com/redis-developer/langgraph-redis) package stores that state in Redis. It has three parts:\n\n| Part | Use it to |\n|------|-----------|\n| **Checkpointers** | Save graph state after every step, so a thread can resume, replay, or recover from a failure. |\n| **Stores** | Keep long-term memories that are shared across threads, with optional vector search. |\n| **Middleware** | Cache model responses by semantic similarity, cache tool results, and recall past messages for a LangChain agent. |\n\nThe package runs on your own Redis database. To use a managed memory service instead, see [Redis Agent Memory](https://redis.io/docs/latest/develop/ai/context-engine/agent-memory), which has its own [LangGraph integration](https://redis.io/docs/latest/develop/ai/context-engine/agent-memory/developer-guide)."
    },
    {
      "id": "requirements",
      "title": "Requirements",
      "role": "content",
      "text": "- Python 3.10 to 3.14\n- Redis with [JSON](https://redis.io/docs/latest/develop/data-types/json) and the [Redis Query Engine](https://redis.io/docs/latest/develop/ai/search-and-query). Redis 8 and later includes both. Earlier versions need Redis Stack.\n\nInstall the package:\n\n[code example]"
    },
    {
      "id": "save-graph-state-with-a-checkpointer",
      "title": "Save graph state with a checkpointer",
      "role": "content",
      "text": "Create a `RedisSaver`, call `setup()` once to create its indexes, and pass it to `compile()`:\n\n[code example]\n\nEach `thread_id` has its own saved state. A new `RedisSaver` that connects to the same database reads it back, so a thread survives a restart of your application.\n\nFor async applications, use `AsyncRedisSaver` and `await checkpointer.asetup()`:\n\n[code example]\n\n`ShallowRedisSaver` and its async counterpart keep only the latest checkpoint for each thread. Use them when you don't need history or replay. They are in `langgraph.checkpoint.redis.shallow` and `langgraph.checkpoint.redis.ashallow`."
    },
    {
      "id": "expire-old-checkpoints",
      "title": "Expire old checkpoints",
      "role": "content",
      "text": "Pass `ttl` to remove checkpoints automatically:\n\n[code example]\n\n`default_ttl` is the time to live (TTL) in **minutes**, not seconds. With `refresh_on_read`, reading a checkpoint resets its TTL. Without `ttl`, checkpoints stay until you delete them."
    },
    {
      "id": "keep-long-term-memories-in-a-store",
      "title": "Keep long-term memories in a store",
      "role": "content",
      "text": "A checkpointer is scoped to one thread. A store holds data that any thread can read, such as a user's preferences. Namespaces organize the data:\n\n[code example]\n\n`AsyncRedisStore` in `langgraph.store.redis.aio` has the same methods with an `a` prefix, such as `aput` and `aget`.\n\nTo search memories by meaning, pass an `index` configuration with an embeddings model. This example uses OpenAI embeddings. Any LangChain `Embeddings` object works if `dims` matches the model's output size:\n\n[code example]\n\nStores accept the same `ttl` setting as checkpointers."
    },
    {
      "id": "cache-model-calls-with-middleware",
      "title": "Cache model calls with middleware",
      "role": "content",
      "text": "The middleware plugs into LangChain's `create_agent()`. It needs the `langchain` package, which `langgraph-checkpoint-redis` doesn't install for you:\n\n[code example]\n\n`SemanticCacheMiddleware` returns a stored response when a new prompt is close enough in meaning to an earlier one. A lower `distance_threshold` makes matches stricter. Pass a `vectorizer` that wraps your embeddings model:\n\n[code example]\n\nThe middleware is async-first, so call `ainvoke()`. By default it caches only final responses, not responses that request a tool call, and it skips the cache when a prompt includes results from tools you haven't listed in `deterministic_tools`.\n\nThe package also provides `ToolResultCacheMiddleware` for caching tool results, `ConversationMemoryMiddleware` for recalling relevant earlier messages, and `SemanticRouterMiddleware`. For their options, see the [package README](https://github.com/redis-developer/langgraph-redis#redis-middleware) and the [middleware example notebooks](https://github.com/redis-developer/langgraph-redis/tree/main/examples/middleware)."
    },
    {
      "id": "upgrade-between-versions",
      "title": "Upgrade between versions",
      "role": "content",
      "text": "Checkpoints written by 0.1.x can't be read by 0.2.0 and later. `get_state()` returns nothing for them. To move to a newer version, start fresh, run the old and new versions side by side until old threads finish, or write a migration script. See the [0.2.0 migration guide](https://github.com/redis-developer/langgraph-redis/blob/main/MIGRATION_0.2.0.md)."
    },
    {
      "id": "more-info",
      "title": "More info",
      "role": "content",
      "text": "- [langgraph-redis on GitHub](https://github.com/redis-developer/langgraph-redis), including [example notebooks](https://github.com/redis-developer/langgraph-redis/tree/main/examples)\n- [langgraph-checkpoint-redis on PyPI](https://pypi.org/project/langgraph-checkpoint-redis/)\n- [LangGraph Redis checkpoint 0.1.0 announcement](https://redis.io/blog/langgraph-redis-checkpoint-010/), which covers the storage redesign and benchmarks\n- [Redis with LangChain](https://redis.io/docs/latest/integrate/langchain-redis)\n- [Redis Agent Memory](https://redis.io/docs/latest/develop/ai/context-engine/agent-memory)"
    }
  ],
  "examples": [
    {
      "id": "requirements-ex0",
      "language": "bash",
      "code": "pip install langgraph-checkpoint-redis",
      "section_id": "requirements"
    },
    {
      "id": "save-graph-state-with-a-checkpointer-ex0",
      "language": "python",
      "code": "from typing import TypedDict\n\nfrom langgraph.graph import END, START, StateGraph\nfrom langgraph.checkpoint.redis import RedisSaver\n\n\nclass State(TypedDict):\n    count: int\n\n\ndef increment(state: State):\n    return {\"count\": state[\"count\"] + 1}\n\n\nbuilder = StateGraph(State)\nbuilder.add_node(\"increment\", increment)\nbuilder.add_edge(START, \"increment\")\nbuilder.add_edge(\"increment\", END)\n\nwith RedisSaver.from_conn_string(\"redis://localhost:6379\") as checkpointer:\n    checkpointer.setup()\n    graph = builder.compile(checkpointer=checkpointer)\n\n    config = {\"configurable\": {\"thread_id\": \"thread-1\"}}\n    graph.invoke({\"count\": 0}, config)\n    print(graph.get_state(config).values)",
      "section_id": "save-graph-state-with-a-checkpointer"
    },
    {
      "id": "save-graph-state-with-a-checkpointer-ex1",
      "language": "python",
      "code": "from langgraph.checkpoint.redis.aio import AsyncRedisSaver\n\nasync with AsyncRedisSaver.from_conn_string(\"redis://localhost:6379\") as checkpointer:\n    await checkpointer.asetup()\n    graph = builder.compile(checkpointer=checkpointer)\n    await graph.ainvoke({\"count\": 0}, config)",
      "section_id": "save-graph-state-with-a-checkpointer"
    },
    {
      "id": "expire-old-checkpoints-ex0",
      "language": "python",
      "code": "ttl = {\"default_ttl\": 60, \"refresh_on_read\": True}\n\nwith RedisSaver.from_conn_string(\"redis://localhost:6379\", ttl=ttl) as checkpointer:\n    checkpointer.setup()",
      "section_id": "expire-old-checkpoints"
    },
    {
      "id": "keep-long-term-memories-in-a-store-ex0",
      "language": "python",
      "code": "from langgraph.store.redis import RedisStore\n\nwith RedisStore.from_conn_string(\"redis://localhost:6379\") as store:\n    store.setup()\n    store.put((\"users\", \"user-1\", \"memories\"), \"m1\", {\"text\": \"likes green tea\"})\n\n    item = store.get((\"users\", \"user-1\", \"memories\"), \"m1\")\n    print(item.value)",
      "section_id": "keep-long-term-memories-in-a-store"
    },
    {
      "id": "keep-long-term-memories-in-a-store-ex1",
      "language": "python",
      "code": "from langchain_openai import OpenAIEmbeddings\n\nindex = {\n    \"dims\": 1536,\n    \"distance_type\": \"cosine\",\n    \"fields\": [\"text\"],\n    \"embed\": OpenAIEmbeddings(model=\"text-embedding-3-small\"),\n}\n\nwith RedisStore.from_conn_string(\"redis://localhost:6379\", index=index) as store:\n    store.setup()\n    store.put((\"users\", \"user-1\", \"memories\"), \"m1\", {\"text\": \"likes green tea\"})\n\n    results = store.search((\"users\", \"user-1\", \"memories\"), query=\"favorite drink\", limit=3)",
      "section_id": "keep-long-term-memories-in-a-store"
    },
    {
      "id": "cache-model-calls-with-middleware-ex0",
      "language": "bash",
      "code": "pip install langchain",
      "section_id": "cache-model-calls-with-middleware"
    },
    {
      "id": "cache-model-calls-with-middleware-ex1",
      "language": "python",
      "code": "from langchain.agents import create_agent\nfrom langchain_core.messages import HumanMessage\nfrom langchain_openai import OpenAIEmbeddings\nfrom langgraph.middleware.redis import (\n    LangChainVectorizer,\n    SemanticCacheConfig,\n    SemanticCacheMiddleware,\n)\n\nvectorizer = LangChainVectorizer(\n    embeddings=OpenAIEmbeddings(model=\"text-embedding-3-small\"),\n    dims=1536,\n)\n\nsemantic_cache = SemanticCacheMiddleware(\n    SemanticCacheConfig(\n        redis_url=\"redis://localhost:6379\",\n        name=\"llm_cache\",\n        distance_threshold=0.15,\n        ttl_seconds=3600,\n        vectorizer=vectorizer,\n    )\n)\n\nagent = create_agent(\n    model=\"openai:gpt-4o-mini\",\n    tools=[],\n    middleware=[semantic_cache],\n)\n\nresult = await agent.ainvoke({\"messages\": [HumanMessage(content=\"What is Redis?\")]})",
      "section_id": "cache-model-calls-with-middleware"
    }
  ]
}
