agent-sdk/typescript.md +28 −6
2190 kind: "task-notification";2190 kind: "task-notification";
2191 subkind?: "scheduled-trigger" | "peer-send-message";2191 subkind?: "scheduled-trigger" | "peer-send-message";
2192 fireReason?: string;2192 fireReason?: string;
2193 runId?: string;
2193 }2194 }
2194 | { kind: "coordinator" }2195 | { kind: "coordinator" }
2195 | { kind: "auto-continuation" }2196 | { kind: "auto-continuation" }
2217 2218
2218`fireReason` says why a `scheduled-trigger` notification fired, as a short lowercase token such as `scheduled`, `manual`, `retry`, `catch_up`, or `api`. Anthropic servers set it on a [routine](/docs/en/routines)'s deliveries, and your application sets it when it declares a scheduled run. It's absent when neither sent one. Requires TypeScript Agent SDK v0.3.280 or later.2219`fireReason` says why a `scheduled-trigger` notification fired, as a short lowercase token such as `scheduled`, `manual`, `retry`, `catch_up`, or `api`. Anthropic servers set it on a [routine](/docs/en/routines)'s deliveries, and your application sets it when it declares a scheduled run. It's absent when neither sent one. Requires TypeScript Agent SDK v0.3.280 or later.
2219 2220
2221A background task can run more than once under the same `task_id`, such as when Claude [resumes a subagent](/docs/en/agent-sdk/subagents#resume-subagents). Each run has its own [`run_id`](#sdktaskstartedmessage). When a run's notification reaches your session as a user message, `runId` on that message's `task-notification` origin holds the same value. A notification can arrive after the task's next run has started, and it still carries the earlier run's ID. To tell which run a turn belongs to, compare `runId`, not `task_id`. Requires TypeScript Agent SDK v0.3.292 or later.
2222
2220#### Declare a scheduled run2223#### Declare a scheduled run
2221 2224
2222If your application runs prompts on its own schedule, declare each run so Claude Code frames the turn to the model as a scheduled task rather than as live input from the user. Start the session with `CLAUDE_CODE_HOST_SCHEDULED_RUN` set to `1` in [`env`](#options), then send the run's [`SDKUserMessage`](#sdkusermessage) with `origin: { kind: "task-notification", subkind: "scheduled-trigger", fireReason: "scheduled" }` and without `isSynthetic`. Claude Code ignores the declaration in a process started without that variable. It also ignores it in a process whose environment carries [`CLAUDECODE`](/docs/en/env-vars) or `CLAUDE_CODE_CHILD_SESSION`. Claude Code keeps `fireReason` only when the value is 1 to 32 lowercase letters or underscores. Requires TypeScript Agent SDK v0.3.280 or later.2225If your application runs prompts on its own schedule, declare each run so Claude Code frames the turn to the model as a scheduled task rather than as live input from the user. Start the session with `CLAUDE_CODE_HOST_SCHEDULED_RUN` set to `1` in [`env`](#options), then send the run's [`SDKUserMessage`](#sdkusermessage) with `origin: { kind: "task-notification", subkind: "scheduled-trigger", fireReason: "scheduled" }` and without `isSynthetic`. Claude Code ignores the declaration in a process started without that variable. It also ignores it in a process whose environment carries [`CLAUDECODE`](/docs/en/env-vars) or `CLAUDE_CODE_CHILD_SESSION`. Claude Code keeps `fireReason` only when the value is 1 to 32 lowercase letters or underscores. Requires TypeScript Agent SDK v0.3.280 or later.
2975 2978
2976## Tool Input Types2979## Tool Input Types
2977 2980
29782981Documentation of input schemas for all built-in Claude Code tools. These types are exported from `@anthropic-ai/claude-agent-sdk/sdk-tools` and can be used for type-safe tool interactions.Documentation of input schemas for built-in Claude Code tools. These types are exported from `@anthropic-ai/claude-agent-sdk/sdk-tools` and can be used for type-safe tool interactions.
2979 2982
2980### `ToolInputSchemas`2983### `ToolInputSchemas`
2981 2984
3707 3710
3708## Tool Output Types3711## Tool Output Types
3709 3712
37103713Documentation of output schemas for all built-in Claude Code tools. These types are exported from `@anthropic-ai/claude-agent-sdk/sdk-tools` and represent the actual response data returned by each tool.Documentation of output schemas for built-in Claude Code tools. These types are exported from `@anthropic-ai/claude-agent-sdk/sdk-tools` and represent the actual response data returned by each tool.
3711 3714
3712### `ToolOutputSchemas`3715### `ToolOutputSchemas`
3713 3716
5226 5229
5227### `SDKTaskNotificationMessage`5230### `SDKTaskNotificationMessage`
5228 5231
52295232Notification when a background task completes, fails, or is stopped. Background tasks include `run_in_background` Bash commands, [Monitor](#monitor) watches, and background subagents. For the `ambient` field, see [`SDKTaskStartedMessage`](#sdktaskstartedmessage), which defines it and its version requirement.Notification when a background task completes, fails, or is stopped. Background tasks include `run_in_background` Bash commands, [Monitor](#monitor) watches, and background subagents. For the `run_id` and `ambient` fields, see [`SDKTaskStartedMessage`](#sdktaskstartedmessage), which defines them and their version requirements.
5230 5233
5231```typescript theme={null}5234```typescript theme={null}
5232type SDKTaskNotificationMessage = {5235type SDKTaskNotificationMessage = {
5233 type: "system";5236 type: "system";
5234 subtype: "task_notification";5237 subtype: "task_notification";
5235 task_id: string;5238 task_id: string;
5239 run_id?: string;
5236 tool_use_id?: string;5240 tool_use_id?: string;
5237 status: "completed" | "failed" | "stopped";5241 status: "completed" | "failed" | "stopped";
5238 reason?: "worker_restart";5242 reason?: "worker_restart";
5391 type: "system";5395 type: "system";
5392 subtype: "task_started";5396 subtype: "task_started";
5393 task_id: string;5397 task_id: string;
5398 run_id?: string;
5394 tool_use_id?: string;5399 tool_use_id?: string;
5395 description: string;5400 description: string;
5396 task_type?: string;5401 task_type?: string;
5403};5408};
5404```5409```
5405 5410
5411`run_id` identifies one run of the task. When a task runs again under the same `task_id`, such as a [subagent that Claude resumes](/docs/en/agent-sdk/subagents#resume-subagents), the new run gets a new `run_id`. The field requires Agent SDK v0.3.292 or later. Use it to keep the runs of one task apart:
5412
5413* **Match messages to a run**: the `task_started`, [`task_progress`](#sdktaskprogressmessage), [`task_updated`](#sdktaskupdatedmessage), and [`task_notification`](#sdktasknotificationmessage) messages of one run carry the same `run_id`. The run's entry in each [`SDKBackgroundTasksChangedMessage`](#sdkbackgroundtaskschangedmessage) carries it too, and so does the [`runId`](#task-notification-subkinds) on the origin of the user message that delivers the run's notification.
5414* **Detect a late message**: compared as plain strings, the run IDs of one task sort in the order the runs started, so a message whose `run_id` sorts before the run you're showing comes from an earlier run. Treat the value as opaque apart from that comparison.
5415* **Fall back to `task_id`**: when a message carries no `run_id`, such as the `task_notification` that a resumed session emits to close a task the earlier process started, attribute it by `task_id`.
5416
5417For example, these three messages, trimmed to their identifying fields, come from a background subagent that Claude resumed once. The two `task_started` messages share a `task_id`, and the notification that ended the first run carries the first run's ID:
5418
5419```json theme={null}
5420{"subtype": "task_started", "task_id": "a5c2e8f41b09d7a36", "run_id": "0mubbs7i8-6f0c2d41"}
5421{"subtype": "task_notification", "task_id": "a5c2e8f41b09d7a36", "run_id": "0mubbs7i8-6f0c2d41", "status": "completed"}
5422{"subtype": "task_started", "task_id": "a5c2e8f41b09d7a36", "run_id": "0mubc18m1-b7719e05"}
5423```
5424
5406`ambient` is `true` for tasks that aren't part of the session's work, such as tasks Claude Code runs for its own operation. Live-update watchers are also ambient, including watchers the user asked for. Exclude ambient tasks from activity indicators. The field requires Agent SDK v0.3.247 or later.5425`ambient` is `true` for tasks that aren't part of the session's work, such as tasks Claude Code runs for its own operation. Live-update watchers are also ambient, including watchers the user asked for. Exclude ambient tasks from activity indicators. The field requires Agent SDK v0.3.247 or later.
5407 5426
5408`ambient` also appears on [`SDKTaskNotificationMessage`](#sdktasknotificationmessage) and on [`SDKBackgroundTasksChangedMessage`](#sdkbackgroundtaskschangedmessage) entries.5427`ambient` also appears on [`SDKTaskNotificationMessage`](#sdktasknotificationmessage) and on [`SDKBackgroundTasksChangedMessage`](#sdkbackgroundtaskschangedmessage) entries.
5424 5443
5425### `SDKTaskProgressMessage`5444### `SDKTaskProgressMessage`
5426 5445
54275446Emitted periodically while a subagent or background task is running.Emitted periodically while a subagent or background task is running. For the `run_id` field, see [`SDKTaskStartedMessage`](#sdktaskstartedmessage), which defines it and its version requirement.
5428 5447
5429For a subagent task, the `summary` field carries a model-generated progress summary and is populated only when [`agentProgressSummaries`](#options) is enabled. For a [backgrounded MCP tool call](/docs/en/mcp#automatic-backgrounding-of-long-tool-calls), `summary` carries the MCP server's latest reported progress and doesn't depend on that option.5448For a subagent task, the `summary` field carries a model-generated progress summary and is populated only when [`agentProgressSummaries`](#options) is enabled. For a [backgrounded MCP tool call](/docs/en/mcp#automatic-backgrounding-of-long-tool-calls), `summary` carries the MCP server's latest reported progress and doesn't depend on that option.
5430 5449
5433 type: "system";5452 type: "system";
5434 subtype: "task_progress";5453 subtype: "task_progress";
5435 task_id: string;5454 task_id: string;
5455 run_id?: string;
5436 tool_use_id?: string;5456 tool_use_id?: string;
5437 description: string;5457 description: string;
5438 subagent_type?: string;5458 subagent_type?: string;
5450 5470
5451### `SDKTaskUpdatedMessage`5471### `SDKTaskUpdatedMessage`
5452 5472
54535473Emitted when a background task's state changes, such as when it transitions from `running` to `completed`. Merge `patch` into your local task map keyed by `task_id`. The `end_time` field is a Unix epoch timestamp in milliseconds, comparable with `Date.now()`.Emitted when a background task's state changes, such as when it transitions from `running` to `completed`. Merge `patch` into your local task map keyed by `task_id`. The `end_time` field is a Unix epoch timestamp in milliseconds, comparable with `Date.now()`. For the `run_id` field, see [`SDKTaskStartedMessage`](#sdktaskstartedmessage), which defines it and its version requirement.
5454 5474
5455```typescript theme={null}5475```typescript theme={null}
5456type SDKTaskUpdatedMessage = {5476type SDKTaskUpdatedMessage = {
5457 type: "system";5477 type: "system";
5458 subtype: "task_updated";5478 subtype: "task_updated";
5459 task_id: string;5479 task_id: string;
5480 run_id?: string;
5460 patch: {5481 patch: {
5461 status?: "pending" | "running" | "completed" | "failed" | "killed";5482 status?: "pending" | "running" | "completed" | "failed" | "killed";
5462 description?: string;5483 description?: string;
5472 5493
5473### `SDKBackgroundTasksChangedMessage`5494### `SDKBackgroundTasksChangedMessage`
5474 5495
54755496Emitted whenever the set of live background tasks changes: a task starts, completes, or is killed; a foreground agent is backgrounded; or a task's `description`, `ambient`, or `parent_task_id` field changes. For the `parent_task_id` field on each entry, see [`SDKTaskStartedMessage`](#sdktaskstartedmessage), which defines it and its version requirement.Emitted whenever the set of live background tasks changes: a task starts, completes, or is killed; a foreground agent is backgrounded; or a task's `description`, `ambient`, or `parent_task_id` field changes. For the `run_id` and `parent_task_id` fields on each entry, see [`SDKTaskStartedMessage`](#sdktaskstartedmessage), which defines them and their version requirements.
5476 5497
5477The `tasks` array is the full live set. Replace any cached set with each payload instead of pairing `task_started` and `task_notification` events, so the next membership change corrects any event you missed.5498The `tasks` array is the full live set. Replace any cached set with each payload instead of pairing `task_started` and `task_notification` events, so the next membership change corrects any event you missed.
5478 5499
5490 subtype: "background_tasks_changed";5511 subtype: "background_tasks_changed";
5491 tasks: {5512 tasks: {
5492 task_id: string;5513 task_id: string;
5514 run_id?: string;
5493 task_type: string;5515 task_type: string;
5494 subagent_type?: string;5516 subagent_type?: string;
5495 description: string;5517 description: string;