{"record":{"id":"de4d170c87ec7bad","repo":"xtekky/gpt4free","slug":"tool-loop-detected-function-key-0-was-called","errorCode":null,"errorMessage":"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]}","messagePattern":"Tool loop detected: function '(.+?)' was called (.+?) times with identical arguments \\((.+?) retries, limit is (.+?)\\)\\. Stop repeating this call\\. Arguments: (.+?)","errorType":"exception","errorClass":"ToolLoopError","httpStatus":null,"severity":"error","filePath":"g4f/api/tool_loop_detection.py","lineNumber":184,"sourceCode":"                    if tc_id:\n                        pending_calls[tc_id] = key\n\n                    # Hard global cap.\n                    if total_tool_calls > GLOBAL_TOOL_CALL_LIMIT:\n                        raise ToolLoopError(\n                            f\"Tool loop detected: request contains {total_tool_calls} \"\n                            f\"tool calls (limit {GLOBAL_TOOL_CALL_LIMIT}). The assistant \"\n                            f\"is stuck calling tools repeatedly. Stop calling tools and \"\n                            f\"answer directly, or switch strategy (e.g. use read_file \"\n                            f\"with an absolute path instead of search).\",\n                            function_name=key[0],\n                            arguments=key[1],\n                            repeats=total_tool_calls,\n                        )\n\n                    # Per-query repeat cap.\n                    if call_counts[key] > MAX_REPEATS_PER_QUERY:\n                        raise ToolLoopError(\n                            f\"Tool loop detected: function '{key[0]}' was called \"\n                            f\"{call_counts[key]} times with identical arguments \"\n                            f\"({call_counts[key] - 1} retries, limit is \"\n                            f\"{MAX_REPEATS_PER_QUERY - 1}). Stop repeating this call. \"\n                            f\"Arguments: {key[1]}\",\n                            function_name=key[0],\n                            arguments=key[1],\n                            repeats=call_counts[key],\n                        )\n\n        # Tool result message: attribute to the originating call.\n        elif role == \"tool\":\n            tool_call_id = message.get(\"tool_call_id\")\n            content = message.get(\"content\")\n            key = pending_calls.get(tool_call_id) if tool_call_id else None\n            # If we can't attribute via id, fall back to the most recent call.\n            if key is None and pending_calls:\n                # Heuristic: attribute to the last registered pending call.","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/api/tool_loop_detection.py#L166-L202","documentation":"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.","triggerScenarios":"Thrown at g4f/api/tool_loop_detection.py:184 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Do not repeat the same function call with identical arguments; change the arguments or switch tools."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}