Study FAQ
How does the Claude Agent SDK decide when to call tools and finish a run?
Also asked as 2
- What happens from prompt to final result in a Claude Agent SDK loop?
- How are tool results fed back during a Claude Agent SDK run?
Answer
The Claude Agent SDK runs an iterative loop. Claude receives the task, current conversation and tool definitions, then emits text, tool requests, or both. The harness checks and executes permitted tool requests, adds their results to the conversation, and calls Claude again with the updated evidence. A response without another tool call ends the normal loop. Limits and runtime errors can also stop it.
For an evidence-gathering agent, the first tool result might contain a reference to a document. Claude can request the document in a later turn and use the returned evidence in its final response. A denied or failed action should remain visible as that outcome. A denial matters even when the model can still answer from other evidence.
Use the SDK’s result message to observe how the run ended. Text can appear alongside a tool request mid-run, so watch for the result event when deciding whether the run has reached its normal completion point. Your application sets the task, permission policy and success checks throughout.
Full explanation
Claude Agent Loops, Sessions, and Runtime Controls
Study guide · CCAR-F
Read the full explanation