huggingface/smolagents · error · InterpreterError
Reached the max number of operations of {MAX_OPERATIONS}. Ma
Error message
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. What it means
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.
Source
Thrown at src/smolagents/local_python_executor.py:1445
This function will recurse through the nodes of the tree provided.
Args:
expression (`ast.AST`):
The code to evaluate, as an abstract syntax tree.
state (`Dict[str, Any]`):
A dictionary mapping variable names to values. The `state` is updated if need be when the evaluation
encounters assignments.
static_tools (`Dict[str, Callable]`):
Functions that may be called during the evaluation. Trying to change one of these static_tools will raise an error.
custom_tools (`Dict[str, Callable]`):
Functions that may be called during the evaluation. These custom_tools can be overwritten.
authorized_imports (`List[str]`):
The list of modules that can be imported by the code. By default, only a few safe modules are allowed.
If it contains "*", it will authorize any import. Use this at your own risk!
"""
if state.setdefault("_operations_count", {"counter": 0})["counter"] >= MAX_OPERATIONS:
raise InterpreterError(
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."
)
state["_operations_count"]["counter"] += 1
common_params = (state, static_tools, custom_tools, authorized_imports)
if isinstance(expression, ast.Assign):
# Assignment -> we evaluate the assignment which should update the state
# We return the variable assigned as it may be used to determine the final result.
return evaluate_assign(expression, *common_params)
elif isinstance(expression, ast.AnnAssign):
return evaluate_annassign(expression, *common_params)
elif isinstance(expression, ast.AugAssign):
return evaluate_augassign(expression, *common_params)
elif isinstance(expression, ast.Call):
# Function call -> we return the value of the function call
return evaluate_call(expression, *common_params)
elif isinstance(expression, ast.Constant):
# Constant -> just return the value
return expression.valueView on GitHub (pinned to 30bb116109)
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
Example fix
# before
while True:
if check_done():
break
time.sleep(1)
# after
for _ in range(100): # bounded
if check_done():
break
time.sleep(1) Defensive patterns
Strategy: validation
Validate before calling
# enforce bounded loops in generated code assert 'while True' not in code, 'unbounded loop detected' # and vectorize heavy work before execution
Try / catch
try:
evaluate_python(code, ...)
except InterpreterError as e:
if 'max number of operations' in str(e):
# split task into smaller chunks / vectorize / add bounds, then retry Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Code execution exceeded the maximum execution time of {timeo
- Maximum number of {MAX_WHILE_ITERATIONS} iterations in While
- Invoking a builtin function that has not been explicitly add
- Forbidden call to dunder function: {func.__name__}
- Import of {alias.name} is not allowed. Authorized imports ar
AI-assisted analysis of huggingface/smolagents@30bb116109 (2026-08-28).
Data as JSON: /api/errors/5b09a79efc947b43.
Report an issue: GitHub.