# Streaming and reattachment

> Follow SSE events, replace state from snapshots and recover a subscription without cancelling the run.

Source: https://docs.corfedo.ai/connect/api/ai/streaming/

Set `stream: true` on a message or tool-result request, or use `GET /api/v1/ai/runs/{runId}/events`. The response is `text/event-stream`. SDKs parse the stream and maintain run state for you.

## Events

| Event | What the client does |
| --- | --- |
| `run.started` | Retain run, conversation, message and effective profile IDs |
| `run.snapshot` | Replace local state with the snapshot |
| `run.status` | Display `queued`, `running`, `waitingForClient` or `cancelRequested` |
| `text.delta` | Append provisional text to the indicated step |
| `text.discarded` | Remove provisional text from this step and later steps |
| `step.completed` | Record the step's `toolCalls`, `stop` or `length` finish |
| `tool.started`, `tool.completed` | Display Corfedo's tool activity. `subagent` is true when a subagent made the call. |
| `tool.requested` | Display a client-tool request as information |
| `run.requires_action` | Read the calls still open now, then handle the turn's pause |
| `tool_results.outcomes` | Inspect the per-call results of a tool-result submission |
| `run.completed` | Read the final outcome, answer, citations and usage |
| `error` | Inspect the failure and stop this subscription |

Only the main agent's text streams. Tool activity can also come from subagents. A replayed `tool.requested` is history, not an instruction to run a tool. See [client tools](https://docs.corfedo.ai/connect/api/ai/client-tools/#who-runs-a-call).

## Parse the stream yourself

The SSE `event:` name equals the JSON `type`. Events of the run carry a sequence number as the SSE `id` and as the JSON `sequence`, a decimal string.

```text
: heartbeat

event: text.delta
id: 42
data: {"type":"text.delta","sequence":"42","step":1,"text":"Searching the code"}

```

Heartbeats are comments, not events, and arrive at least every 15 seconds. Count them as activity for an idle timeout. The SDKs time out after 30 idle seconds.

## Recover a subscription

Reattach with `GET /api/v1/ai/runs/{runId}/events?after=N`, where `N` is the last processed sequence. `Last-Event-ID: N` is also accepted. The query value takes precedence if both are sent.

Corfedo replays the events after the cursor when it still has them. Otherwise, or without a cursor, the stream starts with a snapshot and continues after its sequence. Skip events at or below a sequence you already processed.

A snapshot holds the status, the step text so far and `pendingCalls`. When the run has ended it also holds the outcome, answer, citations and usage. Replace your state with it, do not append it. A final snapshot ends the subscription and no `run.completed` follows. The final answer stays readable after streamed text is removed.

Attaching to a run that waits for client results returns the open calls and ends at `run.requires_action`. Your call does not own those calls.

## SDK recovery

During one message or tool-result call, the SDKs recover from transient failures. If `run.started` was lost, they resend the message with its `clientMessageId`. Once the run is known, they reattach after the last sequence.

Recovery retries with backoff for up to 30 seconds from the first failure in a row. A `Retry-After` header can use up that time. API-key credentials renew on their own. Human sessions need the [renewal callback](https://docs.corfedo.ai/connect/authentication/#keep-a-human-session-alive). A permission or reference failure ends the call at once. A closed stream does not mean the run completed. Check the error the SDK raises.

## Display text and final answers

A step can end in tool calls, or its text can be discarded during recovery. Show step text as progress. Store and cite the answer from `run.completed` or the final snapshot.

Framework integrations cannot take back text they already passed on. If that text is discarded they fail with `CorfedoOutputDiscardedError` (`CorfedoOutputDiscardedException` in .NET) while the run continues. Read its answer with the core SDK. With an output schema, they return the answer once, after the run ends.

## Close or cancel

Closing or aborting a subscription leaves the run going. To stop it, call `POST /api/v1/ai/runs/{runId}/cancel` or the SDK's cancel method, then check the run's state. The [CLI](https://docs.corfedo.ai/connect/cli/ask/) requests cancellation on the first Ctrl+C.
