You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(docs): add examples on recursion counter access and proactive handling in graph-api (#1166)
## Overview
Expanded docs for accessing and handling the recursion counter in
LangGraph graphs, enabling developers to implement proactive recursion
management before hitting limits.
## Type of change
**Type:** Update existing documentation
## Checklist
- [x] I have read the [contributing guidelines](README.md)
- [x] I have tested my changes locally using `docs dev`
- [x] All code examples have been tested and work correctly
- [x] I have used **root relative** paths for internal links
- [n/a] I have updated navigation in `src/docs.json` if needed
- I have gotten approval from the relevant reviewers
## Additional notes
This PR extends the existing "Recursion limit" section in the Graph API
documentation with detailed guidance on accessing
`config.metadata.langgraph_step` and implementing proactive recursion
handling.
### What's Added
**1. How it works** - Explains where the step counter is stored and how
the recursion limit check logic works in both Python
(`config["metadata"]["langgraph_step"]`) and TypeScript
(`config.metadata.langgraph_step`).
**2. Accessing the current step counter** - Simple code examples showing
how to read the step counter within node functions.
**3. Proactive recursion handling** - Complete working examples
demonstrating:
- Checking if approaching the limit (e.g., 80% threshold)
- Routing to fallback nodes before hitting the limit
- Full graph setup with conditional edges for graceful degradation
**4. Proactive vs reactive approaches** - Side-by-side comparison
including:
- Code examples of both proactive monitoring and reactive error catching
- Comparison table highlighting detection timing, handling location, and
control flow differences
- Lists of advantages for each approach with recommendation for
proactive handling
**5. Other available metadata** - Documents additional metadata fields
available in config (node, triggers, path, checkpoint namespace).
### Motivation
Currently, the documentation explains the recursion limit configuration
but doesn't cover how developers can access the current step counter or
implement proactive handling strategies. This leads developers to only
discover reactive error handling (catching `GraphRecursionError`) rather
than implementing graceful degradation patterns within their graphs.
This addition enables better user experiences by allowing graphs to
complete normally with partial results rather than throwing exceptions.
Co-authored-by: Lauren Hirata Singh <lauren@langchain.dev>
The current step counter is accessible in `config["metadata"]["langgraph_step"]` within any node, allowing for proactive recursion handling before hitting the recursion limit. This enables you to implement graceful degradation strategies within your graph logic.
1128
+
:::
1129
+
1130
+
:::js
1131
+
The current step counter is accessible in `config.metadata.langgraph_step` within any node, allowing for proactive recursion handling before hitting the recursion limit. This enables you to implement graceful degradation strategies within your graph logic.
1132
+
:::
1133
+
1134
+
#### How it works
1135
+
1136
+
:::python
1137
+
1138
+
The step counter is stored in `config["metadata"]["langgraph_step"]`. The recursion limit check follows the logic: `step > stop` where `stop = step + recursion_limit + 1`. When the limit is exceeded, LangGraph raises a `GraphRecursionError`.
1139
+
1140
+
:::
1141
+
1142
+
:::js
1143
+
1144
+
The step counter is stored in `config.metadata.langgraph_step`. The recursion limit check follows the logic: `step > stop` where `stop = step + recursionLimit + 1`. When the limit is exceeded, LangGraph raises a `GraphRecursionError`.
1145
+
1146
+
:::
1147
+
1148
+
#### Accessing the current step counter
1149
+
1150
+
You can access the current step counter within any node to monitor execution progress.
1151
+
1152
+
:::python
1153
+
1154
+
```python
1155
+
from langchain_core.runnables import RunnableConfig
You can check the step counter and proactively route to a different node before hitting the limit. This allows for graceful degradation within your graph.
1184
+
1185
+
:::python
1186
+
1187
+
```python
1188
+
from langchain_core.runnables import RunnableConfig
It's often nice to be able to visualize graphs, especially as they get more complex. LangGraph comes with several built-in ways to visualize graphs. See [this how-to guide](/oss/langgraph/use-graph-api#visualize-your-graph) for more info.
0 commit comments