anthropic4s
GitHub

Reference Integrations

FutureAnthropic

The client for Future-based code, such as a Play application. It wraps the cats-effect client and runs each call on a cats-effect runtime.

Reference#

FutureAnthropic.start(config)#

ember · FutureAnthropic.scala
def start(config: AnthropicConfig)(implicit runtime: IORuntime = IORuntime.global): FutureAnthropic
def fromEnv()(implicit runtime: IORuntime = IORuntime.global): FutureAnthropic
def wrap(client: AnthropicClient[IO], shutdown: IO[Unit])(implicit
    runtime: IORuntime = IORuntime.global
): FutureAnthropic

Parameters

Caveats

Calls#

ember · FutureAnthropic.scala
def create(request: MessageRequest): Future[Message]
def create(request: MessageRequest, options: RequestOptions): Future[Message]
def parse[A](request: MessageRequest)(implicit schema: Schema[A]): Future[Parsed[A]]
def countTokens(request: MessageRequest): Future[TokenCount]
def stream(request: MessageRequest)(onEvent: StreamEvent => Unit): Future[Message]
def streamText(request: MessageRequest)(onText: String => Unit): Future[Message]
def runTools(request: MessageRequest, tools: List[Tool[IO]]): Future[RunResult]
def runTools(request: MessageRequest, tools: List[Tool[IO]], config: RunnerConfig[IO]): Future[RunResult]
def run[A](effect: IO[A]): Future[A]
def close(): Future[Unit]

Caveats

FutureAnthropic.tool#

ember · FutureAnthropic.scala
def tool[In, Out](name: String, description: String)(
    f: In => Future[Out]
)(implicit in: Schema[In], out: ToolOutput[Out]): Tool[IO]

A Tool from a function that returns a Future.

Usage#

From Play#

ember · FutureAnthropic.scala
@Singleton
class Claude @Inject() (lifecycle: ApplicationLifecycle)(implicit ec: ExecutionContext) {
  private val anthropic = FutureAnthropic.fromEnv()
  lifecycle.addStopHook(() => anthropic.close())

  def ask(question: String): Future[String] =
    anthropic.create(MessageRequest(Model.ClaudeOpus5_5, 16000, MessageParam.user(question))).map(_.text)
}

Using anthropic4s from Play covers the whole integration.

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.