Everything else in this guide happens in a conversation: you type, Claude works, you read. But the same agent can run with nobody watching: in a CI job, a pre-commit check, or a script that triages issues overnight. There are two ways in, and both run the agent loop that powers Claude Code.
From the shell: claude -p
Add -p (or --print) to any claude command and it runs non-interactively. It takes the prompt, works through the loop, prints the result and exits, with code 0 on success and non-zero on failure, so scripts can branch on it. It reads stdin, so it pipes like any command-line tool:
cat build-error.txt | claude -p 'explain the root cause' > output.txtThe flags you’ll reach for:
--output-format jsonreturns the result with the session ID, usage and an estimated cost;stream-jsonstreams events as they happen. Add--json-schemato get output validated against a schema.--allowedTools "Read,Edit,Bash"pre-approves tools, since nobody is there to answer a permission prompt.--permission-modesets a baseline for the whole run instead.--continueand--resumepick an earlier conversation back up.
By default, -p loads everything an interactive session would: CLAUDE.md, hooks, skills, MCP servers. In CI, add --bare to skip that auto-discovery, so every machine gets the same result. You then pass in what you need, for example with --mcp-config or --append-system-prompt. Bare mode doesn’t read your subscription login; it needs ANTHROPIC_API_KEY.
From code: the Agent SDK
The Agent SDK gives you the same tools, agent loop and context management as a library: @anthropic-ai/claude-agent-sdk for TypeScript, claude-agent-sdk for Python. You call query() with a prompt and options, then iterate over messages as Claude works. First comes an init message, then Claude’s tool calls and the tool results, and finally a result message with the answer, cost, usage and session ID. Hooks, subagents, MCP, permissions, sessions and skills all carry over, and you can cap a run with maxTurns or maxBudgetUsd.
It isn’t the same as calling the Claude API directly. With the plain client SDK you write the tool loop yourself; the Agent SDK runs Claude Code’s loop for you.
The catch
Nobody is watching. Without --bare, a -p run executes the project’s hooks and connects the servers in its .mcp.json, even in a folder you’ve never trusted, with no trust dialog. Grant the narrowest tools that work (Bash(git diff *) rather than all of Bash), set a budget, and if you build a product on the SDK, authenticate with an API key: the docs don’t allow offering claude.ai login in third-party products without approval.