- Notifications
You must be signed in to change notification settings - Fork2
Unofficial Kotlin multiplatform variant of the Anthropic SDK
License
xemantic/anthropic-sdk-kotlin
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Unofficial Kotlin multiplatform variant of theAnthropic SDK.
Important
🤖Build Your Own AI Agents - Join our one-day Agentic AI & Creative Coding Workshop in Berlin (Spring 2025), led by AI hack Berlin hackathon winner Kazik Pogoda. Learn to create autonomous AI agents using Anthropic API, engineer advanced prompts, and give your agents tools to control machines. Workshops run Tuesdays (Feb 25 - Mar 25) at Prachtsaal Berlin, limited to 15 participants. 150 EUR contribution supports open source development (solidarity access available, no questions asked). All examples use Kotlin (crash course included) but focus on meta-principles of AI agent development. Details:https://xemantic.com/ai/workshops
Because I believe that coding AI agents should be as easy as possible. I am coming from thecreative coding community, where we are teaching artists, without prior programming experience, how to express their creations through code as a medium.I want to give creators of all kinds this extremely powerful tool, so that alsoyou can turn your own machine into an outside window, through which, the AI system can perceive your world, values and needs, and act upon this information. My first AI agent, which emerged on top of this project, is calledClaudine.
There is no official Anthropic SDK for Kotlin, a de facto standard for Android development. The one for Java is also lacking. Even if they will appear one day, we can expect them to be autogenerated by theStainless API bot, which is used by both, Anthropic and OpenAI, to automatetheir SDK development based on evolving API. While such an approach seem to work with dynamically typed languages,it might fail short with statically typed languages like Kotlin, sacrificing typical language idioms in favorofover-verbose constructs.This library is aKotlin multiplatformone, therefore your AI agents developed with it can be seamlessly used in Android, JVM, JavaScript, macOS, iOS, WebAssembly,and many other environments.
Caution
This SDK is in the early stage of development, so still a subject to API changes,however at the same time it is completely functional and passing all thetest cases.
The easiest way to use this project is to start withanthropic-sdk-kotlin-jvm-template as a template repository. There are also many ready examples and use cases in theanthropic-sdk-kotlin-demo repo.
Otherwise, you need to add to yourbuild.gradle.kts
:
dependencies { implementation("com.xemantic.ai:anthropic-sdk-kotlin:0.19.0")}
, and in case of JVM:
dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0") implementation("io.ktor:ktor-client-java:3.0.1")// or the latest ktor version// and if you don't care about configuring logging implementation("org.slf4j:slf4j-simple:2.0.16")}
, if you are planning to use tools, you will also need:
plugins {// ... other plugins like kotlin jvm or multiplatform kotlin("plugin.serialization") version"2.1.10"}dependencies { implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.7.3")}
The simplest code look like:
funmain() {val anthropic=Anthropic()val response= runBlocking { anthropic.messages.create {+"Hello, Claude" } }println(response)}
Streaming is also possible:
funmain()= runBlocking {val client=Anthropic() client.messages.stream {+"Write me a poem." } .filterIsInstance<ContentBlockDeltaEvent>() .map { (it.deltaasDelta.TextDelta).text } .collect { delta->print(delta) }}
Note
CheckTool Use conventions document for full documentation.
If you want to write AI agents, you need tools, and this is where this library shines, while removing any boilerplate code:
@SerialName("get_weather")@Description("Get the weather for a specific location")data classGetWeather(vallocation:String)funmain()= runBlocking {val tool=Tool<GetWeather> {"The weather is 73f" }val myTools=listOf(tool)val anthropic=Anthropic()val conversation= mutableListOf<Message>() conversation+="What is the weather in SF?"val initialResponse= client.messages.create { messages= conversation tools= myTools }println("Initial response:${initialResponse.text}") conversation+= initialResponse conversation+= initialResponse.useTools()val finalResponse= client.messages.create { messages= conversation tools= myTools }println("Final response:${finalResponse.text}")}
The JSON schema ofget_weather
tool is automatically extracted from the class definition and sent to the Anthropic API when creating the message containingtools
.
For the reference check equivalent examples in the official Anthropic SDKs:
- anthropic-sdk-kotlin-demo: more complex examplesand use cases
- claudine: Claudine, the only AI assistant you will ever need, the actualreason why
anthropic-sdk-kotlin
came to being, to allow me building Claudine and other AI agents.
export ANTHROPIC_API_KEY=your-key-goes-here./gradlew build
Manyunit tests are actually integration tests calling Anthropic APIsand asserting against results. This is the reason why they might be flaky from time to time. Forexample if the test image is misinterpreted, or Claude is randomly fantasizing too much.
API dependencies (will be provided as transitive dependencies ofanthropic-sdk-kotlin
):
Implementation dependencies:
About
Unofficial Kotlin multiplatform variant of the Anthropic SDK