Home / File/ test_model_call_limit.py — langchain Source File

test_model_call_limit.py — langchain Source File

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

File python LangChainCore LanguageModelBase 8 imports 7 functions

Entity Profile

Dependency Diagram

graph LR
  43d91b2d_3fa0_6f03_b3b8_f87c64a4f30a["test_model_call_limit.py"]
  f69d6389_263d_68a4_7fbf_f14c0602a9ba["pytest"]
  43d91b2d_3fa0_6f03_b3b8_f87c64a4f30a --> f69d6389_263d_68a4_7fbf_f14c0602a9ba
  9444498b_8066_55c7_b3a2_1d90c4162a32["langchain_core.messages"]
  43d91b2d_3fa0_6f03_b3b8_f87c64a4f30a --> 9444498b_8066_55c7_b3a2_1d90c4162a32
  121262a1_0bd6_d637_bce3_307ab6b3ecd4["langchain_core.tools"]
  43d91b2d_3fa0_6f03_b3b8_f87c64a4f30a --> 121262a1_0bd6_d637_bce3_307ab6b3ecd4
  2ff5ef5d_5050_1ab2_e4f2_f72e391945a2["langgraph.checkpoint.memory"]
  43d91b2d_3fa0_6f03_b3b8_f87c64a4f30a --> 2ff5ef5d_5050_1ab2_e4f2_f72e391945a2
  e07f6d54_afcc_052d_d33f_8ccdcc46f752["langgraph.runtime"]
  43d91b2d_3fa0_6f03_b3b8_f87c64a4f30a --> e07f6d54_afcc_052d_d33f_8ccdcc46f752
  998e41c8_6a3e_eb9b_699f_db967a1242f7["langchain.agents.factory"]
  43d91b2d_3fa0_6f03_b3b8_f87c64a4f30a --> 998e41c8_6a3e_eb9b_699f_db967a1242f7
  e5ada690_c0aa_cb0a_7295_6cf85318873d["langchain.agents.middleware.model_call_limit"]
  43d91b2d_3fa0_6f03_b3b8_f87c64a4f30a --> e5ada690_c0aa_cb0a_7295_6cf85318873d
  069947d2_727b_035a_0691_c12203e2f5a6["tests.unit_tests.agents.model"]
  43d91b2d_3fa0_6f03_b3b8_f87c64a4f30a --> 069947d2_727b_035a_0691_c12203e2f5a6
  style 43d91b2d_3fa0_6f03_b3b8_f87c64a4f30a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import pytest
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage
from langchain_core.tools import tool
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.runtime import Runtime

from langchain.agents.factory import create_agent
from langchain.agents.middleware.model_call_limit import (
    ModelCallLimitExceededError,
    ModelCallLimitMiddleware,
    ModelCallLimitState,
)
from tests.unit_tests.agents.model import FakeToolCallingModel


@tool
def simple_tool(value: str) -> str:
    """A simple tool."""
    return value


def test_middleware_unit_functionality() -> None:
    """Test that the middleware works as expected in isolation."""
    # Test with end behavior
    middleware = ModelCallLimitMiddleware(thread_limit=2, run_limit=1)

    runtime = Runtime()

    # Test when limits are not exceeded
    state = ModelCallLimitState(messages=[], thread_model_call_count=0, run_model_call_count=0)
    result = middleware.before_model(state, runtime)
    assert result is None

    # Test when thread limit is exceeded
    state = ModelCallLimitState(messages=[], thread_model_call_count=2, run_model_call_count=0)
    result = middleware.before_model(state, runtime)
    assert result is not None
    assert result["jump_to"] == "end"
    assert "messages" in result
    assert len(result["messages"]) == 1
    assert "thread limit (2/2)" in result["messages"][0].content

    # Test when run limit is exceeded
    state = ModelCallLimitState(messages=[], thread_model_call_count=1, run_model_call_count=1)
    result = middleware.before_model(state, runtime)
    assert result is not None
    assert result["jump_to"] == "end"
    assert "messages" in result
    assert len(result["messages"]) == 1
    assert "run limit (1/1)" in result["messages"][0].content

    # Test with error behavior
    middleware_exception = ModelCallLimitMiddleware(
        thread_limit=2, run_limit=1, exit_behavior="error"
    )

    # Test exception when thread limit exceeded
    state = ModelCallLimitState(messages=[], thread_model_call_count=2, run_model_call_count=0)
    with pytest.raises(ModelCallLimitExceededError) as exc_info:
        middleware_exception.before_model(state, runtime)
// ... (168 more lines)

Domain

Subdomains

Dependencies

  • langchain.agents.factory
  • langchain.agents.middleware.model_call_limit
  • langchain_core.messages
  • langchain_core.tools
  • langgraph.checkpoint.memory
  • langgraph.runtime
  • pytest
  • tests.unit_tests.agents.model

Frequently Asked Questions

What does test_model_call_limit.py do?
test_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 test_model_call_limit.py?
test_model_call_limit.py defines 7 function(s): simple_tool, test_exception_error_message, test_middleware_initialization_validation, test_middleware_unit_functionality, test_run_limit_resets_between_invocations, test_run_limit_with_create_agent, test_thread_limit_with_create_agent.
What does test_model_call_limit.py depend on?
test_model_call_limit.py imports 8 module(s): langchain.agents.factory, langchain.agents.middleware.model_call_limit, langchain_core.messages, langchain_core.tools, langgraph.checkpoint.memory, langgraph.runtime, pytest, tests.unit_tests.agents.model.
Where is test_model_call_limit.py in the architecture?
test_model_call_limit.py is located at libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_model_call_limit.py (domain: LangChainCore, subdomain: LanguageModelBase, directory: libs/langchain_v1/tests/unit_tests/agents/middleware/implementations).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free