examples/v2/docs/reference/use-human-in-the-loop.mdx
useHumanInTheLoop is a React hook that creates interactive tools requiring human approval or input before the agent
can proceed. It enables you to build approval workflows, confirmation dialogs, and interactive decision points where
human oversight is needed.
The useHumanInTheLoop hook:
respond callback for user interactionsimport { useHumanInTheLoop } from "@copilotkit/react-core";
import { ToolCallStatus } from "@copilotkit/core";
import { z } from "zod";
function ApprovalComponent() {
useHumanInTheLoop({
name: "requireApproval",
description: "Requires human approval before proceeding",
parameters: z.object({
action: z.string(),
reason: z.string(),
}),
render: ({ status, args, respond }) => {
if (status === ToolCallStatus.Executing && respond) {
return (
<div className="approval-request">
<p>Approve action: {args.action}</p>
<p>Reason: {args.reason}</p>
<button onClick={() => respond({ approved: true })}>Approve</button>
<button onClick={() => respond({ approved: false })}>Deny</button>
</div>
);
}
return <div>Status: {status}</div>;
},
});
return <div>Approval system active</div>;
}
The hook accepts:
ReactHumanInTheLoop object describing the tooloptions object for controlling dependency behaviorstring (required)
A unique identifier for the human-in-the-loop tool.
useHumanInTheLoop({
name: "confirmDeletion",
// ...
});
string (optional)
Describes the tool's purpose to help agents understand when human approval is needed.
useHumanInTheLoop({
name: "sensitiveAction",
description: "Requires human confirmation for sensitive operations",
// ...
});
z.ZodType<T> (optional)
A Zod schema defining the parameters the agent will provide when requesting human input.
import { z } from "zod";
useHumanInTheLoop({
name: "reviewChanges",
parameters: z.object({
changes: z.array(z.string()),
impact: z.enum(["low", "medium", "high"]),
estimatedTime: z.number(),
}),
// ...
});
React.ComponentType (required)
A React component that renders the interactive UI. It receives different props based on the current status:
Initial state when the tool is called but not yet executing:
{
name: string;
description: string;
args: Partial<T>; // Partial arguments as they're being streamed
status: ToolCallStatus.InProgress;
result: undefined;
respond: undefined;
}
Active state where user interaction is required:
{
name: string;
description: string;
args: T; // Complete arguments
status: ToolCallStatus.Executing;
result: undefined;
respond: (result: unknown) => Promise<void>; // Callback to provide response
}
Final state after user has responded:
{
name: string;
description: string;
args: T; // Complete arguments
status: ToolCallStatus.Complete;
result: string; // The response provided via respond()
respond: undefined;
}
boolean (optional, default: true)
Controls whether the agent continues after receiving the human response. Provided for completeness, but typically you would want the agent to continue (default behavior).
useHumanInTheLoop({
name: "finalConfirmation",
followUp: false, // Don't continue after confirmation
// ...
});
string (optional)
Restricts this tool to a specific agent.
useHumanInTheLoop({
name: "adminApproval",
agentId: "admin-assistant",
// ...
});
ReadonlyArray<unknown> (optional)
Additional dependencies that should trigger re-registration of the human-in-the-loop tool. By default, the hook only depends on stable keys like the tool name and CopilotKit instance to avoid re-register loops from object identity changes. Pass a dependency array as the second argument when the tool configuration is derived from changing props or state:
function VersionedApproval({ version }: { version: number }) {
useHumanInTheLoop(
{
name: "versionedApproval",
description: `Approve deployment v${version}`,
// ...
},
[version],
);
return null;
}
While useFrontendTool executes immediately and returns results, useHumanInTheLoop:
respond callback during the Executing stateChoose useHumanInTheLoop when you need human oversight, and useFrontendTool for automated tool execution.