anthropic4s
GitHub

Docs Get started

Quick start

anthropic4s is a pure-Scala SDK for the Claude API, built on Cats Effect and fs2. This page gets it onto your classpath and sends one question to Claude.

You will learn

  • How to add anthropic4s with sbt or Maven
  • Which of the seven artifacts your application needs
  • How to send a request and read the reply
  • Why the client is a Resource, and why you should share one

Installation#

1.0.0 is on Maven Central for Scala 2.13, released 22 November 2026. You need JDK 17 or newer. With sbt, add one line to build.sbt:

build.sbt
libraryDependencies += "io.github.zakariahere" %% "anthropic4s" % "1.0.0"

With Maven, the Scala version is part of the artifact id:

pom.xml
<dependency>
  <groupId>io.github.zakariahere</groupId>
  <artifactId>anthropic4s_2.13</artifactId>
  <version>1.0.0</version>
</dependency>

Which artifact do you need?#

Most applications need only anthropic4s. The optional modules use the same group and version: add them when you reach the chapter that uses them.

ArtifactWhat it is
anthropic4sThe one most applications need: the client on http4s Ember, plus FutureAnthropic
anthropic4s-coreThe client on any http4s Client, the tool runner, the telemetry SPI
anthropic4s-modelRequest and response types with their JSON codecs. No Cats Effect
anthropic4s-schemaSchema[A]: JSON Schema, decoder and encoder from one derivation
anthropic4s-play-jsonStructured outputs and tool arguments from Play JSON Formats (Play guide)
anthropic4s-otel4sOpenTelemetry tracing (Tracing)
anthropic4s-testkitScriptedModel and reply builders for tests (Testing your agents)

Your first request#

Set ANTHROPIC_API_KEY in your environment. Then build a MessageRequest (a model, an output limit and the conversation) and pass it to client.messages.create:

README.md · Quick start
import anthropic4s._
import cats.effect.{IO, IOApp}

object Hello extends IOApp.Simple {
  def run: IO[Unit] =
    Anthropic4s.fromEnv[IO].use { client =>            // reads ANTHROPIC_API_KEY
      val request = MessageRequest(Model.ClaudeOpus5_5, maxTokens = 16000, MessageParam.user("Why do cats purr?"))
      client.messages.create(request).flatMap(reply => IO.println(reply.text))
    }
}

Anthropic4s.fromEnv[IO] is a Resource: the block inside use runs with an open client, and the connections are closed afterwards, even if it fails.

Check the stop reason#

The Hello program in examples/ does one more thing: it looks at why the model stopped before it trusts the content. A refusal is a normal reply, not an exception.

examples/…/Basics.scala · Hello
/** The smallest useful program: one question, one answer. Needs `ANTHROPIC_API_KEY`. */
object Hello extends IOApp.Simple {

  def run: IO[Unit] =
    // `use` opens the client, runs the block, and closes the connections afterwards, even on failure.
    Anthropic4s.fromEnv[IO].use { client =>
      val request = MessageRequest(
        Model.ClaudeOpus5_5,
        maxTokens = 16000,
        MessageParam.user("In one sentence, why do cats purr?")
      )

      client.messages.create(request).flatMap { reply =>
        // Always look at the stop reason before trusting the content.
        if (reply.isRefusal) IO.println("The model declined to answer.")
        else IO.println(reply.text) *> IO.println(reply.usage.show)
      }
    }
}

Run the examples#

Every example in these docs is a program in the repository's examples module. Clone it and run one with sbt. The tests need no network and no API key; the examples call the real API and use real tokens.

README.md · Development
sbt test                 # everything, no network and no API key needed
sbt "examples/runMain anthropic4s.examples.Hello"     # needs ANTHROPIC_API_KEY, uses real tokens
sbt prePR                # format, license headers, CI workflow

Recap

  • Add "io.github.zakariahere" %% "anthropic4s" % "1.0.0"; most applications need nothing else.
  • Anthropic4s.fromEnv[IO] reads ANTHROPIC_API_KEY and gives you a Resource.
  • One client per application, and look at the stop reason before you use the text.

Every snippet on this page is copied, character for character, from the anthropic4s repository: its README, docs/play.md or examples/, which compiles with the build.