Agent SSE Event Reference
Reference documentation for all SSE event types streamed during an agent turn, covering text, tool_call, tool_result, vault_file, and billing_required events with field descriptions and usage guidance.
The MarkDocket agent streams its responses as a sequence of Server-Sent Events (SSE) over an HTTP POST connection. Each event carries a type field that identifies its role in the turn, plus a data payload. This page documents every event type your client may receive, the fields each carries, and how to handle them.
Connection model. Because the agent stream uses POST (to carry the turn payload), it cannot use the browser's built-in EventSource API, which only supports GET. Use a streaming fetch() and parse the text/event-stream body line by line, or use the helper in @markdocket/cli if you are building a Node.js integration.
Event envelope
Every event in the stream follows the SSE wire format:
event: <type>
data: <JSON-encoded payload>
The data field is always a JSON object. The shape of that object depends on type, as described below.
Event types
text
A streaming prose chunk from the model. Multiple text events are emitted in sequence; concatenate their content fields in order to reconstruct the full assistant message.
| Field | Type | Description |
|---|---|---|
type | "text" | Event discriminator. |
content | string | A fragment of the assistant's natural-language response. |
turnId | string | The turn this event belongs to. |
Usage: Append each content fragment to your display buffer as it arrives for a typewriter effect. The stream of text events ends when the model has finished producing prose for the current reasoning step — further events of other types may still follow.
tool_call
Emitted when the model decides to invoke a tool. The agent has not yet executed the tool at this point — the event signals intent.
| Field | Type | Description |
|---|---|---|
type | "tool_call" | Event discriminator. |
toolCallId | string | Unique identifier for this tool invocation within the turn. Matches the toolCallId on the corresponding tool_result event. |
toolName | string | The name of the tool being called, as registered in the tool catalog (for example, trademark_search or web_search). |
args | object | The arguments the model is passing to the tool, as a JSON object matching the tool's input schema. |
turnId | string | The turn this event belongs to. |
Usage: Use tool_call events to show the user which tools the agent is using and with what parameters. The toolName always matches a name from the published tool catalog. toolCallId is the correlation key — hold it until the matching tool_result arrives.
tool_result
Emitted after a tool call completes, carrying the result the agent received back. Every tool_call event will be followed by exactly one tool_result with the same toolCallId, unless the turn is cancelled or fails before the tool returns.
| Field | Type | Description |
|---|---|---|
type | "tool_result" | Event discriminator. |
toolCallId | string | Matches the toolCallId from the originating tool_call event. |
toolName | string | The name of the tool that was called. |
result | object | string | The tool's output. Shape is tool-specific; consult the tool catalog for each tool's result schema. |
isError | boolean | true if the tool call produced an error rather than a successful result. |
turnId | string | The turn this event belongs to. |
Usage: Match toolCallId to the earlier tool_call event to update your UI (for example, mark a tool-call card as completed or errored). When isError is true, the model will typically observe the error and decide whether to retry, use a different tool, or explain the failure in a subsequent text event.
vault_file
Emitted when the agent has saved a file to the user's Vault as part of the turn. This can occur after tools that produce downloadable artifacts (reports, draft documents, figures, and similar outputs).
| Field | Type | Description |
|---|---|---|
type | "vault_file" | Event discriminator. |
vaultFileId | string | The identifier of the newly created Vault file. Use this to fetch or display the file via the Vault API. |
filename | string | The display name of the file as stored in the Vault. |
mimeType | string | MIME type of the file (for example, application/pdf or image/png). |
turnId | string | The turn this event belongs to. |
Usage: Render a file attachment card in your UI when a vault_file event arrives. The user can then open or download the file using vaultFileId with the Vault file endpoint. More than one vault_file event may arrive in a single turn if the agent produces multiple artifacts.
billing_required
Emitted when the agent turn cannot proceed because a billing action is required — for example, the account has no remaining credits, or a plan upgrade is needed to use a particular capability. The turn stops after this event is emitted.
This event carries a BillingBlockEnvelope as its payload.
| Field | Type | Description |
|---|---|---|
type | "billing_required" | Event discriminator. |
reason | string | Machine-readable code explaining why billing was blocked (for example, "no_credits" or "plan_required"). |
requiredAction | string | The action the user must take to unblock the turn. Always corresponds 1-to-1 with reason per the platform's billing contract. |
contractVersion | string | The billing contract version string that produced this envelope. |
turnId | string | The turn this event belongs to. |
Turn is terminal after billing_required. No further events will be emitted for this turn once billing_required arrives. The agent has not taken any billable action. Display the appropriate upgrade or top-up prompt to the user based on requiredAction, then allow them to resubmit the turn after resolving the billing issue.
Usage: The billing_required event is the streaming equivalent of an HTTP 402 response — the same BillingBlockEnvelope shape is used at every layer of the platform (HTTP error bodies, automation signals, CLI output, and MCP structured errors), so you can handle it uniformly. Inspect requiredAction to determine which UI flow to present: a credits top-up, a plan upgrade, or a payment method update.
Turn lifecycle and event ordering
A normal agent turn produces events roughly in this order, though the exact sequence depends on how many tool calls the model makes:
- One or more
textevents — the model's initial reasoning or response prose. - A
tool_callevent — the model requests a tool. - A
tool_resultevent — the tool returns its output. - Steps 1–3 may repeat as the model reasons across multiple tool calls.
- Zero or more
vault_fileevents — if the turn produced saved artifacts. - A final
textevent — the model's concluding response.
If billing is blocked at any point, the sequence ends with a single billing_required event instead of completing normally.
Cancellation. If the user cancels a running turn, the stream closes without a terminal event. Your client should handle an unexpected stream closure gracefully — any tool_call events that arrived before cancellation will not have matching tool_result events.
Correlating events
All events include a turnId field that matches the turn identifier returned when the turn was submitted. Use turnId to associate streamed events with the correct conversation thread when multiple turns are in flight, and use toolCallId to pair each tool_call with its eventual tool_result.
Error handling summary
| Situation | What to expect |
|---|---|
| Tool call fails | tool_result arrives with isError: true; turn may continue |
| Billing blocked | billing_required event; turn is terminal |
| User cancels | Stream closes; no terminal event |
| Network or server error | Stream closes with an HTTP error status before SSE events begin |
For billing errors specifically, the reason and requiredAction fields follow the platform's canonical billing contract — each reason maps to exactly one requiredAction, so you can drive UI flows deterministically from the event payload without additional API calls.
Agent Turn Lifecycle
Documents how an agent turn progresses through its full lifecycle: submission phases, billing pre-authorization, file upload, streaming, and completion or cancellation.
Agent Tool System
Documents the MarkDocket agent tool registry: how it works, which tools are available in agent chat versus the public CLI/MCP/API surface, how the agent calls back to the API with delegated tokens, and how billing blocks flow through tool results.