CorfedoCorfedoDocs

Streaming and reattachment

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

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

EventWhat the client does
run.startedRetain run, conversation, message and effective profile IDs
run.snapshotReplace local state with the snapshot
run.statusDisplay queued, running, waitingForClient or cancelRequested
text.deltaAppend provisional text to the indicated step
text.discardedRemove provisional text from this step and later steps
step.completedRecord the step's toolCalls, stop or length finish
tool.started, tool.completedDisplay Corfedo's tool activity. subagent is true when a subagent made the call.
tool.requestedDisplay a client-tool request as information
run.requires_actionRead the calls still open now, then handle the turn's pause
tool_results.outcomesInspect the per-call results of a tool-result submission
run.completedRead the final outcome, answer, citations and usage
errorInspect 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.

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.

: 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. 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 requests cancellation on the first Ctrl+C.

On this page