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?)#
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]
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
tools: theTools the model may call.config: optionalRunnerConfig: iteration and concurrency limits, approval hooks.
run, turns and events#
def run(request: MessageRequest): F[RunResult]
def turns(request: MessageRequest): Stream[F, Message]
def events(request: MessageRequest): Stream[F, RunEvent]
run: just the outcome, aRunResult.turns: each model reply as it completes.events: everything: text deltas, finished turns, tool starts and results, and the result last.
Caveats
- Every request is streamed internally, so long generations never hit the non-streaming timeout.
- Cancelling the effect or the stream cancels the tools that are running.
- Tools run concurrently, up to
maxConcurrency, and their results go back in the order of the calls. - A
pause_turnstop continues the loop; a call to an unknown tool gets an error result. - When a tool's schema is not strict, the run fails with
InvalidSchemabefore sending anything.
RunResult#
final case class RunResult private[anthropic4s] (
message: Message,
transcript: NonEmptyList[MessageParam],
usage: Usage,
iterations: Int,
stop: RunStop
)
def text: String
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#
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
ToolStartedis emitted only for calls that were allowed to run.Finishedis always the last event.
Usage#
Three ways to run#
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, ...
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.