AI Hints
Home›LangGraph Tutorial›Context and Async
LangGraph · Lesson 82 of 120

Runtime Context

Per-run settings.

⏱ 2 min read▶ Video at 1:28:14🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

Some information isn't part of the state, but every node might need it. Which customer is talking. Their membership tier. Which shop branch. Runtime context passes these in for one run, without storing them in the state.

Runtime Context
From the video: Runtime Context
  • Not part of the state
  • Needed by nodes
  • Passed in per run

A discount from context

The context schema says each run brings a tier. The price node reads it from runtime dot context, and gives members ten percent off. We invoke twice, with different context, and the same state. Run it. The member pays less, and the state never stored the tier.

context.pypython
from dataclasses import dataclass
from typing import TypedDict
from langgraph.graph import StateGraph, START
from langgraph.runtime import Runtime

@dataclass
class Ctx:
    tier: str

class State(TypedDict):
    price: float

def price(s, runtime: Runtime[Ctx]):
    discount = 0.9 if runtime.context.tier == "member" else 1.0
    return {"price": round(s["price"] * discount, 2)}
b = StateGraph(State, context_schema=Ctx); b.add_node(price); b.add_edge(START, "price")
graph = b.compile()
for tier in ["guest", "member"]:
    print(tier, graph.invoke({"price": 9.99}, context=Ctx(tier=tier)))
Output
guest {'price': 9.99}
member {'price': 8.99}

Context vs state

State changes as the graph runs, and is saved by the checkpointer. Context is fixed for one run, comes from your code, and isn't saved. Put trusted facts, like the user ID and permissions, in context.

keyvalue
Statechanges, saved
Contextfixed per run
Fromyour code
Use foruser ID, tier, branch

Summary

Runtime context passes fixed, trusted settings to every node for one run. Declare it with context schema. Next, async graphs.

Key takeaways
  • context_schema
  • runtime.context
  • Not saved in state
Next up: Async Graphs — ainvoke