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)#
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
config: theAnthropicConfig.runtime: the cats-effect runtime that runs the calls;IORuntime.globalby default.
Caveats
- Starting opens no connection. Call
close()when the application stops.
Calls#
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]
stream, streamText: call your callback for every event, or every piece of text, and complete with the whole reply.run: runs anyIOof the underlyingclient, for the APIs without aFuturemethod.
Caveats
- The
streamcallbacks run on the SDK's threads: keep them short and never block in them.
FutureAnthropic.tool#
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#
@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.