Pausing the Agent for Input
Pause an agent run mid-tool, hand control to a custom React component, and resume with the user's answer.
"""AG2 scheduling agent -- interrupt-adapted.This agent powers two demos (gen-ui-interrupt, interrupt-headless) that in theLangGraph showcase rely on the native `interrupt()` primitive withcheckpoint/resume. AG2 does NOT have that primitive, so we adapt using thesame "Strategy B" pattern as the MS Agent Framework port: the backend agent'ssystem prompt tells the LLM to call `schedule_meeting`, but no localimplementation is registered -- the tool is provided entirely by the frontendvia `useFrontendTool` with an async handler that returns a Promise resolvingonly once the user picks a time slot (or cancels).See `src/agents/agent.py` for the shared ConversableAgent used by most otherAG2 demos."""from __future__ import annotationsfrom autogen import ConversableAgent, LLMConfigfrom autogen.ag_ui import AGUIStreamfrom fastapi import FastAPISYSTEM_PROMPT = ( "You are a scheduling assistant. Whenever the user asks you to book a call " "or schedule a meeting, you MUST call the `schedule_meeting` tool. Pass a " "short `topic` describing the purpose of the meeting and, if known, an " "`attendee` describing who the meeting is with.\n\n" "The `schedule_meeting` tool is implemented on the client: it surfaces a " "time-picker UI to the user and returns the user's selection. After the " "tool returns, briefly confirm whether the meeting was scheduled and at " "what time, or note that the user cancelled. Do NOT ask for approval " "yourself -- always call the tool and let the picker handle the decision.\n\n" "Keep responses short and friendly. After you finish executing tools, " "always send a brief final assistant message summarizing what happened so " "the message persists.")interrupt_agent = ConversableAgent( name="scheduling_agent", system_message=SYSTEM_PROMPT, llm_config=LLMConfig({"model": "gpt-4o-mini", "stream": True}), human_input_mode="NEVER", max_consecutive_auto_reply=5, # No backend tools. `schedule_meeting` is registered on the frontend # via `useFrontendTool` and dispatched through the CopilotKit runtime. # When the agent calls `schedule_meeting`, the request is routed to # the frontend handler, which returns a Promise that only resolves # once the user picks a slot -- equivalent to `interrupt()` in the # LangGraph reference. functions=[],)# AG-UI stream wrapperinterrupt_stream = AGUIStream(interrupt_agent)# FastAPI sub-app so agent_server.py can mount at /interrupt-adaptedinterrupt_app = FastAPI(title="AG2 Interrupt Agent")interrupt_app.mount("/", interrupt_stream.build_asgi())What is this?#
useInterrupt lets your agent pause mid-run, hand control to the user
through a custom React component, and resume with whatever the user
returns. How that pause is implemented depends on the framework's
runtime.
Not available on this framework.
useInterruptis only meaningful when the underlying runtime exposes either a nativeinterrupt(...)primitive (LangGraph) or a Promise-resolving frontend tool path (Microsoft Agent Framework). For all other integrations, useuseHumanInTheLoopinstead — it's the standard hook for tool-call-based pause/resume flows and works on every framework that supports tool calls.
When should I use this?#
Reach for useInterrupt when the pause is a graph-enforced
checkpoint where the code path must stop and wait for a human,
not an LLM-initiated tool call. Typical cases:
- A sensitive action (payments, irreversible writes) must be approved
- A required piece of state isn't known and can only be collected from the user
- The agent explicitly reaches an approval node in a longer workflow
- You want the server-side contract to be
interrupt(...)and resume with a payload
For LLM-initiated pauses where the model decides on the fly to ask
the user, prefer useHumanInTheLoop.
Going further#
- Tool-based HITL with
useHumanInTheLoop— for LLM-initiated pauses. - Headless interrupts — compose the lower-level primitives
(
useAgent,agent.subscribe,copilotkit.runAgent) to resolve interrupts outside a chat surface.
