anthropic4s
GitHub

Reference Agents

ToolRunner

The agent loop: sends a request, runs the tools the model asks for, sends the results back, and repeats until the model is done.

Reference#

messages.toolRunner(tools, config?)#

core · AnthropicClient.scala
def toolRunner(tools: List[Tool[F]], config: RunnerConfig[F])(implicit F: Concurrent[F]): ToolRunner[F]
def toolRunner(tools: List[Tool[F]])(implicit F: Concurrent[F]): ToolRunner[F]
core · ToolRunner.scala
def apply[F[_]: Concurrent](
    messages: Messages[F],
    tools: List[Tool[F]],
    config: RunnerConfig[F]
): ToolRunner[F]
def apply[F[_]: Concurrent](messages: Messages[F], tools: List[Tool[F]]): ToolRunner[F]

Parameters

run, turns and events#

core · ToolRunner.scala
def run(request: MessageRequest): F[RunResult]
def turns(request: MessageRequest): Stream[F, Message]
def events(request: MessageRequest): Stream[F, RunEvent]

Caveats

RunResult#

core · ToolRunner.scala
final case class RunResult private[anthropic4s] (
    message: Message,
    transcript: NonEmptyList[MessageParam],
    usage: Usage,
    iterations: Int,
    stop: RunStop
)
def text: String
core · ToolRunner.scala
sealed trait RunStop extends Product with Serializable
case object Completed extends RunStop
case object MaxIterations extends RunStop
final case class Refused(details: Option[StopDetails]) extends RunStop
case object MaxTokens extends RunStop
final case class Aborted(reason: String) extends RunStop
final case class Other(reason: StopReason) extends RunStop

transcript is the whole conversation, ready to continue; usage adds up every turn.

RunEvent#

core · ToolRunner.scala
sealed trait RunEvent extends Product with Serializable
final case class Model(iteration: Int, event: StreamEvent) extends RunEvent
final case class TurnCompleted(iteration: Int, message: Message) extends RunEvent
final case class ToolStarted(call: ContentBlock.ToolUse) extends RunEvent
final case class ToolFinished(call: ContentBlock.ToolUse, result: ContentBlock.ToolResult) extends RunEvent
final case class Finished(result: RunResult) extends RunEvent

Caveats

Usage#

Three ways to run#

core · ToolRunner.scala
val runner = client.messages.toolRunner(List(weather, calendar))

runner.run(request)                                  // IO[RunResult]: just the outcome
runner.turns(request)                                // Stream[IO, Message]: each model reply
runner.events(request)                               // Stream[IO, RunEvent]: text deltas, tool starts, ...
README.md
final case class GetWeather(@description("City name") city: String)

val weather = Tool("get_weather", "Current weather for a city") { (in: GetWeather) =>
  weatherService.lookup(in.city)            // IO[String], IO[SomeCaseClass], ...
}

val runner = client.messages.toolRunner(List(weather))

runner.run(request)       // IO[RunResult]: the final reply, the transcript, total usage, why it stopped
runner.events(request)    // Stream[IO, RunEvent]: text deltas, tool starts and results, as they happen

Tools and agents explains the loop step by step.

Every signature and example on this page is copied, character for character, from the public source of anthropic4s and its Scaladoc. The complete API, every type included, is in the Scaladoc of each module.