Home / Function/ test_thread_limit_with_create_agent() — langchain Function Reference

test_thread_limit_with_create_agent() — langchain Function Reference

Architecture documentation for the test_thread_limit_with_create_agent() function in test_model_call_limit.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  d92e56cf_f123_6567_ccd4_b396b4d4e27c["test_thread_limit_with_create_agent()"]
  43d91b2d_3fa0_6f03_b3b8_f87c64a4f30a["test_model_call_limit.py"]
  d92e56cf_f123_6567_ccd4_b396b4d4e27c -->|defined in| 43d91b2d_3fa0_6f03_b3b8_f87c64a4f30a
  style d92e56cf_f123_6567_ccd4_b396b4d4e27c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_model_call_limit.py lines 72–107

def test_thread_limit_with_create_agent() -> None:
    """Test that thread limits work correctly with create_agent."""
    model = FakeToolCallingModel()

    # Set thread limit to 1 (should be exceeded after 1 call)
    agent = create_agent(
        model=model,
        tools=[simple_tool],
        middleware=[ModelCallLimitMiddleware(thread_limit=1)],
        checkpointer=InMemorySaver(),
    )

    # First invocation should work - 1 model call, within thread limit
    result = agent.invoke(
        {"messages": [HumanMessage("Hello")]}, {"configurable": {"thread_id": "thread1"}}
    )

    # Should complete successfully with 1 model call
    assert "messages" in result
    assert len(result["messages"]) == 2  # Human + AI messages

    # Second invocation in same thread should hit thread limit
    # The agent should jump to end after detecting the limit
    result2 = agent.invoke(
        {"messages": [HumanMessage("Hello again")]}, {"configurable": {"thread_id": "thread1"}}
    )

    assert "messages" in result2
    # The agent should have detected the limit and jumped to end with a limit exceeded message
    # So we should have: previous messages + new human message + limit exceeded AI message
    assert len(result2["messages"]) == 4  # Previous Human + AI + New Human + Limit AI
    assert isinstance(result2["messages"][0], HumanMessage)  # First human
    assert isinstance(result2["messages"][1], AIMessage)  # First AI response
    assert isinstance(result2["messages"][2], HumanMessage)  # Second human
    assert isinstance(result2["messages"][3], AIMessage)  # Limit exceeded message
    assert "thread limit" in result2["messages"][3].content

Domain

Subdomains

Frequently Asked Questions

What does test_thread_limit_with_create_agent() do?
test_thread_limit_with_create_agent() is a function in the langchain codebase, defined in libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_model_call_limit.py.
Where is test_thread_limit_with_create_agent() defined?
test_thread_limit_with_create_agent() is defined in libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_model_call_limit.py at line 72.

Analyze Your Own Codebase

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

Try Supermodel Free