Data: Anthropic CLI
Anthropic CLI (ant)
The ant CLI exposes every Claude API resource as a shell subcommand. Compared to curl: request bodies are built from typed flags or piped YAML instead of hand-written JSON, @path inlines file contents into any string field, --transform extracts fields with a GJSON path (no jq), list endpoints auto-paginate (cap total results with --max-items N; --limit only sets the server page size), and the beta: prefix auto-sets the right anthropic-beta header.
When to use the CLI vs the SDK
CLI for the control plane, SDK for the data plane. Agents and environments are relatively static resources you define, configure, and debug with ant — check the YAML into your repo, apply from CI, inspect from a terminal. Sessions are dynamic and driven by your application through the SDK — create per task, stream events, react to tool calls, integrate into your product. Both hit the same API; the split is about where the call lives, not what’s possible.
Control plane → ant | Data plane → SDK | |
|---|---|---|
| Resources | agents, environments, skills, vaults, files | sessions, events |
| Cadence | Once per deploy / ad-hoc | Every task / every turn |
| Lives in | *.yaml in your repo + CI + terminal | Application code |
| Typical calls | create < agent.yaml, update --version N, list, retrieve, archive, --debug | sessions.create(), events.stream(), events.send() |
Install and auth
Auth is ANTHROPIC_API_KEY from the environment. Override the host with ANTHROPIC_BASE_URL or --base-url.
Command structure
Beta resources (agents, sessions, environments, deployments, skills, vaults, memory stores) live under beta: — the CLI auto-sends the right anthropic-beta header, so don’t pass it yourself unless overriding with --beta <header>.
ant --help lists resources; append --help to any subcommand for its flags.
Global flags
| Flag | Purpose |
|---|---|
--format | auto (default: pretty if TTY, compact if piped), json, jsonl, yaml, pretty, raw, explore (interactive TUI) |
--transform | GJSON path applied to the response (per-item on list endpoints). Not applied when --format raw. |
-r, --raw-output | If the transformed result is a string, print it without quotes (jq semantics). Pair with --transform for scalar capture. |
--max-items | Cap total results returned from auto-paginating list endpoints (distinct from --limit, which is the server page size). |
--format-error / --transform-error | Same as --format/--transform, applied to error responses. -r does not apply to the error path — use --format-error yaml for unquoted error scalars. |
--base-url | Override API host |
--debug | Print full HTTP request + response to stderr (API key redacted) |
Output — --transform + --format
--transform takes a GJSON path. On list endpoints it runs per item, not on the envelope.
Extract a scalar for shell use: pair --transform with -r (--raw-output — prints strings unquoted, jq-style):
Input — flags, stdin, @file
Flags — scalar fields map directly. Structured fields accept relaxed-YAML syntax (unquoted keys) or strict JSON. Repeatable flags build arrays (each --tool, --event, --message appends one element):
Stdin — pipe a full JSON or YAML body. Merged with flags; flags win on conflict (for array fields, any flag replaces the stdin array entirely — it does not append). Quote the heredoc delimiter (<<'YAML') to disable shell expansion inside the body:
@file references — inline a file’s contents into any string-valued field. Inside structured flag values, quote the path. Binary files are auto-base64’d; force with @file:// (text) or @data:// (base64). Escape a literal leading @ as \@.
Flags that natively take a file path (e.g. --file on beta:files upload) accept a bare path without @.
Version-controlled Managed Agents resources
This is the recommended flow for defining agents and environments — check the YAML into your repo and sync via create (first time) / update (thereafter). See shared/managed-agents-core.md for the field reference.
Same pattern for environments (ant beta:environments create|update < env.yaml), then start a session with both IDs:
Interactive session loop (stream-before-send)
ant beta:sessions:events stream only delivers events emitted after the stream opens — so open it before sending the kickoff to avoid missing early events. Use process substitution to hold the stream on a file descriptor, send, then read:
This works for interactive exploration and demos. For application code that needs to react to agent.tool_use / agent.custom_tool_use events, reconnect after drops, or dedup against events.list, use the SDK — see shared/managed-agents-client-patterns.md.
Scripting patterns
--transform id -r on a list endpoint emits one bare ID per line — compose with xargs, or use --max-items N to bound the result set without piping through head:
Error shaping mirrors the success path (note: -r does not apply to error output — use --format-error yaml for an unquoted scalar here):
Shell completion: ant @completion {zsh|bash|fish|powershell}.
For the full, always-current reference (including per-endpoint flags), WebFetch the Anthropic CLI URL in shared/live-sources.md.