Skip to main content

Overview

The prompt method is the core of your agent’s functionality. It handles the entire lifecycle of processing a user’s request, from receiving the initial messages to executing tool calls and returning a final response.
string
required
The session ID to process the prompt in
Message[]
required
The user messages with optional context (files, images, etc.)
StopReason
required
The reason the prompt turn ended: “end_turn”, “cancelled”, or “error”
See protocol docs: Prompt Turn

Prompt Lifecycle

The prompt method handles the complete lifecycle of a turn:
1

Receive Messages

The client sends user messages with optional context (files, images, etc.)
2

Process with Language Model

Send the messages to your language model and receive a response
3

Stream Updates to Client

Send real-time updates as the model generates content using sessionUpdate
4

Handle Tool Calls

If the model wants to use tools, request permission (if needed) and execute them
5

Return Stop Reason

Return the final stop reason indicating why the turn ended

Sending Session Updates

Use the sessionUpdate method on your AgentSideConnection to send real-time updates during prompt processing:

Message Chunks

Send text content as it’s generated by the language model:

Tool Calls

Report when the model wants to execute a tool:

Tool Calls and Permission Requests

When the language model wants to execute a sensitive operation, request permission from the user:
If the client cancels the prompt turn via session/cancel, the requestPermission call MUST respond with RequestPermissionOutcome::Cancelled.

Stop Reasons

Return one of the following stop reasons when the prompt completes:
StopReason
The language model completed its turn naturally
StopReason
The prompt was cancelled by the client via session/cancel
StopReason
An error occurred during processing

Handling Cancellation

Implement proper cancellation handling using AbortController:
Continue accepting tool call updates even after receiving a session/cancel notification, as you may need to send final updates before responding with the cancelled stop reason.

Complete Example

Here’s a complete example from the SDK’s example agent:
See the full example agent for more details.