Skip to main content

Overview

LangChain’s createAgent runs on LangGraph’s runtime under the hood. LangGraph exposes a Runtime object with the following information:
  1. Context: static information like user id, db connections, or other dependencies for an agent invocation
  2. Store: a BaseStore instance used for long-term memory
  3. Stream writer: an object used for streaming information via the "custom" stream mode
  4. Execution info: identity and retry information for the current execution (thread ID, run ID, attempt number)
  5. Server info: server-specific metadata when running on LangGraph Server (assistant ID, graph ID, authenticated user)
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:

Inside tools

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.

Execution info and server info inside tools

Access execution identity (thread ID, run ID) via runtime.executionInfo, and server-specific metadata (assistant ID, authenticated user) via runtime.serverInfo when running on LangGraph Server:
serverInfo is null when not running on LangGraph Server (e.g., during local development).
Requires deepagents>=1.9.0 (or @langchain/langgraph>=1.2.8) for runtime.executionInfo and runtime.serverInfo.

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.

Execution info and server info inside middleware

Middleware hooks can also access runtime.executionInfo and runtime.serverInfo:
Requires deepagents>=1.9.0 (or @langchain/langgraph>=1.2.8).