#Responses API infinite loop of tool calls
1 messages · Page 1 of 1 (latest)
Hi @warped marsh. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
This behavior is usually caused by how tool use is configured, not by the Responses API itself.
From the official docs and examples, there are three main ways to stop infinite tool-call loops:
-
Limit how many tools can be called per response
The Responses API supports
max_tool_calls, which hard-stops further built‑in tool calls once the limit is reached. Any additional attempts by the model to call tools are ignored, which prevents runaway loops (for example withimage_gen). [Image tool answer] -
Constrain tool‑use behavior in your agent / orchestration
In the
openai-agents-pythonframework, tool behavior is explicitly configurable:- Default:
"run_llm_again"– run tools, then call the LLM again with results. "stop_on_first_tool"– treat the first tool’s output as the final answer.StopAtTools(stop_at_tool_names=[...])– stop once a specific tool has been called and use its output as final. [Tool use behavior]
These options are designed specifically to avoid the pattern “tool → LLM → tool → LLM …” when the task is already complete.
- Default:
-
Reset tool choice to avoid forced re‑calling
The agents framework automatically resets
tool_choiceto"auto"after a tool call viaAgent.reset_tool_choice(defaultTrue) to prevent infinite loops of tool usage. Iftool_choiceis pinned (e.g., always a specific tool), the LLM can be forced to call tools again and again. [reset_tool_choice]
“To prevent infinite loops, the framework automatically resets
tool_choiceto "auto" after a tool call… The infinite loop is because tool results are sent to the LLM, which then generates another tool call because oftool_choice, ad infinitum.” [Tool use behavior]
What you can do concretely
- Set a hard cap with
max_tool_callsin your Responses request. [Image tool answer] - If you’re using
openai-agents-python, ensure:reset_tool_choiceis left asTrue, or explicitly set toTrue. [reset_tool_choice]tool_use_behavioris"stop_on_first_tool"orStopAtTools(...)when the tool output itself should be the final answer. [Tool use behavior]
- In custom loops (your own orchestration), add:
- A max iterations guard (like the 5‑turn loop in the Node.js agent example). [Node agent loop]
- A check that breaks once you have a satisfactory tool result instead of always sending it back to the model.