Advanced 1 concept to know first

Run it programmatically

Run Claude Code with nobody at the keyboard: claude -p for scripts and CI, or the Agent SDK to drive the same agent loop, tools and context handling from your own Python or TypeScript code.

The animation needs JavaScript; every step is described below.

0:16

Know these first

1 concept · basics first
  1. Context

    The context window is everything Claude can see in a session: your messages, the files it reads, tool output and auto-loaded instructions. It fills as you work, and Claude Code compacts it when it gets full.

  2. Run it programmatically · you are here

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.txt

The flags you’ll reach for:

  • --output-format json returns the result with the session ID, usage and an estimated cost; stream-json streams events as they happen. Add --json-schema to get output validated against a schema.
  • --allowedTools "Read,Edit,Bash" pre-approves tools, since nobody is there to answer a permission prompt. --permission-mode sets a baseline for the whole run instead.
  • --continue and --resume pick 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.

Where it sits

Before this

Context
Run it programmatically

Leads to

Nothing yet.

The ideas underneath · Field Guide

Sources

  1. Run Claude Code programmatically . Claude Code docs: claude -p, bare mode, output formats, auto-approving tools
  2. Agent SDK overview . Claude Code docs: what the SDK is, how it compares, capabilities
  3. How the agent loop works . Claude Code docs: turns, message types, budgets

Verified against the official Claude Code docs on October 1, 2026 · next review by January 1, 2027. Claude Code changes quickly; the docs are the source of truth.