Messages API, Agent SDK, Claude Code or Managed Agents: where should your agent run?
Four ways to run a Claude agent compared by who runs the loop, where tools execute and who keeps state, with a decision path for CCAR-P scenarios.
The question behind the choice
The same Claude model can sit behind very different runtimes. What changes is who runs the agent loop, where tools and files live, and who keeps the state and recovers when something fails. The CCAR-P guide tests this directly, as deployment surface and agent runtime selection, and many integration scenarios depend on it too.
Four options side by side
| Who runs the loop | Where tools run | Typical fit | |
|---|---|---|---|
| Messages API (client SDK) | Your code: you send tool results back each turn | Your application for client tools; Anthropic for server tools such as web search | Custom loops and existing services that need full control |
| Claude Agent SDK | A library in a process you operate, running Claude Code's agent harness | That process: built-in tools, hooks, permissions, subagents and MCP | Embedding an agent in your own Python or TypeScript application |
| Claude Code headless (claude -p) | The Claude Code CLI, as a subprocess or a CI step | The machine or runner it runs on | Scripts, scheduled jobs and CI pipelines |
| Claude Managed Agents | Anthropic's hosted agent harness | An Anthropic-managed cloud sandbox, or a self-hosted sandbox | Long-running, asynchronous or scheduled work with little infrastructure |
When the Messages API is enough
If the job is a single request, or a loop you already run inside a service, call the Messages API directly. You own the loop: when Claude returns a tool_use block, your code runs the tool and sends the result back in the next user message, with the tool_result blocks first. You also own conversation history, retries and timeouts.
This gives you the most control, and it keeps tools that must stay inside your network where they are. The cost is that everything a harness would give you, such as file tools, permissions and session storage, is yours to build.
When to embed the Agent SDK
The Agent SDK is Claude Code's agent as a library. It runs the Claude Code binary inside a process you operate and gives you its capabilities: built-in tools for files, commands and search, hooks at points in the agent lifecycle, permission rules, subagents, MCP connections, and sessions you can resume or fork.
Choose it when the agent has to run next to your own data, binaries or network, and you don't want to write the tool loop yourself. You still operate the process, so scaling, monitoring and recovery are your team's job.
When headless Claude Code fits
Running claude -p turns Claude Code into a non-interactive command for scripts and CI. It exits with 0 on success and a non-zero code on failure, and --output-format json returns the result with metadata, including total_cost_usd, so a pipeline can branch on the outcome and track spend.
When Managed Agents fits
Claude Managed Agents hosts the harness for you. You define an agent (model, system prompt, tools, MCP servers and skills) and an environment, then start sessions and exchange events with them. Sessions are stateful: they keep a persistent filesystem and conversation history, so a long task can run for minutes or hours and a later request can continue the same session.
- Vaults hold credentials. The sandbox sees only a placeholder, and the real secret is substituted at egress for the hosts you allow, so clients that sign requests locally, such as AWS SigV4, don't work with them.
- A session budget sets a hard ceiling on one session's list cost.
- Scheduled deployments run agents on a cron schedule.
- Sandboxes can be self-hosted for compliance or data-residency needs, and the whole service can be self-hosted.
Two constraints matter in architecture reviews. First, Managed Agents is in beta, and the hosted service isn't eligible for zero data retention because transcripts persist until you delete them; a self-hosted deployment is the exception. Second, it runs on the Claude API, Claude Platform on AWS and Anthropic-managed Claude on Google Cloud, but not on Amazon Bedrock or Microsoft Foundry.
A quick decision path
- Must the tools touch files, binaries or systems that can't leave your environment? Use the Agent SDK or headless Claude Code there, or Managed Agents with a self-hosted sandbox.
- Is a zero data retention arrangement a hard requirement? Hosted Managed Agents is out; self-hosted Managed Agents or your own loop remain.
- Does the team want to avoid running the agent loop and sandbox at all? Managed Agents is built for that.
- Is it a job in an existing scheduler or CI runner? Headless Claude Code fits the runner you already have.
- Do you need a custom loop with full control over every turn? Use the Messages API.