DEV Community

Cover image for My autonomous Python agent keeps repeating failed API calls. How do I make it learn from past errors?
Edward Izgorodin
Edward Izgorodin

Posted on

My autonomous Python agent keeps repeating failed API calls. How do I make it learn from past errors?

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

By default, when a tool raises on a 401, smolagents and crewai pass the reason to the model, openai-agents a fixed sentence, langchain-core BaseTool and pydantic-ai nothing

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 (1)

Collapse
 
userxnom profile image
Abdullah Naseer •

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?