In the event you’re studying LangGraph or exploring extra about it then it’s good to know in regards to the pre-built node-level caching in LangGraph. Caching not solely eliminates pointless computation but additionally fastens the latency. We’ll be trying on the implementation of the identical within the article. It’s assumed that you’ve got an thought about brokers and nodes in LangGraph as we received’t be specializing in that facet of the story, so with none additional ado let’s stroll into the ideas and implementation.
What’s Caching?
Caching shops information in short-term storage so the system can retrieve it rapidly. Within the context of LLMs and AI Brokers, it saves earlier requests and reuses them when the identical prompts are despatched to the mannequin or agent. As a result of it’s not a brand new request, the system doesn’t cost for it, and the response arrives sooner as a result of short-term reminiscence. When a part of a immediate stays the identical, the system reuses the earlier response and generates a brand new one just for the extra half, which considerably reduces prices even for brand new requests.
Caching parameters and reminiscence
It’s vital to know in regards to the ttl (time to reside) parameter which is used to outline the period of time (in seconds) the cache will stay within the reminiscence. If we set ttl=None or depart it as it’s then the cache won’t ever depart the reminiscence.
We have to specify a cache when compiling a graph. We’ll use InMemoryCache to retailer the node’s inputs and outputs that can be utilized later to retrieve the node’s earlier response on this article. Alternatively, you may implement SqliteCache, redisCache or customized cache as properly relying on the wants.
Caching in Motion
Let’s implement Node-level-caching for a operate that helps convert celsius to fahrenheit.
Step 1: Installations
!pip set up langgraph
Step 2: Defining the Graph
We’ll first outline the graph construction and a easy operate that simulates a sluggish computation utilizing time.sleep() to make the caching impact seen.
import time
from typing_extensions import TypedDict
from langgraph.graph import StateGraph
from langgraph.cache.reminiscence import InMemoryCache
from langgraph.varieties import CachePolicy
class State(TypedDict):
celsius: float
fahrenheit: float
builder = StateGraph(State)
def convert_temperature(state: State) -> dict[str, float]:
time.sleep(2)
fahrenheit = (state['celsius'] * 9/5) + 32
return {"fahrenheit": fahrenheit}
builder.add_node("convert_temperature", convert_temperature, cache_policy=CachePolicy(ttl=None))
builder.set_entry_point("convert_temperature")
builder.set_finish_point("convert_temperature")
cache=InMemoryCache()
graph = builder.compile(cache=cache)
Step 3: Invoking the Graph
Now, let’s invoke the graph a number of occasions and observe how the cache behaves.
print(graph.invoke({"celsius": 25}))
print(graph.invoke({"celsius": 25}, stream_mode="updates")) # Cached
print(graph.invoke({"celsius": 36}, stream_mode="updates"))
time.sleep(10)
print(graph.invoke({"celsius": 36}, stream_mode="updates"))
cache.clear() # clears your complete cache
Output
{'celsius': 25, 'fahrenheit': 77.0} [{'convert_temperature': {'fahrenheit': 77.0}, '__metadata__': {'cached': True}}]
[{'convert_temperature': {'fahrenheit': 96.8}}]
[{'convert_temperature': {'fahrenheit': 96.8}}]
The system fetches the response from the cache on the primary repeated request and the TTL is about to five seconds. It treats the following repeated request as a brand new one when the hole exceeds the TTL. We used cache.clear() to clear your complete cache, that is helpful once we set ttl=None.
Now, let’s implement the caching for node with an agent.
Conditions: Caching for Node with an Agent
We’ll want a Gemini API key to make use of Gemini fashions within the agent, go to Google AI Studio to get your API key: https://aistudio.google.com/api-keys
Installations
The langchain_google_genai module will assist us combine the Gemini fashions within the node.
!pip set up langgraph langchain_google_genai
Agent definition
Let’s outline a basic math agent that has entry to the calculator instrument and we’ll set the ttl=None for now.
from langgraph.prebuilt import create_react_agent
from langchain_google_genai import ChatGoogleGenerativeAI
def solve_math_problem(expression: str) -> str:
"""Resolve a math downside."""
strive:
# Consider the mathematical expression
outcome = eval(expression, {"__builtins__": {}})
return f"The reply is {outcome}."
besides Exception:
return "I could not clear up that expression."
# Initialize the Gemini mannequin with API key
mannequin = ChatGoogleGenerativeAI(
mannequin="gemini-2.5-flash",
google_api_key=GOOGLE_API_KEY
)
# Create the agent
agent = create_react_agent(
mannequin=mannequin,
instruments=[solve_math_problem],
immediate=(
"You're a Math Tutor AI. "
"When a person asks a math query, motive by way of the steps clearly "
"and use the instrument `solve_math_problem` for numeric calculations. "
"All the time clarify your reasoning earlier than giving the ultimate reply."
),
)
Defining the node
Subsequent, we’ll wrap the agent inside a LangGraph node and connect caching to it.
import time
from typing_extensions import TypedDict
from langgraph.graph import StateGraph
from langgraph.cache.reminiscence import InMemoryCache
from langgraph.varieties import CachePolicy
class AgentState(TypedDict):
immediate: str
response: str
builder = StateGraph(AgentState)
def run_agent(state: AgentState) -> AgentState:
print("Operating agent...") # this line helps present caching habits
response = agent.invoke({"messages": [{"role": "user", "content": state["prompt"]}]})
return {"response": response}
builder.add_node("run_agent", run_agent, cache_policy=CachePolicy(ttl=None))
builder.set_entry_point("run_agent")
builder.set_finish_point("run_agent")
graph = builder.compile(cache=InMemoryCache())
Invoking the agent
Lastly, let’s name the agent twice to see caching in motion.
# Invoke graph twice to see caching
print("First name")
result1 = graph.invoke({"immediate": "What's (12 + 8) * 3?"},stream_mode="updates")
print(result1)
print("Second name (needs to be cached)")
result2 = graph.invoke({"immediate": "What's (12 + 8) * 3?"},stream_mode="updates")
print(result2)
Output:

Discover how the second name doesn’t have ‘Operating agent..’ which is a print assertion within the node. So we managed to get the response from the agent with out operating the agent utilizing the cache reminiscence.
Conclusion
LangGraph’s built-in node-level caching offers a easy but highly effective approach to scale back latency and computation by reusing earlier outcomes. With parameters like ttl to handle cache lifetime and choices comparable to InMemoryCache, SqliteCache, or RedisCache, it provides flexibility primarily based on use instances. By examples like temperature conversion to agent-based nodes! We noticed how caching avoids redundant execution and saves value. General, caching in LangGraph enormously improves effectivity, making workflows sooner and extra optimized.
Incessantly Requested Questions
A. The key_func parameter defines how LangGraph generates a singular cache key for every node’s enter. By default, it makes use of the node’s enter values to create this key. You possibly can override it to customise caching habits. For instance, to disregard particular fields or normalize inputs earlier than comparability.
A. You possibly can manually clear the cache anytime utilizing cache.clear(). This removes all saved node responses, forcing LangGraph to re-execute the nodes on the following name. It’s helpful throughout debugging, when working with dynamic inputs, or when the cached information turns into outdated.
A. Sure, every node can have its personal CachePolicy with a customized ttl worth. This lets you cache heavy or sluggish computations longer whereas preserving incessantly altering nodes recent. High-quality-tuning TTL values helps stability efficiency, accuracy, and reminiscence effectivity in giant graphs.
Login to proceed studying and luxuriate in expert-curated content material.
