Intermediate

This tutorial provides 30 intermediate examples covering Claude Code CLI automation in production environments. Learn GitHub Actions integration (Examples 31-35), advanced CI/CD patterns (Examples 36-40), multi-language subprocess integration (Examples 41-45), and production-ready development workflows (Examples 46-60).

GitHub Actions Integration (Examples 31-35)

Example 31: Basic GitHub Actions Workflow with Claude

Run Claude Code in GitHub Actions for automated code analysis, generation, or validation on push/PR events.

# .github/workflows/claude-analysis.yml
name: Claude Code Analysis
on: [push, pull_request]

jobs:
  analyze:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
        with:
          api-key: ${{ secrets.CLAUDE_API_KEY }}
      - name: Analyze codebase
        run: claude -p "analyze code for security issues" --output-format json > analysis.json
      - name: Check for critical issues
        run: |
          CRITICAL=$(jq '.issues[] | select(.severity=="critical") | length' analysis.json)
          if [ "$CRITICAL" -gt 0 ]; then exit 1; fi

Key Takeaway: GitHub Actions runs Claude CLI like any bash command. Use secrets for API keys, output to files, parse with jq.

Why It Matters: CI/CD integration catches issues before code review. Automated Claude analysis scales team quality - every push gets analyzed consistently. Exit codes (exit 1) fail builds on critical issues, preventing bad code from merging.

Example 32: Matrix Strategy with Claude - Testing Across Node Versions

Use GitHub Actions matrix strategy to run Claude analysis across multiple Node.js versions simultaneously.

# .github/workflows/cross-version-test.yml
name: Cross-Version Claude Tests
on: [pull_request]

jobs:
  test-matrix:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18, 20, 22]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
      - uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
      - name: Generate tests for Node ${{ matrix.node-version }}
        run: |
          claude -p "generate compatibility tests for Node ${{ matrix.node-version }}" > tests/node${{ matrix.node-version }}.spec.ts
      - name: Run generated tests
        run: npm test

Key Takeaway: Matrix builds parallelize Claude operations. Each matrix cell runs independent Claude command with different context.

Why It Matters: Parallel execution speeds up CI/CD - testing 3 Node versions simultaneously vs sequentially saves significant time. Claude customizes output per matrix cell (Node 18 vs 20 vs 22 compatibility tests). This enables comprehensive testing without manual effort. Use matrix builds to test across platforms, languages, frameworks simultaneously.

Example 33: Artifact Generation in CI/CD

Generate documentation, reports, or code as GitHub Actions artifacts using Claude, then download or use in subsequent jobs.

# .github/workflows/artifact-gen.yml
name: Generate Documentation Artifacts
on: [push]

jobs:
  generate-docs:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
      - name: Generate API docs
        run: |
          claude -p "generate comprehensive API documentation from src/api/**/*.ts" --output-format json > api-docs.json
          claude -p "convert JSON docs to markdown" < api-docs.json > API.md
      - name: Upload artifacts
        uses: actions/upload-artifact@v3
        with:
          name: documentation
          path: |
            api-docs.json
            API.md

  deploy-docs:
    needs: generate-docs
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v3
        with:
          name: documentation
      - name: Deploy to docs site
        run: ./deploy-docs.sh API.md

Key Takeaway: Generate artifacts with Claude, upload with actions/upload-artifact, download in dependent jobs.

Why It Matters: Artifact workflow separates generation from deployment. Documentation generated once, used in multiple downstream jobs (deploy to site, create GitHub release, send to stakeholders). This avoids regenerating expensive Claude analysis.

Example 34: Secret Management for Claude API Keys

Secure Claude API keys using GitHub Secrets, environment variables, and permission scoping.

# .github/workflows/secure-claude.yml
name: Secure Claude Usage
on: [pull_request]

jobs:
  secure-analysis:
    runs-on: ubuntu-latest
    environment: production # Requires approval for production secrets
    steps:
      - uses: actions/checkout@v3
      - uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
        with:
          api-key: ${{ secrets.CLAUDE_API_KEY }} # Never hardcode!
      - name: Run Claude with scoped permissions
        env:
          CLAUDE_MAX_BUDGET: "5.00" # Limit spending per run
        run: |
          claude -p "analyze security vulnerabilities" \
            --max-budget-usd $CLAUDE_MAX_BUDGET \
            --output-format json > vulnerabilities.json
      - name: Never log secrets
        run: |
          # WRONG: echo $CLAUDE_API_KEY
          # RIGHT: Use masked variables
          echo "Analysis complete"

Key Takeaway: Store API keys in GitHub Secrets, never in code. Use environment protection for production access. Set budget limits.

Why It Matters: Exposed API keys enable unauthorized usage, costing money and risking data leaks. GitHub Secrets encrypt keys and mask them in logs. Environment protection requires manual approval for production runs. Budget limits prevent runaway costs if workflow loops infinitely. Use secret scanning to detect accidentally committed keys.

Example 35: Conditional Workflow Execution

Run Claude only when specific files change or conditions are met. Saves CI/CD time and API costs.

# .github/workflows/conditional.yml
name: Conditional Claude Analysis
on:
  pull_request:
    paths:
      - "src/**/*.ts" # Only run on TypeScript changes
      - "src/**/*.js"
      - "!src/**/*.spec.ts" # Skip test files

jobs:
  analyze-code:
    runs-on: ubuntu-latest
    if: github.event.pull_request.draft == false # Skip drafts
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0 # Full history for comparison
      - uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}

      - name: Get changed files
        id: changed-files
        run: |
          CHANGED=$(git diff --name-only origin/${{ github.base_ref }}...HEAD | grep -E '\.(ts|js)$' || true)
          echo "files=$CHANGED" >> $GITHUB_OUTPUT

      - name: Analyze only changed files
        if: steps.changed-files.outputs.files != ''
        run: |
          for FILE in ${{ steps.changed-files.outputs.files }}; do
            echo "Analyzing $FILE"
            claude -p "review $FILE for issues" --output-format json > $FILE.analysis.json
          done

Key Takeaway: Use paths, if conditions, and git diff to run Claude only when relevant. Analyze only changed files, not entire codebase.

Why It Matters: Unconditional analysis wastes CI/CD time and API costs. Path filters skip irrelevant changes (README edits don’t need code analysis). Draft PR skip prevents spending on work-in-progress. Analyzing only changed files (not entire codebase) significantly reduces analysis time for typical PRs. Use conditional execution to balance thorough analysis with fast feedback.

Advanced CI/CD Patterns (Examples 36-40)

Example 36: Multi-Stage Pipeline (Lint → Test → Claude Analysis → Deploy)

Orchestrate multi-stage pipeline where each stage depends on previous success. Claude analysis runs after tests pass.

# .github/workflows/pipeline.yml
name: Multi-Stage Pipeline
on: [push]

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: npm run lint

  test:
    needs: lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: npm test

  claude-analysis:
    needs: test # Only runs if tests pass
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
      - name: Deep code analysis
        run: |
          claude -p "analyze architecture and suggest improvements" \
            --output-format json > architecture-review.json
      - name: Generate improvement report
        run: |
          claude -c -p "create prioritized improvement roadmap from analysis" \
            > improvement-roadmap.md
      - uses: actions/upload-artifact@v3
        with:
          name: analysis-reports
          path: |
            architecture-review.json
            improvement-roadmap.md

  deploy:
    needs: claude-analysis
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    steps:
      - uses: actions/checkout@v3
      - run: ./deploy.sh

Key Takeaway: Chain jobs with needs: dependency. Each stage gates the next - Claude runs only if tests pass, deploy only if Claude approves.

Why It Matters: Multi-stage pipelines provide progressive quality gates. Fast checks (lint) fail quickly. Expensive operations (Claude analysis) run only on validated code. This optimizes CI/CD time - failed lints don’t waste Claude API calls. Deploy gating on Claude approval prevents deploying code with detected issues.

Example 37: PR Comment Generation with Claude

Post Claude analysis results as PR comments, providing inline feedback to developers.

# .github/workflows/pr-comment.yml
name: Claude PR Review
on: [pull_request]

jobs:
  review-and-comment:
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write # Required to post comments
    steps:
      - uses: actions/checkout@v3
      - uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}

      - name: Analyze PR changes
        run: |
          claude -p "review this PR for code quality, security, and best practices" \
            --output-format json > review.json

      - name: Format review as markdown
        run: |
          claude -p "convert review JSON to markdown with severity icons" \
            < review.json > review-comment.md

      - name: Post comment
        uses: actions/github-script@v6
        with:
          script: |
            const fs = require('fs');
            const body = fs.readFileSync('review-comment.md', 'utf8');
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: `## 🤖 Claude Code Review\n\n${body}`
            });

Key Takeaway: Use actions/github-script to post Claude analysis as PR comments. Requires pull-requests: write permission.

Why It Matters: PR comments provide contextual feedback where developers work. Instead of checking separate logs, developers see Claude reviews inline. This increases engagement with issues when feedback appears in PR vs separate reports. Use this for automated code review, security scanning, style enforcement.

Example 38: Release Notes Automation

Generate release notes from git commits using Claude, then create GitHub releases automatically.

# .github/workflows/release.yml
name: Automated Release Notes
on:
  push:
    tags:
      - "v*" # Trigger on version tags

jobs:
  create-release:
    runs-on: ubuntu-latest
    permissions:
      contents: write # Required to create releases
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0 # Full history for changelog

      - uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}

      - name: Get commits since last tag
        id: commits
        run: |
          PREVIOUS_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || echo "")
          if [ -z "$PREVIOUS_TAG" ]; then
            COMMITS=$(git log --pretty=format:"%s (%h)" HEAD)
          else
            COMMITS=$(git log --pretty=format:"%s (%h)" $PREVIOUS_TAG..HEAD)
          fi
          echo "commits<<EOF" >> $GITHUB_OUTPUT
          echo "$COMMITS" >> $GITHUB_OUTPUT
          echo "EOF" >> $GITHUB_OUTPUT

      - name: Generate release notes
        run: |
          echo "${{ steps.commits.outputs.commits }}" | \
          claude -p "create professional release notes from these commits, categorizing by Features, Bug Fixes, and Improvements" \
            > release-notes.md

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v1
        with:
          body_path: release-notes.md
          generate_release_notes: false # Use Claude's notes instead

Key Takeaway: Extract commits between tags, pass to Claude for formatting, create GitHub release with generated notes.

Why It Matters: Manual release notes are time-consuming and inconsistent. Claude categorizes commits (features vs fixes), writes user-friendly descriptions, and formats professionally. Automated notes never miss commits or misrepresent changes.

Example 39: Deployment Approval Gates with Claude Risk Assessment

Use Claude to assess deployment risk, requiring manual approval for high-risk changes.

# .github/workflows/deploy-gate.yml
name: Deployment with Risk Assessment
on:
  push:
    branches: [main]

jobs:
  assess-risk:
    runs-on: ubuntu-latest
    outputs:
      risk-level: ${{ steps.risk.outputs.level }}
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 50 # Last 50 commits for context

      - uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}

      - name: Assess deployment risk
        id: risk
        run: |
          git diff HEAD~1 HEAD > changes.diff
          RISK=$(claude -p "assess deployment risk (low/medium/high) based on these changes" \
            --output-format json < changes.diff | jq -r '.risk_level')
          echo "level=$RISK" >> $GITHUB_OUTPUT
          echo "Deployment risk: $RISK"

  deploy-staging:
    needs: assess-risk
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: ./deploy-staging.sh

  deploy-production:
    needs: [assess-risk, deploy-staging]
    runs-on: ubuntu-latest
    environment:
      name: production
      # High-risk deployments require manual approval
    steps:
      - uses: actions/checkout@v3
      - name: Check risk level
        run: |
          if [ "${{ needs.assess-risk.outputs.risk-level }}" = "high" ]; then
            echo "⚠️ High-risk deployment detected. Manual approval required."
          fi
      - run: ./deploy-production.sh

Key Takeaway: Claude assesses deployment risk, outputs risk level, GitHub environment protection requires approval for high-risk changes.

Why It Matters: Automated risk assessment prevents dangerous deployments without slowing down safe ones. Low-risk changes (docs, typos) deploy immediately. High-risk changes (database migrations, API changes) require human approval. This balances speed with safety.

Example 40: Automated Rollback with Claude Failure Detection

Monitor deployment health with Claude analyzing logs, trigger automatic rollback on detected issues.

# .github/workflows/auto-rollback.yml
name: Deploy with Auto-Rollback
on:
  workflow_dispatch:
    inputs:
      version:
        description: "Version to deploy"
        required: true

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          ref: ${{ github.event.inputs.version }}
      - run: ./deploy.sh
      - name: Save deployment info
        run: |
          echo "VERSION=${{ github.event.inputs.version }}" >> deployment-info.txt
          echo "TIMESTAMP=$(date -u +%s)" >> deployment-info.txt
      - uses: actions/upload-artifact@v3
        with:
          name: deployment-info
          path: deployment-info.txt

  monitor-health:
    needs: deploy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}

      - name: Wait for deployment to stabilize
        run: sleep 120 # 2 minutes

      - name: Fetch and analyze logs
        id: health-check
        run: |
          # Fetch last 100 log lines from production
          ./fetch-prod-logs.sh > production.log

          HEALTH=$(claude -p "analyze these production logs for errors, anomalies, or performance issues. Return JSON with {healthy: boolean, issues: string[]}" \
            --output-format json < production.log)

          HEALTHY=$(echo "$HEALTH" | jq -r '.healthy')
          echo "healthy=$HEALTHY" >> $GITHUB_OUTPUT

          if [ "$HEALTHY" = "false" ]; then
            echo "🚨 Health check failed!"
            echo "$HEALTH" | jq '.issues[]'
          fi

      - name: Trigger rollback if unhealthy
        if: steps.health-check.outputs.healthy == 'false'
        run: |
          echo "Initiating automatic rollback..."
          ./rollback.sh

Key Takeaway: Deploy → monitor logs → Claude analyzes health → auto-rollback on detected issues. Reduces incident response time.

Why It Matters: Manual health monitoring delays incident detection. Claude analyzes logs immediately post-deployment, detecting errors, performance degradation, or anomalies. Automatic rollback restores service quickly vs manual detection and response.

Multi-Language Subprocess Integration (Examples 41-45)

Example 41: Python Subprocess Calling Claude

Call Claude Code from Python scripts using subprocess module for automated code analysis or generation.

# analyze.py
import subprocess
import json
import sys

def analyze_file(filepath):
    """Analyze Python file using Claude Code."""
    result = subprocess.run(
        ['claude', '-p', f'analyze {filepath} for code quality issues',
         '--output-format', 'json'],
        capture_output=True,
        text=True,
        timeout=60
    )

    if result.returncode != 0:
        print(f"Error: {result.stderr}", file=sys.stderr)
        sys.exit(1)

    analysis = json.loads(result.stdout)
    return analysis

def main():
    filepath = sys.argv[1]
    analysis = analyze_file(filepath)

    print(f"Issues found: {len(analysis['issues'])}")
    for issue in analysis['issues']:
        print(f"  - [{issue['severity']}] {issue['message']}")

if __name__ == '__main__':
    main()
python analyze.py src/main.py   # => Runs Claude analysis
                                    # => Parses JSON output
                                    # => Prints formatted results

Key Takeaway: Use subprocess.run() with capture_output=True, parse stdout as JSON, handle timeouts and errors.

Why It Matters: Python integration enables Claude in data pipelines, automated scripts, CI/CD Python tools. Common pattern: data analysis scripts use Claude to generate insights, validate data quality, or explain anomalies. Integrate Claude into pytest for dynamic test generation based on data characteristics.

Example 42: Node.js Child Process with Claude

Call Claude from Node.js/JavaScript using child_process module for build scripts or automation tools.

// generate-docs.js
const { exec } = require("child_process");
const { promisify } = require("util");
const execAsync = promisify(exec);

async function generateDocs(sourceDir) {
  try {
    const { stdout, stderr } = await execAsync(
      `claude -p "generate API documentation from ${sourceDir}" --output-format json`,
      { timeout: 60000 }, // 60 second timeout
    );

    if (stderr) {
      console.error("Claude stderr:", stderr);
    }

    const docs = JSON.parse(stdout);
    return docs;
  } catch (error) {
    console.error("Failed to generate docs:", error.message);
    process.exit(1);
  }
}

async function main() {
  const docs = await generateDocs("src/api/");

  console.log(`Generated docs for ${docs.endpoints.length} endpoints`);

  // Write to file
  const fs = require("fs").promises;
  await fs.writeFile("docs/api.json", JSON.stringify(docs, null, 2));
}

main();
node generate-docs.js           # => Calls Claude via exec
                                    # => Parses JSON output
                                    # => Writes to docs/api.json

Key Takeaway: Use child_process.exec() with promisify for async/await, set timeouts, handle stdout/stderr separately.

Why It Matters: Node.js integration enables Claude in build scripts (Webpack, Vite, Rollup), npm scripts, serverless functions. Example: build process uses Claude to optimize bundle size by identifying unused code. Use Claude in Next.js build to generate SEO metadata or pre-render content.

Example 43: Java ProcessBuilder with Claude

Call Claude from Java applications using ProcessBuilder for enterprise integration or build tools.

// ClaudeAnalyzer.java
import java.io.*;
import java.util.concurrent.TimeUnit;
import com.google.gson.Gson;

public class ClaudeAnalyzer {
    private static final int TIMEOUT_SECONDS = 60;

    public static class Analysis {
        public String summary;
        public Issue[] issues;
    }

    public static class Issue {
        public String severity;
        public String message;
        public String file;
        public int line;
    }

    public static Analysis analyzeCodebase(String directory) throws IOException, InterruptedException {
        ProcessBuilder pb = new ProcessBuilder(
            "claude", "-p",
            "analyze code in " + directory + " for security issues",
            "--output-format", "json"
        );

        pb.redirectErrorStream(true);
        Process process = pb.start();

        // Read output
        BufferedReader reader = new BufferedReader(
            new InputStreamReader(process.getInputStream())
        );
        StringBuilder output = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            output.append(line);
        }

        // Wait for completion
        boolean finished = process.waitFor(TIMEOUT_SECONDS, TimeUnit.SECONDS);
        if (!finished) {
            process.destroyForcibly();
            throw new RuntimeException("Claude process timed out");
        }

        if (process.exitValue() != 0) {
            throw new RuntimeException("Claude failed with exit code " + process.exitValue());
        }

        // Parse JSON
        Gson gson = new Gson();
        return gson.fromJson(output.toString(), Analysis.class);
    }

    public static void main(String[] args) throws Exception {
        Analysis analysis = analyzeCodebase("src/main/java/");

        System.out.println("Summary: " + analysis.summary);
        System.out.println("Issues found: " + analysis.issues.length);

        for (Issue issue : analysis.issues) {
            System.out.printf("[%s] %s:%d - %s%n",
                issue.severity, issue.file, issue.line, issue.message);
        }
    }
}
javac ClaudeAnalyzer.java       # => Compile Java class
java ClaudeAnalyzer             # => Run Claude analysis
                                    # => Prints formatted results

Key Takeaway: Use ProcessBuilder with timeout, read stdout line-by-line, handle exit codes, parse JSON with Gson/Jackson.

Why It Matters: Java integration enables Claude in Maven/Gradle builds, Spring Boot applications, enterprise tools. Example: Maven plugin uses Claude to generate boilerplate code during build. Integrate Claude into IntelliJ IDEA plugins or Jenkins pipelines for automated code review.

Example 44: Go exec.Command with Claude

Call Claude from Go programs using os/exec package for CLI tools or backend services.

// analyze.go
package main

import (
 "bytes"
 "encoding/json"
 "fmt"
 "os"
 "os/exec"
 "time"
)

type Analysis struct {
 Summary string  `json:"summary"`
 Issues  []Issue `json:"issues"`
}

type Issue struct {
 Severity string `json:"severity"`
 Message  string `json:"message"`
 File     string `json:"file"`
 Line     int    `json:"line"`
}

func analyzeCode(directory string) (*Analysis, error) {
 ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
 defer cancel()

 cmd := exec.CommandContext(ctx, "claude", "-p",
  fmt.Sprintf("analyze code in %s for issues", directory),
  "--output-format", "json")

 var stdout, stderr bytes.Buffer
 cmd.Stdout = &stdout
 cmd.Stderr = &stderr

 err := cmd.Run()
 if err != nil {
  return nil, fmt.Errorf("claude failed: %w, stderr: %s", err, stderr.String())
 }

 var analysis Analysis
 if err := json.Unmarshal(stdout.Bytes(), &analysis); err != nil {
  return nil, fmt.Errorf("failed to parse JSON: %w", err)
 }

 return &analysis, nil
}

func main() {
 analysis, err := analyzeCode("./pkg")
 if err != nil {
  fmt.Fprintf(os.Stderr, "Error: %v\n", err)
  os.Exit(1)
 }

 fmt.Printf("Summary: %s\n", analysis.Summary)
 fmt.Printf("Issues found: %d\n", len(analysis.Issues))

 for _, issue := range analysis.Issues {
  fmt.Printf("[%s] %s:%d - %s\n",
   issue.Severity, issue.File, issue.Line, issue.Message)
 }
}
go run analyze.go               # => Runs Claude analysis from Go
                                    # => Parses JSON output
                                    # => Prints formatted results

Key Takeaway: Use exec.CommandContext() with timeout, capture stdout/stderr with bytes.Buffer, unmarshal JSON output.

Why It Matters: Go integration enables Claude in CLI tools (cobra/urfave/cli), backend services, Kubernetes operators. Example: CLI tool uses Claude to explain Kubernetes YAML or generate Helm charts. Integrate Claude into CI/CD tools written in Go (Drone, Argo) for custom quality gates.

Example 45: Advanced Piping - Multi-Stage Claude Processing

Chain multiple Claude invocations through pipes, where each stage refines previous output for complex transformations.

#!/bin/bash
# Multi-stage code analysis pipeline

# Stage 1: Extract function signatures
cat src/**/*.ts | \
  claude -p "extract all function signatures" --output-format json > signatures.json

# Stage 2: Identify complex functions
cat signatures.json | \
  jq '.functions[] | select(.complexity > 10)' | \
  claude -p "analyze these complex functions and suggest refactoring" --output-format json > complex-analysis.json

# Stage 3: Generate refactoring plan
cat complex-analysis.json | \
  claude -p "create prioritized refactoring roadmap with effort estimates" > refactoring-plan.md

# Stage 4: Validate plan feasibility
cat refactoring-plan.md | \
  claude -p "validate this plan for risks and dependencies" --output-format json > validation.json

# Output final results
echo "Refactoring Plan:"
cat refactoring-plan.md

echo -e "\nValidation Results:"
jq '.risks[]' validation.json
./multi-stage-analysis.sh       # => Stage 1: Extract signatures
                                    # => Stage 2: Analyze complexity
                                    # => Stage 3: Generate plan
                                    # => Stage 4: Validate plan
                                    # => Outputs final roadmap

Key Takeaway: Pipe Claude output to jq for filtering, pipe filtered results to next Claude command. Each stage refines previous output.

Why It Matters: Multi-stage piping enables complex analysis impossible in single pass. Example: extract → filter → analyze → plan → validate creates comprehensive refactoring roadmaps. Each stage specializes - first extracts raw data, second filters relevant subset, third generates recommendations, fourth validates feasibility. Build custom analysis pipelines combining Claude (semantic understanding) with jq (data manipulation).

Example 46: Async/Await Migration from Callbacks

Convert callback-based async code to async/await. Claude identifies callback patterns and refactors to modern promise-based syntax.

Before - callback approach:

function fetchUserData(userId, callback) {
  db.query("SELECT * FROM users WHERE id = ?", [userId], (err, results) => {
    // => Callback receives error and results
    if (err) {
      // => Error passed to callback
      callback(err, null);
      return;
    }
    // => Success: pass null error, results data
    callback(null, results[0]);
  });
}

Text explanation: Callback pattern passes error-first callback. Nested callbacks lead to “callback hell” with deep indentation.

After - async/await approach:

async function fetchUserData(userId) {
  // => Returns Promise, can use await
  try {
    const results = await db.query("SELECT * FROM users WHERE id = ?", [userId]);
    // => await pauses execution until query resolves
    return results[0];
    // => Returns data directly, no callback needed
  } catch (err) {
    // => Promise rejection caught here
    throw err;
    // => Re-throw for caller to handle
  }
}

Text explanation: Async/await provides synchronous-looking async code. Try-catch replaces error-first callbacks. Eliminates nesting.

Commands:

You: Convert src/services/legacy.ts from callbacks to async/await
                                    # => Claude reads legacy.ts
                                    # => Identifies callback patterns:
                                    # =>   - 12 functions using error-first callbacks
                                    # =>   - 3 levels of callback nesting (callback hell)
                                    # => Refactors each function:
                                    # =>   - Wraps callback APIs in Promises
                                    # =>   - Converts functions to async
                                    # =>   - Replaces callbacks with await
                                    # =>   - Replaces error-first pattern with try-catch
                                    # => Confirms: "Migrated 12 functions to async/await"

Key Takeaway: Claude converts callback-based code to async/await, eliminating callback hell and improving readability with modern syntax.

Why It Matters: Callback-to-async migration is tedious and error-prone - easy to miss error handling or introduce race conditions. AI migration preserves error handling semantics while modernizing syntax.

Example 47: Error Handling Pattern Standardization

Standardize error handling across codebase. Claude identifies inconsistent patterns and updates to project-standard approach.

Commands:

You: Standardize all error handling in src/api/ to use our custom ApiError class
                                    # => Claude searches for error patterns
                                    # => Finds inconsistencies:
                                    # =>   - 8 endpoints use: throw new Error('message')
                                    # =>   - 5 endpoints use: res.status(500).send('Error')
                                    # =>   - 3 endpoints use: throw { code: 500, message: '...' }
                                    # => Reads ApiError class definition
                                    # => Refactors all error handling:
                                    # =>   throw new Error('Not found')
                                    # =>   → throw new ApiError(404, 'USER_NOT_FOUND', 'User not found')
                                    # => Ensures consistent structure:
                                    # =>   - HTTP status code
                                    # =>   - Error code constant
                                    # =>   - User-facing message
                                    # => Updates 16 error throw sites

Key Takeaway: Claude identifies error handling inconsistencies and refactors to standard pattern, ensuring uniform error responses across API.

Why It Matters: Inconsistent error handling causes poor client experience - different error formats per endpoint. Standardization improves API usability and debugging. This makes APIs more developer-friendly.

Example 48: Configuration Extraction and Environment Variables

Extract hardcoded values into configuration files. Claude identifies magic numbers and strings that should be configurable.

Commands:

You: Extract all hardcoded configuration values in src/ to environment variables
                                    # => Claude searches for configuration candidates:
                                    # =>   - Database connection strings
                                    # =>   - API keys and secrets
                                    # =>   - Port numbers
                                    # =>   - Feature flags
                                    # =>   - Rate limit thresholds
                                    # => Identifies 23 hardcoded values:
                                    # =>   - src/config/database.ts: 'localhost:5432'
                                    # =>   - src/api/auth.ts: 'secret-key-123'
                                    # =>   - src/server.ts: port 3000
                                    # =>   - src/middleware/rateLimiter.ts: 100 requests/hour
                                    # => Creates .env.example:
                                    # =>   DATABASE_URL=postgresql://localhost:5432/dbname
                                    # =>   JWT_SECRET=your-secret-key
                                    # =>   PORT=3000
                                    # =>   RATE_LIMIT_PER_HOUR=100
                                    # => Updates code to use process.env:
                                    # =>   const dbUrl = process.env.DATABASE_URL;
                                    # => Creates config module: src/config/env.ts

Key Takeaway: Claude identifies hardcoded config values, extracts to environment variables, and creates .env.example template with sensible defaults.

Why It Matters: Hardcoded config prevents environment-specific deployment (dev vs staging vs production). Config extraction is tedious - requires finding all hardcoded values across codebase. AI extraction ensures comprehensive identification. Teams report 60% faster environment setup with extracted configs.

Example 49: Design Pattern Implementation - Strategy Pattern

Implement design patterns to improve code structure. Claude refactors procedural code to use appropriate patterns based on problem structure.

Commands:

You: Refactor the payment processing code in src/services/payments.ts to use Strategy pattern
                                    # => Claude reads payments.ts
                                    # => Identifies current structure:
                                    # =>   function processPayment(type, data) {
                                    # =>     if (type === 'credit_card') { ... }
                                    # =>     else if (type === 'paypal') { ... }
                                    # =>     else if (type === 'crypto') { ... }
                                    # =>   }
                                    # => Recognizes Strategy pattern opportunity
                                    # => Refactors to pattern:
                                    # =>   1. Creates interface PaymentStrategy
                                    # =>   2. Implements: CreditCardStrategy, PaypalStrategy, CryptoStrategy
                                    # =>   3. Creates PaymentProcessor with strategy map
                                    # =>   4. Updates processPayment to use strategy
                                    # => Result: extensible, testable, follows OCP
                                    # => Creates 4 new files:
                                    # =>   - PaymentStrategy.ts (interface)
                                    # =>   - CreditCardStrategy.ts
                                    # =>   - PaypalStrategy.ts
                                    # =>   - CryptoStrategy.ts

Key Takeaway: Claude identifies code smells (if-else chains) and refactors to appropriate design patterns (Strategy), improving extensibility and testability.

Why It Matters: Design pattern implementation requires architectural knowledge and multi-file coordination. AI refactoring applies patterns correctly with proper structure. Teams report 50% improvement in code maintainability after AI pattern refactoring. This makes adding new payment methods trivial (new strategy class, no conditionals).

Example 50: Dead Code Elimination

Identify and remove unused code. Claude analyzes imports, function calls, and exports to find dead code safely removable.

  graph TD
    A[Scan Exports] -->|Find Unused| B[Identify Dead Code]
    B -->|Check Imports| C[Verify Unreferenced]
    C -->|Safe to Remove| D[Delete Code]
    C -->|Still Used| E[Keep Code]

    style A fill:#0173B2,stroke:#000,color:#fff
    style B fill:#DE8F05,stroke:#000,color:#fff
    style C fill:#029E73,stroke:#000,color:#fff
    style D fill:#CC78BC,stroke:#000,color:#fff
    style E fill:#CA9161,stroke:#000,color:#fff

Commands:

You: Find and remove unused functions in src/utils/
                                    # => Claude analyzes each file in src/utils/
                                    # => For each exported function:
                                    # =>   1. Searches project for imports
                                    # =>   2. Checks if function called anywhere
                                    # => Identifies unused functions:
                                    # =>   - src/utils/legacy.ts: formatDate (not imported)
                                    # =>   - src/utils/helpers.ts: calculateTotal (imported but never called)
                                    # =>   - src/utils/validators.ts: validateZipCode (not imported)
                                    # => Asks for confirmation:
                                    # => "Found 3 unused functions. Safe to remove?"
You: Yes, remove them
                                    # => Claude deletes functions from files
                                    # => Removes empty files if no exports remain
                                    # => Confirms: "Removed 3 unused functions, deleted 1 empty file"

Key Takeaway: Claude finds unused code by analyzing exports, imports, and call sites across project. Safely removes dead code after confirmation.

Why It Matters: Dead code bloats codebase and confuses maintainers. Manual dead code analysis is tedious - requires searching every function across entire project. AI analysis is comprehensive and fast. Teams report 30% codebase size reduction after AI dead code elimination in mature projects.

Git and Deployment Integration (Examples 51-55)

Example 51: Automated Git Workflow - Branch Creation and Commits

Automate git operations through conversation. Claude creates branches, stages changes, and commits with descriptive messages.

  sequenceDiagram
    participant User as User
    participant Claude as Claude
    participant Git as Git

    User->>Claude: Create feature branch + commit
    Claude->>Git: git checkout -b feature/rate-limiting
    Git-->>Claude: Branch created
    Claude->>Git: git status
    Git-->>Claude: 3 modified files
    Claude->>Git: git add (specific files)
    Git-->>Claude: Files staged
    Claude->>Claude: Generate conventional commit message
    Claude->>Git: git commit -m "feat(api): ..."
    Git-->>Claude: Commit created
    Claude-->>User: Branch + commit confirmed

Commands:

You: Create a new feature branch for the rate limiting feature and commit the changes
                                    # => Claude uses Bash tool
                                    # => Runs: git checkout -b feature/rate-limiting
                                    # => Branch created: feature/rate-limiting
                                    # => Runs: git status
                                    # => Modified files:
                                    # =>   - src/middleware/rateLimiter.ts (new file)
                                    # =>   - src/server.ts (modified)
                                    # =>   - package.json (modified)
                                    # => Runs: git add (specific files, not git add .)
                                    # =>   git add src/middleware/rateLimiter.ts
                                    # =>   git add src/server.ts
                                    # =>   git add package.json
                                    # => Generates commit message:
                                    # =>   feat(api): add rate limiting middleware
                                    # =>
                                    # =>   - Implement Redis-based rate limiter
                                    # =>   - Support tier-based limits (free: 100/hr, premium: 1000/hr)
                                    # =>   - Add rate limit middleware to server
                                    # => Runs: git commit -m "..."
                                    # => Confirms: "Created branch + committed 3 files"

Key Takeaway: Claude automates git workflow: creates branches, stages specific files, generates conventional commit messages, and executes commits.

Why It Matters: Git operations are repetitive context switches from coding. AI automation maintains flow state. Generated commit messages follow conventional commits format automatically. Teams report 40% faster feature commits with AI git automation. This reduces friction in git workflow.

Example 52: Commit Message Generation from Changes

Generate descriptive commit messages by analyzing staged changes. Claude reads diffs and summarizes changes in conventional commit format.

Commands:

You: Generate a commit message for the staged changes
                                    # => Claude runs: git diff --staged
                                    # => Analyzes changes:
                                    # =>   - src/api/users.ts: Added error handling (8 insertions)
                                    # =>   - src/api/posts.ts: Added error handling (12 insertions)
                                    # =>   - src/utils/errors.ts: Created ApiError class (new file)
                                    # => Generates message:
                                    # =>   feat(api): standardize error handling with ApiError class
                                    # =>
                                    # =>   - Create custom ApiError class for consistent error responses
                                    # =>   - Add error handling to users and posts endpoints
                                    # =>   - Include HTTP status, error codes, and messages
                                    # =>
                                    # =>   Improves API client error handling with predictable format.
                                    # => Shows message for review
You: Commit with that message
                                    # => Claude runs: git commit -m "..."
                                    # => Confirms: "Committed with generated message"

Key Takeaway: Claude analyzes git diffs to generate descriptive conventional commit messages summarizing what changed and why.

Why It Matters: Descriptive commit messages are time-consuming to write. Developers often write lazy messages (“fix bug”, “update code”). AI-generated messages provide consistent quality and format. Teams report 70% improvement in commit message quality with AI generation. This makes git history more valuable for debugging and understanding changes.

Example 53: Pull Request Description Generation

Generate comprehensive PR descriptions from branch commits. Claude analyzes commit history and creates structured PR summary.

  graph TD
    A[Branch Commits] -->|Analyze| B[5 Commits]
    A -->|Diff| C[12 Files Changed]
    B -->|Categorize| D[Features + Fixes]
    C -->|Measure| E[450 Insertions, 120 Deletions]
    D -->|Generate| F[PR Description]
    E -->|Include| F
    F -->|Sections| G[Summary]
    F -->|Sections| H[Changes]
    F -->|Sections| I[Testing]
    F -->|Sections| J[Breaking Changes]

    style A fill:#0173B2,stroke:#000,color:#fff
    style B fill:#DE8F05,stroke:#000,color:#fff
    style C fill:#DE8F05,stroke:#000,color:#fff
    style D fill:#029E73,stroke:#000,color:#fff
    style E fill:#029E73,stroke:#000,color:#fff
    style F fill:#CC78BC,stroke:#000,color:#fff
    style G fill:#CA9161,stroke:#000,color:#fff
    style H fill:#CA9161,stroke:#000,color:#fff
    style I fill:#CA9161,stroke:#000,color:#fff
    style J fill:#CA9161,stroke:#000,color:#fff

Commands:

You: Generate a pull request description for the current branch
                                    # => Claude runs: git diff main...HEAD
                                    # => Analyzes all commits in branch:
                                    # =>   - 5 commits
                                    # =>   - Files changed: 12
                                    # =>   - Insertions: 450, Deletions: 120
                                    # => Reads commit messages and changes
                                    # => Generates PR description:
                                    # => ## Summary
                                    # => Implements rate limiting for API endpoints with tier-based limits.
                                    # =>
                                    # => ## Changes
                                    # => - Add Redis-based rate limiter middleware
                                    # => - Implement tier detection from JWT
                                    # => - Configure limits: free (100/hr), premium (1000/hr)
                                    # => - Add rate limit headers to responses
                                    # => - Include integration tests for rate limiting
                                    # =>
                                    # => ## Testing
                                    # => - [x] Unit tests for middleware
                                    # => - [x] Integration tests for tier-based limits
                                    # => - [x] Manual testing with free and premium accounts
                                    # =>
                                    # => ## Breaking Changes
                                    # => None

Key Takeaway: Claude analyzes branch commits and diffs to generate structured PR descriptions with summary, changes, testing, and breaking changes sections.

Why It Matters: Comprehensive PR descriptions improve code review quality but are time-consuming to write. AI generation ensures consistent format and completeness. Teams report 60% faster PR creation with AI descriptions. This improves review efficiency - reviewers understand changes before reading code.

Example 54: Merge Conflict Resolution Assistance

Get help resolving merge conflicts. Claude reads conflict markers, understands both versions, and suggests resolutions.

Commands:

You: Help me resolve the merge conflict in src/api/users.ts
                                    # => Claude reads conflicted file
                                    # => Sees conflict markers:
                                    # =>   <<<<<<< HEAD
                                    # =>   export async function createUser(data: CreateUserDTO) {
                                    # =>     const user = await db.users.create(data);
                                    # =>   =======
                                    # =>   export async function createUser(userData: UserInput) {
                                    # =>     const user = await database.insert('users', userData);
                                    # =>   >>>>>>> feature/database-refactor
                                    # => Analyzes differences:
                                    # => "Conflict: Parameter naming and database API differences.
                                    # =>  HEAD uses: CreateUserDTO + db.users.create
                                    # =>  Branch uses: UserInput + database.insert
                                    # =>
                                    # =>  Recommendation: Keep HEAD's approach (ORM-based),
                                    # =>  but update parameter name if UserInput is preferred.
                                    # =>
                                    # =>  Suggested resolution:
                                    # =>  export async function createUser(data: CreateUserDTO) {
                                    # =>    const user = await db.users.create(data);
                                    # =>  }"
You: Apply that resolution
                                    # => Claude removes conflict markers
                                    # => Updates file with suggested code
                                    # => Runs: git add src/api/users.ts

Key Takeaway: Claude reads conflict markers, explains differences, recommends resolution strategy, and can apply resolutions automatically.

Why It Matters: Merge conflicts are frustrating and error-prone - easy to accidentally keep wrong version or introduce syntax errors. AI conflict analysis explains what changed in each branch. Teams report 50% faster conflict resolution with AI assistance. This is especially valuable for complex conflicts spanning multiple functions.

Example 55: CI/CD Configuration Generation

Generate CI/CD pipeline configs. Claude creates GitHub Actions, GitLab CI, or other pipeline files based on project stack.

Commands:

You: Create a GitHub Actions workflow for testing and deploying this Node.js app
                                    # => Claude analyzes project:
                                    # =>   - Detects: Node.js 20.x (from package.json engines)
                                    # =>   - Detects: Jest tests (from package.json scripts)
                                    # =>   - Detects: TypeScript (from tsconfig.json)
                                    # => Generates .github/workflows/ci.yml:
                                    # =>   name: CI/CD
                                    # =>   on: [push, pull_request]
                                    # =>   jobs:
                                    # =>     test:
                                    # =>       runs-on: ubuntu-latest
                                    # =>       steps:
                                    # =>         - uses: actions/checkout@v4
                                    # =>         - uses: actions/setup-node@v4
                                    # =>           with: { node-version: '20.x' }
                                    # =>         - run: npm ci
                                    # =>         - run: npm run build
                                    # =>         - run: npm test
                                    # =>     deploy:
                                    # =>       needs: test
                                    # =>       if: github.ref == 'refs/heads/main'
                                    # =>       runs-on: ubuntu-latest
                                    # =>       steps:
                                    # =>         - (deployment steps)
                                    # => Creates workflow file

Key Takeaway: Claude generates CI/CD pipeline configs based on detected tech stack, including test, build, and deployment steps.

Why It Matters: CI/CD config is boilerplate-heavy and error-prone. AI generation creates working pipelines matching project stack. Teams report 70% faster CI/CD setup with AI config generation. This eliminates configuration debugging and gets projects to automated testing/deployment faster.

Code Documentation and Architecture (Examples 56-60)

Example 56: Automated API Documentation Generation

Generate API documentation from endpoint implementations. Claude extracts routes, parameters, request/response formats, and error codes.

Commands:

You: Generate API documentation for all endpoints in src/api/
                                    # => Claude scans all files in src/api/
                                    # => Identifies endpoints:
                                    # =>   - GET    /api/users
                                    # =>   - POST   /api/users
                                    # =>   - GET    /api/users/:id
                                    # =>   - PATCH  /api/users/:id
                                    # =>   - DELETE /api/users/:id
                                    # =>   - (15 more endpoints...)
                                    # => For each endpoint, extracts:
                                    # =>   - HTTP method and path
                                    # =>   - Request body schema (from validation)
                                    # =>   - Response format (from code)
                                    # =>   - Status codes (success + errors)
                                    # =>   - Authentication requirements
                                    # => Generates docs/api-reference.md:
                                    # =>   ## POST /api/users
                                    # =>   Create a new user account.
                                    # =>
                                    # =>   **Request Body:**
                                    # =>   ```json
                                    # =>   { "name": "string", "email": "string", "password": "string" }
                                    # =>   ```
                                    # =>
                                    # =>   **Responses:**
                                    # =>   - 201: User created (returns user object)
                                    # =>   - 400: Validation error
                                    # =>   - 409: Email already exists

Key Takeaway: Claude generates API documentation by analyzing endpoint code, extracting schemas, responses, and error conditions automatically.

Why It Matters: Manual API documentation is tedious and becomes outdated. AI-generated docs match actual implementation. Teams report 80% reduction in documentation staleness with AI generation. This makes APIs more discoverable and reduces support burden.

Example 57: Architecture Diagram Generation with Mermaid

Generate architecture diagrams visualizing system structure. Claude analyzes imports and dependencies to create component relationship diagrams.

Commands:

You: Create an architecture diagram showing the layers of this application
                                    # => Claude analyzes project structure:
                                    # =>   - src/api/ (HTTP handlers)
                                    # =>   - src/services/ (business logic)
                                    # =>   - src/models/ (data models)
                                    # =>   - src/database/ (data access)
                                    # => Analyzes imports to understand dependencies
                                    # => Generates Mermaid diagram:
                                    # =>   graph TD
                                    # =>     A[HTTP Layer: src/api/] -->|calls| B[Service Layer: src/services/]
                                    # =>     B -->|uses| C[Model Layer: src/models/]
                                    # =>     B -->|accesses| D[Database Layer: src/database/]
                                    # =>     C -->|schema| D
                                    # =>
                                    # =>     style A fill:#0173B2,stroke:#000,color:#fff
                                    # =>     style B fill:#DE8F05,stroke:#000,color:#fff
                                    # =>     style C fill:#029E73,stroke:#000,color:#fff
                                    # =>     style D fill:#CC78BC,stroke:#000,color:#fff
                                    # => Adds to docs/architecture.md

Key Takeaway: Claude analyzes project structure and import relationships to generate Mermaid architecture diagrams showing system layers and dependencies.

Why It Matters: Architecture diagrams help onboard new developers but are tedious to maintain. AI-generated diagrams reflect actual code structure, not outdated documentation. Teams report 60% faster new developer onboarding with up-to-date architecture diagrams.

Example 58: Code Comment and Docstring Addition

Add explanatory comments to complex code. Claude analyzes logic and generates comments explaining intent, not just restating code.

Commands:

You: Add explanatory comments to the algorithm in src/utils/recommendation.ts
                                    # => Claude reads recommendation.ts
                                    # => Analyzes complex algorithm:
                                    # =>   function calculateSimilarity(userA, userB) {
                                    # =>     const dotProduct = ...
                                    # =>     const magnitudeA = ...
                                    # =>     const magnitudeB = ...
                                    # =>     return dotProduct / (magnitudeA * magnitudeB);
                                    # =>   }
                                    # => Adds explanatory comments:
                                    # =>   /**
                                    # =>    * Calculates cosine similarity between two user preference vectors.
                                    # =>    * Higher values (closer to 1) indicate more similar preferences.
                                    # =>    * Used for collaborative filtering recommendations.
                                    # =>    */
                                    # =>   function calculateSimilarity(userA, userB) {
                                    # =>     // Compute dot product of preference vectors
                                    # =>     const dotProduct = ...
                                    # =>     // Calculate magnitude (Euclidean norm) for normalization
                                    # =>     const magnitudeA = ...
                                    # =>     const magnitudeB = ...
                                    # =>     // Cosine similarity: dot product divided by product of magnitudes
                                    # =>     return dotProduct / (magnitudeA * magnitudeB);
                                    # =>   }

Key Takeaway: Claude adds comments explaining algorithm intent and mathematical concepts, not just restating obvious operations.

Why It Matters: Good comments explain WHY, not WHAT. AI comments add context about algorithms, business rules, and design decisions. Teams report 50% faster code comprehension with AI-added comments. This is especially valuable for complex algorithms where intent is unclear from code alone.

Example 59: Performance Optimization Suggestions

Request performance analysis and optimization recommendations. Claude identifies bottlenecks and suggests improvements.

Commands:

You: Analyze src/services/search.ts for performance issues and suggest optimizations
                                    # => Claude reads search.ts
                                    # => Analyzes code:
                                    # =>   function search(query) {
                                    # =>     const allItems = loadAllItems();  // Loads 10,000+ items
                                    # =>     return allItems.filter(item => item.name.includes(query));
                                    # =>   }
                                    # => Identifies issues:
                                    # =>   1. Loading all items inefficient (O(n) every search)
                                    # =>   2. Linear search through all items
                                    # =>   3. Case-sensitive matching misses results
                                    # => Suggests optimizations:
                                    # =>   1. Add database index on name column
                                    # =>   2. Push filter to SQL WHERE clause (database-level)
                                    # =>   3. Use ILIKE for case-insensitive search
                                    # =>   4. Add pagination to limit result set
                                    # => Provides optimized implementation:
                                    # =>   async function search(query, page = 1, limit = 20) {
                                    # =>     // Database performs filtering (much faster)
                                    # =>     return await db.query(
                                    # =>       'SELECT * FROM items WHERE name ILIKE $1 LIMIT $2 OFFSET $3',
                                    # =>       [`%${query}%`, limit, (page - 1) * limit]
                                    # =>     );
                                    # =>   }

Key Takeaway: Claude identifies performance bottlenecks (loading all data, inefficient filtering) and provides optimized implementations using database-level filtering.

Why It Matters: Performance issues are often non-obvious to inexperienced developers. AI analysis identifies common anti-patterns (N+1 queries, loading all data, missing indexes). Teams report 40% performance improvement on average after applying AI optimization suggestions.

Example 60: Accessibility Audit and Improvements

Audit frontend code for accessibility issues. Claude identifies WCAG violations and suggests fixes for keyboard navigation, ARIA labels, and color contrast.

  graph TD
    A[Scan Components] -->|Identify Issues| B[Keyboard Nav]
    A -->|Identify Issues| C[ARIA Labels]
    A -->|Identify Issues| D[Color Contrast]
    B -->|Generate Fixes| E[Accessible Code]
    C -->|Generate Fixes| E
    D -->|Generate Fixes| E

    style A fill:#0173B2,stroke:#000,color:#fff
    style B fill:#DE8F05,stroke:#000,color:#fff
    style C fill:#029E73,stroke:#000,color:#fff
    style D fill:#CC78BC,stroke:#000,color:#fff
    style E fill:#CA9161,stroke:#000,color:#fff

Commands:

You: Audit src/components/Modal.tsx for accessibility issues
                                    # => Claude reads Modal.tsx
                                    # => Identifies issues:
                                    # =>   1. Missing focus trap (keyboard users can tab outside modal)
                                    # =>   2. Missing ARIA role and labels
                                    # =>   3. No keyboard close (ESC key)
                                    # =>   4. Missing focus management (return focus on close)
                                    # =>   5. Close button has no accessible label (icon only)
                                    # => Suggests fixes:
                                    # =>   1. Add role="dialog" and aria-modal="true"
                                    # =>   2. Add aria-labelledby pointing to title
                                    # =>   3. Implement ESC key handler
                                    # =>   4. Use useEffect to manage focus
                                    # =>   5. Add aria-label="Close" to close button
                                    # => Generates improved component:
                                    # =>   <div role="dialog" aria-modal="true" aria-labelledby="modal-title">
                                    # =>     <h2 id="modal-title">{title}</h2>
                                    # =>     <button onClick={onClose} aria-label="Close">
                                    # =>       <CloseIcon aria-hidden="true" />
                                    # =>     </button>
                                    # =>     {children}
                                    # =>   </div>
                                    # => Adds keyboard handlers and focus management

Key Takeaway: Claude audits components for WCAG compliance, identifies keyboard navigation, ARIA, and focus management issues, then generates accessible implementations.

Why It Matters: Accessibility is often overlooked but legally required in many contexts. AI accessibility audits catch issues developers miss. Teams report 70% improvement in WCAG compliance with AI accessibility fixes. This makes applications usable for screen reader users and keyboard-only navigation.

Next Steps

This intermediate tutorial covered Examples 31-60 (40-75% of Claude Code capabilities). You learned multi-file refactoring, advanced prompting techniques, comprehensive testing workflows, git automation, and code quality improvements for production-ready AI-assisted development.

Continue learning:

  • Beginner - Examples 1-30 reviewing essential commands and basic workflows
  • Advanced - Examples 61-90 covering custom agents, skill development, and enterprise integration patterns
Last updated