← back to blog

How to Automate Workflow with Claude Code: A Step-by-Step Guide

· Jordi Hako

Learn to automate workflow with Claude Code: setup, one-shot mode, task chaining, and real pipeline patterns — from generation to live verification.

What You'll Learn

If you're ready to automate workflow with Claude Code, this guide covers everything from initial setup to running your first real end-to-end automation. You'll learn how Claude Code differs from a standard chat interface, why it suits multi-step engineering tasks, and how to structure prompts so they reliably produce the output you need.

Claude Code terminal showing an automated workflow running in a macOS terminal window

What Makes Claude Code Different

Most AI assistants respond to a prompt and stop. Claude Code operates as an agent: it reads files, runs shell commands, edits code, inspects results, and loops until the task is done or it hits a blocker it cannot resolve on its own.

That loop — read → act → verify → repeat — is what makes real automation possible. You are not manually copy-pasting output into your editor; Claude Code is doing the work directly inside your file system.

The CLAUDE.md File

Before writing a single automation script, create a CLAUDE.md file at the root of your project. This file is loaded automatically at the start of every Claude Code session. Think of it as a standing brief: conventions, architectural decisions, and constraints that would otherwise get re-explained every session.

A minimal CLAUDE.md should cover:

  • What the project does and why it exists
  • Stack and tech choices (language, frameworks, test runner)
  • File naming and code-style conventions
  • What Claude Code must NOT do (e.g., never push to main without confirmation, never hardcode credentials)

Getting this right pays dividends immediately — you will spend far less time correcting Claude Code's assumptions and far more time watching it actually work.

Setting Up Your First Automation

1. Install Claude Code

Claude Code runs as a CLI tool. Install it globally:

npm install -g @anthropic-ai/claude-code

Authenticate with your Anthropic account when prompted. Claude Code will request file system permissions on first launch — grant them, or it cannot do anything useful.

2. Define a Clear, Bounded Task

The most common failure mode for first-time users is an underspecified brief. "Refactor my whole codebase" will produce worse results than "Extract the three database query functions in db.py into a new db/queries.py module, update all imports, and run the existing test suite to confirm nothing breaks."

A well-scoped automation task has:

  • A clear start state ("the file currently looks like this")
  • A clear end state ("the file should look like this after")
  • A verifiable success condition ("all tests pass", "the build completes without errors")

3. Run Claude Code in One-Shot Mode

For scripted automation — CI pipelines, scheduled jobs, publish scripts — use the -p flag (non-interactive/print mode):

claude -p "Add an updated_at column to the clients table in schema.sql and update the corresponding SQLAlchemy model in models.py. Run pytest afterward to confirm existing tests still pass."

Claude Code works through the task, prints its reasoning and tool calls to stdout, and exits when done. You can capture the exit code and log the output exactly like any other shell command.

Claude Code one-shot mode output in a terminal showing tool use steps and a final pass confirmation

4. Chain Tasks Into a Pipeline

Once individual one-shot calls work reliably, chain them. A minimal Python wrapper:

import subprocess, sys

def run_claude(prompt: str) -> int:
    result = subprocess.run(
        ["claude", "-p", prompt],
        capture_output=True,
        text=True,
    )
    print(result.stdout)
    if result.returncode != 0:
        print(result.stderr, file=sys.stderr)
    return result.returncode

Call this in sequence, check return codes, and abort early on failure. That is the core pattern for any Claude Code-driven pipeline.

Common Patterns and Pitfalls

Pattern: Gate Before You Ship

Always run a verification step after generation and before any publish or commit action. Whether that is pytest, eslint, a custom QA script, or a Playwright browser check, the gate separates a useful automation from one that confidently ships broken output.

Pattern: Keep Secrets Out of Prompts

Never paste credentials, API keys, or personal data into a Claude Code prompt. Pass them as environment variables and reference them by name. Claude Code reads environment variables directly from the shell — there is no reason to expose actual values in the prompt text.

Pitfall: Nested Sessions

Claude Code guards against launching itself inside an already-running Claude Code session (via the CLAUDECODE environment variable). If your automation calls Claude Code as a subprocess, it must run from a plain terminal or CI runner — not from inside an active Claude Code session. Attempting the latter raises a hard error by design.

Pitfall: Skipping Idempotency

If your automation generates or publishes content, check your local state store before the main work runs. Abort cleanly if the work was already completed. Running the same generation task twice is not just wasteful — it can produce duplicate live content on a real site.

A Real End-to-End Example

Here is what a minimal publish pipeline looks like in practice:

  1. Generate — call Claude Code with a structured content brief, capture the JSON output.
  2. QA gate — run quality and structural checks against the generated content. Fail hard if anything critical is wrong.
  3. Stage — push to a draft or preview state (WordPress draft, Shopify unpublished product, a Git pull request).
  4. Verify staged — run a Playwright browser check against the preview URL to confirm the page actually renders, images load, and meta tags are present.
  5. Promote — flip to live only after staged verification passes.

Each step is a subprocess call or a function call with a checked return value. The entire pipeline is deterministic, auditable, and reversible — exactly what you want before any automation touches a real live site.