产品介绍

Human interaction and recovery

Use HITL (Human-in-the-loop) to pause for clarification, confirm a tool call, or review a result, then continue after an answer. This guide builds one minimal flow: ask for a deduplication key before execution and pass the answer to the model to continue the task.

Prepare a working model using the Python SDK guide. Add the independent plugin below and enable it in the selected product configuration. The example uses the default root Agent; leave dataagent.entry unset. Custom compiled entries do not automatically receive this root middleware.

Prepare a minimal plugin

Create the following under DataAgent Home:

<home>/plugins/clarification/
  .plugin.yaml
  hitl/
    clarification.py

Merge the plugin configuration into the selected config.yaml, retaining its existing models and other settings:

plugins:
  enabled:
  - clarification

The directory name must match the manifest's id. To load another location, point plugins.paths at the plugin directory itself. See Plugin integration.

Customize the clarification question

Save the class below as hitl/clarification.py to customize the question. Other methods keep their defaults: ask on a match and pass the answer to the model to continue reasoning.

from dataagent import HITLHandler, InteractionRequest


class ClarificationHandler(HITLHandler):
    def interaction_builder(self, context, *, params):
        return InteractionRequest(
            question="Which identifier should deduplicate active users?",
            options=["user_id", "device_id"],
        )

The default handler asks on every match. Override trigger_policy to ask only under a particular business condition; return updates from resume_handler to commit business state. See the HITL Python API for method signatures, context fields, and state commit rules.

Register and enable the policy

Save this complete manifest as .plugin.yaml:

schema_version: 3
id: clarification
hitl:
  enabled: true
  policies:
  - id: choose_key
    version: '1'
    at: before_agent
    handler: hitl/clarification.py:ClarificationHandler
    params: {}

The handler path is relative to the plugin root and the class must support parameterless construction. The full policy ID is clarification/choose_key. It pauses before the first model call without depending on the model choosing a tool. For tool confirmation, select before_tool and provide the tool's matcher. See HITL declarations.

To let the model ask when it notices missing information, set hitl.instructions to a nonempty prompt file that explains the shared request_human_feedback tool. You can also enable HITL with policies: [] for model-initiated questions alone. See the middleware API for deterministic policies, model requests, and explicit graph assembly.

Verify request, answer, and resume

Prepare the terminal client using the startup instructions, then run from the repository root. Replace the path with the product file that configures your model and enables this plugin:

npm run start:tui -- --config /absolute/path/to/config.yaml
  1. Enter “Explain which key the active-user count will use.”
  2. Before the first model call, expect the clarification question and its two options.
  3. Enter user_id. Confirm that the execution resumes with this feedback instead of starting an unrelated conversation.

This policy asks before each invocation. For one question per session, check persisted business state in trigger_policy.

For embedded calls, reuse the same Agent, checkpointer, and thread_id. In the SDK example, import InMemorySaver from langgraph.checkpoint.memory, create the Agent with create_agent(runtime, checkpointer=InMemorySaver()), and replace the first invocation with this code.

from langgraph.types import Command

config = {"configurable": {"thread_id": "clarification-example"}}
result = await agent.ainvoke(
    {"messages": [("user", "Explain which key the active-user count will use.")]}, config
)
pending = result["__interrupt__"][0]
print(pending.value["message"])
result = await agent.ainvoke(Command(resume={pending.id: "user_id"}), config)
print(result["messages"][-1].content)

The first print shows the clarification question. After submitting user_id, the resumed example should print a model reply that uses that answer.

Answers must be nonblank text. Blank or structured answers pause again with validation_error. Options are suggestions, not automatic enum validation; the handler owns stricter domain checks. For parallel interrupts, submit each answer under its interrupt ID rather than guessing from display order. Use async ainvoke or astream. There is currently no automatic answer or interaction timeout.

Existing clients do not need the SDK script: answer the prompt in TUI and use /resume <threadId> to reopen a session. Protocol clients submit answers and interrupt IDs using AG-UI human interaction or A2A human interaction.

Change behavior after the answer

The default handler returns replan. At this example's before_agent phase, feedback goes forward to the model without looping. Custom handlers can return continue or reject; effects depend on the phase. In particular, after_tool cannot undo an executed call. See resume action reference for all six phases and state merging rules.

Persist and recover pending interactions

The InMemorySaver above only retains state in the current process. To answer after restarting, reuse a persistent checkpointer, the same thread_id, and compatible plugin configuration. Starting a new session does not resume an old interaction.

Keep policy IDs, phases, and versions unchanged while an interaction is pending. Handlers can replay and interrupted tools can retry, so external effects must be idempotent. A completed tool result is reused when resuming after_tool. See HITL execution and recovery for persistence and replay boundaries.

Scenario example

For SQL and IR tasks that choose when to ask, confirm business semantics, and review before completion, see de-agent human feedback. Those policies belong to the scenario plugin. The shared API lives in the human interaction reference.