Overview
LangChain’s createAgent runs on LangGraph’s runtime under the hood.
LangGraph exposes a Runtime object with the following information:
- Context: static information like user id, db connections, or other dependencies for an agent invocation
- Store: a BaseStore instance used for long-term memory
- Stream writer: an object used for streaming information via the
"custom" stream mode
The runtime context is how you thread data through your agent. Rather than storing things in global state, you can attach values — like a database connection, user session, or configuration — to the context and access them inside tools and middleware. This keeps things stateless, testable, and reusable.
You can access the runtime information within tools and middleware.
Access
When creating an agent with createAgent, you can specify a contextSchema to define the structure of the context stored in the agent Runtime.
When invoking the agent, pass the context argument with the relevant configuration for the run:
You can access the runtime information inside tools to:
- Access the context
- Read or write long-term memory
- Write to the custom stream (ex, tool progress / updates)
Use the runtime parameter to access the Runtime object inside a tool.
Inside middleware
You can access runtime information in middleware to create dynamic prompts, modify messages, or control agent behavior based on user context.
Use the runtime parameter to access the Runtime object inside middleware.