Execute Headless Claude Code Commands with Local Context | Alpha | PandaiTech

Execute Headless Claude Code Commands with Local Context

Run Claude Code in headless mode using n8n SSH and chain commands to provide local directory context for file-aware AI responses.

Learning Timeline
Key Insights

The Power of the '-p' Flag

The `-p` (print) flag is mandatory for automation. Without it, Claude Code attempts to open an interactive session, which will cause headless workflows (like n8n) to hang or fail.

Local Context vs. RAG

By changing the directory (`cd`) before running the prompt, you effectively give the AI 'memories' of that specific project without needing to set up a vector database or upload files to a web GUI. The AI reads the local files immediately.
Prompts

Context Validation Prompt

Target: Claude Code CLI
Hey, is this video going to be any good?
Step by Step

Executing Context-Aware Headless Claude Commands

  1. Open the configuration panel for your SSH execution node (e.g., within n8n).
  2. Begin the command string with the change directory command `cd` followed by the path to the specific folder containing your relevant project files/scripts.
  3. Input the chain operator `&&` immediately after the directory path to ensure the next command runs only if the directory change is successful.
  4. Append the Claude CLI command `clo`.
  5. Add the `-p` flag after `clo` to enable 'print' mode (headless execution), preventing the tool from opening an interactive interface.
  6. Type your prompt in double quotes (e.g., "Analyze these scripts") immediately after the `-p` flag.
  7. Execute the node/command to run the prompt against the local files located in the specified directory.

More from AI-Powered Coding & App Development

View All