Interactive components

Create approval flows where the agent pauses and waits for human input.

Not available for Langchain DeepAgents yet

The code on this page comes from the showcase's gen-ui-interrupt demo, and Langchain DeepAgents doesn't have it yet. See this page for CopilotKit's Built-in Agent, LangGraph (Python), LangGraph (TypeScript).

What is this?

Interactive generative UI creates flows where the agent pauses execution and waits for user input before continuing. This enables approval workflows, confirmation dialogs, and any scenario where human judgment is needed mid-execution.

When should I use this?

Use interactive generative UI when you need:

  • Approval/rejection flows (e.g. "Run this command?")
  • User decisions that the agent should know about
  • Confirmation dialogs with structured responses
  • Any flow where the agent pauses for human judgment

How it works in code

Deep Agents compile to LangGraph graphs, so they support both patterns. CopilotKitMiddleware forwards a useHumanInTheLoop tool to the model, the same way it forwards any frontend tool. For useInterrupt, call LangGraph's native interrupt() from a middleware hook. Deep Agents has no live demo on these pages yet, so the steps below carry the code.

Call interrupt from a middleware hook

A Deep Agent has no custom nodes, so put the pause in a middleware hook. This hook runs before the model call and asks the user to name the agent the first time. The middleware also declares the agent_name state field that stores the answer.

agent.py
from typing import Any, NotRequired

from copilotkit import CopilotKitMiddleware, CopilotKitState
from deepagents import create_deep_agent
from langchain_openai import ChatOpenAI
from langchain.agents.middleware import AgentMiddleware
from langgraph.runtime import Runtime
from langgraph.types import interrupt

class AgentState(CopilotKitState):
    agent_name: NotRequired[str]

class AgentNameMiddleware(AgentMiddleware[AgentState, Any]):
    state_schema = AgentState

    def before_model(self, state: AgentState, runtime: Runtime[Any]) -> dict[str, Any] | None:
        if not state.get("agent_name"):
            # Interrupt and wait for the user to respond with a name
            name = interrupt("Before we start, what would you like to call me?")
            return {"agent_name": name}
        return None

agent = create_deep_agent(
    model=ChatOpenAI(model="gpt-6-luna", reasoning_effort="none"),
    middleware=[
        AgentNameMiddleware(),
        CopilotKitMiddleware(expose_state=["agent_name"]),
    ],
    system_prompt=(
        "You are a helpful assistant. After the user chooses a name, "
        "Current agent state contains agent_name. Use that value as your own name."
    ),
)

Make the agent aware of the answer

Resuming an interrupt and telling the model about the answer are separate steps. By default the model is not aware of the interaction: the run resumes, and the model never sees what the user chose.

The custom middleware saves the answer in thread state. In TypeScript, zodState also keeps the custom field in the graph's output, so AG-UI can send it to the frontend. The CopilotKit middleware then adds only the allowlisted field (expose_state / exposeState) to each model request, and the system prompt says what that field means. State exposure is off by default, so without the allowlist the model never sees the name. Keep the allowlist narrow, and do not expose unrelated application state.

Handle the interrupt in your frontend

Give useInterrupt a component to render, and call resolve with the user's answer. Whatever you pass to resolve becomes the return value of interrupt().

app/page.tsx
import { useInterrupt } from "@copilotkit/react-core/v2";
// ...

const YourMainContent = () => {
  // ...
  // styles omitted for brevity
  useInterrupt({
    render: ({ event, resolve }) => (
      <div>
        <p>{event.value}</p>
        <form onSubmit={(e) => {
          e.preventDefault();
          resolve((e.target as HTMLFormElement).response.value);
        }}>
          <input type="text" name="response" placeholder="Enter your response" />
          <button type="submit">Submit</button>
        </form>
      </div>
    )
  });
  // ...

  return <div>{/* ... */}</div>
}

The AG-UI LangGraph adapter sends an object interrupt value as a JSON string, so parse event.value before you read fields from it.

When your app already selects an agent in its CopilotKit provider, useInterrupt uses that agent. Pass agentId only when this component must target a different agent in a multi-agent app.

On the frontend, register an interrupt renderer with useInterrupt. When the agent pauses, your component mounts inline in the chat, captures the user's choice, and resumes the run with that input. The excerpt imports the demo's TimePickerCard, its fallback slots, and its suggestions. Use your own picker component in their place.

On the backend, the agent pauses the run with its framework's interrupt primitive and waits for the user's answer before it continues.

Ask the agent to book a call, for example Book an intro call with the sales team to discuss pricing. The time picker appears in the chat. Pick a slot, and the agent confirms the booked time.

How is this guide?