claudio.cool
CCDV-FLessons

CCDV-F · Agents and Workflows

How to build the agent, with your own loop, the Agent SDK or Managed Agents

⏱ 3 min readDraft

Inside, an agent is always the same thing: Claude asks for a tool, someone runs it, the result comes back and the cycle repeats until the task is done. That cycle is the agent loop, and the code that holds it up is the harness. The exam question is almost never “can it be done?”, but “who should write and operate that harness?”.

Three ways to build it

Option Who runs the loop When it fits
Your own loop with the client SDK (Messages API) Your code, end to end Fine-grained control, a few custom tools, no files or terminal
Claude Agent SDK A library in a process you operate You want Claude Code’s loop, built-in tools, permissions, sessions, hooks and compaction inside your app or your CI
Claude Managed Agents A harness hosted by Anthropic Long, asynchronous tasks, with nobody to build infrastructure; the sandbox can be Anthropic’s or your own

Watch for the clues in the scenario:

  • “We maintain file reading, permissions, session resume and compaction by hand” → that already comes with the Agent SDK.
  • “There are two of us, we have no infrastructure and the agent runs for hours at night” → Managed Agents.
  • “One call with one custom tool” → the API directly is enough; no framework or agent SDK is needed.

If you write the loop yourself

The loop is governed by the stop_reason of each response, not by the text:

  1. While stop_reason is tool_use, you run the requested tools and continue.
  2. You return one tool_result for every tool_use block, with its tool_use_id, all together in the next user message. If Claude asked for three tools and you return one, the API answers with a 400.
  3. On any other value (end_turn, max_tokens, refusal) the loop ends or is handled separately.
  4. There is always an iteration or budget cap, as a safety net.

Stopping when the model writes “DONE” fails both ways: sometimes it doesn’t write it and the loop runs away, sometimes it writes it too early.

Whatever must always happen goes in a hook

A hook is your own code that runs at a fixed point in the cycle, for example before a tool runs (PreToolUse) or after it (PostToolUse). It always runs, whatever the model decides:

  • logging every call and its result to the audit trail;
  • blocking a destructive command or an amount outside policy;
  • normalizing a result before Claude reads it.

An instruction in the system prompt (“log every action”) is a suggestion: it will fail in some percentage of cases.

The usual traps

  • Asking in the prompt, in capitals or with examples, for what a hook guarantees in code.
  • A loop with no cap, or one that stops on a word in the text.
  • Reimplementing tools, permissions and sessions that the Agent SDK already provides.
  • Building your own loop, sandbox and sessions when the team has nobody to operate them and no requirement calls for it.
  • Adding an agent framework “just in case” to a simple call.

On the exam

Read who will operate the agent and what is already solved. If the team is reinventing what Claude Code does, the answer is the Agent SDK; if they don’t want to operate anything and the task is long, Managed Agents; if the problem is that the loop never ends or ends badly, look for the option that checks stop_reason and adds a cap. And if “no exceptions” appears, rule out anything that is a prompt and look for the hook.

Quick question

A practice exam question on this domain. It counts towards your progress.

An insurance broker's platform team built an intake assistant with the Claude Agent SDK. Every new claim must go through the same four steps in order: extract the policy number from the uploaded form, validate it against the policy database, classify the claim type, and draft an acknowledgment email. The agent receives all four instructions in its system prompt and has a tool for each step.

After a month in production, the operations lead reviewed 300 runs. In about 6% of them the agent skipped validation when the policy number "looked right", and in a few others it drafted the email before classifying the claim. The team tried rewording the system prompt with "ALWAYS" in capitals, which reduced the skips but did not eliminate them.

The team is four developers working in Python, and the next audit of the claims process is in two months.

What is the best change to the design?

Practise this domain in a practice exam →