Skip to main content

Endpoint

POST /api/messages/cancel
Cancels all AI messages currently being processed in a project. This stops the AI agent mid-execution and marks messages as cancelled.

Authentication

Requires a valid Clerk session token. See Authentication for details.

Request Body

projectId
string
required
The Convex ID of the project containing messages to cancel.

Response

success
boolean
Always true if the request was processed.
cancelled
boolean
true if messages were cancelled, false if no messages were processing.
messageIds
array
Array of Convex IDs for cancelled messages. Only present if cancelled is true.

Request Example

const response = await fetch('/api/messages/cancel', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    projectId: 'k57abc123def456'
  })
});

const data = await response.json();
if (data.cancelled) {
  console.log(`Cancelled ${data.messageIds.length} messages`);
}

Response Example

{
  "success": true,
  "cancelled": true,
  "messageIds": [
    "k58xyz789ghi012",
    "k59abc456def789"
  ]
}

Cancellation Process

When messages are cancelled:
1

Find processing messages

All messages with status: "processing" in the project are identified.
2

Send cancel events

Inngest cancel events are sent for each processing message.
3

Update status

Message status is updated to cancelled in the database.
4

Stop AI agent

The running AI agent receives the cancel signal and stops execution.

When to Use Cancellation

Wrong request

User realizes they sent the wrong prompt or selected wrong files.

Too slow

AI is taking too long and user wants to try a different approach.

Unexpected behavior

AI is modifying files in an unexpected way.

Resource limits

Need to stop to avoid hitting API rate limits.

UI Integration

Typically called when the user clicks a “Cancel” or “Stop” button in the conversation sidebar:
const handleCancel = async () => {
  const response = await fetch('/api/messages/cancel', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ projectId })
  });

  const data = await response.json();
  
  if (data.cancelled) {
    // Show success message
    toast.success('AI stopped');
  } else {
    // No messages were processing
    toast.info('No AI requests to cancel');
  }
};

Behavior Notes

Cancellation is immediate - the AI agent stops as soon as it receives the cancel signal. Any partial changes are NOT rolled back.
If the AI already modified files before cancellation, those changes remain. You may need to manually undo unwanted changes.

Multiple Conversations

This endpoint cancels all processing messages in the project, across all conversations. If you have multiple conversations with active AI requests, they will all be cancelled.

Learn More

Build docs developers (and LLMs) love