Class BetaToolRunner
-
- All Implemented Interfaces:
-
kotlin.collections.Iterable
public final class BetaToolRunner implements Iterable<BetaMessage>
A BetaToolRunner handles the automatic conversation loop between the assistant and tools.
It's an Iterable that yields either BetaMessage objects by default or StreamResponse objects when calling streaming.
The runner keeps its conversation in a message thread by default, so each request sends only the new messages. To send the whole conversation on every request instead, set ToolRunnerCreateParams.Builder.thread to
false. Unless that is set,systemandtoolscannot change during a run.Tools can be added and removed during a run, without missing the prompt cache, with addTool and removeTool.
-
-
Method Summary
Modifier and Type Method Description final MessageCreateParamsparams()Returns the current params being used by BetaToolRunner. final Optional<BetaMessageParam>lastToolResponse()Get the tool response for the last message from the assistant. Iterator<BetaMessage>iterator()final Iterable<StreamResponse<BetaRawMessageStreamEvent>>streaming()Returns an Iterable that yields streamed assistant messages instead of buffered ones. final UnitsetNextParams(MessageCreateParams nextParams)Sets the parameters for the next API call, invalidating any cached tool response. final UnitcompactBeforeNextTurn()Compacts the conversation before the model's next turn. final UnitcompactBeforeNextTurn(BetaCompactionConfig compaction)final UnitaddTool(BetaRunnableTool tool)Gives the model another tool without changing MessageCreateParams.tools, which would miss the prompt cache. final UnitaddTool(Class<?> toolParametersType, JsonSchemaLocalValidation localValidation)Gives the model the tool defined by toolParametersType, as MessageCreateParams.Builder.addTool does up front. final UnitaddTool(Class<?> toolParametersType)Gives the model the tool defined by toolParametersType, as MessageCreateParams.Builder.addTool does up front. final UnitaddTool(McpBetaTool tool)Gives the model an MCP tool, which the runner runs when the model calls it. final UnitaddTool(BetaToolUnion definition)Gives the model a tool the runner has nothing to run for, such as a server tool (web search): definition is sent as given, and the runner stops running any tool of that name. final UnitremoveTool(String name)Takes the tool named name away from the model without changing MessageCreateParams.tools, which would miss the prompt cache. final UnitremoveTool(BetaRunnableTool tool)Takes tool away if it is the tool the runner has under its name. final UnitremoveTool(Class<?> toolParametersType)Takes away the tool that was made from toolParametersType, whether it was given to addTool or to MessageCreateParams.Builder.addTool. final UnitremoveTool(McpBetaTool tool)Takes away whatever tool the runner has under the MCP tool's name: each addTool call wraps an MCP tool anew, so there is no one object to look for. -
-
Method Detail
-
params
final MessageCreateParams params()
Returns the current params being used by BetaToolRunner.
-
lastToolResponse
final Optional<BetaMessageParam> lastToolResponse()
Get the tool response for the last message from the assistant.
Avoids redundant tool executions by caching results.
-
iterator
Iterator<BetaMessage> iterator()
-
streaming
final Iterable<StreamResponse<BetaRawMessageStreamEvent>> streaming()
Returns an Iterable that yields streamed assistant messages instead of buffered ones.
-
setNextParams
final Unit setNextParams(MessageCreateParams nextParams)
Sets the parameters for the next API call, invalidating any cached tool response.
On a message thread,
systemandtoolscannot change: use addTool and removeTool, or set ToolRunnerCreateParams.Builder.thread tofalsewhen you create the runner.
-
compactBeforeNextTurn
final Unit compactBeforeNextTurn()
Compacts the conversation before the model's next turn.
This only schedules the compaction. Once the current turn has finished, including any tool calls, the runner requests a summary and replaces the message history with the compaction response the API returns. That response is yielded like any other message, with a stop reason of BetaStopReason.COMPACTION, and the runner then carries on. If the current turn is the last one, the runner compacts and then stops.
Takes the same BetaCompactionConfig as MessageService.create does in its params, e.g. to give your own summarization instructions. Calling this again before the compaction runs replaces the pending one. Requires the
compact-2026-09-04beta.For example, to compact once the conversation grows past 100,000 input tokens:
for (BetaMessage message : toolRunner) { if (message.usage().inputTokens() > 100_000) { toolRunner.compactBeforeNextTurn(); } }
-
compactBeforeNextTurn
final Unit compactBeforeNextTurn(BetaCompactionConfig compaction)
-
addTool
final Unit addTool(BetaRunnableTool tool)
Gives the model another tool without changing MessageCreateParams.tools, which would miss the prompt cache.
The next request carries the definition in a
tool_additionblock, and the runner runs the tool straight away, in place of any tool of the same name, even for a call already in the message being handled. Changes made while handling a message go out together, in call order, as one"system"message after its tool results; a turn that stopped onpause_turnis resent first. Changes still queued when the run ends are never sent. Requires theinline-tools-2026-09-15beta, which the runner does not add.In the rare case where a compaction response comes back without
tool_changeseven though the summarized messages added or removed tools, the model goes back to the tools in MessageCreateParams.tools and the runner does not detect it. Call addTool or removeTool again after that compaction if you need the change restored.
-
addTool
@JvmOverloads() final Unit addTool(Class<?> toolParametersType, JsonSchemaLocalValidation localValidation)
Gives the model the tool defined by toolParametersType, as MessageCreateParams.Builder.addTool does up front. The tool's name is the class's
@JsonTypeNamevalue if it has one, otherwise its simple name in snake case. See addTool.
-
addTool
@JvmOverloads() final Unit addTool(Class<?> toolParametersType)
Gives the model the tool defined by toolParametersType, as MessageCreateParams.Builder.addTool does up front. The tool's name is the class's
@JsonTypeNamevalue if it has one, otherwise its simple name in snake case. See addTool.
-
addTool
final Unit addTool(McpBetaTool tool)
-
addTool
final Unit addTool(BetaToolUnion definition)
Gives the model a tool the runner has nothing to run for, such as a server tool (web search): definition is sent as given, and the runner stops running any tool of that name. See addTool.
-
removeTool
final Unit removeTool(String name)
Takes the tool named name away from the model without changing MessageCreateParams.tools, which would miss the prompt cache.
The runner stops running the tool straight away: a call to it, even one in the message being handled, gets the same error result as a call to an unknown tool. The model is told with the next request, as for addTool. addTool brings the tool back. Requires the
inline-tools-2026-09-15beta, which the runner does not add.A conversation that starts from a compaction block made elsewhere, whose
tool_changesremoves a tool that is also in MessageCreateParams.tools, needs this call too: the API applies that removal for the model, but the runner would still run the tool.
-
removeTool
final Unit removeTool(BetaRunnableTool tool)
Takes tool away if it is the tool the runner has under its name. A different tool of the same name is left alone. See removeTool.
-
removeTool
final Unit removeTool(Class<?> toolParametersType)
Takes away the tool that was made from toolParametersType, whether it was given to addTool or to MessageCreateParams.Builder.addTool. A tool that another class gives the same name is left alone, and nothing happens if no tool was made from the class. See removeTool.
-
removeTool
final Unit removeTool(McpBetaTool tool)
Takes away whatever tool the runner has under the MCP tool's name: each addTool call wraps an MCP tool anew, so there is no one object to look for. See removeTool.
-
-
-
-