Class BetaMessageThread
-
- All Implemented Interfaces:
public final class BetaMessageThreadA message thread (beta) keeps the conversation on the server, so each request sends only the new messages instead of the whole conversation. A thread runs one turn at a time.
Create one with MessageService.thread, which takes what MessageService.create takes and sends nothing until the first turn. For example:
BetaMessageThread thread = client.beta().messages().thread( ThreadCreateParams.builder() .model("claude-sonnet-5-5") .maxTokens(1024) .build()); BetaMessage first = thread.send("Hello"); BetaMessage second = thread.send("And again");toolRunner runs the tool loop over this thread. For replies in the JSON schema derived from a class, see StructuredBetaMessageThread.
-
-
Method Summary
Modifier and Type Method Description final List<BetaMessageParam>messages()Returns a copy of the conversation: the messages that were given and the replies. final Optional<String>previousMessageId()Returns the ID of the reply that the next turn continues from, if it continues from one. final Optional<BetaMessage>lastMessage()Returns the last reply of the thread, or an empty value before the first reply. final ThreadCreateParamsparams()Returns the parameters for MessageService.thread to create a new thread with this thread's fields and messages. final Unitappend(BetaMessageParam message)Adds message to the conversation without sending it. final Unitappend(BetaMessage message)Adds the reply message as an assistant message. final Unitappend(List<BetaMessageParam> messages)final BetaMessagesend()Sends the messages that the API does not hold yet as one turn, and returns the reply. final BetaMessagesend(String text)Appends a user message with the given text, and sends the turn. final BetaMessagesend(BetaMessageParam message)Appends message, and sends the turn. final BetaMessagesend(BetaMessage message)Appends the reply message as an assistant message, and sends the turn. final BetaMessagesend(List<BetaMessageParam> messages)Appends messages, and sends the turn. final BetaMessagesend(Consumer<ThreadSendParams.Builder> modifier)Sends a turn as the modifier shapes it. final BetaMessagesend(Consumer<ThreadSendParams.Builder> modifier, RequestOptions requestOptions)Sends a turn like send, with requestOptions for this turn only. final StreamResponse<BetaRawMessageStreamEvent>sendStreaming()Sends a turn like send, and returns the reply as a stream of events. final StreamResponse<BetaRawMessageStreamEvent>sendStreaming(String text)Appends a user message with the given text, and sends the turn. final StreamResponse<BetaRawMessageStreamEvent>sendStreaming(BetaMessageParam message)Appends message, and sends the turn. final StreamResponse<BetaRawMessageStreamEvent>sendStreaming(BetaMessage message)Appends the reply message as an assistant message, and sends the turn. final StreamResponse<BetaRawMessageStreamEvent>sendStreaming(List<BetaMessageParam> messages)Appends messages, and sends the turn. final StreamResponse<BetaRawMessageStreamEvent>sendStreaming(Consumer<ThreadSendParams.Builder> modifier)Sends a turn as the modifier shapes it, as for send. final StreamResponse<BetaRawMessageStreamEvent>sendStreaming(Consumer<ThreadSendParams.Builder> modifier, RequestOptions requestOptions)Sends a turn with requestOptions for this turn only, as for send. final Unitupdate(Consumer<ThreadSendParams.Builder> modifier)Changes the fields that the thread sends on later turns, and sends nothing. final Unitupdate(Consumer<ThreadSendParams.Builder> modifier, RequestOptions requestOptions)Changes the thread's fields like update, and the request options of later turns. final BetaMessageThreadfork()Returns a new, independent thread with a copy of this thread's conversation, fields and request options. final BetaToolRunnertoolRunner()Returns a tool runner whose turns are turns of this thread, with the thread's tools. final BetaToolRunnertoolRunner(Consumer<ToolRunnerCreateParams.Builder> modifier)Returns a tool runner like toolRunner. -
-
Method Detail
-
messages
final List<BetaMessageParam> messages()
Returns a copy of the conversation: the messages that were given and the replies.
-
previousMessageId
final Optional<String> previousMessageId()
Returns the ID of the reply that the next turn continues from, if it continues from one.
-
lastMessage
final Optional<BetaMessage> lastMessage()
Returns the last reply of the thread, or an empty value before the first reply.
-
params
final ThreadCreateParams params()
Returns the parameters for MessageService.thread to create a new thread with this thread's fields and messages.
-
append
final Unit append(BetaMessageParam message)
Adds message to the conversation without sending it. The next turn sends it.
-
append
final Unit append(BetaMessage message)
-
append
final Unit append(List<BetaMessageParam> messages)
-
send
final BetaMessage send()
Sends the messages that the API does not hold yet as one turn, and returns the reply.
-
send
final BetaMessage send(String text)
-
send
final BetaMessage send(BetaMessageParam message)
-
send
final BetaMessage send(BetaMessage message)
-
send
final BetaMessage send(List<BetaMessageParam> messages)
-
send
final BetaMessage send(Consumer<ThreadSendParams.Builder> modifier)
Sends a turn as the modifier shapes it. See send.
The fields that the modifier sets apply to this turn only, and the messages that it adds are sent with this turn. For example:
thread.send(builder -> builder.addUserMessage("In one word.").maxTokens(16));
-
send
final BetaMessage send(Consumer<ThreadSendParams.Builder> modifier, RequestOptions requestOptions)
Sends a turn like send, with requestOptions for this turn only.
-
sendStreaming
@MustBeClosed() final StreamResponse<BetaRawMessageStreamEvent> sendStreaming()
Sends a turn like send, and returns the reply as a stream of events. The turn stays in progress until the stream has been read to its end or is closed, so close the stream, for example with try-with-resources.
-
sendStreaming
@MustBeClosed() final StreamResponse<BetaRawMessageStreamEvent> sendStreaming(String text)
Appends a user message with the given text, and sends the turn. See sendStreaming.
-
sendStreaming
@MustBeClosed() final StreamResponse<BetaRawMessageStreamEvent> sendStreaming(BetaMessageParam message)
Appends message, and sends the turn. See sendStreaming.
-
sendStreaming
@MustBeClosed() final StreamResponse<BetaRawMessageStreamEvent> sendStreaming(BetaMessage message)
Appends the reply message as an assistant message, and sends the turn. See sendStreaming.
-
sendStreaming
@MustBeClosed() final StreamResponse<BetaRawMessageStreamEvent> sendStreaming(List<BetaMessageParam> messages)
Appends messages, and sends the turn. See sendStreaming.
-
sendStreaming
@MustBeClosed() final StreamResponse<BetaRawMessageStreamEvent> sendStreaming(Consumer<ThreadSendParams.Builder> modifier)
Sends a turn as the modifier shapes it, as for send. See sendStreaming.
-
sendStreaming
@MustBeClosed() final StreamResponse<BetaRawMessageStreamEvent> sendStreaming(Consumer<ThreadSendParams.Builder> modifier, RequestOptions requestOptions)
Sends a turn with requestOptions for this turn only, as for send. See sendStreaming.
-
update
final Unit update(Consumer<ThreadSendParams.Builder> modifier)
Changes the fields that the thread sends on later turns, and sends nothing.
systemandtoolscannot change once a turn has created the thread. Messages that the modifier adds are ignored: use append to add messages.
-
update
@Synchronized() final Unit update(Consumer<ThreadSendParams.Builder> modifier, RequestOptions requestOptions)
Changes the thread's fields like update, and the request options of later turns.
-
fork
final BetaMessageThread fork()
Returns a new, independent thread with a copy of this thread's conversation, fields and request options. Nothing is sent by this call.
-
toolRunner
final BetaToolRunner toolRunner()
Returns a tool runner whose turns are turns of this thread, with the thread's tools. After the run the thread holds the whole conversation.
-
toolRunner
final BetaToolRunner toolRunner(Consumer<ToolRunnerCreateParams.Builder> modifier)
Returns a tool runner like toolRunner. The modifier receives a builder that holds the thread's fields and messages, to set the runner's own options such as ToolRunnerCreateParams.Builder.maxIterations.
-
-
-
-