Skip to content

The agent engineering platform.

fix: only increment thread count on successful executions (#33837)

Sydney Runkle7af79039fc8b
diff --git a/libs/langchain_v1/langchain/agents/middleware/tool_call_limit.py b/libs/langchain_v1/langchain/agents/middleware/tool_call_limit.py
index 72e270cfd5..686ca06ab8 100644
--- a/libs/langchain_v1/langchain/agents/middleware/tool_call_limit.py
+++ b/libs/langchain_v1/langchain/agents/middleware/tool_call_limit.py
@@ -45,13 +45,7 @@ class ToolCallLimitState(AgentState[ResponseT], Generic[ResponseT]):
run_tool_call_count: NotRequired[Annotated[dict[str, int], UntrackedValue, PrivateStateAttr]]
-def _build_tool_message_content(
- tool_name: str | None,
- thread_count: int,
- run_count: int,
- thread_limit: int | None,
- run_limit: int | None,
-) -> str:
+def _build_tool_message_content(tool_name: str | None) -> str:
"""Build the error message content for ToolMessage when limit is exceeded.
This message is sent to the model, so it should not reference thread/run concepts
@@ -59,28 +53,11 @@ def _build_tool_message_content(
Args:
tool_name: Tool name being limited (if specific tool), or None for all tools.
- thread_count: Current thread tool call count.
- run_count: Current run tool call count.
- thread_limit: Thread tool call limit (if set).
- run_limit: Run tool call limit (if set).
Returns:
- A concise message. If only run limit is exceeded (not thread limit),
- returns a simple "limit exceeded" message without instructing model to stop.
- If thread limit is exceeded, includes instruction not to call again.
+ A concise message instructing the model not to call the tool again.
"""
- # Check if thread limit is exceeded
- thread_exceeded = thread_limit is not None and thread_count > thread_limit
- # Check if only run limit is exceeded (not thread limit)
- only_run_exceeded = run_limit is not None and run_count > run_limit and not thread_exceeded
-
- if only_run_exceeded:
- # Run limit exceeded but thread limit not exceeded - simpler message
- if tool_name:
- return f"Tool call limit exceeded for '{tool_name}'."
- return "Tool call limit exceeded."
-
- # Thread limit exceeded (or both) - include instruction not to call again
+ # Always instruct the model not to call again, regardless of which limit was hit
if tool_name:
return f"Tool call limit exceeded. Do not call '{tool_name}' again."
return "Tool call limit exceeded. Do not make additional tool calls."
@@ -242,7 +219,8 @@ class ToolCallLimitMiddleware(
calls to other tools or multiple pending tool calls.
Raises:
- ValueError: If both limits are `None`.
+ ValueError: If both limits are `None`, if exit_behavior is invalid,
+ or if run_limit exceeds thread_limit.
"""
super().__init__()
@@ -250,6 +228,18 @@ class ToolCallLimitMiddleware(
msg = "At least one limit must be specified (thread_limit or run_limit)"
raise ValueError(msg)
+ valid_behaviors = ("continue", "error", "end")
+ if exit_behavior not in valid_behaviors:
+ msg = f"Invalid exit_behavior: {exit_behavior!r}. Must be one of {valid_behaviors}"
+ raise ValueError(msg)
+
+ if thread_limit is not None and run_limit is not None and run_limit > thread_limit:
+ msg = (
+ f"run_limit ({run_limit}) cannot exceed thread_limit ({thread_limit}). "
+ "The run limit should be less than or equal to the thread limit."
+ )
+ raise ValueError(msg)
+
self.tool_name = tool_name
self.thread_limit = thread_limit
self.run_limit = run_limit
@@ -375,8 +365,10 @@ class ToolCallLimitMiddleware(
last_ai_message.tool_calls, current_thread_count, current_run_count
)
- # Update counts to include ALL tool call attempts (both allowed and blocked)
- thread_counts[count_key] = new_thread_count + len(blocked_calls)
+ # Update counts to include only allowed calls for thread count
+ # (blocked calls don't count towards thread-level tracking)
+ # But run count includes blocked calls since they were attempted in this run
+ thread_counts[count_key] = new_thread_count
run_counts[count_key] = new_run_count + len(blocked_calls)
# If no tool calls are blocked, just update counts
@@ -394,8 +386,10 @@ class ToolCallLimitMiddleware(
# Handle different exit behaviors
if self.exit_behavior == "error":
+ # Use hypothetical thread count to show which limit was exceeded
+ hypothetical_thread_count = final_thread_count + len(blocked_calls)
raise ToolCallLimitExceededError(
- thread_count=final_thread_count,
+ thread_count=hypothetical_thread_count,
run_count=final_run_count,
thread_limit=self.thread_limit,
run_limit=self.run_limit,
@@ -403,13 +397,7 @@ class ToolCallLimitMiddleware(
)
# Build tool message content (sent to model - no thread/run details)
- tool_msg_content = _build_tool_message_content(
- self.tool_name,
- final_thread_count,
- final_run_count,
- self.thread_limit,
- self.run_limit,
- )
+ tool_msg_content = _build_tool_message_content(self.tool_name)
# Inject artificial error ToolMessages for blocked tool calls
artificial_messages: list[ToolMessage | AIMessage] = [
@@ -439,8 +427,11 @@ class ToolCallLimitMiddleware(
raise NotImplementedError(msg)
# Build final AI message content (displayed to user - includes thread/run details)
+ # Use hypothetical thread count (what it would have been if call wasn't blocked)
+ # to show which limit was actually exceeded
+ hypothetical_thread_count = final_thread_count + len(blocked_calls)
final_msg_content = _build_final_ai_message_content(
- final_thread_count,
+ hypothetical_thread_count,
final_run_count,
self.thread_limit,
self.run_limit,
diff --git a/libs/langchain_v1/tests/unit_tests/agents/test_tool_call_limit.py b/libs/langchain_v1/tests/unit_tests/agents/test_tool_call_limit.py
index 094d07f11f..d382eb885d 100644
--- a/libs/langchain_v1/tests/unit_tests/agents/test_tool_call_limit.py
+++ b/libs/langchain_v1/tests/unit_tests/agents/test_tool_call_limit.py
@@ -37,6 +37,27 @@ def test_middleware_initialization_validation():
middleware = ToolCallLimitMiddleware(thread_limit=5, exit_behavior=behavior)
assert middleware.exit_behavior == behavior
+ # Test invalid exit behavior
+ with pytest.raises(ValueError, match="Invalid exit_behavior"):
+ ToolCallLimitMiddleware(thread_limit=5, exit_behavior="invalid") # type: ignore[arg-type]
+
+ # Test run_limit exceeding thread_limit
+ with pytest.raises(
+ ValueError,
+ match="run_limit .* cannot exceed thread_limit",
+ ):
+ ToolCallLimitMiddleware(thread_limit=3, run_limit=5)
+
+ # Test run_limit equal to thread_limit (should be valid)
+ middleware = ToolCallLimitMiddleware(thread_limit=5, run_limit=5)
+ assert middleware.thread_limit == 5
+ assert middleware.run_limit == 5
+
+ # Test run_limit less than thread_limit (should be valid)
+ middleware = ToolCallLimitMiddleware(thread_limit=5, run_limit=3)
+ assert middleware.thread_limit == 5
+ assert middleware.run_limit == 3
+
def test_middleware_name_property():
"""Test that the name property includes tool name when specified."""
@@ -79,14 +100,14 @@ def test_middleware_unit_functionality():
assert result["run_tool_call_count"] == {"__all__": 1}
assert "jump_to" not in result
- # Test thread limit exceeded
+ # Test thread limit exceeded (start at thread_limit so next call will exceed)
state = {
"messages": [
HumanMessage("Question 2"),
AIMessage("Response 2", tool_calls=[{"name": "search", "args": {}, "id": "3"}]),
],
- "thread_tool_call_count": {"__all__": 3},
- "run_tool_call_count": {"__all__": 3},
+ "thread_tool_call_count": {"__all__": 3}, # Already exceeds thread_limit=3
+ "run_tool_call_count": {"__all__": 0}, # No calls yet
}
result = middleware.after_model(state, runtime) # type: ignore[arg-type]
assert result is not None
@@ -96,16 +117,19 @@ def test_middleware_unit_functionality():
assert isinstance(tool_msg, ToolMessage)
assert tool_msg.status == "error"
assert "Tool call limit exceeded" in tool_msg.content
- # When thread limit exceeded, should include "Do not" instruction
+ # Should include "Do not" instruction
assert "Do not" in tool_msg.content, (
- "Tool message should include 'Do not' instruction when thread limit exceeded"
+ "Tool message should include 'Do not' instruction when limit exceeded"
)
# Check the final AI message (displayed to user - includes thread/run details)
final_ai_msg = result["messages"][-1]
assert isinstance(final_ai_msg, AIMessage)
assert "limit" in final_ai_msg.content.lower()
assert "thread limit exceeded" in final_ai_msg.content.lower()
- assert result["thread_tool_call_count"] == {"__all__": 4}
+ # Thread count stays at 3 (blocked call not counted)
+ assert result["thread_tool_call_count"] == {"__all__": 3}
+ # Run count goes to 1 (includes blocked call)
+ assert result["run_tool_call_count"] == {"__all__": 1}
# Test run limit exceeded (thread count must be >= run count)
state = {
@@ -123,13 +147,12 @@ def test_middleware_unit_functionality():
final_ai_msg = result["messages"][-1]
assert "run limit exceeded" in final_ai_msg.content
assert "3/2 calls" in final_ai_msg.content
- # Check the tool message (sent to model) - when only run limit exceeded,
- # should not include "Do not" instruction since run is ending anyway
+ # Check the tool message (sent to model) - should always include "Do not" instruction
tool_msg = result["messages"][0]
assert isinstance(tool_msg, ToolMessage)
assert "Tool call limit exceeded" in tool_msg.content
- assert "Do not" not in tool_msg.content, (
- "Tool message should not include 'Do not' instruction when only run limit exceeded"
+ assert "Do not" in tool_msg.content, (
+ "Tool message should include 'Do not' instruction for both run and thread limits"
)
@@ -212,8 +235,11 @@ def test_middleware_error_behavior():
middleware.after_model(state, runtime) # type: ignore[arg-type]
error = exc_info.value
+ # Thread count in error message shows hypothetical count (what it would have been)
assert error.thread_count == 3
assert error.thread_limit == 2
+ # Run count includes the blocked call
+ assert error.run_count == 3
assert error.tool_name is None
@@ -447,6 +473,79 @@ def test_exit_behavior_continue():
assert len(successful_calc_msgs) == 3, "All calculator calls should succeed"
+def test_thread_count_excludes_blocked_run_calls():
+ """Test that thread count only includes allowed calls, not blocked run-scoped calls.
+
+ When run_limit is lower than thread_limit and multiple parallel calls are made,
+ only the allowed calls should increment the thread count.
+
+ Example: If run_limit=1 and 3 parallel calls are made, thread count should be 1
+ (not 3) because the other 2 were blocked by the run limit.
+ """
+ # Set run_limit=1, thread_limit=10 (much higher)
+ middleware = ToolCallLimitMiddleware(thread_limit=10, run_limit=1, exit_behavior="continue")
+ runtime = None
+
+ # Make 3 parallel tool calls - only 1 should be allowed by run_limit
+ state = {
+ "messages": [
+ AIMessage(
+ "Response",
+ tool_calls=[
+ {"name": "search", "args": {}, "id": "1"},
+ {"name": "search", "args": {}, "id": "2"},
+ {"name": "search", "args": {}, "id": "3"},
+ ],
+ )
+ ],
+ "thread_tool_call_count": {},
+ "run_tool_call_count": {},
+ }
+ result = middleware.after_model(state, runtime) # type: ignore[arg-type]
+ assert result is not None
+
+ # Thread count should be 1 (only the allowed call)
+ assert result["thread_tool_call_count"]["__all__"] == 1, (
+ "Thread count should only include the 1 allowed call, not the 2 blocked calls"
+ )
+ # Run count should be 3 (all attempted calls)
+ assert result["run_tool_call_count"]["__all__"] == 3, (
+ "Run count should include all 3 attempted calls"
+ )
+
+ # Verify 2 error messages were created for blocked calls
+ assert "messages" in result
+ error_messages = [msg for msg in result["messages"] if isinstance(msg, ToolMessage)]
+ assert len(error_messages) == 2, "Should have 2 error messages for the 2 blocked calls"
+
+
+def test_unified_error_messages():
+ """Test that error messages instruct model not to call again for both run and thread limits.
+
+ Previously, only thread limit messages included 'Do not' instruction.
+ Now both run and thread limit messages should include it.
+ """
+ middleware = ToolCallLimitMiddleware(thread_limit=10, run_limit=1, exit_behavior="continue")
+ runtime = None
+
+ # Test with run limit exceeded (thread limit not exceeded)
+ state = {
+ "messages": [AIMessage("Response", tool_calls=[{"name": "search", "args": {}, "id": "2"}])],
+ "thread_tool_call_count": {"__all__": 1}, # Under thread limit
+ "run_tool_call_count": {"__all__": 1}, # At run limit, next call will exceed
+ }
+ result = middleware.after_model(state, runtime) # type: ignore[arg-type]
+ assert result is not None
+
+ # Check the error message includes "Do not" instruction
+ error_messages = [msg for msg in result["messages"] if isinstance(msg, ToolMessage)]
+ assert len(error_messages) == 1
+ error_content = error_messages[0].content
+ assert "Do not" in error_content, (
+ "Run limit error message should include 'Do not' instruction to guide model behavior"
+ )
+
+
def test_end_behavior_creates_artificial_messages():
"""Test that 'end' behavior creates an AI message explaining why execution stopped.