Search⌘ K
AI Features

Building a Single-Agent Workflow

Explore the process of building a single-agent workflow with Claude's SDK, guiding a task from start to finish using focused tools, guardrails, and limits. Understand how to structure read and action tools, enforce business rules through hooks, and manage agent runtime safely. This lesson helps you implement, test, and explain workflows that can reliably support real-world customer processes.

A single-agent workflow is one Claude agent, with one focused set of tools, that carries a task from start to finish. It is the right first design for most customer problems. One agent is easier to test, cheaper to run, and simpler to explain than a team of agents, and many tasks never need more.

The workflow built here reviews purchase orders for a manufacturer’s procurement team. The agent looks up each order, checks it against the supplier’s contract and the department’s budget, and then approves it or flags it for a human reviewer. The code is one Python program built in four parts, and the complete version appears at the end.

The same build order works for almost any single agent. We give the agent tools to read, then tools to act, then guardrails around the actions, and finally limits on the run itself.

How the agent loop runs

When we start an agent, the SDK runs a loop. Claude reads the prompt, the system prompt, and the tool definitions, and decides what to do. If it requests tools, the SDK runs them and sends the results back, and Claude decides again. Each full cycle of request, tool run, and result is one turn. The loop ends when Claude responds without requesting any tools.

The SDK streams messages as the loop runs. An AssistantMessage shows each step Claude takes, including its tool calls, and a ResultMessage closes the run with the final answer, the number of turns, and the cost. Everything in a run accumulates in Claude’s context, including the prompt, every tool call, and every result, so tool outputs work best when they stay compact. The max_turns limit counts only turns that use tools, which makes it a direct cap on how many rounds of tool calls one run can take.

The sequence below shows the loop each run follows.

How the agent loop runs
How the agent loop runs: stage 1 of 5, Receive the prompt.
Overview

The SDK repeats this cycle until Claude responds without requesting a tool.

Do you find this helpful?

Because the loop runs on its own, our design work happens before it starts. Five decisions shape ...