anthropic4s
GitHub

Reference Agents

Tool

Something the model can call: a definition the API sees, and the code that runs when it is called.

Reference#

Tool(name, description)(f)#

core · Tool.scala
def apply[F[_], In, Out](name: String, description: String)(
    f: In => F[Out]
)(implicit F: MonadThrow[F], in: Schema[In], out: ToolOutput[Out]): Tool[F]

Parameters

Returns

A Tool[F] to hand to the ToolRunner.

Caveats

Tool[F]#

core · Tool.scala
trait Tool[F[_]]
def definition: ToolDefinition.Custom
def run(call: ContentBlock.ToolUse): F[ContentBlock.ToolResult]
def schemaIssues: List[SchemaIssue]
final def name: String

schemaIssues lists why the input schema is not strict; the runner refuses to start when any tool has some.

Tool.raw(definition)(f)#

core · Tool.scala
def raw[F[_]](
    custom: ToolDefinition.Custom
)(f: ContentBlock.ToolUse => F[ContentBlock.ToolResult]): Tool[F]

A tool with a hand-written definition that receives the raw call.

ToolOutput[A]#

core · Tool.scala
trait ToolOutput[A]
def content(value: A): ToolResultContent
implicit def fromSchema[A](implicit schema: Schema[A]): ToolOutput[A]

Usage#

A tool from a case class#

core · Tool.scala
final case class GetWeather(@description("City name, e.g. Brussels") city: String)

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

Tools and agents builds a working agent with it.

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.