Home / File/ model_call_limit.py — langchain Source File

model_call_limit.py — langchain Source File

Architecture documentation for model_call_limit.py, a python file in the langchain codebase. 6 imports, 0 dependents.

File python LangChainCore LanguageModelBase 6 imports 2 functions 3 classes

Entity Profile

Dependency Diagram

graph LR
  cd424b61_a87c_1279_2846_1393c1806d7b["model_call_limit.py"]
  feec1ec4_6917_867b_d228_b134d0ff8099["typing"]
  cd424b61_a87c_1279_2846_1393c1806d7b --> feec1ec4_6917_867b_d228_b134d0ff8099
  9444498b_8066_55c7_b3a2_1d90c4162a32["langchain_core.messages"]
  cd424b61_a87c_1279_2846_1393c1806d7b --> 9444498b_8066_55c7_b3a2_1d90c4162a32
  727323fb_5fc0_9885_9264_3a6e6a03be31["langgraph.channels.untracked_value"]
  cd424b61_a87c_1279_2846_1393c1806d7b --> 727323fb_5fc0_9885_9264_3a6e6a03be31
  f85fae70_1011_eaec_151c_4083140ae9e5["typing_extensions"]
  cd424b61_a87c_1279_2846_1393c1806d7b --> f85fae70_1011_eaec_151c_4083140ae9e5
  a681398d_ed44_c914_1a44_5d174223b069["langchain.agents.middleware.types"]
  cd424b61_a87c_1279_2846_1393c1806d7b --> a681398d_ed44_c914_1a44_5d174223b069
  e07f6d54_afcc_052d_d33f_8ccdcc46f752["langgraph.runtime"]
  cd424b61_a87c_1279_2846_1393c1806d7b --> e07f6d54_afcc_052d_d33f_8ccdcc46f752
  style cd424b61_a87c_1279_2846_1393c1806d7b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Call tracking middleware for agents."""

from __future__ import annotations

from typing import TYPE_CHECKING, Annotated, Any, Literal

from langchain_core.messages import AIMessage
from langgraph.channels.untracked_value import UntrackedValue
from typing_extensions import NotRequired, override

from langchain.agents.middleware.types import (
    AgentMiddleware,
    AgentState,
    ContextT,
    PrivateStateAttr,
    ResponseT,
    hook_config,
)

if TYPE_CHECKING:
    from langgraph.runtime import Runtime


class ModelCallLimitState(AgentState[ResponseT]):
    """State schema for `ModelCallLimitMiddleware`.

    Extends `AgentState` with model call tracking fields.

    Type Parameters:
        ResponseT: The type of the structured response. Defaults to `Any`.
    """

    thread_model_call_count: NotRequired[Annotated[int, PrivateStateAttr]]
    run_model_call_count: NotRequired[Annotated[int, UntrackedValue, PrivateStateAttr]]


def _build_limit_exceeded_message(
    thread_count: int,
    run_count: int,
    thread_limit: int | None,
    run_limit: int | None,
) -> str:
    """Build a message indicating which limits were exceeded.

    Args:
        thread_count: Current thread model call count.
        run_count: Current run model call count.
        thread_limit: Thread model call limit (if set).
        run_limit: Run model call limit (if set).

    Returns:
        A formatted message describing which limits were exceeded.
    """
    exceeded_limits = []
    if thread_limit is not None and thread_count >= thread_limit:
        exceeded_limits.append(f"thread limit ({thread_count}/{thread_limit})")
    if run_limit is not None and run_count >= run_limit:
        exceeded_limits.append(f"run limit ({run_count}/{run_limit})")

    return f"Model call limits exceeded: {', '.join(exceeded_limits)}"
// ... (208 more lines)

Domain

Subdomains

Dependencies

  • langchain.agents.middleware.types
  • langchain_core.messages
  • langgraph.channels.untracked_value
  • langgraph.runtime
  • typing
  • typing_extensions

Frequently Asked Questions

What does model_call_limit.py do?
model_call_limit.py is a source file in the langchain codebase, written in python. It belongs to the LangChainCore domain, LanguageModelBase subdomain.
What functions are defined in model_call_limit.py?
model_call_limit.py defines 2 function(s): _build_limit_exceeded_message, langgraph.
What does model_call_limit.py depend on?
model_call_limit.py imports 6 module(s): langchain.agents.middleware.types, langchain_core.messages, langgraph.channels.untracked_value, langgraph.runtime, typing, typing_extensions.
Where is model_call_limit.py in the architecture?
model_call_limit.py is located at libs/langchain_v1/langchain/agents/middleware/model_call_limit.py (domain: LangChainCore, subdomain: LanguageModelBase, 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