Skip to content

🐛 Bug: useInterrupt causes re-renders #6934

Closed

Description

@panfiva

♻️ Reproduction Steps

  1. Build a component similar to the one below
  2. Submit a query
  3. Observe multitple re-renders

The issue stems from the fact that useInterrupt call useAgent without updates: [], resulting in hook re-rendering on any event received

import { useInterrupt, CopilotChat } from "@copilotkit/react-core/v2";
import { useRef } from "react";

const AGENT_NAME = import.meta.env.VITE_AGENT_NAME || "default";

export const ChatInterface2 = () => {
  const renderCountRef = useRef(0);
  renderCountRef.current++;

  useInterrupt({
    render: ({ event, resolve }) => (
      <div className="p-3 border rounded">
        <p>{event.value.question}</p>
        <div className="mt-2 flex gap-2">
          <button onClick={() => resolve({ approved: true })}>Approve</button>
          <button onClick={() => resolve({ approved: false })}>Reject</button>
        </div>
      </div>
    ),
  });

  return (
    <>
      {renderCountRef.current}
      <CopilotChat agentId={AGENT_NAME} />
    </>
  );
};

✅ Expected Behavior

calling useInterrupt hook should not trigger re-renders

❌ Actual Behavior

calling useInterrupt hook triggers re-renders

Note:

A work-around is to wrap useInterrupt into a separate component and then use as a child

  return (
    <>
      <AddUseInterrupt />
      <CopilotChat agentId={AGENT_NAME} />
    </>
  );

Screenshots

Started with 10 re-renders on initial load

Image

Ended with 30 after a basic message

Image

𝌚 CopilotKit Version

├── @copilotkit/[email protected]
├── @copilotkit/[email protected]

📄 Logs (Optional)

Activity

  1. added
    bug
    on Sep 7, 2026
  2. copilotkit-outpost-bot commented on Sep 7, 2026

    @copilotkit-outpost-bot

    ⚠️ This is an AI-generated response. We've escalated this to our engineering team — someone will follow up in this thread shortly.

    Re-render Report: useInterrupt Triggering Excessive Re-renders

    Thanks for the detailed reproduction — this is a well-structured report with a clear workaround already identified.

    What the Code Shows

    Looking at packages/react-core/src/v2/hooks/use-interrupt.tsx, useInterrupt internally calls:

    Code example (ts)
    const { agent } = useAgent({ agentId: config.agentId });

    You're correct that no updates: [] (or equivalent subscription filter) is passed, which means the hook subscribes to all agent state updates. Every event the agent receives (messages streaming in, state changes, etc.) can trigger a re-render of whatever component hosts useInterrupt.

    Additionally, the hook holds several pieces of its own state:

    const [pending, setPending] = useState<PendingInterrupt | null>(null);
    const [handlerResult, setHandlerResult] = useState<InterruptResult<any, TResult>>(null);

    So the component re-renders both when agent state changes and when these internal states change.

    Why Your Workaround Helps

    Isolating useInterrupt into its own child component (<AddUseInterrupt />) is effective because:

    • Re-renders caused by useAgent's subscription are contained to that child
    • The parent component (ChatInterface2) and <CopilotChat> are not in the re-render subtree
    • React's component boundary prevents the render count from bubbling up

    This is actually a solid pattern regardless of how this gets addressed internally.

    Recommended Workaround (Until This Is Addressed)

    Code example (tsx)
    // Isolate useInterrupt into its own component
    const InterruptHandler = () => {
      useInterrupt({
        render: ({ event, resolve }) => (
          <div className="p-3 border rounded">
            <p>{event.value.question}</p>
            <div className="mt-2 flex gap-2">
              <button onClick={() => resolve({ approved: true })}>Approve</button>
              <button onClick={() => resolve({ approved: false })}>Reject</button>
            </div>
          </div>
        ),
      });
      return null; // renders nothing itself; UI goes into <CopilotChat> via renderInChat
    };
    
    export const ChatInterface2 = () => {
      return (
        <>
          <InterruptHandler />
          <CopilotChat agentId={AGENT_NAME} />
        </>
      );
    };

    Note: useInterrupt with default renderInChat: true publishes its element into <CopilotChat> via copilotkit.setInterruptElement(element) (visible in the source at line 489), so returning null from InterruptHandler is correct — the UI still appears inside the chat.

    What I'd Flag for the Team

    • The useAgent call inside useInterrupt without subscription filtering is the likely source of the extra renders — engineering will need to verify whether a subscription filter is the right fix or whether the agent context needs to be consumed differently here
    • I cannot confirm this as a bug or prescribe the internal fix — that's for the maintainers to determine

    I've noted your reproduction steps and version (1.70.0) are clear and actionable. The team should have everything they need to investigate.


    Was this helpful? Let me know if you want help structuring the InterruptHandler pattern for a more complex use case (e.g., multiple interrupt types or renderInChat: false).


    🤖 Generated by CopilotKit AI Support · Documentation


    Was this helpful? React with 👍 or 👎

  3. self-assigned this
    on Sep 7, 2026
  4. added 2 commits that reference this issue on Sep 10, 2026
    e2887b4
    35c1e80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions