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)#
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
name, description: what the model sees.f: your function, from the decoded arguments to a result inF.In: the arguments, usually a case class. ItsSchemabecomes the tool's input schema, sent as a strict tool.Out: the result. Anything with aToolOutput:String,Unit, circeJson, content blocks, or any type with aSchema, sent as JSON.
Returns
A Tool[F] to hand to the ToolRunner.
Caveats
- Arguments that do not decode reach the model as an error result, so it can correct the call. Your function never sees them.
- A streamed call cut off mid-arguments gets an
INVALID_JSONerror result, so the model can send it again.
Tool[F]#
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)#
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]#
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#
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.