tool_call_limit.py — langchain Source File
Architecture documentation for tool_call_limit.py, a python file in the langchain codebase. 7 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 947b307e_4a79_6b78_56c6_a24d2024bcf1["tool_call_limit.py"] 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"] 947b307e_4a79_6b78_56c6_a24d2024bcf1 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3 d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"] 947b307e_4a79_6b78_56c6_a24d2024bcf1 --> d758344f_537f_649e_f467_b9d7442e86df d00f558c_c526_6dd4_0335_e0872c055138["langgraph.channels.untracked_value"] 947b307e_4a79_6b78_56c6_a24d2024bcf1 --> d00f558c_c526_6dd4_0335_e0872c055138 63cd4255_468c_f8b4_547c_beb43ec9a0f1["langgraph.typing"] 947b307e_4a79_6b78_56c6_a24d2024bcf1 --> 63cd4255_468c_f8b4_547c_beb43ec9a0f1 91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"] 947b307e_4a79_6b78_56c6_a24d2024bcf1 --> 91721f45_4909_e489_8c1f_084f8bd87145 50acc543_e5f0_2162_cf07_c2bf50723e0c["langchain.agents.middleware.types"] 947b307e_4a79_6b78_56c6_a24d2024bcf1 --> 50acc543_e5f0_2162_cf07_c2bf50723e0c 5dbfd558_f2f2_9663_3a3f_c317926ac1c1["langgraph.runtime"] 947b307e_4a79_6b78_56c6_a24d2024bcf1 --> 5dbfd558_f2f2_9663_3a3f_c317926ac1c1 style 947b307e_4a79_6b78_56c6_a24d2024bcf1 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
"""Tool call limit middleware for agents."""
from __future__ import annotations
from typing import TYPE_CHECKING, Annotated, Any, Literal
from langchain_core.messages import AIMessage, ToolCall, ToolMessage
from langgraph.channels.untracked_value import UntrackedValue
from langgraph.typing import ContextT
from typing_extensions import NotRequired, override
from langchain.agents.middleware.types import (
AgentMiddleware,
AgentState,
PrivateStateAttr,
ResponseT,
hook_config,
)
if TYPE_CHECKING:
from langgraph.runtime import Runtime
ExitBehavior = Literal["continue", "error", "end"]
"""How to handle execution when tool call limits are exceeded.
- `'continue'`: Block exceeded tools with error messages, let other tools continue
(default)
- `'error'`: Raise a `ToolCallLimitExceededError` exception
- `'end'`: Stop execution immediately, injecting a `ToolMessage` and an `AIMessage` for
the single tool call that exceeded the limit. Raises `NotImplementedError` if there
are other pending tool calls (due to parallel tool calling).
"""
class ToolCallLimitState(AgentState[ResponseT]):
"""State schema for `ToolCallLimitMiddleware`.
Extends `AgentState` with tool call tracking fields.
The count fields are dictionaries mapping tool names to execution counts. This
allows multiple middleware instances to track different tools independently. The
special key `'__all__'` is used for tracking all tool calls globally.
Type Parameters:
ResponseT: The type of the structured response. Defaults to `Any`.
"""
thread_tool_call_count: NotRequired[Annotated[dict[str, int], PrivateStateAttr]]
run_tool_call_count: NotRequired[Annotated[dict[str, int], UntrackedValue, PrivateStateAttr]]
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
that the model has no notion of.
Args:
tool_name: Tool name being limited (if specific tool), or `None` for all tools.
// ... (429 more lines)
Domain
Subdomains
Dependencies
- langchain.agents.middleware.types
- langchain_core.messages
- langgraph.channels.untracked_value
- langgraph.runtime
- langgraph.typing
- typing
- typing_extensions
Source
Frequently Asked Questions
What does tool_call_limit.py do?
tool_call_limit.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, Serialization subdomain.
What functions are defined in tool_call_limit.py?
tool_call_limit.py defines 3 function(s): _build_final_ai_message_content, _build_tool_message_content, langgraph.
What does tool_call_limit.py depend on?
tool_call_limit.py imports 7 module(s): langchain.agents.middleware.types, langchain_core.messages, langgraph.channels.untracked_value, langgraph.runtime, langgraph.typing, typing, typing_extensions.
Where is tool_call_limit.py in the architecture?
tool_call_limit.py is located at libs/langchain_v1/langchain/agents/middleware/tool_call_limit.py (domain: CoreAbstractions, subdomain: Serialization, directory: libs/langchain_v1/langchain/agents/middleware).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free