Human-in-the-Loop (HITL) allows agents to pause execution and request user input or approval before proceeding. This is crucial for scenarios requiring human oversight, such as approval workflows, form submissions, or interactive decision-making. The key mechanism is the interrupt feature, where the agent sends an interrupt event during streaming, pausing until the client resumes with a payload.Documentation Index
Fetch the complete documentation index at: https://docs.ag-kit.dev/llms.txt
Use this file to discover all available pages before exploring further.
Interrupt Flow Overview
Here’s a high-level sequence of how the client and agent interact during an interrupt:Agent-Side Implementation (Brief Overview)
On the backend, agents can trigger interrupts:- AG-Kit: Use a custom control flow handler to return
{ action: 'interrupt', reason: '...', payload: { ... } }. - LangGraph: Use built-in interrupt nodes in the graph.
Client-Side Handling
Focus on the frontend integration using the@ag-kit/ui-react package for React applications.
Understanding the Interrupt Payload
Before diving into the code, let’s clarify the interrupt payload. Thepayload is arbitrary, customizable data sent by your agent when it triggers an interrupt. It’s not fixed; you define what it contains based on what user input your agent needs.
For example:
- In an approval workflow, it might be a list of steps for the user to review.
- In a form submission, it could be a schema defining form fields.
- It can be any JSON-serializable data, like a question or options.
Using the useChat Hook
TheuseChat hook supports HITL via the interrupt option. Here’s a complete simple example:
uiMessages as a special part with type "interrupt".
interrupt: Containsid,reason, andpayloadfrom the agent.resume(payload): Sends the user’s response back to resume execution.