[PR #2876] Add Node Caching Support #2970

Closed
opened 2026-02-20 17:47:52 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langchain-ai/langgraph/pull/2876

State: closed
Merged: No


Users of the StateGraph API can optionally cache the outputs of their nodes by providing a cache_policy to StateGraph::add_node.

builder = StateGraph(State)
builder.add_node(
    node_fn, 
    cache_policy=CachePolicy(
        key=operator.itemgetter("x"), # Mandatory
        ttl=NODE_CACHE_TTL,           # Optional
    ),
)
graph = builder.compile(checkpointer=checkpointer)

Where key is a one argument function that takes the input state of the graph and returns a cache_key as a string.

Important:

  • Node caching requires a checkpoint.
  • Caches are thread scoped

🪚 Sharp Edges 🪚:

  • If two graphs use the same runnable name, the same thread id, and the same cache key, there will be cache contamination 😷.
  • In the same multi-step process, Send calls with identical input will be deduplicated. However, in the V2 API, this behavior is more complex—the node might still execute because it is scheduled in the same iteration it was created, rather than the next one.
**Original Pull Request:** https://github.com/langchain-ai/langgraph/pull/2876 **State:** closed **Merged:** No --- Users of the `StateGraph` API can optionally cache the outputs of their nodes by providing a `cache_policy` to `StateGraph::add_node`. ``` builder = StateGraph(State) builder.add_node( node_fn, cache_policy=CachePolicy( key=operator.itemgetter("x"), # Mandatory ttl=NODE_CACHE_TTL, # Optional ), ) graph = builder.compile(checkpointer=checkpointer) ``` Where `key` is a one argument function that takes the input state of the graph and returns a `cache_key` as a _string_. Important: - Node caching requires a `checkpoint`. - Caches are `thread` scoped 🪚 Sharp Edges 🪚: - If two graphs use the same runnable name, the same thread id, and the same cache key, there will be cache contamination 😷. - In the same multi-step process, Send calls with identical input will be deduplicated. However, in the V2 API, this behavior is more complex—the node might still execute because it is scheduled in the same iteration it was created, rather than the next one.
yindo added the pull-request label 2026-02-20 17:47:52 -05:00
yindo closed this issue 2026-02-20 17:47:52 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#2970