Human-in-the-Loop: Overview
What is Human-in-the-Loop?
Section titled âWhat is Human-in-the-Loop?âHuman-in-the-Loop (HITL) is a pattern where AI agents pause execution to gather user input, ask clarifying questions, or request approval before proceeding. This enables agents to handle ambiguous situations, make better decisions, and provide safety guardrails for sensitive operations.
Core Concept
Section titled âCore Conceptâgraph LR A[User Request] --> B[Agent Processing] B --> C{Need Input?} C -->|Yes| D[Pause & Ask User] C -->|No| E[Continue] D --> F[User Responds] F --> E E --> G{Complete?} G -->|Yes| H[Final Result] G -->|No| BWhy Human-in-the-Loop?
Section titled âWhy Human-in-the-Loop?âCommon Use Cases
Section titled âCommon Use Casesâ-
Clarifying Requirements
- âWhich database should I use?â
- âWhat authentication method do you prefer?â
- âShould this be a REST API or GraphQL?â
-
Approval Workflows
- âDeploy to production? (impacts 10,000 users)â
- âDelete all test data?â
- âSend email to 5,000 customers?â
-
Configuration Gathering
- âWhich features do you want to enable?â
- âSelect deployment regionsâ
- âChoose dependencies to installâ
-
Decision Making
- âMultiple approaches found. Which one?â
- âTrade-off between speed and accuracy. Your preference?â
- âConflicting requirements detected. How to resolve?â
Interaction Flow
Section titled âInteraction FlowâsequenceDiagram participant U as User participant A as Agent participant T as Tools
U->>A: "Add authentication to my app"
Note over A: Need to clarify approach A->>A: Generate question tool call A->>U: "Which auth method?"<br/>â OAuth 2.0<br/>â JWT<br/>â Session
U->>A: "OAuth 2.0"
Note over A: Now have enough info A->>T: read_config T-->>A: Config data A->>T: write_code T-->>A: Code written
A->>U: "OAuth 2.0 auth implemented"Key Characteristics
Section titled âKey Characteristicsââ Benefits
Section titled ââ Benefitsâ- Better Decisions: Agent gets clarification instead of guessing
- Safety: Approval required for risky operations
- Flexibility: Adapts to user preferences
- Transparency: User sees and controls agent actions
- Error Prevention: Catches misunderstandings early
â ď¸ Challenges
Section titled ââ ď¸ Challengesâ- Interrupts Flow: Breaks autonomous operation
- Requires UI: Need to present questions to users
- State Management: Must preserve context during pauses
- Complexity: More moving parts than autonomous agents
Architecture Overview
Section titled âArchitecture Overviewâgraph TB subgraph UserLayer["User Interface"] CLI[CLI Interface] Web[Web Interface] Mobile[Mobile App] end
subgraph AppLayer["Application Layer"] Agent[Agent Controller] State[State Manager] end
subgraph LLMLayer["LLM Layer"] LLM[Language Model] Tools[Tool Definitions] end
UserLayer --> Agent Agent --> LLM LLM --> Tools Tools --> Agent Agent --> State State --> UserLayerHow It Works: Technical View
Section titled âHow It Works: Technical Viewâ1. Tool Calling Mechanism
Section titled â1. Tool Calling MechanismâModern LLMs can generate structured JSON for âtool callsâ - predefined functions the model can invoke:
{ "tool_call": { "name": "ask_user_question", "arguments": { "question": "Which database?", "options": ["PostgreSQL", "MongoDB", "Redis"] } }}2. Execution Pause
Section titled â2. Execution PauseâWhen the agent generates an ask_user_question tool call:
stateDiagram-v2 [*] --> Processing Processing --> ToolCall: Needs input ToolCall --> Paused: Generate question Paused --> WaitUser: Display UI WaitUser --> Resume: User answers Resume --> Processing: Continue with answer Processing --> [*]: Complete3. State Preservation
Section titled â3. State PreservationâThe application must maintain conversation state:
messages = [ SystemMessage("You are a helpful assistant"), HumanMessage("Add authentication"), AIMessage("I need to know which method"), AIMessage(tool_calls=[...]), # â Paused here # User answers... ToolMessage("OAuth 2.0"), # â Answer added AIMessage("Implementing OAuth...") # â Resumed]Provider Comparison
Section titled âProvider ComparisonâDifferent LLM providers handle HITL differently:
graph TB subgraph Claude["Claude Code"] CC[Built-in<br/>AskUserQuestion] CC --> CU[Automatic UI<br/>Rendering] end
subgraph OpenAI["OpenAI"] OF[Function<br/>Calling] OA[Agents SDK<br/>needsApproval] OF --> OM[Manual<br/>Implementation] OA --> OAuto[Semi-Automatic] end
subgraph Agnostic["Model Agnostic"] LC[LangChain] OR[OpenRouter] LC --> Custom[Custom Tool<br/>+ UI Handler] OR --> Custom endKey Differences
Section titled âKey Differencesâ| Aspect | Claude Code | OpenAI | Model Agnostic |
|---|---|---|---|
| Built-in Tool | â
AskUserQuestion | â Must define | â Must define |
| UI Rendering | â Automatic | â ď¸ SDK helps | â Manual |
| Approval Flow | Manual | â
needsApproval | â Manual |
| Flexibility | â CLI only | â ď¸ Limited | â Full control |
| Complexity | Low | Medium | High |
Implementation Approaches
Section titled âImplementation ApproachesâApproach 1: Built-in Tool (Claude Code)
Section titled âApproach 1: Built-in Tool (Claude Code)âPros:
- Dead simple for developers
- Integrated UI
- Zero configuration
Cons:
- Claude Code specific
- Limited to terminal UI
- No customization
Approach 2: Provider SDK (OpenAI Agents)
Section titled âApproach 2: Provider SDK (OpenAI Agents)âPros:
- Standardized approval flow
- Provider-supported
- Good documentation
Cons:
- Provider-specific
- Limited UI options
- SDK dependency
Approach 3: Custom Implementation (Model Agnostic)
Section titled âApproach 3: Custom Implementation (Model Agnostic)âPros:
- Works with any LLM
- Full UI control (CLI, web, mobile)
- Maximum flexibility
Cons:
- Most code to write
- Manual state management
- Complex architecture
Workflow Patterns
Section titled âWorkflow PatternsâPattern 1: Sequential Questions
Section titled âPattern 1: Sequential QuestionsâAsk questions one at a time:
sequenceDiagram User->>Agent: Setup database Agent->>User: Q1: Which engine? User->>Agent: PostgreSQL Agent->>User: Q2: Enable backups? User->>Agent: Yes Agent->>User: Setup completePattern 2: Conditional Branching
Section titled âPattern 2: Conditional BranchingâFollow-up questions based on answers:
flowchart TD A[Start] --> B{Project Type?} B -->|Web API| C[Q: REST or GraphQL?] B -->|Full Stack| D[Q: Frontend Framework?] B -->|Microservices| E[Q: Orchestration?] C --> F[Implement] D --> F E --> FPattern 3: Approval Gates
Section titled âPattern 3: Approval GatesâRequire confirmation for sensitive operations:
sequenceDiagram Agent->>Agent: Prepare to delete data Agent->>User: â ď¸ Delete 10,000 records? alt Approved User->>Agent: Yes, proceed Agent->>DB: DELETE else Rejected User->>Agent: No, cancel Agent->>User: Operation cancelled endWhen to Use HITL
Section titled âWhen to Use HITLâ| â Use HITL When⌠| â Skip HITL When⌠|
|---|---|
| Multiple valid approaches exist | Requirements are crystal clear |
| User preferences matter | Fully automated workflows |
| Operations are risky/irreversible | Simple, safe operations |
| Requirements are ambiguous | User is not available |
| Configuration is complex | Batch processing |
Best Practices
Section titled âBest Practicesâ1. Design Clear Questions
Section titled â1. Design Clear QuestionsâBad:
Question: "Choose one"Options: ["A", "B", "C"]Good:
Question: "Which database engine should we use?"Options: [ "PostgreSQL - Relational, ACID compliant, complex queries", "MongoDB - Document store, flexible schema, horizontal scaling", "Redis - In-memory, microsecond latency, caching"]2. Limit Options
Section titled â2. Limit Optionsâ- 2-4 options per question (5+ is overwhelming)
- Use progressive disclosure for complex choices
- Group related options
3. Provide Context
Section titled â3. Provide Contextâ- Explain why youâre asking
- Describe implications of each choice
- Mention trade-offs when relevant
4. Handle Errors Gracefully
Section titled â4. Handle Errors Gracefullyâ- Validate user input
- Allow âcancelâ or âbackâ
- Provide defaults when appropriate
- Log all interactions
Constraints & Considerations
Section titled âConstraints & ConsiderationsâTechnical Constraints
Section titled âTechnical Constraintsâ- Tool Calling Variability: Not all models have equal tool-calling capabilities
- No Universal Standard: Each provider implements HITL differently
- State Management: Application must handle conversation pauses
- UI Separation: Rendering logic separate from agent logic
Design Constraints
Section titled âDesign Constraintsâ- Response Time: Users expect sub-second UI rendering
- Context Preservation: Must not lose conversation history
- Error Recovery: Handle network issues, timeouts
- Multi-turn Support: Questions may lead to more questions
UX Constraints
Section titled âUX Constraintsâ- Cognitive Load: Too many questions = poor UX
- Progressive Disclosure: Start simple, get detailed only when needed
- Clear Actions: Users must understand what will happen
- Undo/Cancel: Provide escape hatches
Implementation Roadmap
Section titled âImplementation Roadmapâgraph TD A[Choose Approach] --> B{Requirements?} B -->|Claude Only| C[Use Claude Code<br/>AskUserQuestion] B -->|Multiple Models| D[Model Agnostic] B -->|OpenAI + Approvals| E[OpenAI Agents SDK]
C --> F[Implement] E --> F
D --> G[Choose UI] G --> H[CLI / Web / Mobile] H --> I[Implement Tool] I --> J[Build UI Handler] J --> K[State Manager] K --> F
F --> L[Test & Deploy]Next Steps
Section titled âNext StepsâExplore specific implementations:
- Claude Code Implementation - Built-in
AskUserQuestiontool - OpenAI Implementation - Function calling & Agents SDK
- Model Agnostic Implementation - LangChain + OpenRouter approach