Principle:Langchain ai Langgraph Managed Value System
| Attribute | Value |
|---|---|
| Type | Principle |
| Knowledge Sources | LangGraph |
| Domains | Managed_Values, Core, Execution |
| Last Updated | 2026-02-11 15:00 GMT |
Overview
The managed value system allows LangGraph to inject computed, runtime-scoped values into graph node function signatures without storing those values in the graph state.
Description
Managed values are a mechanism for providing graph nodes with execution metadata that is computed on-the-fly from the Pregel execution scratchpad each time a node is invoked. Unlike regular state channels, managed values are not persisted or checkpointed -- they are ephemeral computations derived from the current execution context.
The system is built on the `ManagedValue` abstract base class, a generic ABC parameterized by the value type `V`. Each subclass implements a single static `get(scratchpad)` method that computes and returns the value from the current `PregelScratchpad`. The scratchpad provides execution metadata such as the current step number and the maximum step limit.
LangGraph ships with two built-in managed value implementations:
`IsLastStepManager` -- Computes whether the current step is the last allowed step before the graph hits its recursion limit. Returns `True` when `step == stop - 1`. The corresponding type alias `IsLastStep` (defined as `Annotated[bool, IsLastStepManager]`) can be used as a node function parameter type for automatic injection.
`RemainingStepsManager` -- Computes the number of steps remaining before the recursion limit is reached. Returns `stop - step`. The corresponding type alias `RemainingSteps` (defined as `Annotated[int, RemainingStepsManager]`) works the same way.
The `is_managed_value` type guard function detects managed value annotations in graph state schemas, enabling the runtime to distinguish between regular state fields and managed value injections. The `ManagedValueMapping` type alias represents the mapping from state key names to their managed value classes.
Usage
Use managed values when nodes need execution context information that should not be part of the persisted graph state. The most common use case is implementing graceful degradation -- for example, a node can check `IsLastStep` to decide whether to return a summary response or `RemainingSteps` to adjust the depth of its processing. Custom managed values can be created by subclassing `ManagedValue` and implementing the `get` method.
Theoretical Basis
The managed value system implements the dependency injection pattern: rather than having nodes query the execution environment directly, the runtime automatically provides execution metadata through the function signature. This decouples node logic from the execution infrastructure, making nodes easier to test in isolation and reuse across different graph configurations.
The use of `Annotated` type hints as the injection mechanism follows the metadata annotation pattern from Python's type system (PEP 593). The type annotation serves double duty -- it provides type safety for static analysis tools while also acting as a runtime directive for the Pregel engine to inject the appropriate value.
The distinction between managed values (ephemeral, computed) and state channels (persisted, reduced) reflects the separation of concerns principle: execution metadata like step counts is orthogonal to the application's domain state and should not pollute the checkpoint history or state schema.