Claude Code Interrupts: How Ctrl-C Stops the Agent Loop Without Corrupting History
The agent loop cannot fix one problem alone: when a user changes their mind, they must be able to interrupt it. Claude Code handles a Ctrl-C press by calling QueryEngine.interrupt(), which aborts a shared AbortController that reaches every concurrent operation, including streaming requests, parallel tools, and approval promises. The loop checks the abort signal at key boundaries, such as before starting tools and between tool batches, so no new work begins after cancellation. Because an interruption can leave a tool_use without a matching tool_result, which would cause an API error, the system inserts a synthetic error result marked 'Interrupted by user' to restore valid message pairing. This approach keeps the conversation history structurally sound and lets the model understand that the operation was stopped deliberately.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.


Discussion (0)
Log in to join the discussion and vote.
Log in