Implementation:Langchain ai Langgraph IsLastStep
| Attribute | Value |
|---|---|
| Source | `libs/langgraph/langgraph/managed/is_last_step.py` (24 lines) |
| Domain | Managed_Values, Execution |
| Principle | Managed_Value_System |
| Library | langgraph |
| Import | `from langgraph.managed.is_last_step import IsLastStep, RemainingSteps` |
Overview
The `is_last_step.py` module provides two concrete managed value implementations that allow graph nodes to introspect their position within the execution timeline. `IsLastStep` tells a node whether it is on the final allowed step before the recursion limit, while `RemainingSteps` provides the count of steps remaining.
Description
This module builds on the `ManagedValue` abstract base class to provide runtime execution awareness to graph nodes:
`IsLastStepManager(ManagedValue[bool])` -- Computes whether the current step is the last step before the graph would hit its recursion limit. It returns `True` when `scratchpad.step == scratchpad.stop - 1`.
`IsLastStep` -- A type alias defined as `Annotated[bool, IsLastStepManager]`. When used as a parameter type in a node function, the LangGraph runtime automatically injects the boolean value.
`RemainingStepsManager(ManagedValue[int])` -- Computes the number of steps remaining before the graph hits its recursion limit. It returns `scratchpad.stop - scratchpad.step`.
`RemainingSteps` -- A type alias defined as `Annotated[int, RemainingStepsManager]`. When used as a parameter type in a node function, the LangGraph runtime automatically injects the integer count of remaining steps.
These managed values are useful for implementing graceful degradation logic -- for example, a node can choose to return a summary response or trigger early termination when it detects it is on or near the last step.
Usage
from langgraph.managed.is_last_step import IsLastStep, RemainingSteps
def my_node(state: dict, is_last_step: IsLastStep) -> dict:
if is_last_step:
return {"response": "Wrapping up due to step limit."}
return {"response": "Still processing..."}
Code Reference
Classes
| Class | Base | Return Type | Computation |
|---|---|---|---|
| `IsLastStepManager` | `ManagedValue[bool]` | `bool` | `scratchpad.step == scratchpad.stop - 1` |
| `RemainingStepsManager` | `ManagedValue[int]` | `int` | `scratchpad.stop - scratchpad.step` |
Type Aliases
| Name | Definition | Description |
|---|---|---|
| `IsLastStep` | `Annotated[bool, IsLastStepManager]` | Boolean indicating whether this is the last allowed execution step. |
| `RemainingSteps` | `Annotated[int, RemainingStepsManager]` | Integer count of remaining execution steps. |
I/O Contract
| Aspect | Detail |
|---|---|
| Input | `PregelScratchpad` containing `step` (current step index) and `stop` (maximum step count from `recursion_limit`). |
| Output | `IsLastStepManager.get` returns `bool`. `RemainingStepsManager.get` returns `int`. |
| Side Effects | None. These are pure computations from the scratchpad. |
| Injection | Values are automatically injected by the LangGraph runtime when the `Annotated` type aliases are used as node function parameter types. |
Usage Examples
Graceful Degradation on Last Step
from langgraph.managed.is_last_step import IsLastStep
def agent_node(state: dict, is_last_step: IsLastStep) -> dict:
if is_last_step:
# Force a final response instead of continuing the loop
return {"messages": [{"role": "assistant", "content": "I need to wrap up now."}]}
# Normal processing continues
return {"messages": state["messages"]}
Adjusting Behavior Based on Remaining Steps
from langgraph.managed.is_last_step import RemainingSteps
def research_node(state: dict, remaining_steps: RemainingSteps) -> dict:
if remaining_steps < 3:
# Not enough steps left for a full research cycle; summarize
return {"action": "summarize"}
return {"action": "deep_research"}
Combining Both Managed Values
from langgraph.managed.is_last_step import IsLastStep, RemainingSteps
def planner_node(
state: dict,
is_last_step: IsLastStep,
remaining_steps: RemainingSteps,
) -> dict:
if is_last_step:
return {"plan": "finalize"}
elif remaining_steps <= 5:
return {"plan": "shortcut"}
else:
return {"plan": "full_analysis"}
Related Pages
- Langchain_ai_Langgraph_ManagedValue_Base -- The abstract base class that `IsLastStepManager` and `RemainingStepsManager` extend.
- Langchain_ai_Langgraph_Error_Classes -- `GraphRecursionError` raised when the step limit is actually exceeded.
- Langchain_ai_Langgraph_PregelProtocol -- The execution protocol that manages step counting.