Field Notes

I stopped wrapping the Java SDK: anthropic4s, a pure-Scala SDK for the Claude API

I stopped wrapping the Java SDK: anthropic4s, a pure-Scala SDK for the Claude API

I use Claude from Scala, mostly in Play applications. Until this week that meant calling the official Java SDK and translating at every boundary. I got fed up with it, so I built the thing I wanted instead: anthropic4s, a pure-Scala SDK for the Claude API. Version 1.0.0 is on Maven Central.

It is a community project. It is not affiliated with or endorsed by Anthropic.

What wore me down

The Java SDK is good. The problem is what it costs a Scala build and a Scala codebase.

  • The classpath. The Java SDK brings the Kotlin standard library and Jackson's Kotlin module. Play has its own opinion about Jackson versions, so each of my Play apps carried 14 lines of dependencyOverrides just to keep the two in step.
  • The types. Builders, Optional and CompletableFuture everywhere. Each call site converted to Option and Future and back.
  • Structured output. I think in case classes. Getting a typed answer meant describing the shape in Java terms and mapping the result by hand.

None of these is hard. Together they meant that every new agent started with an hour of plumbing I had already written three times.

Two columns comparing the Java SDK called from Scala with anthropic4s on classpath, build, types and structured output
The API is the same. What changes is what the dependency drags into the build and which types reach my code.

The decision: do not wrap

The obvious fix is a thin Scala layer over the Java SDK. I ruled that out first, because a wrapper keeps the classpath problem, and the classpath problem was the one I could not design around.

So anthropic4s talks to the HTTP API directly. It is built on Cats Effect and fs2, with http4s for transport and circe for JSON. The default artifact has no Kotlin and no Jackson jars on its runtime classpath.

I built it with Claude Code. I set the direction and made the calls: pure Scala, tagless final on Cats Effect, Play JSON support because that is what I know best, parity with the Java SDK for the parts I use. Claude wrote most of the code and the tests, and I ran every example against the live API myself and sent back what broke.

Case classes in, case classes out

This is the part I wanted most. You ask for a type and you get a value of that type:

import anthropic4s.schema.description

sealed trait Difficulty
object Difficulty { case object Easy extends Difficulty; case object Hard extends Difficulty }

final case class Recipe(
    name: String,
    @description("Total time in minutes") minutes: Int,
    difficulty: Difficulty,
    pairing: Option[String]
)

client.messages.parse[Recipe](request).map(_.value)   // IO[Recipe]

The JSON Schema is derived from the case class at compile time and sent as the response format, so the API guarantees a reply that fits. The decoder comes from the same derivation.

Pipeline from a case class to a strict JSON Schema to a typed value
Schema and decoder come from one derivation, so they cannot drift apart.

If you already have Play JSON Formats, the anthropic4s-play-json module pairs the schema with your format, and FutureAnthropic gives you the client as plain Futures. A Play app never has to touch IO.

Tools are functions

A tool is a function from a case class to an effect. The argument schema is derived the same way, and the arguments are decoded before the function runs:

final case class GetWeather(@description("City name, e.g. Brussels") city: String)
final case class Weather(city: String, celsius: Double, conditions: String)

val weather: Tool[IO] =
  Tool("get_weather", "Current weather for a city")((in: GetWeather) =>
    IO.pure(Weather(in.city, 7.5, "light rain"))
  )

The tool runner is the agent loop. It calls the model, runs the tools it asks for in parallel, sends the results back in order, and repeats until the model is done. A hook runs before any tool does, which is where I put approval for anything that spends money. How the run ended is a value you match on (completed, refused, out of tokens), not an exception you catch.

Seeing the agent in Tempo

I wanted the same OpenTelemetry tracing the Java SDK has, so there is an anthropic4s-otel4s module. I ran the weather agent against a local Grafana LGTM container. This is the real trace:

Grafana Tempo trace view with a tool_runner span, three messages.create spans and two custom_tool_use spans over 7.55 seconds
One agent run: six spans in 7.55 s. The runner span contains three model requests and the two tool calls between them.

The shape of the run is readable at a glance. The model asked for the weather, then asked to book a table, then answered. The tools took milliseconds and the model took the rest, which is the first thing you want to know when an agent feels slow.

Span attributes for one messages.create span showing the request model, stream flag, finish reason tool_use and time to first chunk
Each request span records the model, the finish reason, token usage and time to first chunk.

By default no prompt or reply text is put on a span. Recording content is opt-in, because traces tend to end up in places user data should not.

Getting to Maven Central

The release was the least glamorous part and it taught me two things.

First, CI treats Scaladoc warnings as errors and my local build did not. A $ inside a code sample in a doc comment was read as a Scaladoc variable. It was green on my machine and red on GitHub.

Second, Sonatype rejected the signed bundle three times with "could not find a public key", although the key was on the Ubuntu keyserver and I could fetch it myself. Uploading the same key to keys.openpgp.org fixed it on the next attempt. If you are publishing for the first time, put the key on both.

After that:

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

What is not there yet

  • Bedrock, Vertex AI and Foundry.
  • Managed Agents and the Admin API.
  • Scala 3 for anything beyond the wire model. The library targets Scala 2.13 today.

The 84 tests run offline. Against the live API I checked message creation, streaming, structured outputs, typed errors and a three-turn agent with two tools. I have not yet run it under production load, so treat 1.0.0 as a compatibility promise for the API surface, not as a claim about years of battle testing.

Why I enjoyed this one

I started because I was annoyed, and I ended with a library that other people can add in one line. Watching my own agent show up as a clean trace in Tempo, from code where every type was mine, was a very good moment.

This is one of the most exciting projects I have worked on.

Keep reading