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)#
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
config: theAnthropicConfig: credentials, base URL, timeout, retries.telemetry: optional. ATelemetry[F]such asOtelTelemetry; none by default.
Returns
A Resource of an AnthropicClient[F]. Connections open inside use and close when it ends.
Caveats
- Create the client once and share it: it is safe to use from many fibers at the same time.
- Ember's own request and idle timeouts are switched off. The SDK applies
config.timeoutinstead, because a model may think for minutes.
Anthropic4s.fromEnv#
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)#
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]#
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
- After
mapK,filesthrows: uploads and downloads stream bytes inFand cannot be translated.
Usage#
A first program#
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.