npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@cloudbase/agent-examples-claude-agent-human-in-the-loop

v0.0.24

Published

Claude Agent SDK human-in-the-loop example

Readme

Claude Agent Human-in-the-Loop Example

This example demonstrates how to implement human-in-the-loop (HITL) workflows with Claude Agent SDK.

Overview

When a user asks Claude to perform a task, the agent:

  1. Calls the plan_execution_steps tool to generate 10 imperative steps
  2. Returns an interrupt with the steps for user review
  3. Waits for user to enable/disable steps
  4. Continues execution with the approved steps
  5. Generates a creative description of how it will perform the task

Architecture

User: "Help me bake a cake"
  ↓
Claude: Calls plan_execution_steps
  ↓
Tool: Returns { __interrupt__: true, steps: [...] }
  ↓
Agent: Detects __interrupt__, emits RUN_FINISHED (interrupt)
  ↓
Frontend: Shows steps UI (enable/disable checkboxes)
  ↓
User: Disables "Add frosting", clicks Continue
  ↓
Frontend: Sends resume({ steps: [...modified...] })
  ↓
Agent: Uses session ID to resume
  ↓
Claude: Generates creative response based on enabled steps

Key Features

  • Decoupled Design: Agent doesn't know about specific tools, only detects __interrupt__ marker
  • Session Management: Uses Claude SDK's native session mechanism for context preservation
  • Generic Protocol: Any tool can trigger interrupts by returning __interrupt__: true
  • Flexible: Easy to add new HITL scenarios without modifying the agent

Usage

Environment Variables

ANTHROPIC_API_KEY=your-api-key
CLAUDE_MODEL=claude-3-5-sonnet-20241022  # optional

Running the Example

# Install dependencies
pnpm install

# Build
pnpm run build

# Run the server (from examples/agents/server)
cd ../server
pnpm run dev

Testing

  1. Open the UI: http://localhost:5173/?agent=ts-claude-agent-human-in-the-loop
  2. Send a message: "Help me bake a cake"
  3. Claude will generate 10 steps and pause
  4. Review the steps, disable any you don't want
  5. Click "Continue"
  6. Claude will generate a creative response based on your choices

Implementation Details

Tool Definition

const planExecutionStepsTool = tool(
  "plan_execution_steps",
  "Generate 10 imperative steps for a task",
  { steps: z.array(z.object({ description: z.string(), status: z.literal("enabled") })) },
  async (args) => {
    // Return __interrupt__ marker
    return {
      content: [{
        type: "text",
        text: JSON.stringify({
          __interrupt__: true,
          reason: "Task planning requires user confirmation",
          steps: args.steps,
        })
      }],
      isError: false,
    };
  }
);

Agent Configuration

const agent = new ClaudeAgent({
  name: "claude-hitl-agent",
  mcpServers: { hitl: createSdkMcpServer({ tools: [planExecutionStepsTool] }) },
  systemPrompt: "When user asks to perform a task, MUST call plan_execution_steps...",
  includePartialMessages: true,
});

Interrupt Detection (in ClaudeAgent)

private detectInterrupt(sdkMessage: SDKMessage) {
  if (sdkMessage.type === "assistant") {
    for (const content of message.content) {
      if (content.type === "text") {
        const parsed = JSON.parse(content.text);
        if (parsed.__interrupt__) {
          return { id, reason, payload: parsed };
        }
      }
    }
  }
  return null;
}

Resume (in ClaudeAgent)

private async _resume(subscriber, input) {
  const sessionId = this.sessionStore.get(threadId);
  const resumePrompt = this.buildResumePrompt(payload);
  
  const agentQuery = query({
    prompt: resumePrompt,
    options: { resume: sessionId } // SDK auto-loads history
  });
  
  // Continue processing...
}

Extending with New HITL Scenarios

To add a new HITL scenario (e.g., payment approval):

const approvePaymentTool = tool(
  "approve_payment",
  "Request approval for a payment",
  { amount: z.number(), recipient: z.string() },
  async (args) => {
    return {
      content: [{
        type: "text",
        text: JSON.stringify({
          __interrupt__: true,
          type: "approve_payment",
          amount: args.amount,
          recipient: args.recipient,
        })
      }],
      isError: false,
    };
  }
);

No changes needed to ClaudeAgent! It automatically detects any __interrupt__ marker.

References