| title | Human-in-the-Loop (HITL) Workflows |
|---|---|
| description | Pause AI tool execution for user approval before risky operations like file deletion or API calls |
| keywords | hitl, human in the loop, tool confirmation, safety, approval workflow, user consent |
Since: v7.39.0 | Status: Stable | Availability: SDK
What it does: HITL pauses AI tool execution to request explicit user approval before performing risky operations like deleting files, modifying databases, or making expensive API calls.
Why use it: Prevent costly mistakes and give users control over potentially dangerous AI actions. Think of it as an "Are you sure?" dialog for AI assistant operations.
:::warning[Security Best Practice] Only use HITL for truly risky operations. Overusing confirmation prompts degrades user experience and can lead to "confirmation fatigue" where users approve actions without reading them. :::
Common use cases:
- File deletion or modification operations
- Database write/delete operations
- Expensive third-party API calls
- Irreversible actions (sending emails, posting to social media)
- Operations accessing sensitive data
import { NeuroLink } from "@juspay/neurolink";
const neurolink = new NeuroLink({
tools: [
{
name: "deleteFile", // (1)!
description: "Deletes a file from the filesystem", // (2)!
requiresConfirmation: true, // (3)!
execute: async (args) => {
// (4)!
// Your deletion logic
},
},
],
});
// When AI tries to use deleteFile:
// 1. Tool execution pauses; the tool has not run
// 2. hitl:confirmation-request is emitted
// 3. Application shows confirmation dialog
// 4. On approval (hitl:confirmation-response), the paused call runs; the approval
// covers that one call, including any retry of a failed attempt- Tool identifier used by the AI to invoke this function
- Describes tool purpose to the LLM for proper selection
- Triggers HITL checkpoint before execution
- Actual implementation only runs after user approval
HITL uses an event-based workflow where the SDK emits confirmation requests and your app responds with user decisions.
import { NeuroLink } from "@juspay/neurolink";
const neurolink = new NeuroLink({
hitl: {
enabled: true,
dangerousActions: ["delete", "remove", "drop", "truncate"],
timeout: 30000, // 30 seconds
},
});
// (1)! Reach the SDK's event emitter. NeuroLink does not extend EventEmitter,
// so there is no neurolink.on() / neurolink.emit().
const emitter = neurolink.getEventEmitter();
// (2)! Listen for confirmation requests
emitter.on("hitl:confirmation-request", async (event) => {
const receivedAt = Date.now();
const {
confirmationId,
toolName,
arguments: args,
timeoutMs,
} = event.payload;
// (3)! Show your app's confirmation UI
const approved = await showConfirmationDialog({
action: toolName,
details: args,
message: `AI wants to ${toolName}. Allow?`,
timeoutMs,
});
// (4)! Send response back to NeuroLink
emitter.emit("hitl:confirmation-response", {
type: "hitl:confirmation-response",
payload: {
confirmationId, // Must match the request
approved, // (5)! User decision
reason: approved ? undefined : "User denied permission",
metadata: {
timestamp: new Date().toISOString(),
responseTime: Date.now() - receivedAt, // Milliseconds since the request arrived
},
},
});
});
// (6)! Handle confirmation timeouts
emitter.on("hitl:timeout", (event) => {
console.warn(`Confirmation timed out for ${event.payload.toolName}`);
});- Get the event emitter -
NeuroLinkis not itself an emitter, so requests and responses both go throughgetEventEmitter() - Subscribe to confirmation requests - NeuroLink emits one before running a tool that needs approval
- Show your confirmation UI with the tool name, arguments and remaining time
- Reply on the same emitter, echoing the request's
confirmationIdso the response is matched to its request - Approval decision determines if tool executes
- Optional: Handle cases where user doesn't respond in time
| Option | Type | Default | Required | Description |
|---|---|---|---|---|
requiresConfirmation |
boolean |
false |
No | Mark tool as requiring user approval |
const riskyTool = {
name: "sendEmail",
description: "Sends an email to a recipient",
requiresConfirmation: true, // Enable HITL
parameters: {
/* ... */
},
execute: async (args) => {
/* ... */
},
};- AI requests tool execution → The tool gateway checks whether the tool requires confirmation
- Confirmation required? → The call pauses and
hitl:confirmation-requestis emitted; the tool does not run yet and the model is not asked to retry - Your approval UI responds by emitting
hitl:confirmation-responsewith theconfirmationId:- Approve → the paused call resumes and runs (optionally with
modifiedArguments); the approval covers that one call, so a retry of a failed or timed-out attempt runs the tool again without a new approval - Deny → the call returns a refusal to the model and the tool never runs
- No answer → the confirmation times out (a refusal, unless auto-approve on timeout is configured)
- Approve → the paused call resumes and runs (optionally with
- Cancelled → aborting the call's
abortSignalwithdraws the pending confirmation and emitshitl:cancelled - Tool executes → Each approval covers exactly one call, including its retries; the next call asks again
- One-time permissions: Each approval works for exactly one action
- No reuse: AI cannot reuse old permissions for new actions
- Automatic reset: Permission flag clears immediately after use
- Fail-safe: Defaults to requiring permission when in doubt
HITL applies to the built-in tools (readFile, writeFile, listDirectory,
analyzeCSV, calculateMath, getCurrentTime, websearchGrounding and the
opt-in executeBashCommand) exactly as it applies to tools you register:
dangerousActions keywords and customRules are matched against the tool
name on every model-driven call. Before the 2026-10 security audit fix (finding
C3) built-in tools bypassed HITL entirely — only tools executed through the
tool registry were gated — so a
prompt-injected model could write files or, with the bash tool enabled, run
shell commands with no confirmation, while the bash tool's own description
claimed otherwise.
Two built-ins change the machine and are confirmed on every call while HITL
is enabled, whatever dangerousActions lists: writeFile and
executeBashCommand. To gate them by keywords only, like every other tool,
set confirmBuiltinTools: false:
const neurolink = new NeuroLink({
hitl: {
enabled: true,
dangerousActions: ["delete", "drop"],
confirmBuiltinTools: false, // writeFile / executeBashCommand follow the keywords
},
});A denied built-in call is reported to the model as a failed tool call
(Built-in tool execution rejected by user: <reason>); the model sees the denial and
can continue without the side effect. A confirmation that returns
modifiedArguments runs the tool with those arguments. The confirmation
request carries serverId: "direct" for a built-in tool.
Per-call tools passed through generate({ tools }) are the host's own code
and are not gated by HITL; wrap their execute yourself if they need it.
All HITL events ride the SDK emitter returned by
neurolink.getEventEmitter(); emitter below is that object.
Confirmation Request Event (hitl:confirmation-request):
emitter.on("hitl:confirmation-request", (event) => {
event.payload: {
confirmationId: string; // Unique ID for this request
toolName: string; // Tool requiring confirmation
arguments: unknown; // Tool parameters for review
actionType: string; // Human-readable description
timeoutMs: number; // Milliseconds until timeout
allowModification: boolean; // Hint for your UI; NeuroLink applies modifiedArguments either way
metadata: { ... } // Session/user context
}
});Confirmation Response (emit from your app):
emitter.emit("hitl:confirmation-response", {
type: "hitl:confirmation-response",
payload: {
confirmationId: string; // Must match request
approved: boolean; // User decision
reason?: string; // Rejection reason
modifiedArguments?: unknown; // User-edited args
metadata: {
timestamp: string;
responseTime: number;
}
}
});Timeout Event (hitl:timeout):
emitter.on("hitl:timeout", (event) => {
event.payload: {
confirmationId: string;
toolName: string;
timeout: number;
}
});Cancelled Event (hitl:cancelled): a pending confirmation was withdrawn
because the call that asked was cancelled (its abortSignal fired, for
example the turn was aborted). Retire the card in your UI; an answer sent for
it is ignored.
emitter.on("hitl:cancelled", (event) => {
event.payload: {
confirmationId: string;
toolName: string;
reason: string; // The abort reason, when the caller gave one
}
});A call that needs confirmation is confirmed once, before its attempt timeout,
its retries and the circuit breaker start, and nothing runs until the human
approves or the card times out under the rule below. A denial or a
cancellation is final: the call returns a refusal and is not retried. A card
timeout is also final, unless autoApproveOnTimeout is enabled and the tool
is not in autoApproveExclusions: then the timeout approves the call without
a human response, and it runs like any approved call. Once approved, a failed
or timed-out attempt is retried without a new approval, so a tool with side
effects can run more than once. A model loop's per-tool deadline does
not run while the card waits; approval resumes it with the time that was
left.
The rule applies to the tools of the instance and of its worker instances
(createWorkerInstance, and so runIsolatedAgent, registerAgentTool and
delegate_task): registered tools, external MCP tools and NeuroLink's
built-in tools, matched by name (for example readFile by
dangerousActions: ["read"]; writeFile and executeBashCommand are
confirmed on every call a model loop makes unless confirmBuiltinTools is
false, and on a direct executeTool() call only with confirmDirectToolCalls: true, see Built-in tools). Registered and built-in tools
are confirmed by the HITL manager the tool registry holds: the instance's own,
unless another manager was installed there
(getToolRegistry().setHITLManager(), or a worker with HITL of its own, see
below). External MCP tools are confirmed by the external server manager's: the
instance's own, or with none the registry's. A worker with no HITL of its own
shares its host's registry, so its calls are confirmed by the manager that
registry holds, normally the host's, and their cards reach that manager
owner's emitter. When an agent tool's call ends, through its attempt timeout
or the caller's abort, its worker run is stopped and any card it raised is
withdrawn. Agent-tool calls are never batched. Model calls NeuroLink makes for
its own text-only roles (evaluation, scoring, judging, guardrails checks,
summaries, memory condensation, RAG answers and reranking) are given no tools.
Not covered:
- tools passed per call in
options.tools, including the skill and RAG tools NeuroLink adds to a call: they are the caller's own code; - a worker built with
createWorkerInstance({ shareToolRegistry: false }): it does not share the host's registry, so the host's rule does not apply; - a worker with its own enabled HITL: its own rule and emitter apply, and it takes over the shared registry's manager, so from then on the registered and built-in tools of the host, and of every worker on that registry with no HITL of its own, are confirmed by that worker's manager, and their cards reach that worker's emitter.
Pass an abortSignal to executeTool to cancel a call; aborting it withdraws
a pending confirmation:
const controller = new AbortController();
// Abort once the card is pending: that withdraws it and emits hitl:cancelled
neurolink.getEventEmitter().once("hitl:confirmation-request", () => {
controller.abort();
});
const result = await neurolink.executeTool(
"deleteFile",
{ path: "/tmp/x" },
{
abortSignal: controller.signal,
},
);
// result is the { success: false } refusal; a later answer runs nothinghitl:cancelled is delivered after every hitl:confirmation-request listener
has run, so an abort raised inside an early listener does not reach your
approval UI before it has seen the card.
A signal that has already fired when the call starts raises no card, so no
hitl:cancelled is emitted, and the tool does not run. Unlike a cancellation
while the card waits, the call does not return a refusal: executeTool rejects
with an abort error, as it does for any call cancelled before it starts, so
handle the rejection. A HITLManager
of your own that ignores context.signal keeps its card when the call is
aborted; the call still returns the refusal at once, and a later answer to that
card runs nothing. An arguments object changed while its card waits is not
detected, and does not change what runs: confirmation copies the arguments
before the card is raised, the human reviews that copy, and the tool runs it
(or the approver's modifiedArguments). Arguments that cannot be copied (a
function value, for example) are used as they are, so changes to them are not
isolated.
Cause: Tool not marked with requiresConfirmation: true
Solution:
// Add confirmation flag to tool definition
const tool = {
name: "deleteTool",
requiresConfirmation: true, // (1)!
// ...
};- Add this boolean flag to any tool that performs risky operations
Cause: Confirmation responses not being sent or sent with wrong confirmationId
Solution:
// Always respond to confirmation requests with matching ID
const emitter = neurolink.getEventEmitter();
emitter.on("hitl:confirmation-request", async (event) => {
const receivedAt = Date.now();
const { confirmationId } = event.payload; // (1)!
const approved = await showConfirmationDialog(event.payload);
// (2)! Send response with EXACT confirmationId from request
emitter.emit("hitl:confirmation-response", {
type: "hitl:confirmation-response",
payload: {
confirmationId, // (3)! Must match request exactly
approved,
metadata: {
timestamp: new Date().toISOString(),
responseTime: Date.now() - receivedAt,
},
},
});
});- Extract confirmation ID from the request event
- Always respond to every confirmation request
- Critical: Use the same confirmationId from the request
Cause: Not listening to hitl:confirmation-request event
Solution:
// Set up event listener BEFORE making AI requests
const emitter = neurolink.getEventEmitter();
emitter.on("hitl:confirmation-request", async (event) => {
// (1)! Show your confirmation UI
await handleConfirmationPrompt(event);
});
// (2)! Then make AI requests - confirmations will now work
const result = await neurolink.generate({
input: { text: "Delete all temporary files" },
});- Register the event handler early in your application startup
- All subsequent tool executions will trigger confirmations when needed
:::tip[Production Recommendation] Store user confirmation preferences to avoid repeated prompts for the same action type. For example, if a user approves "delete temporary files" once, cache that preference for similar low-risk deletions in the same session. :::
- Mark tools conservatively - If an operation could cause problems, require confirmation
- Clear prompts - Ensure users understand exactly what will happen
- Test confirmation flow - Verify it works smoothly in your UI
- Log approvals - Keep audit trail of user decisions
- Handle denials gracefully - Allow users to try alternative approaches
✅ Do require confirmation:
- File deletions
- Database writes/deletes
- Sending emails or messages
- Making purchases or payments
- Modifying production systems
❌ Don't require confirmation:
- Read-only operations
- Answering questions
- Generating content
- Searching/fetching data
- Guardrails Middleware - Content filtering and safety checks
- Custom Tools - Building your own tools with HITL
- Middleware Architecture - Advanced request interception
If upgrading from versions before v7.39.0:
- Review all existing tools for risk assessment
- Add
requiresConfirmation: trueto risky tools - Implement confirmation dialog in your UI
- Test with low-risk tools first
- Roll out to production gradually