🧩 In short, how to retrieve user_id at graph runtime?
LangGraph introduced the Runtime class (since v0.6.0) to replace RunnableConfig:
🔗 https://github.com/langchain-ai/langgraph/releases/tag/0.6.0
What’s the correct way to access user_id (and other metadata) from Runtime inside a graph or middleware?
The example below is adapted from the 1.0.0+ docs:
🔗 https://docs.langchain.com/oss/python/langchain/runtime
from dataclasses import dataclass
from langchain.agents import create_agent, AgentState
from langchain.agents.middleware import after_model
from langgraph.runtime import Runtime
@dataclass
class Context:
user_id: str
thread_id: str
@after_model
def log_after_model(state: AgentState, runtime: Runtime[Context]) -> dict | None:
print(f"Completed request for user: {runtime.context.user_id}")
return None
agent = create_agent(
model="deepseek-chat",
tools=[],
middleware=[log_after_model],
context_schema=Context,
)
Running the graph with langgraph dev (API v1.0.1) and calling the agent via SDK (instead of invoke) produces this error:
Context.__init__() missing 2 required positional arguments: 'user_id' and 'thread_id'"
Before v0.6.0, this could be accessed through RunnableConfig, but there’s no clear example in the new docs on how to do this via Runtime.
In LangSmith, both user_id and thread_id appear under Metadata, so they clearly exist in the runtime context—but it’s unclear how to retrieve them programmatically.
Here’s the LangSmith screenshot for reference:
