Production-grade guide to agent human in loop covering architecture patterns, implementation strategies, testing approaches, and operational best practices for enterprise engineering teams.
Agent Human In Loop is a pattern for integrating human judgment into the execution of an AI agent. It matters when an agent’s decision requires nuanced, domain-specific, or subjective evaluation—such as approving a loan, curating content, validating a model prediction, or resolving an ambiguous customer query—where automation alone is insufficient. The agent delegates steps to humans, waits for responses, and resumes execution with human input.
The core workflow begins when an agent reaches a decision point requiring human input. It creates a *task*—a structured request with context, data, and instructions—and sends it to a human-facing system. The human reviews, edits, and submits a response. The agent resumes execution using that input. This loop is asynchronous: the agent may progress through other tasks while waiting.
The agent must manage:
Tasks are structured as JSON objects with required fields: id, type, status, created_at, assigned_to, payload, instructions, and due_at. The payload contains the data to be evaluated.
An agent creates a task by sending a POST /tasks request to a human-in-loop service. The service assigns a globally unique id, sets status: "pending", and returns the task.
POST /tasks HTTP/1.1
Host: human-loop.example.com
Content-Type: application/json
Authorization: Bearer agent-token-123
{
"type": "loan-approval",
"instructions": "Review the applicant’s credit history and income stability. Approve if credit score ≥ 650 and employment duration ≥ 2 years.",
"payload": {
"applicant_id": "app-789",
"full_name": "Sarah Chen",
"credit_score": 720,
"employment_duration_months": 36,
"monthly_income": 6800,
"employment_history": [
{ "company": "TechCorp", "start": "2020-01-01", "end": "2023-12-31" },
{ "company": "InnovateX", "start": "2018-03-15", "end": "2019-12-31" }
],
"documents": [
{ "type": "paystub", "url": "https://storage.example.com/docs/paystub-123.pdf" },
{ "type": "credit-report", "url": "https://storage.example.com/docs/credit-456.pdf" }
]
},
"due_at": "2025-04-10T12:00:00Z"
}
Response:
{
"id": "task-1042",
"type": "loan-approval",
"status": "pending",
"created_at": "2025-04-05T09:15:30Z",
"assigned_to": null,
"due_at": "2025-04-10T12:00:00Z",
"payload": { ... },
"instructions": "Review the applicant’s credit history and income stability...",
"updated_at": "2025-04-05T09:15:30Z"
}
The agent stores task-1042 and proceeds to the next step. The task is now visible to humans via a dashboard, email, or Slack.
A task can be assigned to a specific human or a team. Assignment happens via a PATCH /tasks/{id} request.
PATCH /tasks/task-1042 HTTP/1.1
Host: human-loop.example.com
Content-Type: application/json
Authorization: Bearer admin-token-456
{
"assigned_to": "[email protected]",
"priority": "high"
}
The system updates the task and triggers a notification: email, Slack message, or in-app alert. The human sees the task with the full payload and instructions.
A human submits a response by sending a PUT /tasks/{id}/response request. The response is a JSON object with decision, comments, and optional metadata.
PUT /tasks/task-1042/response HTTP/1.1
Host: human-loop.example.com
Content-Type: application/json
Authorization: Bearer james-token-789
{
"decision": "approved",
"comments": "Credit score is strong, but employment history shows a gap. Recommend 3% interest rate.",
"metadata": {
"review_time_minutes": 12,
"approver_id": "[email protected]",
"tools_used": ["PDF Viewer", "Spreadsheet", "Email"]
}
}
The service sets status: "completed" and updated_at. The agent can now resume execution using the response.
The agent polls the service for completed tasks using GET /tasks?status=completed&since=2025-04-05T09:00:00Z.
GET /tasks?status=completed&since=2025-04-05T09:00:00Z HTTP/1.1
Host: human-loop.example.com
Authorization: Bearer agent-token-123
Response:
[
{
"id": "task-1042",
"type": "loan-approval",
"status": "completed",
"created_at": "2025-04-05T09:15:30Z",
"updated_at": "2025-04-06T14:23:11Z",
"assigned_to": "[email protected]",
"payload": { ... },
"response": {
"decision": "approved",
"comments": "Credit score is strong...",
"metadata": { ... }
}
}
]
The agent extracts the decision and comments, and continues the workflow.
For example, in code:
def process_loan_application(applicant_id):
task = create_task(
type="loan-approval",
instructions="Review credit and income stability...",
payload=fetch_applicant_data(applicant_id),
due_at=datetime.now(timezone.utc) + timedelta(days=5)
)
# Agent continues processing other applicants
# Later, fetch completed tasks
completed_tasks = poll_tasks(status="completed", since=datetime.now() - timedelta(hours=1))
for task in completed_tasks:
if task["type"] == "loan-approval":
decision = task["response"]["decision"]
comments = task["response"]["comments"]
if decision == "approved":
grant_loan(applicant_id, interest_rate=0.03)
elif decision == "rejected":
send_rejection_email(applicant_id, comments)
elif decision == "pending_review":
escalate_to_manager(task["id"])
update_task_status(task["id"], "processed")
Tasks can transition through several states: pending, assigned, in_progress, completed, failed, overdue.
pending: created, not assignedassigned: assigned to a human, not yet startedin_progress: human has begun work; may be updated mid-processcompleted: response submittedfailed: human response invalid or missingoverdue: due_at passed, no responseA task fails if the response does not meet schema requirements. The service validates the response against a JSON Schema.
Example schema:
{
"type": "object",
"required": ["decision"],
"properties": {
"decision": { "enum": ["approved", "rejected", "pending_review"] },
"comments": { "type": "string", "maxLength": 1000 },
"metadata": {
"type": "object",
"properties": {
"review_time_minutes": { "type": "number", "minimum": 1 },
"approver_id": { "type": "string", "format": "email" }
},
"required": ["review_time_minutes", "approver_id"]
}
}
}
If a human submits:
{
"decision": "approved",
"comments": "Good applicant.",
"metadata": {
"review_time_minutes": 5.5,
"approver_id": "[email protected]"
}
}
The service accepts it. But if the metadata.approver_id is malformed:
{
"decision": "approved",
"comments": "Good applicant.",
"metadata": {
"review_time_minutes": 5.5,
"approver_id": "james.lopezcompany.com"
}
}
The service returns a 400 Bad Request with:
{
"error": "validation_failed",
"details": [
{
"field": "metadata.approver_id",
"message": "Invalid email format",
"expected": "email",
"actual": "james.lopezcompany.com"
}
]
}
The agent can retry the task or escalate it.
The agent may set a timeout on a task. If the human does not respond by due_at, the task becomes overdue. The agent can trigger alerts or auto-decisions.
Example: auto-decision on overdue tasks.
GET /tasks?status=overdue&due_before=2025-04-05T00:00:00Z HTTP/1.1
Host: human-loop.example.com
Authorization: Bearer agent-token-123
Response:
[
{
"id": "task-1042",
"type": "loan-approval",
"status": "overdue",
"due_at": "2025-04-10T12:00:00Z",
"assigned_to": "[email protected]",
"payload": { ... },
"overdue_since": "2025-04-10T14:00:00Z"
}
]
The agent can then:
For instance, if no human response by 2025-04-15T12:00:00Z, the agent applies a default decision:
def auto_decision(task):
default = {
"approved": {"interest_rate": 0.04, "notes": "Automatic decision due to delay"},
"rejected": {"reason": "No response within 5 days", "notes": "Auto-rejection"}
}
if task["payload"]["credit_score"] >= 650:
task["response"] = default["approved"]
else:
task["response"] = default["rejected"]
update_task_status(task["id"], "completed", auto_decided=True)
unique_id field in the payload and deduplicate on POST /tasks.decision field is missing. The service returns a 400 with a detailed validation error, but the agent assumes decision: "approved" by default. This leads to silent errors: a task is marked completed, but the agent applies the wrong logic.due_at is in UTC, but humans in Europe see it as 4 PM, while the agent expects 12 PM. The agent must normalize timestamps consistently.ETag or version fields in the task object.in_progress tasks with no updates in the last 48 hours and send a reminder.503 Service Unavailable on PUT /tasks/{id}/response may cause a task to be marked failed and re-queued.metadata field is not just for the human but also for the agent and system. The agent can use it to optimize workflows, such as:tools_used to recommend future trainingreview_time_minutes to calculate SLA complianceapprover_id to route future tasks to the same personThe agent can monitor the human-in-loop system using standard tools.
List all overdue tasks:
curl -H "Authorization: Bearer agent-token-123" \
"https://human-loop.example.com/tasks?status=overdue&due_before=2025-04-05T00:00:00Z" \
| jq '.[] | {id, due_at, assigned_to, payload.applicant_id}'
Check task status history:
SELECT task_id, status, updated_at, changed_by
FROM task_history
WHERE task_id = 'task-1042'
ORDER BY updated_at;
Log task creation and resolution:
[2025-04-05T09:15:30Z] INFO: Created task task-1042 (loan-approval)
[2025-04-06T14:23:11Z] INFO: Task task-1042 completed: decision=approved, comments="Credit score is strong..."
[2025-04-10T15:00:00Z] WARNING: Task task-1042 overdue; [email protected]
[2025-04-12T09:30:00Z] INFO: Auto-decision applied: interest_rate=0.04
The human-in-loop pattern enables AI agents to handle complex, judgment-driven tasks. The agent creates tasks with clear payloads and instructions, assigns them, and resumes execution after human input. The system validates responses, tracks state, and supports asynchronous workflows. Failure modes—missing fields, timezones, concurrent updates, and timeouts—are common and must be handled explicitly. The agent must not assume correctness but actively monitor, retry, and adapt to human behavior. This pattern is essential when automation requires human wisdom.
This page was rewritten on 13 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.
We use cookies for analytics (Google Analytics) and advertising (Google AdSense) to improve your experience and support free content. Privacy Policy