Try free

Last updated 13 October 2026

How to use Claude Code in pipe mode

Execute one-off commands using the print flag

Standard use of Claude Code involves an interactive terminal where you chat with the agent. This is excellent for exploring a codebase or debugging a complex issue manually. However, when you need to integrate the agent into a larger workflow, the interactive interface becomes a hurdle. You can bypass the entire chat UI by using the print flag. When you run claude -p followed by a string in quotes, the agent performs the task and outputs the result directly to your terminal. This allows you to treat the agent like any other command line utility that produces standard output.

Using the print flag is the foundation of pipe mode operations. It tells the agent to process the instruction and then exit immediately after the work is finished. This is particularly useful for tasks that do not require a back and forth conversation. For example, if you want to generate a summary of a specific file or check for obvious syntax errors, you can run the command and receive the text output. Because it avoids the interactive overhead, it is the fastest way to get an answer when you already know exactly what you want the agent to do. This method works well for developers who prefer staying in their primary shell without entering a dedicated sub-shell for every small task.

Pipe standard input directly into the agent prompt

The real power of automation comes from connecting the output of one tool to the input of another. Claude Code supports standard Unix piping through the command line interface. You can use the cat command to read a file and pipe it into the agent. By combining a pipe with the print flag, you allow the agent to receive data from stdin. For instance, you might pipe the output of a git diff command into the agent and ask it to write a commit message based on those changes. The agent receives the piped data as part of its context and processes the prompt accordingly.

When you pipe data into the agent, it treats the incoming stream as part of the prompt context. This is highly effective for processing logs or data files that change frequently. You do not need to manually copy and paste text from your terminal into a chat window. Instead, you can chain commands together. If you have a log file and you only care about lines containing the word error, you can filter that log with grep and then pipe the result to the agent with an instruction to explain the cause of those errors. This creates a streamlined pipeline where the agent acts as an intelligent filter for your development data.

Configure automatic permissions for uninterrupted scripts

Automation scripts often fail if a tool pauses to ask the user for permission. By default, the agent asks for confirmation before it performs sensitive actions like editing files or running bash commands. In an interactive session, you simply press y or n. In a script, this prompt will cause the process to hang because there is no human to provide input. To fix this, you must adjust the permission mode before running your automated tasks. You can change these settings in the .claude/settings.json file located in your project root or your home directory. Setting the permission mode to bypassPermissions allows the agent to execute its tools without waiting for manual approval.

If you do not want to grant global permission, you can specify settings for individual tools. In the settings.json file, you will find a permissions key where you can define the behavior for tools like Write or Bash. You can set specific tools to always allow or always deny. This level of control ensures that your automation stays safe while remaining functional. If your script only needs to read files and search for text, you can leave the Read and Glob tools on their default settings but enable more permissive rules for the specific tools required by your task. This prevents the script from stopping midway through a long process.

Leverage project instructions for consistent automated output

Every time you run the agent in a directory, it looks for a file named CLAUDE.md. This file contains project-specific instructions that guide the behavior of the agent. When you use the agent in pipe mode, it still respects these instructions. This is a critical feature for automation because it allows you to define coding standards or specific technical constraints once and have them applied to every automated run. If your script generates code, the agent will follow the rules in CLAUDE.md to ensure the output matches the rest of your project style.

You can also use project instructions to define how the agent should format its responses for your scripts. If your downstream tools expect JSON or a specific markdown format, you can specify this in the instructions. The agent will then prioritize that format even when called through a simple pipe command. This reduces the need for complex string parsing in your shell scripts. By keeping your automation logic in your scripts and your coding standards in CLAUDE.md, you create a clean separation of concerns. This makes your automated workflows easier to maintain as your project evolves over time.

Maintain state across multiple script calls

Sometimes an automation task requires more than one step, and the agent needs to remember what it did in the previous step. You can achieve this by using the continue flag. When you run claude -c or claude --continue, the agent loads the most recent session in that folder. If your script runs one command to analyze a file and then another command to fix it, the second command can use the continue flag to maintain context. This prevents the agent from starting from a blank slate for every single line in your script, which saves time and ensures consistency across a multi-step process.

Using session state is helpful when your script logic depends on previous outcomes. For example, a script might first ask the agent to identify a bug and then, in a separate call, ask it to write a test case for that bug. By carrying over the session, the agent already knows the details of the bug and the surrounding code context. If you need to jump between different tasks, you can use the resume flag to see a list of earlier sessions. This allows you to pick up exactly where a previous automated run left off. This flexibility is vital when you are building complex CI/CD pipelines or local development hooks.

Monitor background task progress through a visual wall

When you run many automated sessions at once, keeping track of their status in a single terminal window is difficult. You might have several scripts running in different project folders, each performing a separate task. This is where adhdTerminal becomes a useful tool for your workflow. adhdTerminal is a native app for macOS and Windows, made by MINDBALANCER OU in Estonia, not affiliated with Anthropic. It runs many Claude Code sessions side by side, each one a tile on a wall. This allows you to see the progress of multiple pipe mode operations at a glance without switching between terminal tabs.

Each tile on the wall has a color, a number, and a name to help you identify the specific automation task. The tile shows whether the session is currently working, waiting for you, done, or idle. It also displays the last exchanges of the conversation, which is helpful for debugging why an automated script might have produced a specific result. If your project publishes numbers or headlines, the tile can show those as well. This visibility is especially valuable when you are using the print flag for long-running background tasks. You can see exactly which tile is active and which one has finished its job.

Tiles in the app reopen in the same place after a restart and resume the same session automatically. This means your automation environment stays organized even if you have to reboot your computer. There is also a free iPhone app that mirrors the wall over an end to end encrypted link. You can use it to answer a prompt if a script hits a permission block or to send the next task to a waiting tile. The app costs 99 dollars once, and one key runs one computer at a time and can be moved. It offers a 7 day free trial and requires no account, making it easy to test with your existing scripts.

Integrate external tools with the model context protocol

The agent is not limited to the files in your local directory. It can interact with external data sources and services through MCP servers. You can manage these servers using the claude mcp command. This is useful for automation because it allows your scripts to pull in data from databases, web searches, or other API-connected tools. If you are running a script to generate a report, the agent can use an MCP server to fetch the latest data from your production environment or a documentation site. This expands the range of what you can automate beyond simple file editing.

To use this in pipe mode, ensure your .mcp.json configuration is correctly set up in your project root or home directory. When the agent runs with the print flag, it will have access to the configured tools. You can prompt the agent to use a specific tool to gather information before it outputs its final answer. This makes the agent a powerful coordinator for various developer tools. You can chain a database query, a web search, and a code generation task into a single automated pipeline that runs with a single command. The agent handles the tool execution and provides a consolidated result to stdout.

Manage session costs during high volume automation

Running the agent in a loop or as part of a high-frequency script can lead to unexpected costs. It is important to monitor your usage regularly. You can use the /cost and /usage commands inside an interactive session to see the spend for that specific project. For automated tasks, you can periodically run a command with the print flag to check these statistics. This helps you understand how much each script run is costing you in terms of tokens and API usage. Being aware of these numbers allows you to optimize your prompts and reduce unnecessary context being sent to the model.

If a session grows too large, the context window fills up with previous exchanges and file contents. This can increase the cost of every subsequent prompt in that session. To manage this, you can use the /compact command to summarize the conversation and free up space. In an automated script using the continue flag, you might want to call a compacting command after a certain number of steps. This keeps the session efficient and prevents the model from becoming overwhelmed by too much historical data. Keeping your sessions lean ensures that your automation remains fast and cost-effective over the long term.

Questions people ask

How can I stop a piped command if it takes too long?

You can use the Escape key to interrupt the agent while it is working in an interactive terminal. For automated scripts or piped commands, you can use the standard Ctrl+C command in your terminal to terminate the process immediately. This will stop the agent and return control to your shell.

Does the print flag support all the same tools as the interactive mode?

Yes, the claude -p command has access to the same tools such as Bash, Read, Edit, and Write. However, you must ensure your permission settings in .claude/settings.json are configured to allow these tools to run without manual confirmation. If a tool requires a permission that is not granted, the piped command will likely fail or hang.

Can I use custom slash commands in a piped prompt?

Custom slash commands defined in the .claude/commands directory can be used within your prompts. When you run a command with the print flag, you can reference these custom commands to trigger complex predefined behaviors. This is a great way to simplify the prompts you use in your shell scripts by moving the logic into markdown files.

Read next

More on claude code basics·All posts