Note
The Anthropic Clojure SDK is not official
The Anthropic Clojure library provides convenient access to the Anthropic REST API from any Clojure application.
The REST API documentation can be found on docs.anthropic.com.
Add the following to your deps.edn:
{:deps {anthropic-sdk-clojure {:local/root "path/to/anthropic-sdk-clojure"}}}Or add to your project.clj for Leiningen:
[anthropic-sdk-clojure "0.1.0-SNAPSHOT"](require '[anthropic.client :as anthropic])
(def message
(anthropic/create-message
{:max-tokens 1024
:messages [{:role "user" :content "Hello, Claude"}]
:model "claude-sonnet-4-20250514"
:options {:api-key (System/getenv "ANTHROPIC_API_KEY")}}))
(println (:content message))You can create a client with default options:
(require '[anthropic.client :as anthropic])
(def client
(anthropic/create-client
{:api-key (System/getenv "ANTHROPIC_API_KEY")}))
;; Create a message
(def message
((:create (:messages client))
{:max-tokens 1024
:messages [{:role "user" :content "Hello, Claude"}]
:model "claude-sonnet-4-20250514"}))Stream responses using Server-Sent Events (SSE):
(require '[anthropic.streaming :as streaming])
(let [stream (streaming/stream-messages
{:max-tokens 1024
:messages [{:role "user" :content "Hello, Claude"}]
:model "claude-sonnet-4-20250514"
:options {:api-key (System/getenv "ANTHROPIC_API_KEY")}})]
(doseq [event (:events stream)]
(println "Event:" (:event event))
(println "Data:" (:data event)))
;; Close the stream when done
((:close stream)))List methods in the Anthropic API are paginated. This library provides utilities for iterating through pages:
(require '[anthropic.models :as models]
'[anthropic.pagination :as pagination])
;; Get all models across all pages
(def all-models
(pagination/fetch-all
(fn [params]
(models/list-models (assoc params :options {:api-key (System/getenv "ANTHROPIC_API_KEY")})))
{}
:data))
;; Or iterate lazily through items
(doseq [model (pagination/page-seq
(fn [params]
(models/list-models (assoc params :options {:api-key (System/getenv "ANTHROPIC_API_KEY")}))))]
(println (:id model)))API errors are thrown as ex-info exceptions with detailed information:
(try
(anthropic/create-message
{:max-tokens 1024
:messages [{:role "user" :content "Hello, Claude"}]
:model "claude-sonnet-4-20250514"})
(catch Exception e
(let [data (ex-data e)]
(case (:type data)
:authentication (println "Authentication failed!")
:rate-limit (println "Rate limit exceeded!")
:not-found (println "Resource not found!")
(println "Error:" (ex-message e))))))Error types include:
:bad-request- HTTP 400:authentication- HTTP 401:permission-denied- HTTP 403:not-found- HTTP 404:conflict- HTTP 409:unprocessable-entity- HTTP 422:rate-limit- HTTP 429:internal-server- HTTP >= 500:api-error- Other HTTP errors
Certain errors are automatically retried 2 times by default with exponential backoff:
- Connection errors
- HTTP 408 (Request Timeout)
- HTTP 409 (Conflict)
- HTTP 429 (Rate Limit)
- HTTP >= 500 (Internal Server Error)
Configure retry behavior via options:
;; Disable retries
(anthropic/create-message
{:max-tokens 1024
:messages [{:role "user" :content "Hello"}]
:model "claude-sonnet-4-20250514"
:options {:max-retries 0}})
;; Custom retry settings
(anthropic/create-message
{:max-tokens 1024
:messages [{:role "user" :content "Hello"}]
:model "claude-sonnet-4-20250514"
:options {:max-retries 5
:initial-retry-delay 1000
:max-retry-delay 10000}})Access beta features through the beta namespace:
(require '[anthropic.beta :as beta])
;; Create message batch
(def batch
(beta/create-message-batch
{:requests [{:custom_id "req-1"
:params {:max-tokens 1024
:messages [{:role "user" :content "Hello"}]
:model "claude-sonnet-4-20250514"}}]
:options {:api-key (System/getenv "ANTHROPIC_API_KEY")}}))
;; Get batch status
(def status
(beta/get-message-batch
{:batch-id (:id batch)
:options {:api-key (System/getenv "ANTHROPIC_API_KEY")}}))Send undocumented parameters using the :extra-* options:
(anthropic/create-message
{:max-tokens 1024
:messages [{:role "user" :content "Hello"}]
:model "claude-sonnet-4-20250514"
:options {:extra-query-params {:my-query-param "value"}
:extra-body-params {:my-body-param "value"}
:extra-headers {"X-Custom-Header" "value"}}})Make requests to undocumented endpoints:
(require '[anthropic.core :as core])
(core/request
{:method :post
:path "undocumented/endpoint"
:query {:dog "woof"}
:body {:hello "world"}
:options {:extra-headers {"Useful-Header" "interesting-value"}}})anthropic.messages/create- Create a messageanthropic.messages/count-tokens- Count tokens in a messageanthropic.streaming/stream-messages- Stream a message
anthropic.models/list-models- List available modelsanthropic.models/get-model- Get model information
anthropic.beta/create-message- Create beta messageanthropic.beta/create-message-batch- Create message batchanthropic.beta/get-message-batch- Get batch statusanthropic.beta/list-message-batches- List batchesanthropic.beta/cancel-message-batch- Cancel batchanthropic.beta/get-batch-results- Get batch results
anthropic.pagination/page-seq- Lazy sequence of items across pagesanthropic.pagination/fetch-all- Fetch all items from all pagesanthropic.pagination/page-iterator- Iterator over page responses
- Clojure 1.11.1 or higher
- Java 8 or higher
MIT License - see LICENSE file for details
This is a community implementation. Contributions are welcome! Please feel free to submit issues or pull requests.