HITL

Use these endpoints to orchestrate Human-in-the-Loop (HITL) pauses from a client application: register a tool, create an agent, stream run updates, and resolve approval requests.

Prerequisites

  • Backend service reachable at $BACKEND_URL (default: http://localhost:8000).

  • API key with access to the HITL endpoints (X-API-Key header).

Examples below assume the API key is stored in $AIP_MASTER_API_KEY.

1. Create an Agent with HITL Enabled

curl -X POST "$BACKEND_URL/agents/" \
  -H "X-API-Key: $AIP_MASTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d @agent_payload.json

Sample agent_payload.json:

{
  "name": "hitl_approval_agent",
  "instruction": "Whenever an action needs approval, call the custom_tool first.",
  "type": "config",
  "framework": "langchain",
  "version": "1.0",
  "tools": ["<TOOL_ID_FROM_UPLOAD>"],
  "agent_config": {
    "hitl_enabled": true,
    "lm_provider": "openai",
    "lm_name": "gpt-4.1"
  },
  "tool_configs": {
    "<TOOL_ID_FROM_UPLOAD>": {
      "hitl": {
        "timeout_seconds": 180
      }
    }
  }
}

The response JSON returns a new agent_id.

2. Run the Agent and Watch for HITL Pauses

The call returns an SSE stream. When a tool requires human approval the chunks include a metadata.hitl object. Example payload captured from the integration test (notes/hitl_e2e_run_raw.log):

Key fields to capture:

  • metadata.hitl.request_id — unique identifier for the pending approval (UUID).

  • metadata.hitl.decision — current state. Expect at least pending, approved, rejected, or timeout_skip.

  • metadata.hitl.timeout_at — ISO 8601 timestamp when the HITL request will timeout if no decision is made (UTC+0 timezone)

  • metadata.hitl.timeout_seconds — static value of timeout duration in seconds for the HITL request (configured per tool with tool config)

  • metadata.tool_info — context about the tool invocation (call id, tool name, arguments, output) for use in operator UIs.

Maintain the SSE connection while the operator decides so your client receives the resumed tokens once the request is resolved.

3. Resolve a HITL Request

Use the request_id from the stream (or from the pending inbox endpoint) when calling the decision API.

Required fields:

  • request_id — matches the identifier from the SSE chunk.

  • decision — one of approved, rejected, or skipped.

Optional fields:

  • operator_input — free-form notes for audit trails (defaults to "").

  • run_id — client-side correlation identifier if you track sessions.

The endpoint returns {"status": "ok", "message": "Request <id> <decision>"} on success. The corresponding SSE stream delivers a new chunk reflecting the final decision so your client can update local state.

4. Optional: List Pending HITL Requests

Sample response:

The runner tracks these requests in-memory, so restarts clear the list. Treat this endpoint as a real-time inbox rather than an audit log.

5. Optional Cleanup

  • Delete agent: DELETE /agents/<AGENT_ID>

  • Delete tool: DELETE /tools/<TOOL_ID>

Keep cleanup scripts handy for integration tests so temporary resources do not accumulate in your tenant.

Last updated

Was this helpful?