LangChain Human-in-the-Loop Tools: Complete Integration Guide
LangChain agents are powerful, but they hit walls when they encounter human verification. A CAPTCHA, an SMS code, a phone call requirement — these break automation. The solution is human-in-the-loop tools: custom LangChain tools that delegate to humans for specific tasks and return results back to the agent.
This guide walks you through implementing human-in-the-loop capabilities in a LangChain agent, step by step, with complete working code. By the end, you'll have an agent that can browse the web, solve CAPTCHAs, relay OTPs, and continue its workflow without getting blocked.
Why Human-in-the-Loop?
Autonomous agents need to handle tasks that require human judgment or human input. Examples:
CAPTCHAs: Vision models fail on semantic challenges. A human sees "select traffic lights" and knows what counts. An AI often guesses wrong.
OTP relay: SMS codes are time-limited and device-locked. Only a human with access to the phone can retrieve them.
Phone verification: Some sites require actual phone calls. No API does this automatically.
KYC documents: Banks require identity verification. Requires human judgment on document authenticity.
Instead of failing silently or retrying infinitely, your agent should detect these gates, delegate to a human, wait for the result, and continue. This is what human-in-the-loop means.
Installation and Setup
First, install LangChain and SiliconBridge:
pip install langchain langchain-openai siliconbridge
Get a free API key (no email, no signup):
curl -X POST https://siliconbridge.xyz/api/signup/wallet \
-H "Content-Type: application/json" \
-d '{"wallet_address": "YOUR_ETH_ADDRESS"}'
You'll get an API key immediately. No credit card required. The free tier includes 100 tasks per day.
Building Human-in-the-Loop Tools
LangChain tools are Python functions decorated with @tool. Here's how to build custom tools that integrate SiliconBridge:
from langchain.tools import tool
from siliconbridge import SiliconBridge
import json
# Initialize SiliconBridge client
sb_client = SiliconBridge(api_key="your_api_key")
@tool
def solve_captcha(image_url: str, context: str = "") -> str:
"""
Solve a CAPTCHA image in real-time using human operators.
Args:
image_url: URL of the CAPTCHA image
context: Optional context (e.g., "signup form", "login page")
Returns:
The CAPTCHA solution string
"""
try:
result = sb_client.solve_captcha(
image_url=image_url,
context={"task": context}
)
return f"CAPTCHA solved: {result['solution']}"
except Exception as e:
return f"Failed to solve CAPTCHA: {str(e)}"
@tool
def relay_otp(phone_number: str, service: str = "sms") -> str:
"""
Relay an OTP code sent to a phone number.
Args:
phone_number: Phone number in E.164 format (e.g., "+1234567890")
service: "sms", "email", or "call"
Returns:
The OTP code
"""
try:
result = sb_client.relay_otp(
phone_number=phone_number,
service_type=service
)
return f"OTP received: {result['code']}"
except Exception as e:
return f"Failed to relay OTP: {str(e)}"
@tool
def web_browse(url: str, instructions: str) -> str:
"""
Browse a website with human assistance for complex interactions.
Args:
url: URL to browse
instructions: What the human should do on the page
Returns:
The resulting HTML or screenshot
"""
try:
result = sb_client.web_browse(
url=url,
instructions=instructions,
take_screenshot=True
)
return f"Browsing complete. Result:\n{result['html'][:500]}..."
except Exception as e:
return f"Failed to browse: {str(e)}"
# Create tools list for LangChain agent
human_tools = [solve_captcha, relay_otp, web_browse]
Building a Complete Agent
Now let's build a complete LangChain agent that uses these human-in-the-loop tools:
from langchain.agents import create_react_agent, AgentExecutor
from langchain_openai import ChatOpenAI
from langchain import hub
# Initialize LLM
llm = ChatOpenAI(model="gpt-4", temperature=0)
# Get the standard ReAct prompt
prompt = hub.pull("hwchase17/react")
# Create agent
agent = create_react_agent(llm, human_tools, prompt)
# Create executor with verbose output
executor = AgentExecutor.from_agent_and_tools(
agent=agent,
tools=human_tools,
verbose=True,
max_iterations=10,
early_stopping_method="force"
)
# Run the agent on a task
result = executor.invoke({
"input": """
Sign up for an account at example.com.
The form requires:
1. Email: test@example.com
2. Password: SecurePass123
3. Solve the CAPTCHA when prompted
4. Enter the verification code sent to +1234567890
5. Complete the signup
"""
})
print("Agent result:", result["output"])
Real-World Example: Multi-Step Signup
Here's a complete example of an agent signing up for a service with CAPTCHAs and OTP verification:
from langchain.agents import create_react_agent, AgentExecutor
from langchain_openai import ChatOpenAI
from langchain.tools import tool
from siliconbridge import SiliconBridge
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
sb = SiliconBridge(api_key="your_api_key")
@tool
def fill_signup_form(email: str, password: str) -> str:
"""Fill the signup form with email and password."""
driver = webdriver.Chrome()
try:
driver.get("https://example.com/signup")
# Fill email
email_field = driver.find_element(By.NAME, "email")
email_field.send_keys(email)
# Fill password
password_field = driver.find_element(By.NAME, "password")
password_field.send_keys(password)
# Scroll to CAPTCHA
driver.execute_script("window.scrollBy(0, 500)")
time.sleep(2)
return "Form filled. CAPTCHA visible."
finally:
return "Form filled successfully"
@tool
def solve_and_submit_captcha() -> str:
"""Detect CAPTCHA, solve it, and submit the form."""
driver = webdriver.Chrome()
driver.get("https://example.com/signup")
# Find CAPTCHA image
captcha_img = driver.find_element(By.XPATH, "//img[@alt='captcha']")
captcha_src = captcha_img.get_attribute("src")
# Solve via SiliconBridge
result = sb.solve_captcha(image_url=captcha_src)
# Inject solution
captcha_input = driver.find_element(By.ID, "captcha-input")
captcha_input.send_keys(result['solution'])
# Submit
submit_btn = driver.find_element(By.ID, "submit")
submit_btn.click()
time.sleep(2)
return "CAPTCHA solved and form submitted"
@tool
def verify_otp(phone: str) -> str:
"""Wait for and relay the OTP."""
result = sb.relay_otp(phone_number=phone)
driver = webdriver.Chrome()
driver.get("https://example.com/verify")
otp_input = driver.find_element(By.ID, "otp-code")
otp_input.send_keys(result['code'])
verify_btn = driver.find_element(By.ID, "verify-btn")
verify_btn.click()
return "OTP verified. Signup complete!"
# Create agent
llm = ChatOpenAI(model="gpt-4")
tools = [fill_signup_form, solve_and_submit_captcha, verify_otp]
prompt = hub.pull("hwchase17/react")
agent = create_react_agent(llm, tools, prompt)
executor = AgentExecutor.from_agent_and_tools(
agent=agent,
tools=tools,
verbose=True
)
# Execute
result = executor.invoke({
"input": "Sign up for example.com with email test@example.com, password SecurePass123, and phone +1234567890"
})
print(result["output"])
Using Built-in SiliconBridge Tool Integration
For convenience, SiliconBridge provides pre-built LangChain tool integration:
from siliconbridge.integrations.langchain_tool import get_siliconbridge_tools
from langchain.agents import create_react_agent, AgentExecutor
from langchain_openai import ChatOpenAI
from langchain import hub
# Get all SiliconBridge tools
sb_tools = get_siliconbridge_tools(api_key="your_api_key")
# Create agent
llm = ChatOpenAI(model="gpt-4")
prompt = hub.pull("hwchase17/react")
agent = create_react_agent(llm, sb_tools, prompt)
executor = AgentExecutor.from_agent_and_tools(
agent=agent,
tools=sb_tools,
verbose=True
)
# Your agent now has:
# - solve_captcha
# - relay_otp
# - phone_verify
# - web_browse
# - kyc_verify
# Run any task
result = executor.invoke({
"input": "Sign up for the service at example.com with my email and verify my phone"
})
print(result["output"])
Error Handling and Timeouts
Human-in-the-loop tasks take time (30–120 seconds). Handle timeouts gracefully:
from langchain.tools import tool
from siliconbridge import SiliconBridge
import time
sb = SiliconBridge(api_key="your_api_key")
@tool
def solve_captcha_with_timeout(image_url: str, timeout_seconds: int = 180) -> str:
"""Solve CAPTCHA with proper error handling."""
try:
start_time = time.time()
result = sb.solve_captcha(
image_url=image_url,
timeout=timeout_seconds
)
elapsed = time.time() - start_time
return f"CAPTCHA solved in {elapsed:.1f}s: {result['solution']}"
except TimeoutError:
return "ERROR: CAPTCHA solving timed out. The human operator may be unavailable."
except Exception as e:
return f"ERROR: {str(e)}"
Monitoring and Logging
Log all human-in-the-loop interactions for debugging and cost tracking:
import logging
from langchain.tools import tool
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@tool
def solve_captcha(image_url: str) -> str:
"""Solve CAPTCHA with logging."""
logger.info(f"[CAPTCHA] Solving: {image_url}")
result = sb.solve_captcha(image_url=image_url)
logger.info(f"[CAPTCHA] Success in {result['time_seconds']}s")
logger.info(f"[COST] $0.50 charged for CAPTCHA")
return f"Solution: {result['solution']}"
For More Advanced Workflows
Check our guide on CrewAI tools for 2026 for multi-agent orchestration. Also see our CAPTCHA solving API guide for deeper integration patterns. For automated tool discovery, read MCP tool discovery and llms.txt.
Best Practices
Detect gates early: Check for "CAPTCHA", "verify", "confirm" keywords in HTML before the agent tries to interact. Proactively call human tools instead of waiting for failure.
Bundle related tasks: Instead of calling solve_captcha and relay_otp separately, combine them into a single web_browse task. Cheaper and faster.
Set appropriate timeouts: 30–120 seconds for most tasks. 5 minutes maximum. If you don't get a result, something's broken.
Monitor costs: CAPTCHA solving is $0.50 per task. OTP relay is $1.00. Web browsing is $2–5. Budget accordingly for your agent.
Test with real gateways: Try your agent against actual reCAPTCHA, hCaptcha, and CloudFlare challenges. See our templates page for pre-built, tested workflows.
Conclusion
Human-in-the-loop tools turn LangChain agents from automation toys into reliable systems. When your agent hits a human-gated checkpoint, it delegates to a real person, waits for the result, and continues. No failed retries, no silent failures, no manual intervention.
Start with $10 free credits. Build your first human-in-the-loop agent in 15 minutes. See the SiliconBridge homepage for getting started, or explore our comparison guide to see how we stack up against alternatives.