Handle Cline SDK run events, streamed output and cancellation
Separate the two event subscriptions, handle streamed text and usage, judge completion from events, and record cancel requests apart from outcomes.
Prerequisites and input · Engineering checks · 1. Fix the subscription surface and record how to unsubscribe
At a glance
An embedded Cline SDK host records streaming events, cancellation intent and outcomes separately. The Events page covers direct-Agent agent.subscribe and Core cline.subscribe. The Production guide adds a documented direct-Agent agent.abort(reason) example, without promising its exact terminal outcome. This workflow classifies events, displays text/tools, issues direct-Agent cancellation, records a terminal outcome or unresolved observation deadline and cleans up listeners.
Runtime and applicability
- Article type
- Engineering practice
- Verified
- Applies to
- Cline Handle Cline SDK run events, streamed output and cancellation
Execution locations and responsibilities
- Host application embedding Cline SDK Use the workflow below to distinguish responsibilities of the caller, runtime, and external actions.
Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.
When to use
- For direct
Agentuse the documentation prefersagent.subscribe(...)withAgentRuntimeEvent; with@cline/coresession orchestration you usecline.subscribe(), and asessionIdfilter narrows it to one session. The event names differ, so one handler branch set cannot serve both.
When not to use
- The Production guide documents direct-Agent
agent.abort(reason). The Events page does not list all run-finished.status values or guarantee the event after cancellation. Core-session cancellation is outside this implementation and must not inherit the direct-Agent method. - Whether
usage-updatedvalues are cumulative or incremental is not settled by the example, which only shows an assignment; verify this before reporting usage. agent.snapshot()appears in the supplied text only in the directAgentexample; how to read state for aClineCoresession is not described, so do not apply the snapshot cross-check to that path.
Prerequisites and input
- A runnable SDK Agent or
ClineCoresession where the host can register a listener before the run. - A defined destination in the host, such as UI, logs or metrics, for text, tool status and usage.
- Access to the event-shape reference that the page links to, because this article relies only on the types and fields the events page names.
Engineering checks
Event surface: know which subscription you hold — For direct Agent use the documentation prefers agent.subscribe(...) with AgentRuntimeEvent; with @cline/core session orchestration you use cline.subscribe(), and a sessionId filter narrows it to one session. The event names differ, so one handler branch set cannot serve both.
Streaming and usage layer: landing incremental content — In runtime events, assistant-text-delta carries text increments, tool-started and tool-finished carry a toolCall, and usage-updated carries usage; a direct Agent can also call agent.snapshot() for current status, iteration and usage. Host-facing events are grouped as content, iterations, usage, notices and completion.
Ending and cancellation layer: what counts as finished — Direct-Agent events can report run-finished or run-failed, and the Production guide supplies agent.abort(reason) to request stopping without guaranteeing a particular terminal outcome. Record observed terminal results or unresolved at the host deadline, with listener cleanup on both paths. Classify Core done/error separately without inheriting the direct-Agent contract.
1. Fix the subscription surface and record how to unsubscribe
Where: the host code that creates the Agent or session. Input: whether you use a direct Agent or ClineCore. Action: use agent.subscribe for a direct Agent and cline.subscribe for a session, passing a sessionId filter when you want one session; both return an unsubscribe function to call after the run. Completion check: each subscription records its event type names and where it unsubscribes. Stop condition: if one path has both subscriptions without distinguishing type names, separate them before continuing.
2. Build an event classification table
Where: the host's event-handling module. Input: runtime events assistant-text-delta, tool-started, tool-finished, run-finished and usage-updated, plus host-facing categories content_start, content_update, content_end, iteration_start, iteration_end, usage, notice, done and error. Action: assign each type a destination such as UI, log or metrics, write ignore explicitly for types you do not handle, and annotate which event surface each type belongs to. Completion check: every type you use has a destination and a surface. Stop condition: when you need event fields that this page does not state, consult the official event-shape reference rather than guessing field names.
3. Handle streamed text and tool status
Where: the UI update function. Input: text from assistant-text-delta, and event.toolCall.toolName from tool-started and tool-finished. Action: append text increments in arrival order, using an empty string when text may be missing as the documentation example does, and update tool state on start and finish. Completion check: the UI shows incremental text and every tool has a start and a finish state. Stop condition: a tool that started and never finished is marked unknown, not successful.
4. Record usage and verify its meaning
Where: the monitoring module. Input: event.usage.inputTokens and event.usage.outputTokens from usage-updated; on the direct Agent path only, also status, iteration and usage from agent.snapshot(). Action: the documentation example assigns on each event rather than adding, which implies it treats each event's value as the current total; confirm that in the reference before choosing between accumulating and overwriting. On the direct Agent path, compare the record with agent.snapshot() usage at the same moment; on the ClineCore path rely only on the usage category of host-facing events, since the supplied text names no session-state interface. Completion check: the usage record has a stated source and, on the direct Agent path, can be compared with a snapshot. Stop condition: if the comparison fails, do not report the usage figure outside the team.
5. Record the cancel request without claiming cancellation
Where: the host cancel control for the direct Agent path. Input: the running Agent instance and user stop intent. Call agent.abort with a reason, as shown in the production guide. Record that a cancellation request was issued and continue observing the run outcome; unsubscribing is not task cancellation. Core-session cancellation is outside this implementation. Completion: preserve the reason and the actual terminal event or unresolved state; before a terminal observation show waiting or unknown. Stop waiting at a host-defined deadline and retain an unresolved record instead of inventing a canceled result.
6. Write the end-of-run judgement and clean up
Where: direct-Agent observation closeout. Input: a terminal event or host observation deadline. On an actual terminal event record its result and optionally compare agent.snapshot. At the deadline without a terminal event record unresolved and the time. Both paths call the subscription-returned unsubscribe; cancelling a task and stopping observation remain separate. Record Core events without an unsupported Core snapshot. Completion: exactly one host closeout record per observation, either observed terminal or unresolved, with listener cleanup. Stop with a retained inconsistency for human review when events and snapshots disagree; unresolved never means SDK cancellation succeeded.
Task example
Unexecuted walkthrough: subscribe to direct-Agent text and tool events. When the user stops, call agent.abort(reason) and record the request without displaying successful cancellation. Observe until an actual terminal event or the host deadline. Record actual status/error for a terminal event, otherwise unresolved at the deadline. Both paths unsubscribe and retain request reason, closeout result and cleanup time. No real runtime or terminal receipt is claimed.
Completion checks
- Classification check: every event type received has a destination and a surface in the table, and unhandled types are explicitly marked ignore.
- Streaming check: text increments display in order, missing text does not break the UI, and every tool has a start and finish state or is marked unknown.
- Each observation has one host closeout record: observed terminal or unresolved with a reason, both with listener cleanup.
agent.abortrecords an issued request and alone does not establish successful cancellation.
Verification scope
Official Cline Events and Production documentation was read, including the direct-Agent agent.abort(reason) example. SDK installation, sign-in, execution, event/snapshot observation and cancellation outcomes were not tested. The example is hypothetical.
Sources
All links are official pages; verification dates are listed per source, and links point to the version checked.