ServerRuntime.
In most cases, customization is best handled by conditioning on the config within individual nodes rather than dynamically changing the whole graph structure. This makes it easier to test and manage.
Prerequisites
- Make sure to check out this how-to guide on setting up your app for deployment first.
ServerRuntimerequireslanggraph-api >= 0.7.31andlanggraph-sdk >= 0.3.5. Prior to that, graph factories only accepted a singleconfig: RunnableConfigargument.
Define graphs
Let’s say you have an app with a simple graph that calls an LLM and returns the response to the user. The app file directory looks like the following:agents.py.
No rebuild
The most common way to deploy your Agent Server is to reference a compiled graph instance that’s defined at the top level of your file. An example is below:CompiledStateGraph instance in your LangGraph API configuration (langgraph.json), e.g.:
Rebuild
To rebuild your graph on each new run, provide a factory function that returns (or yields) a graph. The factory can optionally accept aServerRuntime parameter or a RunnableConfig. The server inspects your function’s type annotations to determine which arguments to inject, so make sure to include the correct type hints. The server’s queue workers will call your factory function any time they need to process a run. The function will also be called for certain other endpoints to update state, read state, or to fetch assistant schemas. The ServerRuntime tells you which context triggered the call.
ServerRuntime is in beta and may change in future releases.Simple factory
The simplest form is a plainasync def that returns a compiled graph:
Context manager factory
If you need to set up and tear down resources (database connections, load MCP tools, etc.), use an async context manager. Useruntime.execution_runtime to check whether the graph is being called for actual execution or just for introspection (schemas, visualization):
langgraph.json:
ServerRuntime reference
Your factory function receives aServerRuntime instance with the following attributes:
Methods:
Access contexts
The server calls your factory in several contexts beyond just executing runs. In all contexts, the returned graph should have the same topology (nodes, edges, state schema). A mismatched topology in write contexts (threads.create_run, threads.update) can cause incorrect state updates. In read contexts (threads.read, assistants.read), a mismatch affects reported pending tasks, schemas, and visualizations but won’t corrupt data. Use execution_runtime to conditionally set up expensive resources without changing the graph structure.
Customize tracing per graph
You can use the factory function to customize or disable tracing for a specific graph. See Conditional tracing: Customize tracing in deployed agents for examples. See more info on the LangGraph API configuration file.Connect these docs to Claude, VSCode, and more via MCP for real-time answers.

