Loading

    Bridge Automated and Human-in-the-Loop AI Workflows with Claude Code

    John Lindquist
    John Lindquist

    Sign-up for the Claude Code Power User Workshop: https://egghead.io/workshop/claude-code

    AI sessions in the terminal are often ephemeral, losing all context the moment you exit. This makes it difficult to automate multi-step processes where one step depends on the context of a previous one.

    This lesson demonstrates how to solve this problem using Claude Code's --session-id and --resume flags to create persistent, reusable sessions. You'll learn how to switch seamlessly between non-interactive mode for scripted tasks and interactive mode for direct human intervention. The key takeaway is a powerful automation pattern: using a cheap, fast AI model for an initial check and automatically escalating to a more powerful model in an interactive session only when an issue is detected.

    Workflow demonstrated in this lesson:

    • Generate a unique ID using uuidgen to create a persistent session.
    • Start a new session with --session-id and resume it later with --resume.
    • Run non-interactive, one-off tasks with --print and a session ID to get direct terminal output.
    • Escalate a non-interactive session into an interactive one by simply resuming it.
    • Build a powerful shell script that uses a "triage" model (Haiku) for a first-pass check and only escalates to a more powerful model (Opus) when human attention is required.

    Key benefits:

    • Maintain Context: Preserve conversation history across multiple terminal commands and sessions.
    • Create Powerful Automations: Build multi-step scripts that leverage a continuous AI context.
    • Build Efficient Triage Workflows: Use cheaper models for initial analysis and expensive models only for complex fixes.
    • Seamlessly Switch Modes: Effortlessly move between fully automated and human-in-the-loop workflows.

    Summary

    This lesson demonstrates how to manage persistent AI sessions in the Claude Code CLI. It covers starting, exiting, and resuming sessions using a unique ID. A key technique shown is switching between non-interactive sessions (for scripted, automated tasks like file analysis) and interactive sessions (for direct user interaction). The main example is a powerful shell script that uses a cheaper AI model for an initial check; if an issue is found, it automatically escalates by resuming the same session with a more powerful model and dropping the user into an interactive mode to supervise the fix, creating an efficient, two-tiered automation workflow.

    Prompts

    hi
    Please summarize my README.md
    Rewrite README.md based on the summary
    Check for anything weird in index.ts. If you find something, print out a <USER-ATTENTION-NEEDED> tag with the reason.
    Please fix the issue

    Terminal Commands

    uuidgen
    id=$(uuidgen)
    echo $id
    claude --session-id $id
    claude --resume $id
    readme_id=$(uuidgen)
    claude --print --session-id $readme_id "Please summarize my README.md"
    claude --resume $readme_id
    claude --resume $readme_id --model opus "Rewrite README.md based on the summary"
    ./check.sh

    Code Snippets

    The following shell script (check.sh) demonstrates a two-tiered automation workflow. A fast, cheap model performs an initial check, and if it detects an issue, it escalates to a more powerful model in an interactive session for the user to resolve.

    #!/bin/bash
    id=$(uuidgen)
    marker="<USER-ATTENTION-NEEDED>"
    instructions="Check for anything weird in index.ts. If you find something, print out a $marker tag with the reason."
    # First pass with a cheap model in non-interactive mode
    result=$(claude --model haiku --session-id $id --print "$instructions")
    # Check if the marker was found in the result
    if [[ $result == *"$marker"* ]]; then
    # Escalate to a powerful model in interactive mode
    claude --model opus --resume $id "Please fix the issue"
    else
    echo "No user attention needed"
    fi
    Share