Agent Construction with Claude
Pick the right way to build and deploy a Claude agent (Messages API loop, Agent SDK, or Managed Agents, self-hosted or Anthropic-hosted), and use harness controls such as hooks, permissions and limits for behavior that must be deterministic.
Key points
- 1
The construction spectrum runs from most control to least effort. A custom loop on the Messages API gives full control, but you build the tools, loop and context handling. The Claude Agent SDK is Claude Code's harness as a Python/TypeScript library in your own process. Claude Managed Agents is a hosted harness driven through the API.
- 2
Custom loop: while
stop_reasonis"tool_use", run the requested tools and sendtool_resultblocks, each matched bytool_use_id, in the next user message. Stop when Claude ends its turn, and always enforce a maximum-iteration cap. - 3
When Claude calls several tools in one turn, return every
tool_resulttogether in a single user message, placed before any text. - 4
The Agent SDK ships the same built-in tools (file read, edit, shell, search), agent loop, permissions, sessions, hooks and automatic compaction that power Claude Code. To reach the same loop from another language, run the CLI with
-pand JSON output. - 5
By default the Agent SDK loads the same filesystem configuration as the CLI (user, project and local settings, CLAUDE.md, skills, hooks). For reproducible services, restrict
settingSourcesand define behavior in code. - 6
Loop limits:
maxTurns/max_turnscaps tool-use turns, andmaxBudgetUsd/max_budget_usdcaps estimated spend. When a limit is hit, the result carries an error subtype (for exampleerror_max_turns), so check it before recording success. - 7
SDK hosting patterns: ephemeral (a container per one-off task), long-running (persistent containers), hybrid (ephemeral containers that resume from a session store), and multi-agent containers. Transcripts on local disk are lost when a container is removed.
- 8
Claude Managed Agents is built on an agent (model, prompt, tools, MCP servers, skills), an environment, a session and events. It suits long-running, asynchronous work with minimal infrastructure, and sessions persist event history server-side.
- 9
Deployment model: a Managed Agents environment runs either in an Anthropic-managed cloud sandbox or in a self-hosted sandbox on your infrastructure. The self-hosted option serves compliance and data-residency needs. For fully self-hosted, run the Agent SDK or your own loop.
- 10
Hooks give deterministic control.
PreToolUseruns before a tool executes and can deny or modify the call (use it for guardrails).PostToolUseruns after execution (use it for formatting, linting and audit logs). Prompt instructions are only guidance. - 11
SDK permission order is: hooks, deny rules, ask rules, permission mode, allow rules, then the
canUseToolcallback. A call approved at an earlier step never reachescanUseTool, and deny rules block even inbypassPermissions. - 12
Put human-in-the-loop approval in the harness, on the high-impact actions (send, pay, delete), rather than on every tool call or in the prompt.
- 13
Trap:
max_tokenscaps a single response, not the number of loop iterations or the total cost. Runaway loops need turn or budget limits in the harness.
Read the source
Test yourself on Agent Construction with Claude
Ten questions, with the answer and explanation after each one.