-
Couldn't load subscription status.
- Fork 19.5k
Description
Checked other resources
- This is a feature request, not a bug report or usage question.
- I added a clear and descriptive title that summarizes the feature request.
- I used the GitHub search to find a similar feature request and didn't find it.
- I checked the LangChain documentation and API reference to see if this feature already exists.
- This is not related to the langchain-community package.
Feature Description
When the recursion or tool call limit is reached during agent execution, the current ToolCallLimitMiddleware only terminates execution by returning a plain string (e.g., "Tool call limits exceeded.") when exit_behavior="end". This abrupt termination prevents the model from using the existing context to produce a final, coherent response.
To improve this behavior, it would be valuable to introduce a built-in middleware (e.g., RecursionLimitFallbackMiddleware) that gracefully handles such termination cases by triggering a context-aware finalization step.
Proposed Behavior
- Detection: Monitor recursion depth or cumulative tool call count, similar to the existing middleware.
- Graceful Termination: When the threshold is exceeded, automatically prevent further tool calls.
- Fallback Response Generation: Instead of returning a static string, instruct the model to summarize or finalize the response based on the latest context (e.g., the agent’s working memory or prior messages).
Benefits
- Prevents infinite tool-calling loops without abrupt terminations.
- Ensures users receive a meaningful, model-generated final answer.
- Reduces API waste from uncontrolled recursive calls.
- Provides a consistent fallback mechanism across different agent or graph configurations.
This enhancement would complement ToolCallLimitMiddleware by introducing an intelligent, context-aware fallback instead of a simple stop signal, improving both reliability and user experience in complex agent executions.
Use Case
Use Case
In complex multi-step agent workflows — for example, an AI research assistant that performs iterative web searches, document retrieval, and summarization — the model may exceed recursion or tool call limits when looping through reasoning chains.
With the proposed RecursionLimitFallbackMiddleware, instead of abruptly stopping with a static error string, the system could gracefully finalize by generating a meaningful conclusion such as:
“I’ve reached the maximum reasoning depth. Based on all the collected sources so far, here’s my summarized finding…”
This ensures that even in limit-triggered cases, users still receive a coherent, contextually relevant output — improving robustness and usability for long-running agents or graphs.
Proposed Solution
No response
Alternatives Considered
No response
Additional Context
No response