Interruption and barge-in

Open in

Interruption lets users send a new message while the agent is still streaming a response. When a user sends a new message, the client transport creates a new turn via an HTTP POST to the server. Because turns are independent, the new turn starts immediately regardless of whether a previous turn is still streaming.

Diagram showing session continuity during interruption

How it works

When the user sends a new message, the client transport posts it to the server, which starts a new turn. The user doesn't need to wait for the current response to finish before sending a new message.

There are two patterns for handling interruption:

PatternBehaviorUse case
Cancel-then-sendCancel the current turn, then send a new messageStop button + new prompt
Send-alongsideSend a new message while the current turn continuesFollow-up without waiting

With cancel-then-send, the active turn is aborted before the new message is dispatched. The agent stops generating, cleans up, and starts a fresh turn. With send-alongside, both turns run concurrently - each with its own stream and cancel handle.

Cancel-then-send

Detect whether a turn is active, cancel it, then send a new message. This is the most common interruption pattern - it mimics a user pressing a stop button and immediately re-prompting.

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

import { useActiveTurns, useClientTransport, useView } from '@ably/ai-transport/react'

function Chat({ channel, clientId }) {
  const transport = useClientTransport({ channel, clientId })
  const { nodes, send } = useView(transport)
  const activeTurns = useActiveTurns(transport)

  const handleSend = async (text) => {
    // If the agent is streaming, cancel first
    if (activeTurns.size > 0) {
      await transport.cancel()
    }

    await send([{ id: crypto.randomUUID(), role: 'user', parts: [{ type: 'text', text }] }])
  }
}

transport.cancel() publishes a cancel signal on the channel. The server's abort signal fires, the LLM stream stops, and the turn ends with reason 'cancelled'. The new message is then sent on a clean turn.

Send-alongside

Send a new message without cancelling the active turn. Both turns run concurrently - the agent continues streaming the first response while processing the new input.

JavaScript

1

2

3

4

const handleSend = async (text) => {
  // Send without cancelling - both turns run concurrently
  await send([{ id: crypto.randomUUID(), role: 'user', parts: [{ type: 'text', text }] }])
}

Each concurrent turn has its own stream and its own cancel handle. You can cancel them independently:

JavaScript

1

2

// Cancel a specific turn, leave others running
await transport.cancel({ turnId: specificTurnId })

Detect active turns

The useActiveTurns hook returns a Map<clientId, Set<turnId>> of all currently streaming turns. Use it to check whether the agent is mid-response:

JavaScript

1

2

3

4

5

6

7

8

const activeTurns = useActiveTurns(transport)

// Check if any turns are streaming
const isStreaming = activeTurns.size > 0

// Check if a specific client has active turns
const agentTurns = activeTurns.get('agent-client-id')
const agentIsStreaming = agentTurns && agentTurns.size > 0

This is useful for toggling UI between a send button and a stop button, or for disabling input while a cancellation is in progress.