Getting Started
Get up and running with Human-in-the-Loop (HITL) in langcrew in just 5 minutes.
Installation
Section titled “Installation”HITL is included with langcrew - no additional installation required:
uv add langcrew --prerelease=allowBasic Example
Section titled “Basic Example”Here’s a simple example that requires approval before writing files:
from langcrew import Agent, Crewfrom langcrew.hitl import HITLConfigfrom langcrew_tools import FileWriteTool
# Configure HITL for file operationshitl_config = HITLConfig( interrupt_before_tools=["file_write"])
# Create agent with HITLagent = Agent( role="File Manager", goal="Manage files with human approval", backstory="Careful file manager who asks before writing", tools=[FileWriteTool()], hitl=hitl_config)
# Create crewcrew = Crew(agents=[agent])
# This will pause for approval when trying to write filesresult = crew.kickoff(inputs={"task": "Create a summary file"})What Happens
Section titled “What Happens”- Agent starts: The agent receives the task
- Tool call: Agent tries to use
file_writetool - Interrupt: System pauses and asks for approval
- User response: You approve or deny the operation
- Continuation: Agent continues based on your decision
User Responses
Section titled “User Responses”# Simple responses that work:"yes" # Approve"no" # Deny"approve" # Approve"deny" # DenyTrue # ApproveFalse # Deny"批准" # Approve (Chinese)"拒绝" # Deny (Chinese)# Modify parameters before execution{ "approved": True, "modified_args": { "file_path": "/tmp/better_filename.txt", "content": "Enhanced content with user input" }}Common Patterns
Section titled “Common Patterns”Approve Dangerous Operations
Section titled “Approve Dangerous Operations”from langcrew.hitl import HITLConfig
# Require approval for potentially dangerous toolshitl_config = HITLConfig( interrupt_before_tools=[ "file_delete", "database_write", "send_email", "external_api" ])Review Generated Content
Section titled “Review Generated Content”# Review results after content generationhitl_config = HITLConfig( interrupt_after_tools=[ "content_generator", "report_writer", "data_analyzer" ])Crew-Level Policy
Section titled “Crew-Level Policy”# Apply HITL policy to entire crewcrew = Crew( agents=[researcher, writer, reviewer], hitl=HITLConfig( interrupt_before_tools=["web_search", "file_write"] ))Testing Your Setup
Section titled “Testing Your Setup”Run the Complete Example
Section titled “Run the Complete Example”The easiest way to test HITL is to run our comprehensive example:
# Set your OpenAI API keyexport OPENAI_API_KEY=your_api_key_here
# Run from the HITL example directorycd examples/components/hitluv run hitl_example.pyThis example demonstrates:
- 9 different HITL configuration patterns
- Tool approval workflows with multi-language support
- Parameter and result modification
- Complete interrupt/resume cycles
More Examples
Section titled “More Examples”For additional HITL examples and patterns, check out:
examples/components/hitl/- Complete HITL implementation with 9 configuration patternsexamples/components/hitl/README.md- Detailed setup and usage instructionsexamples/components/hitl/hitl_example.py- Full runnable example (600+ lines)
Navigate to these files in your local copy or view them in the repository to explore different HITL scenarios.
Simple Test Code
Section titled “Simple Test Code”For a minimal test, you can also try:
from langcrew import Agentfrom langcrew.hitl import HITLConfigfrom langcrew_tools import FileWriteTool
# Simple test agenttest_agent = Agent( role="File Tester", goal="Test HITL file operations", backstory="Testing agent for file operations", tools=[FileWriteTool()], hitl=HITLConfig(interrupt_before_tools=["file_write"]))
# This should trigger an interrupt when trying to write filescrew = Crew(agents=[test_agent])result = crew.kickoff(inputs={"task": "Create a test file"})Troubleshooting
Section titled “Troubleshooting”Interrupts Not Triggering
Section titled “Interrupts Not Triggering”Check that tool names match exactly:
# ❌ Wrong - tool name mismatchhitl_config = HITLConfig(interrupt_before_tools=["file_writer"])agent = Agent(tools=[FileWriteTool()]) # Tool name is "file_write"
# ✅ Correct - exact matchhitl_config = HITLConfig(interrupt_before_tools=["file_write"])Import Errors
Section titled “Import Errors”Make sure you’re importing from the correct modules:
# Core HITL configurationfrom langcrew.hitl import HITLConfig
# User input toolfrom langcrew_tools.hitl import UserInputToolNeed help? Check the configuration guide or ask in the community forum.