Home / Function/ _find_safe_cutoff_point() — langchain Function Reference

_find_safe_cutoff_point() — langchain Function Reference

Architecture documentation for the _find_safe_cutoff_point() function in summarization.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  6bd2bf47_a4ef_fe6f_a2a2_89f8125a4eaa["_find_safe_cutoff_point()"]
  a622f56b_f697_966d_5bc7_095699ccca34["SummarizationMiddleware"]
  6bd2bf47_a4ef_fe6f_a2a2_89f8125a4eaa -->|defined in| a622f56b_f697_966d_5bc7_095699ccca34
  82390de3_387f_3a0a_61c0_4eb09524c027["_find_token_based_cutoff()"]
  82390de3_387f_3a0a_61c0_4eb09524c027 -->|calls| 6bd2bf47_a4ef_fe6f_a2a2_89f8125a4eaa
  d7f278f1_55ee_3351_0744_37d80b5a183d["_find_safe_cutoff()"]
  d7f278f1_55ee_3351_0744_37d80b5a183d -->|calls| 6bd2bf47_a4ef_fe6f_a2a2_89f8125a4eaa
  style 6bd2bf47_a4ef_fe6f_a2a2_89f8125a4eaa fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain_v1/langchain/agents/middleware/summarization.py lines 553–586

    def _find_safe_cutoff_point(messages: list[AnyMessage], cutoff_index: int) -> int:
        """Find a safe cutoff point that doesn't split AI/Tool message pairs.

        If the message at `cutoff_index` is a `ToolMessage`, search backward for the
        `AIMessage` containing the corresponding `tool_calls` and adjust the cutoff to
        include it. This ensures tool call requests and responses stay together.

        Falls back to advancing forward past `ToolMessage` objects only if no matching
        `AIMessage` is found (edge case).
        """
        if cutoff_index >= len(messages) or not isinstance(messages[cutoff_index], ToolMessage):
            return cutoff_index

        # Collect tool_call_ids from consecutive ToolMessages at/after cutoff
        tool_call_ids: set[str] = set()
        idx = cutoff_index
        while idx < len(messages) and isinstance(messages[idx], ToolMessage):
            tool_msg = cast("ToolMessage", messages[idx])
            if tool_msg.tool_call_id:
                tool_call_ids.add(tool_msg.tool_call_id)
            idx += 1

        # Search backward for AIMessage with matching tool_calls
        for i in range(cutoff_index - 1, -1, -1):
            msg = messages[i]
            if isinstance(msg, AIMessage) and msg.tool_calls:
                ai_tool_call_ids = {tc.get("id") for tc in msg.tool_calls if tc.get("id")}
                if tool_call_ids & ai_tool_call_ids:
                    # Found the AIMessage - move cutoff to include it
                    return i

        # Fallback: no matching AIMessage found, advance past ToolMessages to avoid
        # orphaned tool responses
        return idx

Domain

Subdomains

Frequently Asked Questions

What does _find_safe_cutoff_point() do?
_find_safe_cutoff_point() is a function in the langchain codebase, defined in libs/langchain_v1/langchain/agents/middleware/summarization.py.
Where is _find_safe_cutoff_point() defined?
_find_safe_cutoff_point() is defined in libs/langchain_v1/langchain/agents/middleware/summarization.py at line 553.
What calls _find_safe_cutoff_point()?
_find_safe_cutoff_point() is called by 2 function(s): _find_safe_cutoff, _find_token_based_cutoff.

Analyze Your Own Codebase

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

Try Supermodel Free