"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.

Top comments (5)
The error text is half the fix; the other half is a failure ledger. Give the agent a table keyed by (tool, argument signature, error class) — and a hard rule: a retry must change something. Same tool, same args, same error class means no retry, escalate or stop. A 401 with the same key is terminal; a 429 with backoff is retryable; a timeout with a smaller page size is a different call. Without the ledger the model can't tell "I tried this" from "I tried this three times already" — the loop you describe is the agent re-discovering the same failure with a clean context each time.
One check from building this for real: in the Agentware trap pack I ran, the envelope was a hard policy (no sends, no credential use, kill switch on) and the ledger counted identical failures. Containment held at 0 sends / 0 leaks across the whole pack, and the runs that would have looped stopped at the ledger limit instead of burning the budget. The number that matters isn't the retry count, it's the count of retries that changed nothing.
The key in your ledger needs the error class, and that is the part some frameworks keep from the model. Under the OpenAI Agents SDK default, the model gets the same fixed sentence for any ordinary exception, so it cannot fill the ledger from what it sees. The ledger has to be written from the exception in your own code, before any text goes to the model.
Your rule that a retry must change something is broken inside one of the frameworks I read. CrewAI, with a model that has no function calling, runs the same call up to three times by itself before the model hears about the error.
That's a real correction on the ledger key, and it changes where the ledger has to live. If the model only sees the framework's fixed sentence for any ordinary exception, it can't fill a (tool, args, error class) key from what it sees — so the ledger has to be written from the exception in your own code, before any text goes to the model. The ledger is code, not prompt.
The CrewAI case breaks my rule as I stated it, and the fix is the same layering point: the rule 'a retry must change something' has to be enforced where the retry actually happens. A framework that retries the same call three times before the model hears about it is doing retries the ledger can't see. So the ledger has to count framework-initiated retries too — and where the framework won't expose them, the honest options are to disable that built-in retry, or treat the framework as untrusted for the retry budget and cap total attempts at the envelope level instead. The rule still holds; it just can't live at the model layer alone.
Edward, the most useful line in here is the one about checking the tool message before touching the prompt — most people would rewrite the system prompt three times before discovering the model was never told the call failed with a 401. Your point that the same header will get the same 401 is exactly why the retry hint belongs in the same text as the reason: without it, the model has no way to know the retry is doomed.
The part I would add is a small durable record between runs: for every failed call, store the endpoint, the status code and the hint that fixed it — or that it could not be fixed — and inject the matching record into the next run's context before the agent plans its calls. That turns your cross-run memory problem into a lookup, and it also gives you a count of which failures repeat most. One question: when you looked at the five frameworks, did any of them keep that kind of record across runs by default, or did every one of them start the next run with a clean slate?
I did not look at that. I read what each of the five hands the model inside one run when a tool raises, and stopped there, so I cannot say what any of them keeps for the next run by default. Your record is close to what I meant by a stored line with the endpoint, the status and the fix.
The count is the part I would build first. A failure that comes back run after run usually points at the tool or its configuration, and the record shows that before anyone opens the logs.