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, system and tools cannot change during a run.

    Tools can be added and removed during a run, without missing the prompt cache, with addTool and removeTool.

    • Constructor Detail

    • Method Detail

      • 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-04 beta.

        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();
          }
        }
      • 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_addition block, 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 on pause_turn is resent first. Changes still queued when the run ends are never sent. Requires the inline-tools-2026-09-15 beta, which the runner does not add.

        In the rare case where a compaction response comes back without tool_changes even 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

         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-15 beta, which the runner does not add.

        A conversation that starts from a compaction block made elsewhere, whose tool_changes removes 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.