Node Caching
Cache results of individual nodes to skip redundant computation. cache_policy on compile(). Cache invalidation strategies.
Quick Reference
- →cache_policy: set on compile() to enable caching for specific nodes based on input state
- →Cache key: derived from the node name and input state — identical inputs produce cache hits
- →Skip redundant LLM calls: if a node's input state hasn't changed, return the cached result instantly
- →TTL-based invalidation: set expiration times on cached results to ensure freshness
- →Selective caching: only cache expensive nodes (LLM calls, API requests) — skip cheap pure functions
How Node Caching Works
Cache key = node name + input state hash. When a node's input state matches a previous execution, the cached result is returned instantly — no LLM call, no API request.
Node caching intercepts the graph execution loop at the node level. Before executing a node, LangGraph computes a cache key by hashing the node name and the relevant input state fields. If a matching key exists in the cache and hasn't expired, the cached output is returned directly -- the node function never runs. This is transparent to the rest of the graph: downstream nodes receive the cached result exactly as if the node had executed normally. Caching is especially valuable in iterative agent loops where the same node may be called multiple times with unchanged input (e.g., an agent that re-evaluates the same data after a failed tool call).