"An error occurred while running the tool." By default, that sentence is all the OpenAI Agents SDK 0.25.0 gives the model when a tool raises an ordinary exception, a 401 included. Nothing in it tells a rejected key from a timeout. The agent calls again. It cannot learn from a reason it never saw, so the error text has to carry the status code and the server's message, and say whether a retry can help. When something fixes the call, write it down for the next run.
Before changing the prompt of an agent that keeps repeating a failed call, read the tool message the model received. Under the OpenAI default there is no reason in it, and that is deliberate. Its docstring describes a fixed error response "without exposing the exception to the model". I can see why a library would keep raw exceptions to itself, but I would not blame the model for the loop.
I read the source of five Python agent frameworks to see what each does when a tool raises. In smolagents the reason gets through, followed by "Please try again or use another tool", and for a 401 the first half of that will not help. A step later it tells the model to try a completely different approach after several retries. CrewAI passes the error too, but with a model that has no function calling it first runs the same call up to three times by itself, so part of the loop you see may be the framework's. In LangChain's BaseTool and in Pydantic AI, an ordinary exception goes up into your code and the run stops.
The three that keep the reason from the model each have a way to pass it. The OpenAI Agents SDK takes your own failure_error_function in place of the fixed sentence. In LangChain you raise a ToolException and switch on handle_tool_error, and in Pydantic AI you raise ModelRetry or ToolFailed. I would put the retry hint into the same text, because the same header will get the same 401, while after a 429 or a 503 the next try may work.
Inside one run the error stays in the context. The next run starts without it. For an agent, learning from past errors is a stored line with the endpoint, the status and the fix, kept where the next run will read it.
Once its own retries are spent, CrewAI passes the model the error, followed by "Moving on then."
I downloaded openai-agents 0.25.0, langchain-core 1.8.4, crewai 1.15.44, pydantic-ai-slim 2.71.0 and smolagents 1.26.0 from PyPI on 19 November 2026, checked their hashes and read the code without running it. The tool pages of the OpenAI Agents SDK and Pydantic AI, and LangChain's BaseTool reference, agree with the code. Agent loops built on these libraries, and other versions of them, may treat the error differently.
I work on Mnemoverse; none of the five frameworks here is ours.


