anthropic4s
GitHub

Reference Client

Anthropic4s

Anthropic4s opens a client on the Ember HTTP client, as a cats-effect Resource. AnthropicClient.fromClient does the same over an http4s Client you already have.

Reference#

Anthropic4s.resource(config)#

ember · Anthropic4s.scala
def resource[F[_]: Async: Network](config: AnthropicConfig): Resource[F, AnthropicClient[F]]
def resource[F[_]: Async: Network](
    config: AnthropicConfig,
    telemetry: Telemetry[F]
): Resource[F, AnthropicClient[F]]

Parameters

Returns

A Resource of an AnthropicClient[F]. Connections open inside use and close when it ends.

Caveats

Anthropic4s.fromEnv#

ember · Anthropic4s.scala
def fromEnv[F[_]: Async: Network: Env]: Resource[F, AnthropicClient[F]]

Reads the configuration with AnthropicConfig.fromEnv, then opens the client as resource does.

AnthropicClient.fromClient(http, config)#

core · AnthropicClient.scala
def fromClient[F[_]: Async](
    http: Client[F],
    config: AnthropicConfig,
    telemetry: Telemetry[F]
): AnthropicClient[F]
def fromClient[F[_]: Async](http: Client[F], config: AnthropicConfig): AnthropicClient[F]

Builds a client over an http4s Client[F] you manage yourself, for example one shared with the rest of the application.

AnthropicClient[F]#

core · AnthropicClient.scala
trait AnthropicClient[F[_]]
def messages: Messages[F]
def models: Models[F]
def batches: Batches[F]
def files: Files[F]
def mapK[G[_]](fk: F ~> G): AnthropicClient[G]

messages is where most work happens. models, batches and files cover the rest of the API.

Caveats

Usage#

A first program#

ember · Anthropic4s.scala
import anthropic4s._
import cats.effect.{IO, IOApp}

object Main extends IOApp.Simple {
  def run: IO[Unit] =
    Anthropic4s.fromEnv[IO].use { client =>
      client.messages
        .create(MessageRequest(Model.ClaudeOpus5_5, 16000, MessageParam.user("Hello!")))
        .flatMap(reply => IO.println(reply.text))
    }
}

The Quick start walks through 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.