Caching
When to Use
Basic Pattern
from hypergraph import Graph, node, SyncRunner, InMemoryCache
@node(output_name="embedding", cache=True)
def embed(text: str) -> list[float]:
# Expensive API call — only runs once per unique input
return model.embed(text)
@node(output_name="answer")
def generate(embedding: list[float], query: str) -> str:
return llm.generate(embedding, query)
graph = Graph(nodes=[embed, generate])
runner = SyncRunner(cache=InMemoryCache())
# First call — embed executes normally
result = runner.run(graph, {"text": "hello", "query": "What is this?"})
# Second call with same text — embed served from cache
result = runner.run(graph, {"text": "hello", "query": "Different question"})Cache Backends
InMemoryCache
DiskCache
Integrity Verification
Custom Backend
How Cache Keys Work
Observing Cache Hits
Caching Route and IfElse Nodes
Restrictions
InterruptNode
Real-World Example: Cached RAG Pipeline
What's Next?
Last updated