Skip to main content
The SDK emits different message types as the query progresses. Each message type provides specific information about the session state, AI responses, and execution results.

Message Type Overview

SDKAssistantMessage

Contains responses from the AI assistant.

Type Definition

Type Guard

Example Usage

Fields

'assistant'
Message type identifier.
string
Unique identifier for this message.
string
Session identifier.
string
API message ID.
string
Model used to generate this response (e.g., 'gpt-4', 'qwen-max').
ContentBlock[]
Array of content blocks. See Content Blocks below.
string | null
Reason why generation stopped:
  • 'end_turn': Normal completion
  • 'max_tokens': Token limit reached
  • 'stop_sequence': Stop sequence encountered
Usage
Token usage information:
string | null
ID of the parent tool use, if this is a response to a tool invocation.

SDKResultMessage

Indicates query completion with success or error information.

Type Definition

Type Guard

Example Usage

Fields

boolean
Whether the query completed with an error.
string
Result subtype:
  • 'success': Query completed successfully
  • 'error_max_turns': Exceeded maximum turns
  • 'error_during_execution': Error during execution
number
Total duration of the query in milliseconds.
number
Time spent in API calls in milliseconds.
number
Number of conversation turns executed.
string
Result message (only present when is_error is false).
ExtendedUsage
Extended usage information including web search requests and cache metrics.
Record<string, ModelUsage>
Per-model usage breakdown.
CLIPermissionDenial[]
Array of tools that were denied permission during execution.
object
Error information (only present when is_error is true):

SDKSystemMessage

Provides session information and configuration.

Type Definition

Type Guard

Example Usage

SDKUserMessage

Represents a user message sent to the AI.

Type Definition

Type Guard

Example Usage

You typically create these messages when using multi-turn conversations:

SDKPartialAssistantMessage

Streaming events emitted during message generation (when includePartialMessages: true).

Type Definition

Type Guard

Example Usage

Stream Event Types

MessageStartStreamEvent
Signals the start of a new message:
ContentBlockStartEvent
Signals the start of a new content block:
ContentBlockDeltaEvent
Incremental update to a content block:
Delta types:
  • { type: 'text_delta', text: string }
  • { type: 'thinking_delta', thinking: string }
  • { type: 'input_json_delta', partial_json: string }
ContentBlockStopEvent
Signals completion of a content block:
MessageStopStreamEvent
Signals completion of the entire message:

Content Blocks

Content blocks represent different types of content in messages:

TextBlock

Plain text content from the assistant.

ThinkingBlock

Internal reasoning/thinking process of the AI.

ToolUseBlock

Represents a tool being invoked.

ToolResultBlock

Result from a tool execution.

Type Guards for Content Blocks

Complete Example

See Also