Claude unveils agentic loop, detailing stopReason, tool_use and core AI agent pattern
Photo by Possessed Photography on Unsplash
While many expect Claude agents to finish after a single reply, reports indicate they often hit a “stopReason” loop—triggering “tool_use” to run a tool, append results, then repeat, only ending with “end_turn” when truly done.
Key Facts
- •Key company: Claude
Claude’s “agentic loop” is now being codified as a three‑step pattern that any production‑grade integration must follow, according to a March 20 technical walkthrough posted on cloudedventures.com. The author, who goes by “Aj,” explains that the loop hinges on the model’s stopReason field, which can be either tool_use or end_turn. When Claude returns tool_use, the surrounding code must invoke the indicated tool, append the tool’s JSON result to the conversation, and then feed the updated message history back to Claude. The cycle repeats until Claude finally emits end_turn, at which point the assistant’s textual output is returned to the user.
The write‑up stresses two implementation details that are easy to overlook. First, the assistant’s response must be appended to the message list before any tool results are added; otherwise Claude loses context for subsequent reasoning. Aj points out that many tutorials get this order wrong, leading to “missing assistant message” bugs that cause the loop to stall. Second, the stop condition should never be inferred from the content of the response (e.g., looking for a particular phrase). The only reliable signal is the explicit stopReason field, which the Bedrock API guarantees to include. This design choice reflects the fact that Claude itself does not “know” when to call a tool—it merely signals its intent, leaving the orchestration to the client code.
The sample Python implementation shared in the article uses the AWS bedrock-runtime client to converse with Claude‑3‑Sonnet (model ID anthropic.claude-3-sonnet-20240229-v1:0). After defining a single tool—`get_order_status`—the `run_agent` function builds an initial user message, then iterates up to twenty times as a safety cap. Each iteration sends the current message list and the tool specifications to Claude, reads the stopReason, and either returns the final text (when end_turn is seen) or runs the requested tool, wraps its JSON payload in a toolResult block, and appends that block as a new user message before looping again. The code demonstrates the exact ordering required: Claude’s output message is added first, then the tool result is attached as a separate user turn.
Beyond the mechanics, Aj argues that understanding this loop is crucial for any enterprise deployment that relies on Claude’s tool‑use capability. Because the model’s reasoning is stateless between calls, the client must preserve the full conversation history, including both assistant messages and tool results, to give Claude the context it needs to decide whether further tool calls are necessary. Ignoring the loop or using ad‑hoc stop checks can lead to “infinite” cycles or premature termination, both of which waste compute credits on Bedrock and degrade user experience. The article also notes that the loop’s simplicity—just a conditional on stopReason—makes it portable across languages and runtimes, but only if developers respect the ordering and safety‑cap conventions.
The broader AI community has taken note of Claude’s agentic loop as a concrete example of how large language models can be harnessed for autonomous tool use. 9to5Mac recently reported intermittent login and performance issues with Claude AI and Claude Code, underscoring the importance of robust client‑side orchestration when scaling to production workloads. Meanwhile, Engadget highlighted Anthropic’s “AI constitution,” which aims to guard Claude against adversarial prompts—a safeguard that operates independently of the agentic loop but complements it by ensuring the model’s tool‑use requests remain within policy bounds. Together, these developments suggest that Anthropic is moving from experimental demos toward a more disciplined, production‑ready framework for AI agents, with the stopReason‑driven loop at its core.
Sources
No primary source found (coverage-based)
- Dev.to AI Tag
Reporting based on verified sources and public filings. Sector HQ editorial standards require multi-source attribution.