November 20, 2026 · 4 min read
Anthropic’s docs describe three ways to start Claude Code from your own code: the claude -p command, the Agent SDK for Python and TypeScript, and the API trigger of a cloud scheduled task, formerly a routine. To call Claude models directly instead, the same docs point to the Client SDK for the Claude API.
When you call Claude Code for the same job every day, there is a fourth option: have it write the job once as a Whenever workflow that runs on a webhook or a schedule, with no AI model call on a run unless you add an AI step.
Run Claude Code from a script with claude -p #
Anthropic’s docs call this running Claude Code in non-interactive mode: add the -p (or --print) flag to any claude command, with your prompt and the CLI options you need.
claude -p "What does the auth module do?"
Claude Code exits with code 0 on success and a non-zero code when the run fails, so a script can branch on the exit status. With --output-format json, the response includes total_cost_usd, a client-side cost estimate that can differ from your actual bill.
claude -p "Summarize this project" --output-format json | jq -r '.result'
For CI and scripts, the docs recommend --bare: it skips auto-discovery of hooks, skills, plugins, MCP servers and CLAUDE.md, and it does not use your subscription login, so set ANTHROPIC_API_KEY with a key created in the Claude Console.
Embed it with the Agent SDK #
The Agent SDK gives you the same tools, agent loop and context management that power Claude Code, as a Python or TypeScript library in a process you operate. From any other language, the docs say to run the CLI as a subprocess with the -p flag and --output-format json.
A product you offer to other people authenticates with an API key: unless previously approved, Anthropic does not allow third-party developers to offer claude.ai login or rate limits for their products, including agents built on the Agent SDK.
Start a cloud routine from an HTTP request #
A cloud scheduled task, formerly a routine, can have an API trigger: a dedicated endpoint you POST to with the task’s bearer token, which starts a new session and returns its URL. An optional text field passes run-specific context, such as an alert body, alongside the saved prompt.
curl -X POST https://api.anthropic.com/v1/claude_code/routines/<trigger-id>/fire \
-H "Authorization: Bearer <token>" \
-H "anthropic-beta: experimental-cc-routine-2026-04-01" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{"text": "Sentry alert fired in prod."}'
The /fire endpoint is available to claude.ai users only and is not part of the Claude Platform API surface. It ships under a beta header while scheduled tasks are in research preview, and scheduled tasks draw down subscription usage the same way interactive sessions do.
Each call starts Claude again #
All three routes run Claude Code’s agent loop: claude -p and the Agent SDK in your process, and a routine’s API trigger in a new cloud session each time it fires. That is what you want while the task needs judgment, such as reading code and deciding what to change.
It is the wrong shape for a task whose steps are the same every time: a daily report, a sync between two tools, a check that posts to Slack. Each call asks the model to work out the same steps again.
Turn a repeated Claude Code task into a Whenever workflow #
Ask Claude Code to write the job once as a Whenever workflow. Connect it to the Whenever MCP server, or paste one line:
Turn this task into a Whenever workflow and deploy it. Follow www.whenever.dev/skill.md
It writes the workflow and saves a draft; you complete Setup in the browser and publish. The published workflow starts on its own schedule, webhook or app event: a webhook your system calls where it used to run claude -p or the /fire endpoint, or a schedule where a cron entry started it.
- It runs the same way every time: every run executes the workflow’s saved, numbered version, so the same input gives the same result on run 1 and run 1,000.
- One run, however many steps: a run is one execution, start to finish, whatever it touches.
- No AI model call on every run: a published workflow calls a model only in an AI step you added.
- It keeps running without a session: nobody opens Claude Code or a chat for it to run.
Hobby is free with no credit card: 100 runs per month and one active workflow. Pro includes 10,000 runs per month.
Keep calling Claude Code when #
- Each run needs Claude to read the code and decide what to change
- The result is a pull request, a review or a fix, not a fixed set of API calls
- The task is still changing from one week to the next
A few useful answers #
Is there a Claude Code API?
Anthropic’s docs cover three programmatic routes: claude -p and the Agent SDK run Claude Code from your code, and a cloud routine’s API trigger starts one over HTTP. To call Claude models directly, the docs point to the Client SDK for the Claude API.
Does claude -p need an API key?
In bare mode, yes. Bare mode never reads OAuth credentials or the system keychain, so set ANTHROPIC_API_KEY with a key created in the Claude Console, or supply an apiKeyHelper. The docs recommend bare mode for scripted and SDK calls.
Can a webhook start Claude Code?
Yes, through a cloud routine’s API trigger, for claude.ai users. If the job behind the webhook is the same every time, a Whenever workflow can receive the webhook itself and run the steps without starting Claude.
Sources & further reading #
Sources reviewed November 20, 2026.
- Run Claude Code programmatically - Claude Code Docs
- Agent SDK overview - Claude Code Docs
- Automate work with scheduled tasks - Claude Code Docs
Choose your next step #
Your first workflow is one sentence away. #
No credit card required. One workflow is free. AI for building has weekly usage limits.