Chaen.ai Work with me

Guide

claude -p: Run Claude Code From Your Own App

🗓 ⏱ 15 min read

日本語中文

claude -p: Run Claude Code From Your Own App

Key takeaways

  • claude -p is a non-interactive mode that lets your own app call Claude Code
  • To run on your subscription allowance, unset ANTHROPIC_API_KEY
  • If you hand it out, each user logs in officially; productize with an API key

claude -p is Claude Code’s non-interactive mode: it runs one instruction without the chat screen, prints the result, and exits. The official documentation describes running with -p (—print) as “using the Agent SDK from the CLI,” and you can receive the result as JSON or as a stream*1. I have been experimenting with calling Claude Code from apps I build myself, and I think it is a capable feature that surprisingly few people know about.

This article covers how claude -p works, how to use it, the pricing and authentication conditions, and what to weigh before putting it inside a company’s internal tools. The short version: calling the Claude Code you logged into yourself from your own app stays within the official flow. But if you hand the app to other people, each of them has to log in through the official flow themselves, and if you ship it as a product or a service, the rule is to build on an API key. Building a login screen inside your app, or holding someone else’s credentials, is not permitted under the terms*2.

I also shared this on X.

Masahiro Chaen@masahirochaen「claude -p」意外と知られていないけど、めちゃくちゃ優秀。

自分で作ったアプリからこの1行を呼ぶだけで、裏でClaude Codeがそのまま動く。ファイル操作もMCPもSkillsも全部入りで、自分のサブスク枠で使えます。

つまりClaude Codeのデスクトップアプリの画面を、自分好みのUIに丸ごと置き換えられるということ。

Xで投稿している人はまだ少ないけど、私含めて周りはこれでアプリを作っている人が多い。この形式の…
View on X ↗

What claude -p is: running Claude Code with no screen attached

claude -p starts the same Claude Code you normally talk to in the terminal, runs it once per instruction, and exits when the work is done. You can pass the instruction as a command argument or on standard input, so you can pipe in a build error log and have it explain the cause*1.

claude -p: Run Claude Code From Your Own App
Image: from my post on X (@masahirochaen)

What runs underneath is Claude Code itself. Unless you add —bare, it loads hooks, Skills, custom commands, subagents, plugins, MCP servers and CLAUDE.md exactly as it does in interactive use*1. Being able to use the tools already set up in my everyday environment straight from my own app is the main reason I rate this feature.

Table1 claude -p at a glance
ItemDetails
How you call itclaude -p "instruction" (--print is the same thing)*1
What it isThe CLI edition of the Agent SDK; packaged versions exist for Python and TypeScript*1*3
How it behavesRuns one instruction, prints the result and exits. Exit code 0 on success, non-zero on failure*1
Configuration loadedWithout --bare it loads hooks, Skills, MCP servers, CLAUDE.md and the rest, just as in interactive use*1
Output formatstext (default), json, stream-json*1
Continuing a conversation--continue for the previous conversation, --resume with a session ID*1
AuthenticationSubscription login, API key, cloud provider credentials and others. With -p, ANTHROPIC_API_KEY always takes priority when it is set*4

How it differs from interactive mode

The big difference is that it assumes nobody is sitting in front of the screen. A -p run shows neither the prompt asking whether you trust the folder nor the per-MCP-server approval screen*1. That is why, as I come back to below, the folder you run it in is itself a safety decision.

Permissions behave differently too. From v2.1.302 onward, if you specify nothing it starts in manual-approval mode, and any operation that needs approval is denied unless —allowedTools or a hook has allowed it*1. It does not run whatever it likes; it does only what you have permitted.

  • Works as is — Skills and custom commands the user invokes. Include /skill-name in the prompt and it is expanded and run*1
  • Works if you pass a value — /model, /effort and similar with an argument, such as /model sonnet*1
  • Does not work — built-in commands that assume screen interaction, such as /login*1

Why more people are building their own apps on claude -p

How it compares with calling the API directly

There are several ways to put Claude inside your own app. If you call the API directly with the Client SDK, you have to write the loop around tools — reading files, running commands — yourself*3. The Agent SDK hands you the same tools, the same processing loop and the same context management that Claude Code uses*3.

The Agent SDK packages are for Python and TypeScript, but the official documentation points to running the CLI as a subprocess with -p and —output-format json as the way to drive the same loop from any other language*3. A Swift Mac app or a Go internal tool gets Claude Code’s capability as it is from a single command call.

Figure1 Two ways to put Claude inside your own app
  1. 1Build on an API keyKeep an API key on your own server and pay for usage as you go. You either write the tool loop yourself or use the Agent SDK. Products and services for other people are normally built this way
  2. 2Call a logged-in Claude CodeCall the Claude Code the user logged into on their own machine with claude -p. Their own Skills, MCP servers and environment work as they are, and usage counts against their own plan

Why you can replace the desktop app’s screen with your own

When I posted that you can replace the whole screen, it was because almost everything a custom interface needs is already in the output. Add —verbose and —include-partial-messages to —output-format stream-json and the text arrives line by line as JSON events while it is being generated*1. The first system/init event carries the model, the tools, the MCP servers and the plugins that were loaded*1.

Subagent messages carry the parent’s tool-call ID, so you can branch parallel work visually in your own screen*1. If you want operations that need approval to be confirmed in your own interface, you point —permission-prompt-tool at an MCP tool that accepts approvals*5. People around me and I are building personal apps on this combination, and I expect this shape of app to become more common.

I have written about running Claude Code at set times to cut down the time it spends waiting for instructions in the article below.

Related articleClaude Code Scheduled Tasks: End the Bedtime Prompt Loop

How to use claude -p: the options to learn first

To get started, install Claude Code, launch claude once in the terminal and log in through the browser*4. From there, connecting it to your own app in the following order keeps things simple.

Figure2 Connecting claude -p to your own app
  1. 1Log inLaunch claude in the terminal and log in through the official browser flow
  2. 2Try a single runCheck that claude -p 'Explain the structure of this folder' returns a result
  3. 3Take it as JSONUse --output-format json to get the result and the session ID, and read them in your app
  4. 4Set the ceilingsNarrow the permitted operations and the number of iterations with --allowedTools and --max-turns
  5. 5Wire up the screenDisplay output as it arrives with stream-json, and continue the conversation with --resume
Table2 Options you will use most with claude -p
OptionWhat it doesWhen to use it
--output-formatChoose the output format from text, json and stream-json*1Reading the result in an app
--json-schemaReturn a result that follows the JSON Schema you specify, in structured_output*1Receiving lists and tables in a fixed shape
--continue / --resumeContinue the previous conversation, or the conversation with a given session ID*1History in a chat screen
--allowedToolsSpecify the tools that may be used without approval*1Limiting to read-only tools
--permission-modeSet the overall permission mode: auto, dontAsk, acceptEdits and others*1The safe-side setting for unattended runs
--max-turnsLimit the number of processing iterations. Unlimited by default*5A brake on work that does not stop
--max-budget-usdStop once estimated usage reaches the amount you set*5Budget control when using an API key
--append-system-promptAdd instructions to the default system prompt*1Per-app role and tone
--bareSkip the automatic loading of hooks, Skills, MCP and CLAUDE.md for a faster start*1Work that needs reproducibility, such as CI

Designing the output for your own interface

If you are calling from an app, I recommend —output-format json from the start. The body comes back in result and the ID for continuing the conversation in session_id, so passing that to —resume on the next call carries the conversation on*1. The json output also includes total_cost_usd, but the documentation states plainly that this is a local estimate and can differ from what you are actually billed*1.

Decide how failures are handled up front, too. Success exits with code 0 and failure with a non-zero code, while failures during the run — an expired login, for instance — come out as the result on standard output*1. Data passed on standard input is capped at 10MB and anything larger ends in an error, so for large files, write the path into the instruction and let it read them*1.

Set permission and iteration ceilings first

A screen of your own makes operations easier, which also makes it easier to lose track of what you have allowed Claude to do. I suggest starting by permitting only read-oriented tools such as Read, and widening the scope after you have built a way to confirm edits and command execution in the interface.

For work that runs when nobody is around, —permission-prompts none keeps it from stalling on an approval: only the operations that need confirmation are denied, and the run continues*1. This flag is available from v2.1.259 onward*1.

The other one to keep an eye on is —bare. The official documentation recommends —bare for calls from scripts and SDKs, and states that it will become the default for -p in a future release*1. Bare mode does not read subscription login credentials; it authenticates with an API key or similar*1.

claude -p pricing and authentication: when your subscription covers it

Because claude -p is Claude Code itself, the pricing works the same way as Claude Code. If you are logged in with a subscription it runs against that plan’s allowance; if you authenticate with an API key it is billed by usage*4. Claude Code is available on Pro and above*6.

Table3 Claude plans and pricing (as of November 2026)
PlanPrice (USD)Claude CodeAllowance guide
Free$0*6Not included*6—
Pro$20 per month billed monthly (equivalent to $17 per month billed annually)*6IncludedBaseline
Max$100 per month*6Included5× Pro per 5-hour window and per week*6
Ultra$200 per month*6IncludedNo 5-hour cap; 10× Pro per week*6
TeamPer seat, Standard $25 per month billed monthly ($20 billed annually), Premium $125 per month billed monthly ($100 billed annually)*6Included—
EnterprisePer seat, $20 per month billed annually (from 20 seats), plus usage at API rates*6Included—

On the terms side, the allowances described for Pro, Max and Ultra are explicitly premised on normal individual use of Claude Code and the Agent SDK*2. Calling it from a screen of your own is fine in itself, but running heavy workloads continuously would be seen as falling outside that premise.

Where the authentication priority order trips people up

The common stumble is a leftover ANTHROPIC_API_KEY environment variable. In the authentication priority order, subscription login comes seventh and last, and in a -p run ANTHROPIC_API_KEY is always used when it is present*4. You can end up on metered API billing while believing you are running on your subscription allowance.

Opening /status in interactive mode shows which authentication method is in use*4. For environments such as CI where you cannot log in through a browser, there is claude setup-token, which issues a token valid for one year that you set in CLAUDE_CODE_OAUTH_TOKEN*4. Bare mode, however, does not read that token*4.

The lines the terms draw

Pulling together what the official “Legal and compliance” page says about how authentication may be used: OAuth login is there so that subscribers can use Claude Code and Anthropic’s own apps in the normal way*2.

Table4 What an app built on claude -p may and may not do
CaseHow it is treated
Calling the Claude Code you logged into on your own machine from your own appAppears to fall within normal use by a subscriber (my reading, from the stated purpose of OAuth*2)
A user of an app you distributed logging into unmodified Claude Code with their own subscriptionNot prevented*2
Embedding Claude.ai login inside your appNot permitted*2*3
Putting work through your own Free, Pro, Max or Ultra credentials on a user's behalfNot permitted*2
Collecting, storing or relaying Claude.ai credentials or session tokensNot permitted. Login must be completed through Anthropic's own flow*2
Building and offering a product or service with the Agent SDK or similarUse a Claude Console API key or authentication through a cloud provider*2

Anthropic states that it can enforce these restrictions without prior notice*2. If a use case is borderline, the reliable move is not to decide for yourself but to check with the sales contact the official page points you to.

Using claude -p in a company: what to weigh for internal tools

For corporate use of claude -p, I see the starting point as separating “an internal tool where each employee uses the Claude Code on their own seat” from “a product you provide to customers.” The first can run on each person’s own login; the second belongs on the API key side*2.

Where it fits well

  • Routine document work — pass minutes or logs on standard input and get a summary or a reformatted version back. —json-schema keeps the shape of the output consistent*1
  • Review support in development — hand it a diff and have it point out typos and security problems. The official documentation includes an example of reviewing a PR diff*1
  • A dedicated screen for one department — load internal MCP servers and Skills, and show non-engineers only an input box and the result
  • Overnight routine jobs — narrow permissions and iteration counts, set it so it does not stall waiting for approval, and check the results in the morning*1

What to watch when you roll it out

Contracts first. Free, Pro, Max and Ultra fall under the consumer terms, while Team, Enterprise and the API fall under the commercial terms*2. For a company-wide rollout, have each employee log in on Team or Enterprise; the forceLoginOrgUUID managed setting lets you reject logins from outside your own organization*4.

Security next. Without —bare, -p will execute hooks from a project’s .claude/settings.json and connect to the servers in .mcp.json even in a folder you have not trusted*1. Avoid designs where an internal tool runs in a folder holding repositories or files received from outside.

Know where login credentials live and when they expire. They are stored in the keychain on macOS and in ~/.claude/.credentials.json on Linux*4. Logins have an expiry that is not extended by use, so making claude auth status —text part of your routine to check when it ends prevents unattended runs from stopping unexpectedly*4.

I have written about the risk that configuration files run simply because you opened a folder, and what to do about it, in the article below.

Related articleClaude Code Security: Don’t Launch Agents in Downloads

Decision points

Table5 Choosing by type of internal use
UseRecommended authenticationWhat to weigh
A personal tool that speeds up your own workYour own Team or Enterprise seatGo ahead. Put ceilings on the tools and the number of iterations you allow
An internal tool shared across a departmentEach employee's own loginHave everyone log in through the official flow, and keep the app away from authentication*2
A process running continuously on a serverA Claude Console API key or a cloud providerDo not depend on a person's login; cap the budget with --max-budget-usd and similar*5
A product or service for customersAPI key (Agent SDK)Do not use subscription logins*2*3

How this lands in Japan, and what it means for overseas companies

What I see on the ground at Japanese companies

I have supported AI adoption at more than 600 companies and over 50,000 people. What trips most of them up is not the technology but the paperwork: who issues the API key, and how the budget for metered billing gets secured. Calling a per-seat Claude Code contract from an internal tool can be an option that lightens that paperwork.

On the other hand, when people move ahead on “it’s useful, let’s hand it out” without knowing where the terms draw the line, they tend to end up building something that is not permitted — a login screen inside the app, for example. When I am asked about an internal rollout, I think the authentication and the terms deserve as much time as the use case itself.

I have written about when to use cloud-based versus local AI agents in the article below.

Related articlePersonal AI Agents vs Claude Code: How I Split the Work

What this means for AI and tech companies outside Japan

Companies that want to embed Claude Code in their own products have to agree to the commercial terms, and also meet the conditions of not modifying the binary and not paying for users’ usage on their behalf or reselling it*2. It is also stated explicitly that you cannot use “Claude Code” in a product name or logo, and cannot call an agent you built with the Agent SDK “Claude Code”*2*3.

My read is that Japanese companies tend to go through the terms and the data handling in detail before adopting anything. If you are bringing a product to Japan, showing from the start — in Japanese materials — which authentication method it runs on and whose contract the usage is billed to should help it move through the information systems department, the team that signs off on new tools at most Japanese companies.

Summary

  • How it works — claude -p is the CLI edition of the Agent SDK that runs Claude Code non-interactively, and it loads Skills and MCP just as interactive use does
  • How to use it — take the output as json or stream-json, continue conversations with —resume, and cap the tools and iterations you allow
  • Authentication — to run on your subscription allowance, unset ANTHROPIC_API_KEY, and watch the move to make —bare the default
  • Terms — if you hand the app to other people, each of them must log in through the official flow; if you offer it as a product, build on an API key
  • Companies — run internal tools on Team or Enterprise seats, and keep anything external on an API key

What I recommend is picking one routine task of your own and building a small tool that calls claude -p with —output-format json. Once you experience your usual Skills and MCP servers working as they are, your thinking shifts: instead of working the way Claude Code’s screen expects, you build a screen that fits your work. Before you share it internally, putting the authentication method and the permitted operations into a single table makes it safe to widen.

Sources

  1. *1Run Claude Code programmatically (Claude Code Docs)
  2. *2Legal and compliance (Claude Code Docs)
  3. *3Agent SDK overview (Claude Code Docs)
  4. *4Authentication (Claude Code Docs)
  5. *5CLI reference (Claude Code Docs)
  6. *6Plans & Pricing (Claude)

FAQ

What is claude -p?

It is Claude Code's non-interactive mode: it runs without the chat screen, prints the result and exits, and you invoke it with the -p (--print) flag. The official documentation positions it as using the Agent SDK from the CLI, and you can receive the result as text, json or stream-json.

Can I use claude -p on my subscription allowance?

If you are logged into Claude Code with a Pro, Max, Ultra, Team or Enterprise account, it runs against that allowance. Note that in a -p run the ANTHROPIC_API_KEY environment variable always takes priority when it is set, and with --bare it does not read login credentials and authenticates with an API key.

Can I distribute an app that uses claude -p to other people?

Having each user log into unmodified Claude Code with their own subscription through the official flow is not prevented by the terms. Embedding Claude.ai login inside your app, or collecting or relaying credentials, is not permitted. If you offer it as a product or a service, authentication with an API key is what you are directed to use.

What is the difference between claude -p and the Agent SDK?

claude -p is the CLI edition of the Agent SDK, and packaged versions of the Agent SDK exist for Python and TypeScript. The packaged versions let you work with tool-approval callbacks and message objects. From other languages, the official guidance is to call the CLI with -p and --output-format json.

Based on a post on XOriginal post ↗

Share this article

XFacebookLINEはてブ

About the author

Masahiro Chaen
Masahiro Chaen Founder & CEO, DigiRise Inc.

CEO of DigiRise Inc. Has supported 600+ companies and 50,000+ people with AI training, consulting and development. GMO AI & Crypto advisor, Google Gemini AI advisor. 200K+ followers on X.