Human-in-the-Loop: Claude Code Implementation
Overview
Section titled âOverviewâClaude Code provides the most seamless human-in-the-loop experience through its built-in AskUserQuestion tool. Unlike other approaches, Claude Code handles both the tool definition and UI rendering automatically.
graph LR A[User Request] --> B[Claude Processes] B --> C[Generates AskUserQuestion] C --> D[Claude Code Renders UI] D --> E[User Selects Option] E --> F[Answer to Claude] F --> G[Claude Continues]How It Works
Section titled âHow It WorksâThe Built-in Tool
Section titled âThe Built-in ToolâClaude Code automatically provides the AskUserQuestion tool to Claude. You donât need to define it yourself.
Tool Signature:
interface AskUserQuestionInput { questions: Question[]; answers?: Record<string, string | string[]>; metadata?: { source?: string };}
interface Question { question: string; // The question text header: string; // Short label (max 12 chars) multiSelect: boolean; // Allow multiple selections? options: Option[]; // 2-4 choices}
interface Option { label: string; // Display text (1-5 words) description: string; // Explanation (1-2 sentences)}The Architecture
Section titled âThe ArchitectureâsequenceDiagram participant U as User participant CC as Claude Code CLI participant API as Anthropic API participant C as Claude
U->>CC: "Add authentication" CC->>API: Request with tools API->>C: Process request
Note over C: Decides to ask question C->>API: Generate tool_use block API->>CC: Response with tool_use
CC->>CC: Detect AskUserQuestion CC->>U: Render UI (radio buttons)
U->>CC: Select option CC->>API: Tool result API->>C: Continue processing
C->>API: Final response API->>CC: Complete CC->>U: Show resultCreating Interactive Skills
Section titled âCreating Interactive SkillsâBasic Skill Structure
Section titled âBasic Skill StructureâCreate a skill file in ~/.claude/plugins/your-plugin/skills/:
---description: 'Interactive database setup'allowed-tools: ['AskUserQuestion', 'Read', 'Write', 'Bash']---
# Database Setup
Help the user set up a database for their project.
## Phase 1: Gather Requirements
Use the AskUserQuestion tool to collect user preferences.
**Question 1 - Database Engine:**
- question: "Which database engine should we use?"- header: "Database"- multiSelect: false- options: - PostgreSQL (Relational, ACID compliant, best for complex queries) - MongoDB (Document store, flexible schema, rapid iteration) - Redis (In-memory, fast, best for caching and sessions)
**Question 2 - Features:**
- question: "Which features do you want to enable?"- header: "Features"- multiSelect: true- options: - Migrations (Database schema versioning) - Seeding (Sample data generation) - Backups (Automated backup system) - Replication (High availability setup)
## Phase 2: Implementation
Based on the user's selections:
1. Install the chosen database driver2. Generate configuration files3. Set up selected features4. Create example code
## Phase 3: Confirmation
Show what was created and provide next steps.How Claude Interprets This
Section titled âHow Claude Interprets ThisâWhen Claude reads this skill, it:
- Sees instructions to use
AskUserQuestion - Reads the question specifications
- Generates a structured tool call matching the schema
- Waits for the response
- Continues based on the answer
Tool Call Example
Section titled âTool Call ExampleâWhat Claude Generates
Section titled âWhat Claude Generatesâ{ "type": "tool_use", "id": "toolu_01ABC", "name": "AskUserQuestion", "input": { "questions": [ { "question": "Which database engine should we use?", "header": "Database", "multiSelect": false, "options": [ { "label": "PostgreSQL", "description": "Relational, ACID compliant, best for complex queries" }, { "label": "MongoDB", "description": "Document store, flexible schema, rapid iteration" }, { "label": "Redis", "description": "In-memory, fast, best for caching and sessions" } ] } ] }}What Claude Code Does
Section titled âWhat Claude Code Doesâ-
Parses the tool call
-
Renders interactive UI:
â Which database engine should we use?ââââââââââââââââââââââââââââââââââââââ PostgreSQLRelational, ACID compliant, best for complex queriesâ MongoDBDocument store, flexible schema, rapid iterationâ RedisIn-memory, fast, best for caching and sessionsâ Other -
Waits for user selection
-
Returns result to Claude:
{"Database": "PostgreSQL"}
Design Patterns
Section titled âDesign PatternsâPattern 1: Sequential Questions
Section titled âPattern 1: Sequential QuestionsâAsk questions one after another:
## Step 1: Choose Stack
Use AskUserQuestion:
**Question**: "Which technology stack?"
- Node.js + Express- Python + FastAPI- Go + Fiber
## Step 2: Choose Database (Based on Step 1)
If Node.js:**Question**: "Which Node.js database client?"
- pg (PostgreSQL)- mongoose (MongoDB)- ioredis (Redis)
If Python:**Question**: "Which Python database library?"
- psycopg2 (PostgreSQL)- pymongo (MongoDB)- redis-py (Redis)Pattern 2: Multi-Select Features
Section titled âPattern 2: Multi-Select FeaturesâAllow multiple selections:
**Question**: "Which features do you want?"
- header: "Features"- multiSelect: true # â Allow multiple selections- options: - Authentication (User login and sessions) - API (REST API endpoints) - Database (Database integration) - Testing (Unit and integration tests)Pattern 3: Progressive Disclosure
Section titled âPattern 3: Progressive DisclosureâStart simple, get detailed only when needed:
## Question 1: Experience Level
**Question**: "What's your experience level?"
- Beginner (Use defaults and best practices)- Intermediate (Customize common settings)- Advanced (Full control over configuration)
## Conditional Follow-ups
If Beginner:Apply defaults, skip detailed questions
If Intermediate:Ask about key preferences (auth, database, deployment)
If Advanced:Ask detailed questions about architecture, optimization, securityBest Practices
Section titled âBest Practicesâ1. Clear, Specific Questions
Section titled â1. Clear, Specific Questionsââ Bad:
**Question**: "Choose one"
- Option 1- Option 2â Good:
**Question**: "Which authentication method should we implement?"
- OAuth 2.0 (Industry standard, supports Google, GitHub, etc.)- JWT (Stateless tokens, good for APIs and microservices)- Session (Traditional server-side sessions with cookies)2. Descriptive Options
Section titled â2. Descriptive Optionsââ Bad:
- Fast one- Reliable one- Other oneâ Good:
- Redis (In-memory, microsecond latency, best for caching)- PostgreSQL (ACID compliant, data integrity, complex queries)- MongoDB (Flexible schema, horizontal scaling, rapid development)3. Respect Constraints
Section titled â3. Respect Constraintsâ- Header: Max 12 characters
- Options: 2-4 per question (5+ is overwhelming)
- Questions: 1-4 per tool call
- Description: 1-2 sentences, focus on key benefits
â Bad header:
header: "Authentication Method Selection" # 30 chars!â Good header:
header: "Auth method" # 11 chars4. Use Multi-Select Appropriately
Section titled â4. Use Multi-Select Appropriatelyââ Use for non-exclusive choices:
**Question**: "Which features do you want to enable?"multiSelect: true
- Logging- Metrics- Alerts- Backupsâ Donât use for exclusive choices:
**Question**: "Which database engine?"multiSelect: true # â Wrong! Can only use one engineReal-World Example
Section titled âReal-World ExampleâFeature Development Skill
Section titled âFeature Development Skillâ---description: 'Guided feature development with human-in-the-loop'allowed-tools: ['AskUserQuestion', 'Glob', 'Grep', 'Read', 'Write', 'Edit', 'Bash']---
# Feature Development Workflow
## Phase 1: Understand Codebase
Use Glob and Grep to understand existing architecture:
1. Find similar features2. Identify patterns and conventions3. Locate relevant files
## Phase 2: Clarify Requirements
Use AskUserQuestion to gather requirements.
**Question 1 - Feature Scope:**
- question: "What's the scope of this feature?"- header: "Scope"- multiSelect: false- options: - Small (Single file, < 100 lines, 1-2 hours) - Medium (Multiple files, < 500 lines, 1 day) - Large (Architectural changes, > 500 lines, multiple days)
**Question 2 - Implementation Approach:**
- question: "Which implementation approach do you prefer?"- header: "Approach"- multiSelect: false- options: - Quick (Minimal changes, get it working fast) - Balanced (Good balance of speed and quality) - Thorough (Best practices, comprehensive tests, documentation)
**Question 3 - Requirements:**
- question: "What else should this feature include?"- header: "Include"- multiSelect: true- options: - Tests (Unit and integration tests) - Documentation (API docs and examples) - Error Handling (Comprehensive error cases) - Logging (Debug and audit logging)
## Phase 3: Design
Based on answers, design the implementation:
- If Small + Quick: Inline implementation, minimal abstraction- If Medium + Balanced: Modular design, key tests- If Large + Thorough: Full architecture, comprehensive suite
## Phase 4: Implement
Write the code following the chosen approach.
## Phase 5: Review
Show what was created and ask for confirmation:
**Question**: "Does this look good?"
- Yes, perfect (Mark as complete)- Needs tweaks (Make requested changes)- Start over (Redesign from scratch)Tool Response Format
Section titled âTool Response FormatâWhat Claude Receives
Section titled âWhat Claude ReceivesâWhen the user selects options, Claude receives:
{ "Scope": "Medium", "Approach": "Balanced", "Include": ["Tests", "Error Handling"]}Claude can then use this in its reasoning:
The user chose Medium scope with Balanced approach.They want Tests and Error Handling included.
I should:1. Create 2-3 files2. Focus on core functionality + tests3. Include comprehensive error handling4. Skip documentation for nowLimitations
Section titled âLimitationsâUI Constraints
Section titled âUI Constraintsâ- Terminal Only: Only works in Claude Code CLI
- No Custom Styling: Canât change colors, fonts, etc.
- Limited Controls: Radio buttons and checkboxes only
Interaction Constraints
Section titled âInteraction Constraintsâ- Synchronous: Blocks until user responds
- No Validation: Canât add custom validation rules
- No Branching: Canât conditionally show/hide options in same question
Scale Constraints
Section titled âScale Constraintsâ- Max 4 Questions: Per tool call
- Max 4 Options: Per question
- Max 12 Chars: For header
Debugging Tips
Section titled âDebugging TipsâQuestion Not Appearing?
Section titled âQuestion Not Appearing?âCheck 1: Tool in allowed-tools?
---allowed-tools: ['AskUserQuestion', ...] # â
Must include---Check 2: Valid format?
# â
Correct format
**Question**: "Which one?"
- Option A (Description)- Option B (Description)
# â Wrong format
Question: Which one?Options: A, BCheck 3: Constraints met?
- 2-4 options? â
- Header ⤠12 chars? â
- Questions clear? â
Invalid Response?
Section titled âInvalid Response?âClaude Code validates the tool call. If invalid:
- Check required fields (
question,header,options) - Verify
multiSelectis boolean - Ensure 2-4 options per question
Comparison with Other Approaches
Section titled âComparison with Other Approachesâgraph TB subgraph Claude["Claude Code (Built-in)"] C1[Write Skill] --> C2[Claude Uses Tool] C2 --> C3[Auto UI Rendering] C3 --> C4[Done] end
subgraph Manual["Manual Implementation"] M1[Define Tool Schema] --> M2[Implement Handler] M2 --> M3[Build UI Layer] M3 --> M4[State Management] M4 --> M5[Done] end| Aspect | Claude Code | Manual |
|---|---|---|
| Setup | Zero | Define tool + UI |
| UI | Automatic | Build yourself |
| Flexibility | Limited | Full control |
| Complexity | Low (~0 LOC) | High (~200+ LOC) |
| Portability | Claude Code only | Any platform |
When to Use Claude Codeâs Approach
Section titled âWhen to Use Claude Codeâs Approachâ| â Use When⌠| â Avoid When⌠|
|---|---|
| Building Claude Code skills | Need custom UI |
| Terminal interface is fine | Web/mobile required |
| Rapid prototyping | Multi-provider support |
| Standard Q&A patterns | Complex validation |
| Learning HITL concepts | Production web apps |
Next Steps
Section titled âNext Stepsâ- Want more control? â See OpenAI Implementation
- Need flexibility? â Check Model Agnostic Approach