Agents, Tool Use & MCP·Question 41 of 90Intermediate

The user hits Stop while your agent is in the middle of a tool call. What should happen, and how do you design streaming and cancellation for an agent UI?

Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.

Model answer: the part to lead with

Stop is a state transition with a defined outcome, not a killed process. The model stream ends at once, each in-flight tool call is resolved according to its side-effect class, and the result is recorded honestly so the next turn starts from the truth.

Full worked answer with diagram

Stop is a state transition with a defined outcome, not a killed process. The model stream ends at once, each in-flight tool call is resolved according to its side-effect class, and the result is recorded honestly so the next turn starts from the truth.

By class:

  • Reads: abort and discard. Nothing to undo.
  • Writes not yet committed: cancel through the executor. Long-running remote work gets an explicit cancel on its task handle, and over MCP that means the protocol's own cancellation for the request or task.
  • Irreversible and already dispatched: an email that has left, a payment submitted. You cannot un-send it. Let it finish, record the real outcome, and tell the user what actually happened.

Then repair the transcript. Provider APIs expect every tool call to have a matching tool result, so a dangling call breaks the next request. Write a synthetic result: "cancelled by user before completion", or "cancelled, but the write completed: invoice 4412 sent". On the next turn the model knows the true state, so it neither repeats a side effect nor assumes one happened.

Cancellation must propagate. A cancel token flows from the UI through the runtime to tool executors and subagents, so stopping the orchestrator stops its children instead of leaving them burning tokens. Handle the race where a tool finishes just as the cancel arrives: the recorded outcome wins, not the user's intent.

Streaming has its own trap. Tool arguments stream as partial JSON, which is fine to render as "searching for..." but must never be executed until the block is complete and validated. Show tool activity as it happens, since perceived latency drops even when total time does not, and mark interrupted assistant text as interrupted so the model does not later treat a half-sentence as its considered answer.

Tokens generated before Stop are still billed, so count cancelled runs in cost per task.

Worth sketching. Branching on side-effect class, then converging on one transcript repair, is the whole design.

flowchart TD
    A["User hits Stop"] --> B["End the model stream"]
    B --> C{"In-flight tool:<br/>side-effect class?"}
    C -->|"read"| D["Abort and discard"]
    C -->|"write, not committed"| E["Cancel via the executor"]
    C -->|"irreversible, already sent"| F["Let it finish,<br/>record the real outcome"]
    D --> G["Synthetic tool result<br/>for every open call"]
    E --> G
    F --> G

What a good answer covers

  • Stop is a state transition with a defined outcome, not a killed process
  • The model stream ends at once, each in-flight tool call is resolved according to its side-effect class,…
  • Reads: abort and discard
  • Writes not yet committed: cancel through the executor
  • Irreversible and already dispatched: an email that has left, a payment submitted

Key terms: Reads · Writes not yet committed · Irreversible and already dispatched

Interviewer follow-ups to expect

  1. The user hits Stop, then types "actually, carry on". What does the agent do with the half-finished work?

  2. How would you test cancellation behaviour in CI?

Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep