You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 75f18f6
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: content/en/docs/claude-code/agent-sdk/hooks.md
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -771,7 +771,9 @@ Claude Code runs each callback with a timeout, which you set in seconds with the
771
771
772
772
When a callback exceeds its timeout, Claude Code cancels it and discards its output, and the session continues rather than hanging. What happens next depends on the event:
773
773
774
-
*`PreToolUse`: Claude Code blocks the tool call. In its place, Claude receives a tool result saying the hook didn't respond before its timeout, and the turn continues. Before v2.1.210, Claude Code reported the timeout to Claude as a user rejection, which made unattended sessions stop and wait for input.
774
+
*`PreToolUse`: Claude Code blocks the tool call. If every other hook returned nothing or only an `allow` decision, the tool result tells Claude the hook didn't respond before its timeout, and the turn continues. If another of your hooks already denied the call, its deny still reaches Claude: the tool result includes that hook's reason, and the timeout text can follow it.
775
+
776
+
Before v2.1.210, Claude Code reported the timeout to Claude as a user rejection, which made unattended sessions stop and wait for input.
775
777
*`PostToolUse` and `PostToolUseFailure`: Claude Code keeps the tool result and the turn continues.
776
778
*`UserPromptSubmit` and [`UserPromptExpansion`](https://code.claude.com/docs/en/hooks#userpromptexpansion): Claude Code blocks the prompt with a message naming the hook and the timeout, and the session continues. Before v2.1.208, Claude Code ended the query with `error_during_execution` when a callback on these events timed out.
777
779
*`Stop` and `SubagentStop`: the timed-out callback counts as returning no decision. The agent or subagent stops as if that callback had allowed it, and a decision from your other hooks on the event still applies. Before Claude Code v2.1.273, a timed-out `Stop` or `SubagentStop` callback counted as a failed hook run, and Claude Code discarded the decisions of your other hooks on the event.
Copy file name to clipboardExpand all lines: content/en/docs/claude-code/agent-sdk/typescript.md
+59-1Lines changed: 59 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -309,12 +309,13 @@ function getSessionMessages(
309
309
|`options.dir`|`string`|`undefined`| Project directory to find the session in. When omitted, searches all projects |
310
310
|`options.limit`|`number`|`undefined`| Maximum number of messages to return |
311
311
|`options.offset`|`number`|`undefined`| Number of messages to skip from the start |
312
+
|`options.includeSystemMessages`|`boolean`|`false`| When `true`, also return system messages such as compact boundaries alongside user and assistant messages |
312
313
313
314
#### Return type: `SessionMessage`
314
315
315
316
| Property | Type | Description |
316
317
| :- | :- | :- |
317
-
|`type`|`"user" \| "assistant"`| Message role |
318
+
|`type`|`"user" \| "assistant" \| "system"`| Message role. `"system"` appears only when `options.includeSystemMessages` is `true`|
318
319
|`uuid`|`string`| Unique message identifier |
319
320
|`session_id`|`string`| Session this message belongs to |
320
321
|`message`|`unknown`| Raw message payload from the transcript |
|`reloadPlugins(options?)`| Reloads plugins from disk, so plugins you install or edit mid-session reach the running session. Resolves with an [`SDKControlReloadPluginsResponse`](#sdkcontrolreloadpluginsresponse) listing the session's commands, subagents, plugins, and MCP server status. Requires Agent SDK v0.2.85 or later. The [`holdOnCacheImpact` option](#sdkcontrolreloadpluginsresponse) requires Agent SDK v0.3.268 or later |
666
673
|`reloadSkills()`| Reloads skills from disk, so skills you add or edit mid-session become available to the running session. Resolves with an [`SDKControlReloadSkillsResponse`](#sdkcontrolreloadskillsresponse) listing the skills available after the reload. Requires Agent SDK v0.3.163 or later |
667
674
|`reloadOutputStyles()`| Re-reads [output styles](https://code.claude.com/docs/en/output-styles.md) from disk, so a style file you add or edit mid-session becomes available to the running session. Resolves with an [`SDKControlReloadOutputStylesResponse`](#sdkcontrolreloadoutputstylesresponse) listing the style names available after the reload. Requires Agent SDK v0.3.261 or later |
675
+
|`seedReadState(path, mtime)`| Marks a file as already read so a later Edit doesn't fail with `File has not been read yet`. See [`seedReadState()`](#seedreadstate). Requires TypeScript SDK v0.2.83 or later |
668
676
|`accountInfo()`| Returns account information |
669
677
|`reconnectMcpServer(serverName)`| Reconnect an MCP server by name. If the name also matches an entry in a settings file such as `.mcp.json` or `~/.claude.json`, Claude Code reconnects the server you configured through [`mcpServers`](#options) or `setMcpServers()`, not the settings-file entry. That resolution order requires Claude Code v2.1.257 or later |
670
678
|`toggleMcpServer(serverName, enabled)`| Enable or disable an MCP server by name, with the same name resolution as `reconnectMcpServer()`. Disabling a server disconnects it and removes its tools. See [`toggleMcpServer()`](#togglemcpserver) for the Claude Code version this needs for each kind of server |
671
679
|`setMcpServers(servers)`| Replace the MCP servers this method manages: servers added through it and [in-process SDK servers](#createsdkmcpserver). Resolves with an [`McpSetServersResult`](#mcpsetserversresult) naming which servers were added and removed, and any errors; that section says which other servers stay connected |
680
+
|`setMcpPermissionModeOverride(serverName, mode)`| Overrides the permission mode for one MCP server's tools with a stricter mode than the session's, or clears the override with `null`. Only available in streaming input mode. See [`setMcpPermissionModeOverride()`](#setmcppermissionmodeoverride). Requires TypeScript SDK v0.3.187 or later |
672
681
|`readMcpResource(serverName, uri)`|*Alpha.* Reads one MCP Apps `ui://` resource from a connected MCP server so your application can render a tool's widget. Resolves with an [`SDKControlMcpReadResourceResponse`](#sdkcontrolmcpreadresourceresponse). Requires TypeScript Agent SDK v0.3.280 or later |
673
682
|`streamInput(stream)`| Stream input messages to the query for multi-turn conversations |
674
683
|`stopTask(taskId)`| Stop a running background task by ID |
684
+
|`backgroundTasks(toolUseId?)`| Moves the running Bash commands and subagents that the turn is waiting on to the background, the same as pressing Ctrl+B in the interactive terminal. See [`backgroundTasks()`](#backgroundtasks). Requires TypeScript SDK v0.2.139 or later |
675
685
|`close()`| Close the query and terminate the underlying process. Forcefully ends the query and cleans up all resources |
676
686
687
+
#### `setMcpPermissionModeOverride()`
688
+
689
+
Overrides the permission mode for one MCP server's tools with a stricter mode than the session's, or clears the override with `null`. Use it when a session runs in a mode that skips per-action prompts but one server's tools should still be reviewed. Requires TypeScript SDK v0.3.187 or later.
690
+
691
+
Pass one of three values as `mode`:
692
+
693
+
*`'default'`: Claude Code evaluates that server's tool calls as [Manual mode](https://code.claude.com/docs/en/permission-modes.md#available-modes) does.
694
+
*`'auto'`: Claude Code routes that server's tool calls through the [auto mode](https://code.claude.com/docs/en/permission-modes.md#eliminate-prompts-with-auto-mode) classifier. Claude Code rejects this value when the classifier isn't available to the session.
695
+
*`null`: clears the override.
696
+
697
+
The override only takes effect while the session is in a mode that skips per-action prompts: `bypassPermissions` or `auto`. In any other session mode Claude Code ignores the override, so it can never widen what the session allows.
698
+
699
+
The promise resolves with `{ warning }` when no server by that name is known yet. Claude Code stores the override anyway and applies it once a server with that exact name connects, so the warning is a typo check, not a failure.
700
+
701
+
Only available in streaming input mode, the same constraint as `setModel()` and `setPermissionMode()`.
702
+
703
+
#### `seedReadState()`
704
+
705
+
Marks a file as already read so a later Edit doesn't fail with `File has not been read yet`. Use it when your application has dropped an earlier `Read` result from the conversation context and Claude will edit that file next. Requires TypeScript SDK v0.2.83 or later.
706
+
707
+
Pass `mtime` as the file's modification time at the moment of that earlier read, in whole milliseconds since the epoch, for example `Math.floor(stat.mtimeMs)`. If the file on disk changed after that time, Claude Code skips the mark.
708
+
709
+
The promise resolves whether or not the file was marked, so a resolved promise doesn't tell you which happened.
710
+
711
+
#### `backgroundTasks()`
712
+
713
+
Moves the running Bash commands and subagents that the turn is waiting on to the background, the same as pressing Ctrl+B in the interactive terminal. Pass the `toolUseId` of a tool call to background only the Bash command or subagent that call started, or omit it to background every foreground task. Requires TypeScript SDK v0.2.139 or later.
714
+
715
+
Each backgrounded tool call returns to Claude immediately with a running-in-the-background result, the turn continues, and Claude Code emits an [`SDKTaskNotificationMessage`](#sdktasknotificationmessage) when the task finishes.
716
+
717
+
In a session where background tasks are enabled, the promise resolves to a boolean:
718
+
719
+
***`true`**: a task was backgrounded, or you omitted `toolUseId`.
720
+
***`false`**: the `toolUseId` you passed matched no foreground task.
721
+
722
+
If background tasks are disabled for the session, such as with [`CLAUDE_CODE_DISABLE_BACKGROUND_TASKS`](https://code.claude.com/docs/en/env-vars), the promise rejects instead and nothing is backgrounded.
723
+
677
724
#### `applyFlagSettings()`
678
725
679
726
Changes [settings](https://code.claude.com/docs/en/settings.md) on a running session without restarting the query. Use it when a setting that has no dedicated setter needs to change mid-session, such as tightening `permissions` after the agent reads untrusted input. `setModel()` and `setPermissionMode()` are dedicated setters for those two keys; `applyFlagSettings()` is the general form that accepts any subset of the settings keys, and passing `model` here behaves the same as `setModel()`.
@@ -1472,11 +1519,20 @@ type SDKMessage =
1472
1519
|SDKPermissionDeniedMessage
1473
1520
|SDKPromptSuggestionMessage
1474
1521
|SDKAPIRetryMessage
1522
+
|SDKControlRequestProgressMessage
1523
+
|SDKModelRefusalFallbackMessage
1524
+
|SDKModelRefusalNoFallbackMessage
1475
1525
|SDKMirrorErrorMessage
1476
1526
|SDKInformationalMessage
1477
1527
|SDKConversationResetMessage;
1478
1528
```
1479
1529
1530
+
Three messages in this union report control-request progress and model refusals. Each has `type: "system"`, so tell them apart by the `subtype` listed with each one:
1531
+
1532
+
*`SDKControlRequestProgressMessage`, `subtype: "control_request_progress"`: reports progress on a long-running control request that your client sent. `request_id` matches the message to that request, and `status` is `"started"` or `"api_retry"`. Requires Agent SDK v0.3.203 or later.
1533
+
*`SDKModelRefusalFallbackMessage`, `subtype: "model_refusal_fallback"`: emitted when the model ends the stream with a refusal and the turn is retried on a fallback model. Carries `original_model` and `fallback_model`. Requires Agent SDK v0.3.170 or later.
1534
+
*`SDKModelRefusalNoFallbackMessage`, `subtype: "model_refusal_no_fallback"`: emitted when the model ends the stream with a refusal and no retry runs, for example because no fallback model is configured. Carries `original_model`. Requires Agent SDK v0.3.193 or later.
1535
+
1480
1536
### `SDKAssistantMessage`
1481
1537
1482
1538
Assistant response message.
@@ -1636,6 +1692,7 @@ type SDKResultMessage =
1636
1692
stop_reason:string|null;
1637
1693
ttft_ms?:number;
1638
1694
ttft_stream_ms?:number;
1695
+
time_to_request_ms?:number;
1639
1696
user_message_uuid?:string;
1640
1697
user_message_uuids?:string[];
1641
1698
resume_reason?:string;
@@ -1701,6 +1758,7 @@ Several fields on the result carry diagnostic detail beyond `subtype`:
1701
1758
*`api_error_status`: the HTTP status code of the API error that terminated the conversation. Absent or `null` when the turn ended without an API error.
1702
1759
*`ttft_ms`: time to first token in milliseconds, measured when the first complete assistant message arrives. Present on the success arm only.
1703
1760
*`ttft_stream_ms`: time in milliseconds until the first `message_start` stream event, when the response stream opens. Lower than `ttft_ms`; the gap between the two is time spent streaming the first message. Present on the success arm only.
1761
+
*`time_to_request_ms`: time in milliseconds from the start of the turn until Claude Code sends the API request. Present on the success arm only. Requires Claude Code v2.1.162 or later.
1704
1762
*`user_message_uuid`: the `uuid` of the message you sent that this turn answered. See [`user_message_uuid`](#user_message_uuid) for which results carry it.
1705
1763
*`user_message_uuids`: the `uuid`s of every message you sent that Claude Code answered in this turn. See [`user_message_uuids`](#user_message_uuids).
1706
1764
*`resume_reason`: why this turn continues one that a restart interrupted. Present on both arms. See [`resume_reason`](#resume_reason).
0 commit comments