ag2-hitl
Skill12 starsChanged 10 days agoApache-2.0View on GitHub
Pause an AG2 `Agent` mid-run for human input via `context.input()` + `hitl_hook`, or gate a tool call with `approval_required()` (`ApprovalRequired`) tool middleware. The hook receives a `HumanInputRequest`; approvals arrive as `ToolApprovalRequest` (1.3.0) carrying `tool_call_id`, so a programmatic approver can look up the tool name and arguments. Covers timeouts and `HumanInputError` (`HumanInputNotProvidedError`, `HumanInputFailedError`, `HumanInputTimeoutError`), 'always' approvals, and policy-only gates without a human. Use when the agent must ask for confirmation, request missing info, or have a human/policy approve sensitive, irreversible or expensive tool calls. See `ag2-middleware` for `on_human_input`, `ag2-subagent-delegation` for HITL in workers.
1 other project uses a version of this.
Adopted by
Most recently taken up by
- ag2ai/build-with-ag2their own version, first seen Oct 9, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- ag2ai/build-with-ag2★ 252same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository27 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- ag2-a2aSkill
- ag2-acpSkill
- ag2-add-custom-toolSkill
- ag2-ag-uiSkill
- ag2-eval-comparisonSkill
- ag2-evaluationSkill
- ag2-knowledge-and-memorySkill
- ag2-liveSkill
- ag2-mcpSkill
- ag2-middlewareSkill
- ag2-multimodal-inputSkill
- ag2-network-discussionSkill
- ag2-network-governanceSkill
- ag2-network-quickstartSkill
- ag2-network-tools-and-viewsSkill
- ag2-network-workflowSkill
- ag2-observers-and-alertsSkill
- ag2-overviewSkill
- ag2-quickstartSkill
- ag2-shell-toolSkill
- ag2-structured-outputSkill
- ag2-subagent-delegationSkill
- ag2-telemetrySkill
- ag2-testingSkill
- ag2-use-builtin-toolsSkill
The file
We split this skill into the rules it gives an agent
3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 78
If no person is involved, skip HITL and write a tool middleware.
- Very specific: line 94
Every way context.input() fails ends the turn (it is not recorded as a tool error the model could route around):
- Very specific: line 102
All subclass HumanInputError (ag2.exceptions).
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Human-in-the-loop
When to use
| Need | Use |
| A tool asks an open question and waits for a typed answer | await context.input(...) in the tool + hitl_hook on the agent |
| Approve / deny a specific tool call before its body runs | approval_required() tool middleware + hitl_hook |
| Automatic allow/deny by rule, no human at all | a plain tool middleware function (Pattern 3) |
Pattern 1 — context.input() for open questions
from ag2 import Agent, Context, tool
from ag2.config import OpenAIResponsesConfig
from ag2.events import HumanInputRequest, HumanMessage
@tool
async def execute_query(query: str, context: Context) -> str:
"""Run a SQL query after the user confirms."""
answer = await context.input(f"Run this query? (yes/no)\n{query}", timeout=60.0)
if answer.strip().lower() != "yes":
return "Query cancelled."
return "Query executed successfully."
def hitl_hook(event: HumanInputRequest) -> HumanMessage:
return HumanMessage(content=input(f"{event.content}\n> "))
agent = Agent("dba", config=OpenAIResponsesConfig(model="gpt-4.1-mini"),
tools=[execute_query], hitl_hook=hitl_hook)- The hook gets
HumanInputRequest(.content,.id,.timeout,.task_idwhen a worker asks) and returnsHumanMessageorstr. Sync or async. Supports DI (Context,Variable,Inject,Depends). - Or register with
@agent.hitl_hook(overrides a constructor hook with aRuntimeWarning— pick one place). timeout=defaults to None (wait forever) and covers the hook call itself.
Pattern 2 — approval_required() with a programmatic approver
from ag2 import Agent, Context, tool
from ag2.config import OpenAIResponsesConfig
from ag2.events import HumanInputRequest, ToolApprovalRequest, ToolCallEvent
from ag2.middleware import approval_required
@tool(middleware=[approval_required(timeout=120)])
def delete_file(path: str) -> str:
"""Delete a file."""
return f"deleted {path}"
async def approver(event: HumanInputRequest, context: Context) -> str:
if isinstance(event, ToolApprovalRequest): # 1.3.0: names the gated call
call = await context.stream.history.where(ToolCallEvent.id == event.tool_call_id).first()
if call and call.serialized_arguments.get("path", "").startswith("/tmp/"):
return "y" # y / yes / 1 approve; "always" too
return "n" # anything else denies
return "n" # ordinary context.input() questions: answer from your UI/queue, not blocking input()
agent = Agent("ops", config=OpenAIResponsesConfig(model="gpt-4.1-mini"),
tools=[delete_file], hitl_hook=approver)approval_required(message=None, denied_message="User denied the tool call request", *, timeout=None, allow_always=True)returns anApprovalRequiredinstance (.describe()reports its settings).ToolApprovalRequestis aHumanInputRequestsubclass withtool_call_id;call.name/call.serialized_arguments(dict) /call.arguments(JSON str) come from the matchingToolCallEvent.event.contentis the human-readable prompt ({tool_name},{tool_arguments}interpolated).- On denial the model receives
denied_messageand continues; the tool body never runs. "always"approves this and later calls of the same tool implementation in this conversation (stored under a reservedag:variable; not inherited by sub-agents, stripped over A2A/AG-UI/etc.).- Over AG-UI with no hook, the approval becomes a client interrupt instead (see
ag2-ag-ui).
Pattern 3 — policy gate, no human
If no person is involved, skip HITL and write a tool middleware; it sees the call directly:
from ag2.events import ToolResultEvent
async def only_tmp(call_next, event, context):
if not event.serialized_arguments.get("path", "").startswith("/tmp/"):
return ToolResultEvent.from_call(event, result="Denied by policy: only /tmp/ may be deleted.")
return await call_next(event, context)
@tool(middleware=[only_tmp])
def delete_file(path: str) -> str: ...When nobody can answer
Every way context.input() fails ends the turn (it is not recorded as a tool error the model could route around):
| What happened | Raised from agent.ask() |
No hitl_hook registered | HumanInputNotProvidedError |
| The hook raised | HumanInputFailedError (original on .cause) |
No answer within timeout= | HumanInputTimeoutError |
All subclass HumanInputError (ag2.exceptions). Catch it in the tool to carry on, or have the hook return "n" as a policy. The gated tool never runs in any of these cases, and the stream stays reusable for the next ask().
Going deeper
- Upstream:
website/docs/user-guide/context/human_in_the_loop.mdx,website/docs/user-guide/tools/approval_required.mdx,website/docs/user-guide/tools/tool_middleware.mdx. - Agent-wide interception of every request:
BaseMiddleware.on_human_input()(ag2-middleware). - Tool middleware syntax / DI:
ag2-add-custom-tool.
Common pitfalls
approval_required()without ahitl_hook—HumanInputNotProvidedErrorends the turn (except when served over AG-UI).- Parsing
event.contentto find the tool — useToolApprovalRequest.tool_call_id+ history lookup (above). - Only branching on "yes" in
context.input()— validate the answer; anything unexpected should deny. - Blocking
input()in an async app — use an async hook backed by a queue/websocket. - Expecting the old 30 s default timeout — default is now
None; settimeout=for production. - Sync tool side effects — a sync tool already running in a thread cannot be cancelled when the turn ends; ask for approval before the side effect.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

