xtekky/gpt4free · error · ToolLoopError

Tool loop detected: function '{key[0]}' was called {call_cou

Error message

Tool loop detected: function '{key[0]}' was called {call_counts[key]} times with identical arguments ({call_counts[key] - 1} retries, limit is {MAX_REPEATS_PER_QUERY - 1}). Stop repeating this call. Arguments: {key[1]}

What it means

Error "Tool loop detected: function '{key[0]}' was called {call_counts[key]} times with identical arguments ({call_counts[key] - 1} retries, limit is {MAX_REPEATS_PER_QUERY - 1}). Stop repeating this call. Arguments: {key[1]}" thrown in xtekky/gpt4free.

Source

Thrown at g4f/api/tool_loop_detection.py:184

                    if tc_id:
                        pending_calls[tc_id] = key

                    # Hard global cap.
                    if total_tool_calls > GLOBAL_TOOL_CALL_LIMIT:
                        raise ToolLoopError(
                            f"Tool loop detected: request contains {total_tool_calls} "
                            f"tool calls (limit {GLOBAL_TOOL_CALL_LIMIT}). The assistant "
                            f"is stuck calling tools repeatedly. Stop calling tools and "
                            f"answer directly, or switch strategy (e.g. use read_file "
                            f"with an absolute path instead of search).",
                            function_name=key[0],
                            arguments=key[1],
                            repeats=total_tool_calls,
                        )

                    # Per-query repeat cap.
                    if call_counts[key] > MAX_REPEATS_PER_QUERY:
                        raise ToolLoopError(
                            f"Tool loop detected: function '{key[0]}' was called "
                            f"{call_counts[key]} times with identical arguments "
                            f"({call_counts[key] - 1} retries, limit is "
                            f"{MAX_REPEATS_PER_QUERY - 1}). Stop repeating this call. "
                            f"Arguments: {key[1]}",
                            function_name=key[0],
                            arguments=key[1],
                            repeats=call_counts[key],
                        )

        # Tool result message: attribute to the originating call.
        elif role == "tool":
            tool_call_id = message.get("tool_call_id")
            content = message.get("content")
            key = pending_calls.get(tool_call_id) if tool_call_id else None
            # If we can't attribute via id, fall back to the most recent call.
            if key is None and pending_calls:
                # Heuristic: attribute to the last registered pending call.

View on GitHub (pinned to 973504e177)

Solutions

  1. Do not repeat the same function call with identical arguments; change the arguments or switch tools.

When it happens

Trigger: Thrown at g4f/api/tool_loop_detection.py:184 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14). Data as JSON: /api/errors/de4d170c87ec7bad. Report an issue: GitHub.