Discovered: How to detect if Claude Code is running your terminal commands

r/ClaudeCoder/ClaudeAI

I discovered a way to detect whether your terminal commands are being run by Claude Code or by a human.

Discovery 1: Claude Environment Variable

Claude Code sets CLAUDECODE=1 when it runs commands:

if (process.env.CLAUDECODE) {
  console.error("Claude detected - this command is human-only")
  process.exit(1)
}

Discovery 2: TTY Detection

Terminal has TTY, Claude doesn't:

// Terminal → process.stdout.isTTY = true
// Claude → process.stdout.isTTY = false

if (!process.stdout.isTTY) {
  console.error("No TTY - running from subprocess")
}

Now you can make certain commands human-only. Useful when you have sensitive operations or different interfaces for humans vs AI.

Last updated

Was this helpful?