Why does my agent stop with stop_reason "max_tokens"?
stop_reason “max_tokens” means the model’s response reached the max_tokens limit set in your request and was cut off before it finished. Anthropic’s docs say stop_reason is part of every successful Messages API response and, unlike an error, tells you why generation ended. The API returns 200, so nothing raises.
The fix is to raise max_tokens or ask the model to continue the response. Read the value on every response, not only when something looks wrong.
The case that hurts an agent is a cut-off tool call. When the truncated response contains an incomplete tool_use block, the docs say to retry the request with a higher max_tokens to get the full call. Running the partial call, or treating the cut-off text as the answer, sends the agent on with bad input.
A loop that never checks stop_reason passes a truncated reply as a finished one, and the run ends as a silent failure.
A larger max_tokens fixes one run, but a response that keeps hitting the limit is worth reading in the trace.
published