{"record":{"id":"70a1c02b63159227","repo":"BerriAI/litellm","slug":"exceeded-max-agentic-loops-max-loops-for-model","errorCode":null,"errorMessage":"Exceeded max_agentic_loops={max_loops} for model={model}","messagePattern":"Exceeded max_agentic_loops=(.+?) for model=(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/chat_completion_agentic_loop.py","lineNumber":79,"sourceCode":"def _fingerprint_tools(tool_calls: object) -> str:\n    try:\n        return json.dumps(tool_calls, sort_keys=True, default=str)\n    except Exception:\n        return str(tool_calls)\n\n\ndef _check_agentic_loop_safety(\n    tool_calls: object,\n    fingerprints: list[str],\n    depth: int,\n    max_loops: int,\n    model: str,\n) -> str:\n    fingerprint: Final = _fingerprint_tools(tool_calls)\n    if fingerprint in fingerprints:\n        raise ValueError(\"Agentic loop detected repeated tool-call fingerprint; aborting rerun\")\n    if depth >= max_loops:\n        raise ValueError(f\"Exceeded max_agentic_loops={max_loops} for model={model}\")\n    return fingerprint\n\n\ndef _wrap_response_as_fake_stream(response: object) -> object:\n    if getattr(response, \"object\", None) == \"chat.completion.chunk\":\n        return response\n    if not hasattr(response, \"choices\"):\n        return response\n    from litellm.llms.base_llm.base_model_iterator import (\n        convert_model_response_to_streaming,\n    )\n\n    return convert_model_response_to_streaming(cast(ModelResponse, response))\n\n\ndef _add_agentic_loop_metadata(kwargs_for_followup: dict[str, object]) -> None:\n    metadata = kwargs_for_followup.get(\"litellm_metadata\")\n    metadata = dict(metadata) if isinstance(metadata, dict) else {}","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/chat_completion_agentic_loop.py#L61-L97","documentation":"Agentic-loop safety guard: the rerun loop reached the configured depth equal to max_agentic_loops for the model without finishing. This is the cap that stops unbounded agent/tool-call reruns (distinct from the repeated-fingerprint abort checked just before it).","triggerScenarios":"Thrown at litellm/litellm_core_utils/chat_completion_agentic_loop.py:79 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Raise max_agentic_loops if the task legitimately needs more iterations.","Inspect the loop's tool calls to find why the model never finishes; adjust the prompt or tools."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}