{"record":{"id":"5b09a79efc947b43","repo":"huggingface/smolagents","slug":"reached-the-max-number-of-operations-of-max-opera","errorCode":null,"errorMessage":"Reached the max number of operations of {MAX_OPERATIONS}. Maybe there is an infinite loop somewhere in the code, or you're just asking too many calculations.","messagePattern":"Reached the max number of operations of (.+?)\\. Maybe there is an infinite loop somewhere in the code, or you're just asking too many calculations\\.","errorType":"exception","errorClass":"InterpreterError","httpStatus":null,"severity":"error","filePath":"src/smolagents/local_python_executor.py","lineNumber":1445,"sourceCode":"\n    This function will recurse through the nodes of the tree provided.\n\n    Args:\n        expression (`ast.AST`):\n            The code to evaluate, as an abstract syntax tree.\n        state (`Dict[str, Any]`):\n            A dictionary mapping variable names to values. The `state` is updated if need be when the evaluation\n            encounters assignments.\n        static_tools (`Dict[str, Callable]`):\n            Functions that may be called during the evaluation. Trying to change one of these static_tools will raise an error.\n        custom_tools (`Dict[str, Callable]`):\n            Functions that may be called during the evaluation. These custom_tools can be overwritten.\n        authorized_imports (`List[str]`):\n            The list of modules that can be imported by the code. By default, only a few safe modules are allowed.\n            If it contains \"*\", it will authorize any import. Use this at your own risk!\n    \"\"\"\n    if state.setdefault(\"_operations_count\", {\"counter\": 0})[\"counter\"] >= MAX_OPERATIONS:\n        raise InterpreterError(\n            f\"Reached the max number of operations of {MAX_OPERATIONS}. Maybe there is an infinite loop somewhere in the code, or you're just asking too many calculations.\"\n        )\n    state[\"_operations_count\"][\"counter\"] += 1\n    common_params = (state, static_tools, custom_tools, authorized_imports)\n    if isinstance(expression, ast.Assign):\n        # Assignment -> we evaluate the assignment which should update the state\n        # We return the variable assigned as it may be used to determine the final result.\n        return evaluate_assign(expression, *common_params)\n    elif isinstance(expression, ast.AnnAssign):\n        return evaluate_annassign(expression, *common_params)\n    elif isinstance(expression, ast.AugAssign):\n        return evaluate_augassign(expression, *common_params)\n    elif isinstance(expression, ast.Call):\n        # Function call -> we return the value of the function call\n        return evaluate_call(expression, *common_params)\n    elif isinstance(expression, ast.Constant):\n        # Constant -> just return the value\n        return expression.value","sourceCodeStart":1427,"sourceCodeEnd":1463,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/local_python_executor.py#L1427-L1463","documentation":"Every AST node evaluation increments an operation counter; when it exceeds MAX_OPERATIONS the interpreter aborts to prevent runaway/infinite loops in agent-generated code. This is a hard sandbox resource limit, not a bug in your code's logic per se.","triggerScenarios":"while True loops without break, very long loops (millions of iterations), deeply chained comprehensions/recursion, or asking for a huge computation — each evaluated node counts toward the cap.","commonSituations":"Agent writes an unbounded polling/wait loop; large dataframe row-wise loops instead of vectorized ops; recursive functions without termination; legitimate heavy computation exceeding the fixed cap.","solutions":["Vectorize or batch the computation (numpy/pandas) so per-node operation counts drop drastically","Add explicit loop bounds/counters and break conditions in generated code","Split the work across multiple executor runs (re-invoke the agent/tool with smaller chunks)","Increase MAX_OPERATIONS in your own fork/config if you control the deployment and accept the risk"],"exampleFix":"# before\nwhile True:\n    if check_done():\n        break\n    time.sleep(1)\n# after\nfor _ in range(100):  # bounded\n    if check_done():\n        break\n    time.sleep(1)","handlingStrategy":"validation","validationCode":"# enforce bounded loops in generated code\nassert 'while True' not in code, 'unbounded loop detected'\n# and vectorize heavy work before execution","typeGuard":null,"tryCatchPattern":"try:\n    evaluate_python(code, ...)\nexcept InterpreterError as e:\n    if 'max number of operations' in str(e):\n        # split task into smaller chunks / vectorize / add bounds, then retry","preventionTips":["Always bound loops (for _ in range(N)) in agent code","Vectorize with numpy/pandas to cut node evaluations","Chunk large computations across multiple executor runs","Treat this error as an infinite-loop smell: review termination conditions"],"tags":["python-executor","operation-limit","infinite-loop","sandbox","smolagents"],"backgroundTag":"sandbox-operation-limit-exceeded","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}