{"id":20546211,"url":"https://github.com/xemantic/anthropic-sdk-kotlin","last_synced_at":"2026-05-23T00:10:53.981Z","repository":{"id":257832852,"uuid":"864220889","full_name":"xemantic/anthropic-sdk-kotlin","owner":"xemantic","description":"Unofficial Kotlin multiplatform variant of the Anthropic SDK","archived":false,"fork":false,"pushed_at":"2026-01-23T19:40:22.000Z","size":960,"stargazers_count":61,"open_issues_count":5,"forks_count":5,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-24T09:23:33.081Z","etag":null,"topics":["agent","agentic","agentic-ai","agentic-ai-development","ai","claude","claude-ai","claude-api","kotlin","kotlin-library","kotlin-multiplatform","ktor","ktor-client","machine-learning","sdk"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xemantic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"xemantic"}},"created_at":"2024-09-27T18:08:26.000Z","updated_at":"2026-01-23T18:57:57.000Z","dependencies_parsed_at":"2024-11-05T18:23:27.846Z","dependency_job_id":"5f64bef6-bd57-44c8-a611-aa7ae8539b24","html_url":"https://github.com/xemantic/anthropic-sdk-kotlin","commit_stats":null,"previous_names":["xemantic/anthropic-sdk-kotlin"],"tags_count":48,"template":false,"template_full_name":null,"purl":"pkg:github/xemantic/anthropic-sdk-kotlin","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xemantic%2Fanthropic-sdk-kotlin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xemantic%2Fanthropic-sdk-kotlin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xemantic%2Fanthropic-sdk-kotlin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xemantic%2Fanthropic-sdk-kotlin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xemantic","download_url":"https://codeload.github.com/xemantic/anthropic-sdk-kotlin/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xemantic%2Fanthropic-sdk-kotlin/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29492013,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-15T19:29:10.908Z","status":"ssl_error","status_checked_at":"2026-02-15T19:29:10.419Z","response_time":118,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["agent","agentic","agentic-ai","agentic-ai-development","ai","claude","claude-ai","claude-api","kotlin","kotlin-library","kotlin-multiplatform","ktor","ktor-client","machine-learning","sdk"],"created_at":"2024-11-16T02:00:43.091Z","updated_at":"2026-05-23T00:10:53.974Z","avatar_url":"https://github.com/xemantic.png","language":"Kotlin","funding_links":["https://github.com/sponsors/xemantic"],"categories":["API Wrappers","人工智能"],"sub_categories":["Kotlin","LLM客户端"],"readme":"# anthropic-sdk-kotlin\n\nUnofficial Kotlin multiplatform variant of the\n[Anthropic SDK](https://docs.anthropic.com/en/api/client-sdks).\n\n[\u003cimg alt=\"Maven Central Version\" src=\"https://img.shields.io/maven-central/v/com.xemantic.ai/anthropic-sdk-kotlin\"\u003e](https://central.sonatype.com/artifact/com.xemantic.ai/anthropic-sdk-kotlin)\n[\u003cimg alt=\"GitHub Release Date\" src=\"https://img.shields.io/github/release-date/xemantic/anthropic-sdk-kotlin\"\u003e](https://github.com/xemantic/anthropic-sdk-kotlin/releases)\n[\u003cimg alt=\"license\" src=\"https://img.shields.io/github/license/xemantic/anthropic-sdk-kotlin?color=blue\"\u003e](https://github.com/xemantic/anthropic-sdk-kotlin/blob/main/LICENSE)\n\n[\u003cimg alt=\"GitHub Actions Workflow Status\" src=\"https://img.shields.io/github/actions/workflow/status/xemantic/anthropic-sdk-kotlin/build-main.yml\"\u003e](https://github.com/xemantic/anthropic-sdk-kotlin/actions/workflows/build-main.yml)\n[\u003cimg alt=\"GitHub branch check runs\" src=\"https://img.shields.io/github/check-runs/xemantic/anthropic-sdk-kotlin/main\"\u003e](https://github.com/xemantic/anthropic-sdk-kotlin/actions/workflows/build-main.yml)\n[\u003cimg alt=\"GitHub commits since latest release\" src=\"https://img.shields.io/github/commits-since/xemantic/anthropic-sdk-kotlin/latest\"\u003e](https://github.com/xemantic/anthropic-sdk-kotlin/commits/main/)\n[\u003cimg alt=\"GitHub last commit\" src=\"https://img.shields.io/github/last-commit/xemantic/anthropic-sdk-kotlin\"\u003e](https://github.com/xemantic/anthropic-sdk-kotlin/commits/main/)\n\n[\u003cimg alt=\"GitHub contributors\" src=\"https://img.shields.io/github/contributors/xemantic/anthropic-sdk-kotlin\"\u003e](https://github.com/xemantic/anthropic-sdk-kotlin/graphs/contributors)\n[\u003cimg alt=\"GitHub commit activity\" src=\"https://img.shields.io/github/commit-activity/t/xemantic/anthropic-sdk-kotlin\"\u003e](https://github.com/xemantic/anthropic-sdk-kotlin/commits/main/)\n[\u003cimg alt=\"GitHub code size in bytes\" src=\"https://img.shields.io/github/languages/code-size/xemantic/anthropic-sdk-kotlin\"\u003e]()\n[\u003cimg alt=\"GitHub Created At\" src=\"https://img.shields.io/github/created-at/xemantic/anthropic-sdk-kotlin\"\u003e](https://github.com/xemantic/anthropic-sdk-kotlin/commits)\n[\u003cimg alt=\"kotlin version\" src=\"https://img.shields.io/badge/dynamic/toml?url=https%3A%2F%2Fraw.githubusercontent.com%2Fxemantic%2Fanthropic-sdk-kotlin%2Fmain%2Fgradle%2Flibs.versions.toml\u0026query=versions.kotlin\u0026label=kotlin\"\u003e](https://kotlinlang.org/docs/releases.html)\n[\u003cimg alt=\"ktor version\" src=\"https://img.shields.io/badge/dynamic/toml?url=https%3A%2F%2Fraw.githubusercontent.com%2Fxemantic%2Fanthropic-sdk-kotlin%2Fmain%2Fgradle%2Flibs.versions.toml\u0026query=versions.ktor\u0026label=ktor\"\u003e](https://ktor.io/)\n[\u003cimg alt=\"discord users online\" src=\"https://img.shields.io/discord/811561179280965673\"\u003e](https://discord.gg/vQktqqN2Vn)\n[![Bluesky](https://img.shields.io/badge/Bluesky-0285FF?logo=bluesky\u0026logoColor=fff)](https://bsky.app/profile/xemantic.com)\n\n\u003e [!IMPORTANT]\n\u003e 🤖 **Build Your Own AI Agents** - Details: \u003chttps://xemantic.com/ai/workshops\u003e\n\n## Why?\n\nBecause I believe that coding AI agents should be as easy as possible. I am coming from the [creative coding community](https://creativecode.berlin/), where we are teaching artists, without prior programming experience, how to express their creations through code as a medium.\nI want to give creators of all kinds this extremely powerful tool, so that also **you 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 called [Claudine](https://github.com/xemantic/claudine).\n\nThere 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 the\n[Stainless API bot](https://www.stainlessapi.com/), which is used by both, Anthropic and OpenAI, to automate\ntheir SDK development based on evolving API. While such an approach seem to work with dynamically typed languages,\nit might fail short with statically typed languages like Kotlin, sacrificing typical language idioms in favor\nof [over-verbose constructs](https://github.com/anthropics/anthropic-sdk-go/blob/main/examples/tools/main.go).\nThis library is a [Kotlin multiplatform](https://kotlinlang.org/docs/multiplatform.html)\none, therefore your AI agents developed with it can be seamlessly used in Android, JVM, JavaScript, macOS, iOS, WebAssembly,\nand many other environments.\n\n## Usage\n\n\u003e [!CAUTION]\n\u003e This SDK is in the early stage of development, so still a subject to API changes,\n\u003e however at the same time it is completely functional and passing all the\n\u003e [test cases](src/commonTest/kotlin).\n\nThe easiest way to use this project is to start with [anthropic-sdk-kotlin-jvm-template](https://github.com/xemantic/anthropic-sdk-kotlin-jvm-template) as a template repository. There are also many ready examples and use cases in the\n[anthropic-sdk-kotlin-demo](https://github.com/xemantic/anthropic-sdk-kotlin-demo) repo.\n\nOtherwise, you need to add to your `build.gradle.kts`:\n\n```kotlin\ndependencies {\n    implementation(\"com.xemantic.ai:anthropic-sdk-kotlin:0.26.0\")\n}\n```\n\n, and in case of JVM:\n\n```kotlin\ndependencies {\n    implementation(\"org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0\")\n    implementation(\"io.ktor:ktor-client-java:3.0.1\") // or the latest ktor version\n    // and if you don't care about configuring logging\n    implementation(\"org.slf4j:slf4j-simple:2.0.16\")\n}\n```\n\n, if you are planning to use tools, you will also need:\n\n```kotlin\nplugins {\n    // ... other plugins like kotlin jvm or multiplatform\n    kotlin(\"plugin.serialization\") version \"2.1.10\"\n}\n\ndependencies {\n    implementation(\"org.jetbrains.kotlinx:kotlinx-serialization-core:1.7.3\")\n}\n```\n\nThe simplest code look like:\n\n```kotlin\nfun main() {\n    val anthropic = Anthropic()\n    val response = runBlocking {\n        anthropic.messages.create {\n            +\"Hello, Claude\"\n        }\n    }\n    println(response)\n}\n```\n\n### Response streaming\n\nStreaming is also possible:\n\n```kotlin\nfun main() = runBlocking {\n    val client = Anthropic()\n    client.messages.stream {\n        +\"Write me a poem.\"\n    }\n        .filter { it.delta is Event.ContentBlockDelta.Delta.TextDelta }\n        .map { (it.delta as Event.ContentBlockDelta.Delta.TextDelta).text }\n        .collect { delta -\u003e print(delta) }\n}\n```\n\nThe `toMessageResponse` function will return the complete `MessageResponse` from the stream:\n\n```kotlin\nfun main() = runBlocking {\n    val client = Anthropic()\n    val response = client.messages.stream {\n        +\"Write me a poem.\"\n    }\n        .onEach { println(\"Event: $it\") }\n        .toMessageResponse()\n    println(response)\n}\n```\n\n### Customizing the HTTP client\n\nUnder the hood the SDK uses [ktor](https://ktor.io/)'s `HttpClient`. The `httpClientConfig` block on `Anthropic.Config` is applied last, so it can install additional plugins or layer extra defaults on top of the SDK's own configuration. This is the place to add custom headers (for example `Authorization: Bearer …` when routing through a gateway), tweak timeouts, or attach a custom logger.\n\n```kotlin\nval anthropic = Anthropic {\n    httpClientConfig = {\n        install(HttpTimeout) {\n            requestTimeoutMillis = 60_000\n        }\n        defaultRequest {\n            header(\"Authorization\", \"Bearer $gatewayToken\")\n            header(\"X-Tenant-Id\", tenantId)\n        }\n    }\n}\n```\n\nNotes:\n\n- Multiple `defaultRequest { }` blocks accumulate in ktor 3.x, so headers added here are appended to the SDK's defaults (`x-api-key`, `anthropic-version`, etc.) rather than replacing them.\n- `apiKey` is still required at construction time and the SDK will always send `x-api-key`. If your gateway only accepts a Bearer token, supply a placeholder `apiKey` and have the gateway strip the unwanted header.\n- For proxies and self-hosted gateways, override the base URL via `apiBase` instead of trying to rewrite it from `httpClientConfig`.\n- Avoid re-installing plugins already set up by the SDK (`SSE`, `ContentNegotiation`, `Logging`, `HttpRequestRetry`) — ktor will fail at install time or silently override SDK behavior.\n\n### Using tools\n\n\u003e [!NOTE]\n\u003e Check [Tool Use Guide](docs/tool_use_guide.md) document for full documentation.\n\nIf you want to write AI agents, you need tools, and this is where this library shines, while removing any boilerplate code:\n\n```kotlin\n@Description(\"Get the weather for a specific location\")\ndata class GetWeather(val location: String)\n\nfun main() = runBlocking {\n    val toolbox = Toolbox {\n        tool\u003cGetWeather\u003e {\n            \"The weather in $location is 73f\" // it would be returned by API\n        }\n    }\n    val anthropic = Anthropic {\n        defaultTools = toolbox.tools\n    }\n\n    val conversation = mutableListOf\u003cMessage\u003e()\n    conversation += \"What is the weather in SF?\"\n\n    val initialResponse = client.messages.create {\n        messages = conversation\n    }\n    println(\"Initial response: ${initialResponse.text}\")\n\n    conversation += initialResponse\n    conversation += initialResponse.useTools(toolbox)\n\n    val finalResponse = client.messages.create {\n        messages = conversation\n    }\n    println(\"Final response: ${finalResponse.text}\")\n}\n```\n\nThe JSON schema of `get_weather` tool is automatically extracted from the class definition and sent to the Anthropic API when creating the message containing `tools`.\n\nFor the reference check equivalent examples in the official Anthropic SDKs:\n\n* [TypeScript](https://github.com/anthropics/anthropic-sdk-typescript/blob/main/examples/tools.ts)\n* [Python](https://github.com/anthropics/anthropic-sdk-python/blob/main/examples/tools.py)\n* [Go](https://github.com/anthropics/anthropic-sdk-go/blob/main/examples/tools/main.go)\n\n### Calculating usage costs\n\nEach `MessageResponse` carries the `Usage` reported by the API. Cost is computed on the caller side — `response.usage * model.cost` for a single response, or accumulate across calls with `costWithUsage += response.usage.pricedBy(model)`. See the [Cost aggregation guide](docs/cost_aggregation.md) for details, including how to share an accumulator across coroutines.\n\n## Projects using anthropic-sdk-kotlin\n\n* [anthropic-sdk-kotlin-demo](https://github.com/xemantic/anthropic-sdk-kotlin-demo): more complex examples\n  and use cases\n* [claudine](https://github.com/xemantic/claudine): Claudine, the only AI assistant you will ever need, the actual\n  reason why `anthropic-sdk-kotlin` came to being, to allow me building Claudine and other AI agents.\n\n## Building the project\n\n```shell\nexport ANTHROPIC_API_KEY=your-key-goes-here\n./gradlew build\n```\n\nMany [unit tests](src/commonTest/kotlin) are actually integration tests calling Anthropic APIs\nand asserting against results. Tests default to Claude Haiku model to reduce API costs.\nThese integration test might be flaky from time to time. For example, if the test image\nis misinterpreted, or Claude is randomly fantasizing too much.\n\n### Environment variables in browser tests\n\nBrowser-based tests for the `js` and `wasmJs` targets run inside a headless Chrome via Karma and have no access to the operating system environment. Any env var that a browser test needs to read (via `process.env.X` or the `getEnv(name)` helper from `xemantic-kotlin-test`) must be explicitly injected into the Webpack bundle through [`webpack.config.d/env-config.js`](webpack.config.d/env-config.js), which uses Webpack's `DefinePlugin` to substitute `process.env.X` references at bundle time from the OS environment of the Gradle process.\n\nWhen adding a new integration test that depends on a new env var, update three places:\n\n1. `build.gradle.kts` — forward the var to the test task via `environment(name, value)` for `KotlinJvmTest`, `KotlinJsTest`, and `KotlinNativeTest` (and `SIMCTL_CHILD_*` for iOS simulator).\n2. `webpack.config.d/env-config.js` — add `'\u003cNAME\u003e': JSON.stringify(process.env.\u003cNAME\u003e)` so browser bundles can see it.\n3. The CI workflow secrets — ensure the var is exported into the Gradle process by the reusable workflow at `xemantic/.github`.\n\nWithout step 2, the JVM, Native and Node-based JS/wasmJs tests pass while the browser test fails with \"No such environment variable: \\\u003cNAME\\\u003e\".\n\n## Project dependencies\n\nAPI dependencies (will be provided as transitive dependencies of `anthropic-sdk-kotlin`):\n\n* [xemantic-ai-tool-schema](https://github.com/xemantic/xemantic-ai-tool-schema)\n* [xemantic-ai-money](https://github.com/xemantic/xemantic-ai-money)\n* [xemantic-kotlin-core](https://github.com/xemantic/xemantic-kotlin-core)\n\nImplementation dependencies:\n\n* [ktor](https://ktor.io/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxemantic%2Fanthropic-sdk-kotlin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxemantic%2Fanthropic-sdk-kotlin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxemantic%2Fanthropic-sdk-kotlin/lists"}