The Anthropic Java SDK provides convenient access to the Anthropic REST API from applications written in Java.
The REST API documentation can be found on docs.anthropic.com. Javadocs are available on javadoc.io.
implementation("com.anthropic:anthropic-java:2.11.1")<dependency>
<groupId>com.anthropic</groupId>
<artifactId>anthropic-java</artifactId>
<version>2.11.1</version>
</dependency>This library requires Java 8 or later.
See the anthropic-java-example directory for complete and runnable examples.
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import com.anthropic.models.messages.Message;
import com.anthropic.models.messages.MessageCreateParams;
import com.anthropic.models.messages.Model;
// Configures using the `anthropic.apiKey`, `anthropic.authToken` and `anthropic.baseUrl` system properties
// Or configures using the `ANTHROPIC_API_KEY`, `ANTHROPIC_AUTH_TOKEN` and `ANTHROPIC_BASE_URL` environment variables
AnthropicClient client = AnthropicOkHttpClient.fromEnv();
MessageCreateParams params = MessageCreateParams.builder()
.maxTokens(1024L)
.addUserMessage("Hello, Claude")
.model(Model.CLAUDE_SONNET_4_20250514)
.build();
Message message = client.messages().create(params);Configure the client using system properties or environment variables:
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
// Configures using the `anthropic.apiKey`, `anthropic.authToken` and `anthropic.baseUrl` system properties
// Or configures using the `ANTHROPIC_API_KEY`, `ANTHROPIC_AUTH_TOKEN` and `ANTHROPIC_BASE_URL` environment variables
AnthropicClient client = AnthropicOkHttpClient.fromEnv();Or manually:
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
AnthropicClient client = AnthropicOkHttpClient.builder()
.apiKey("my-anthropic-api-key")
.build();Or using a combination of the two approaches:
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
AnthropicClient client = AnthropicOkHttpClient.builder()
// Configures using the `anthropic.apiKey`, `anthropic.authToken` and `anthropic.baseUrl` system properties
// Or configures using the `ANTHROPIC_API_KEY`, `ANTHROPIC_AUTH_TOKEN` and `ANTHROPIC_BASE_URL` environment variables
.fromEnv()
.apiKey("my-anthropic-api-key")
.build();See this table for the available options:
| Setter | System property | Environment variable | Required | Default value |
|---|---|---|---|---|
apiKey |
anthropic.apiKey |
ANTHROPIC_API_KEY |
false | - |
authToken |
anthropic.authToken |
ANTHROPIC_AUTH_TOKEN |
false | - |
baseUrl |
anthropic.baseUrl |
ANTHROPIC_BASE_URL |
true | "https://api.anthropic.com" |
System properties take precedence over environment variables.
Tip
Don't create more than one client in the same application. Each client has a connection pool and thread pools, which are more efficient to share between requests.
To temporarily use a modified client configuration, while reusing the same connection and thread pools, call withOptions() on any client or service:
import com.anthropic.client.AnthropicClient;
AnthropicClient clientWithOptions = client.withOptions(optionsBuilder -> {
optionsBuilder.baseUrl("https://example.com");
optionsBuilder.maxRetries(42);
});The withOptions() method does not affect the original client or service.
To send a request to the Anthropic API, build an instance of some Params class and pass it to the corresponding client method. When the response is received, it will be deserialized into an instance of a Java class.
For example, client.messages().create(...) should be called with an instance of MessageCreateParams, and it will return an instance of Message.
Important
We highly encourage you to use streaming for longer running requests.
We do not recommend setting a large maxTokens value without using streaming. Some networks may drop idle connections after a certain period of time, which can cause the request to fail or timeout without receiving a response from Anthropic. We periodically ping the API to keep the connection alive and reduce the impact of these networks.
The SDK throws an error if a non-streaming request is expected to take longer than 10 minutes. Using a streaming method or overriding the timeout at the client or request level disables the error.
Each class in the SDK has an associated builder or factory method for constructing it.
Each class is immutable once constructed. If the class has an associated builder, then it has a toBuilder() method, which can be used to convert it back to a builder for making a modified copy.
Because each class is immutable, builder modification will never affect already built class instances.
The default client is synchronous. To switch to asynchronous execution, call the async() method:
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import com.anthropic.models.messages.Message;
import com.anthropic.models.messages.MessageCreateParams;
import com.anthropic.models.messages.Model;
import java.util.concurrent.CompletableFuture;
// Configures using the `anthropic.apiKey`, `anthropic.authToken` and `anthropic.baseUrl` system properties
// Or configures using the `ANTHROPIC_API_KEY`, `ANTHROPIC_AUTH_TOKEN` and `ANTHROPIC_BASE_URL` environment variables
AnthropicClient client = AnthropicOkHttpClient.fromEnv();
MessageCreateParams params = MessageCreateParams.builder()
.maxTokens(1024L)
.addUserMessage("Hello, Claude")
.model(Model.CLAUDE_SONNET_4_20250514)
.build();
CompletableFuture<Message> message = client.async().messages().create(params);Or create an asynchronous client from the beginning:
import com.anthropic.client.AnthropicClientAsync;
import com.anthropic.client.okhttp.AnthropicOkHttpClientAsync;
import com.anthropic.models.messages.Message;
import com.anthropic.models.messages.MessageCreateParams;
import com.anthropic.models.messages.Model;
import java.util.concurrent.CompletableFuture;
// Configures using the `anthropic.apiKey`, `anthropic.authToken` and `anthropic.baseUrl` system properties
// Or configures using the `ANTHROPIC_API_KEY`, `ANTHROPIC_AUTH_TOKEN` and `ANTHROPIC_BASE_URL` environment variables
AnthropicClientAsync client = AnthropicOkHttpClientAsync.fromEnv();
MessageCreateParams params = MessageCreateParams.builder()
.maxTokens(1024L)
.addUserMessage("Hello, Claude")
.model(Model.CLAUDE_SONNET_4_20250514)
.build();
CompletableFuture<Message> message = client.messages().create(params);The asynchronous client supports the same options as the synchronous one, except most methods return CompletableFutures.
The SDK defines methods that return response "chunk" streams, where each chunk can be individually processed as soon as it arrives instead of waiting on the full response. Streaming methods generally correspond to SSE or JSONL responses.
Some of these methods may have streaming and non-streaming variants, but a streaming method will always have a Streaming suffix in its name, even if it doesn't have a non-streaming variant.
These streaming methods return StreamResponse for synchronous clients:
import com.anthropic.core.http.StreamResponse;
import com.anthropic.models.messages.RawMessageStreamEvent;
try (StreamResponse<RawMessageStreamEvent> streamResponse = client.messages().createStreaming(params)) {
streamResponse.stream().forEach(chunk -> {
System.out.println(chunk);
});
System.out.println("No more chunks!");
}Or AsyncStreamResponse for asynchronous clients:
import com.anthropic.core.http.AsyncStreamResponse;
import com.anthropic.models.messages.RawMessageStreamEvent;
import java.util.Optional;
client.async().messages().createStreaming(params).subscribe(chunk -> {
System.out.println(chunk);
});
// If you need to handle errors or completion of the stream
client.async().messages().createStreaming(params).subscribe(new AsyncStreamResponse.Handler<>() {
@Override
public void onNext(RawMessageStreamEvent chunk) {
System.out.println(chunk);
}
@Override
public void onComplete(Optional<Throwable> error) {
if (error.isPresent()) {
System.out.println("Something went wrong!");
throw new RuntimeException(error.get());
} else {
System.out.println("No more chunks!");
}
}
});
// Or use futures
client.async().messages().createStreaming(params)
.subscribe(chunk -> {
System.out.println(chunk);
})
.onCompleteFuture();
.whenComplete((unused, error) -> {
if (error != null) {
System.out.println("Something went wrong!");
throw new RuntimeException(error);
} else {
System.out.println("No more chunks!");
}
});Async streaming uses a dedicated per-client cached thread pool Executor to stream without blocking the current thread. This default is suitable for most purposes.
To use a different Executor, configure the subscription using the executor parameter:
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
Executor executor = Executors.newFixedThreadPool(4);
client.async().messages().createStreaming(params).subscribe(
chunk -> System.out.println(chunk), executor
);Or configure the client globally using the streamHandlerExecutor method:
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import java.util.concurrent.Executors;
AnthropicClient client = AnthropicOkHttpClient.builder()
.fromEnv()
.streamHandlerExecutor(Executors.newFixedThreadPool(4))
.build();The SDK provides conveniences for streamed messages. A
MessageAccumulator
can record the stream of events in the response as they are processed and accumulate a
Message object
similar to that which would have been returned by the non-streaming API.
A BetaMessageAccumulator
is also available for the accumulation of a
BetaMessage
object. It is used in the same manner as the MessageAccumulator.
For a synchronous response add a
Stream.peek()
call to the stream pipeline to accumulate each event:
import com.anthropic.core.http.StreamResponse;
import com.anthropic.helpers.MessageAccumulator;
import com.anthropic.models.messages.Message;
import com.anthropic.models.messages.RawMessageStreamEvent;
MessageAccumulator messageAccumulator = MessageAccumulator.create();
try (StreamResponse<RawMessageStreamEvent> streamResponse =
client.messages().createStreaming(createParams)) {
streamResponse.stream()
.peek(messageAccumulator::accumulate)
.flatMap(event -> event.contentBlockDelta().stream())
.flatMap(deltaEvent -> deltaEvent.delta().text().stream())
.forEach(textDelta -> System.out.print(textDelta.text()));
}
Message message = messageAccumulator.message();For an asynchronous response, add the MessageAccumulator to the subscribe() call:
import com.anthropic.helpers.MessageAccumulator;
import com.anthropic.models.messages.Message;
MessageAccumulator messageAccumulator = MessageAccumulator.create();
client.messages()
.createStreaming(createParams)
.subscribe(event -> messageAccumulator.accumulate(event).contentBlockDelta().stream()
.flatMap(deltaEvent -> deltaEvent.delta().text().stream())
.forEach(textDelta -> System.out.print(textDelta.text())))
.onCompleteFuture()
.join();
Message message = messageAccumulator.message();Anthropic Structured Outputs (beta) is a feature that ensures that the model will always generate responses that adhere to a supplied JSON schema.
A JSON schema can be defined by creating a
BetaJsonOutputFormat
and setting it on the input parameters. However, for greater convenience, a JSON schema can instead
be derived automatically from the structure of an arbitrary Java class. The JSON content from the
response will then be converted automatically to an instance of that Java class. A full, working
example of the use of Structured Outputs with arbitrary Java classes can be seen in
BetaStructuredOutputsExample.
Java classes can contain fields declared to be instances of other classes and can use collections (see Defining JSON schema properties for more details):
class Person {
public String name;
public int birthYear;
}
class Book {
public String title;
public Person author;
public int publicationYear;
}
class BookList {
public List<Book> books;
}Pass the top-level class—BookList in this example—to outputFormat(Class<T>) when building the
parameters and then access an instance of BookList from the generated message content in the
response:
import com.anthropic.models.beta.messages.MessageCreateParams;
import com.anthropic.models.beta.messages.StructuredMessageCreateParams;
import com.anthropic.models.messages.Model;
StructuredMessageCreateParams<BookList> createParams = MessageCreateParams.builder()
.model(Model.CLAUDE_SONNET_4_20250514)
.maxTokens(2048)
.outputFormat(BookList.class)
.addUserMessage("List some famous late twentieth century novels.")
.build();
client.beta().messages().create(createParams).content().stream()
.flatMap(contentBlock -> contentBlock.text().stream())
.flatMap(textBlock -> textBlock.text().books.stream())
.forEach(book -> System.out.println(book.title + " by " + book.author.name));You can start building the parameters with an instance of
MessageCreateParams.Builder
or
StructuredMessageCreateParams.Builder.
If you start with the former (which allows for more compact code) the builder type will change to
the latter when MessageCreateParams.Builder.outputFormat(Class<T>) is called.
If a field in a class is optional and does not require a defined value, you can represent this using
the java.util.Optional class.
It is up to the AI model to decide whether to provide a value for that field or leave it empty.
import java.util.Optional;
class Book {
public String title;
public Person author;
public int publicationYear;
public Optional<String> isbn;
}Generic type information for fields is retained in the class's metadata, but generic type erasure
applies in other scopes. While, for example, a JSON schema defining an array of books can be derived
from the BookList.books field with type List<Book>, a valid JSON schema cannot be derived from a
local variable of that same type, so the following will not work:
List<Book> books = new ArrayList<>();
StructuredMessageCreateParams<List<Book>> params = MessageCreateParams.builder()
.outputFormat(books.getClass())
// ...
.build();If an error occurs while converting a JSON response to an instance of a Java class, the error message will include the JSON response to assist in diagnosis. For instance, if the response is truncated, the JSON data will be incomplete and cannot be converted to a class instance. If your JSON response may contain sensitive information, avoid logging it directly, or ensure that you redact any sensitive details from the error message.
Structured Outputs supports a subset of the JSON Schema language. Schemas are generated automatically from classes to align with this subset. However, due to the inherent structure of the classes, the generated schema may still violate certain Anthropic schema restrictions, such as utilizing unsupported data types.
To facilitate compliance, the method outputFormat(Class<T>) performs a validation check on the
schema derived from the specified class. This validation ensures that all restrictions are adhered
to. If any issues are detected, an exception will be thrown, providing a detailed message outlining
the reasons for the validation failure.
- Local Validation: The validation process occurs locally, meaning no requests are sent to the remote AI model. If the schema passes local validation, it is likely to pass remote validation as well.
- Remote Validation: The remote AI model will conduct its own validation upon receiving the JSON schema in the request.
- Version Compatibility: There may be instances where local validation fails while remote validation succeeds. This can occur if the SDK version is outdated compared to the restrictions enforced by the remote AI model.
- Disabling Local Validation: If you encounter compatibility issues and wish to bypass local
validation, you can disable it by passing
JsonSchemaLocalValidation.NOto theoutputFormat(Class<T>, JsonSchemaLocalValidation)method when building the parameters. (The default value for this parameter isJsonSchemaLocalValidation.YES.)
import com.anthropic.core.JsonSchemaLocalValidation;
import com.anthropic.models.beta.messages.MessageCreateParams;
import com.anthropic.models.beta.messages.StructuredMessageCreateParams;
import com.anthropic.models.messages.Model;
StructuredMessageCreateParams<BookList> createParams = MessageCreateParams.builder()
.model(Model.CLAUDE_SONNET_4_20250514)
.maxTokens(2048)
.outputFormat(BookList.class, JsonSchemaLocalValidation.NO)
.addUserMessage("List some famous late twentieth century novels.")
.build();By following these guidelines, you can ensure that your structured outputs conform to the necessary schema requirements and minimize the risk of remote validation errors.
Structured Outputs can also be used with Streaming and the Messages API. As responses are returned in "stream events", the full response must first be accumulated to concatenate the JSON strings that can then be converted into instances of the arbitrary Java class. Normal streaming operations can be performed while accumulating the JSON strings.
Use the BetaMessageAccumulator
as described in the section on Streaming helpers to accumulate the JSON
strings. Once accumulated, use BetaMessageAccumulator.message(Class<T>) to convert the
accumulated BetaMessage into a
StructuredMessage.
The StructuredMessage can then automatically deserialize the JSON strings into instances of your
Java class.
For a full example of the usage of Structured Outputs with Streaming and the Messages API, see
BetaStructuredOutputsStreamingExample.
When a JSON schema is derived from your Java classes, all properties represented by public fields
or public getter methods are included in the schema by default. Non-public fields and getter
methods are not included by default. You can exclude public, or include non-public fields or
getter methods, by using the @JsonIgnore or @JsonProperty annotations respectively (see
Annotating classes and JSON schemas for details).
If you do not want to define public fields, you can define private fields and corresponding
public getter methods. For example, a private field myValue with a public getter method
getMyValue() will result in a "myValue" property being included in the JSON schema. If you
prefer not to use the conventional Java "get" prefix for the name of the getter method, then you
must annotate the getter method with the @JsonProperty annotation and the full method name will
be used as the property name. You do not have to define any corresponding setter methods if you do
not need them.in
Each of your classes must define at least one property to be included in the JSON schema. A validation error will occur if any class contains no fields or getter methods from which schema properties can be derived. This may occur if, for example:
- There are no fields or getter methods in the class.
- All fields and getter methods are
public, but all are annotated with@JsonIgnore. - All fields and getter methods are non-
public, but none are annotated with@JsonProperty. - A field or getter method is declared with a
Maptype. AMapis treated like a separate class with no named properties, so it will result in an empty"properties"field in the JSON schema.
You can use annotations to add further information to the JSON schema derived from your Java classes, or to control which fields or getter methods will be included in the schema. Details from annotations captured in the JSON schema may be used by the AI model to improve its response. The SDK supports the use of Jackson Databind annotations.
import com.fasterxml.jackson.annotation.JsonClassDescription;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
class Person {
@JsonPropertyDescription("The first name and surname of the person")
public String name;
public int birthYear;
@JsonPropertyDescription("The year the person died, or 'present' if the person is living.")
public String deathYear;
}
@JsonClassDescription("The details of one published book")
class Book {
public String title;
public Person author;
@JsonPropertyDescription("The year in which the book was first published.")
public int publicationYear;
@JsonIgnore public String genre;
}
class BookList {
public List<Book> books;
}- Use
@JsonClassDescriptionto add a detailed description to a class. - Use
@JsonPropertyDescriptionto add a detailed description to a field or getter method of a class. - Use
@JsonIgnoreto exclude apublicfield or getter method of a class from the generated JSON schema. - Use
@JsonPropertyto include a non-publicfield or getter method of a class in the generated JSON schema.
If you use @JsonProperty(required = false), the false value will be ignored. Anthropic JSON
schemas must mark all properties as required, so the schema generated from your Java classes will
respect that restriction and ignore any annotation that would violate it.
You can also use OpenAPI Swagger 2
@Schema and
@ArraySchema
annotations. These allow type-specific constraints to be added to your schema properties. You can
learn more about the supported constraints in the Anthropic documentation on
Supported properties.
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.media.ArraySchema;
class Article {
@ArraySchema(minItems = 1)
public List<String> authors;
public String title;
@Schema(format = "date")
public String publicationDate;
@Schema(minimum = "1")
public int pageCount;
}Local validation will check that you have not used any unsupported constraint keywords. However, the
values of the constraints are not validated locally. For example, if you use a value for the
"format" constraint of a string property that is not in the list of
supported format names,
then local validation will pass, but the AI model may report an error.
If you use both Jackson and Swagger annotations to set the same schema field, the Jackson annotation
will take precedence. In the following example, the description of myProperty will be set to
"Jackson description"; "Swagger description" will be ignored:
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import io.swagger.v3.oas.annotations.media.Schema;
class MyObject {
@Schema(description = "Swagger description")
@JsonPropertyDescription("Jackson description")
public String myProperty;
}Anthropic Tool Use lets you integrate external tools and functions directly into the AI model's responses. Instead of producing plain text, the model can output instructions (with parameters) for invoking a tool or calling a function when appropriate. You define JSON schemas for tools, and the model uses the schemas to decide when and how to use these tools, enabling more interactive, data-driven applications.
Now in beta, the tool use feature supports a "strict" mode that guarantees that the JSON output from the AI model will conform to a JSON schema that you provide in the input parameters.
Use the API to define a JSON schema describing a tool's parameters with a
BetaTool.InputSchema,
then build a BetaTool
that uses that InputSchema, then add that BetaTool to the
MessageCreateParams
using addTool. The response from the AI model may then contain "tool_use" requests to invoke
your tools (or call your functions), detailing the tools' names and their parameter values as JSON
data that conforms to the JSON schema from the InputSchema definition. You can then parse the
parameter values from this JSON, invoke your tools, and pass your tools' results back to the AI
model. A full, working example of Tool Use using the low-level API can be seen in
BetaMessagesToolsRawExample.
However, for greater convenience, the SDK can derive a tool and its parameters automatically from
the structure of an arbitrary Java class: the class's name (converted to snake case) provides the
tool name, and the class's fields define the tool's parameters. When the AI model responds with the
parameter values in JSON form, you can then easily convert that JSON to an instance of your Java
class and use the parameter values to invoke your custom tool. A full, working example of the use of
Tool Use with Java classes to define a tool and its parameters can be seen in
BetaMessagesToolsExample.
Like for Structured Outputs, Java classes can contain fields declared to be instances of other classes and can use collections (see Defining JSON schema properties for more details). Optionally, annotations can be used to set the descriptions of the tool (class) and its parameters (fields) to assist the AI model in understanding the purpose of the tool and the possible values of its parameters.
import com.fasterxml.jackson.annotation.JsonClassDescription;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
enum Unit {
CELSIUS, FAHRENHEIT;
public String toString() {
switch (this) {
case CELSIUS: return "°C";
case FAHRENHEIT: default: return "°F";
}
}
public double fromKelvin(double temperatureK) {
switch (this) {
case CELSIUS: return temperatureK - 273.15;
case FAHRENHEIT: default: return (temperatureK - 273.15) * 1.8 + 32.0;
}
}
}
@JsonClassDescription("Get the weather in a given location")
static class GetWeather {
@JsonPropertyDescription("The city and state, e.g. San Francisco, CA")
public String location;
@JsonPropertyDescription("The unit of temperature")
public Unit unit;
public Weather execute() {
double temperatureK;
switch (location) {
case "San Francisco, CA": temperatureK = 300.0; break;
case "New York, NY": temperatureK = 310.0; break;
case "Dallas, TX": temperatureK = 305.0; break;
default: temperatureK = 295; break;
}
return new Weather(String.format("%.0f%s", unit.fromKelvin(temperatureK), unit));
}
}
static class Weather {
public String temperature;
public Weather(String temperature) {
this.temperature = temperature;
}
}When your tool classes are defined, add them to the message parameters using
MessageCreateParams.addTool(Class<T>) and then call them if requested to do so in the AI model's
response.
BetaToolUseBlock.input(Class<T>)
can be used to parse a tool's parameters in JSON form to an instance of your tool-defining class.
The fields of that instance will be set to the values of the parameters to the tool use.
After invoking the tool, use
BetaToolResultBlockParam.Builder.contentAsJson(Object)
to pass the tool's result back to the AI model. The method will convert the result to JSON form for
consumption by the model. The Object can be any object, including simple String instances and
boxed primitive types.
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import com.anthropic.models.beta.messages.*;
import com.anthropic.models.messages.Model;
import java.util.List;
AnthropicClient client = AnthropicOkHttpClient.fromEnv();
MessageCreateParams.Builder createParamsBuilder = MessageCreateParams.builder()
.model(Model.CLAUDE_SONNET_4_20250514)
.maxTokens(2048)
.addTool(GetWeather.class)
.addUserMessage("What's the temperature in New York?");
client.beta().messages().create(createParamsBuilder.build()).content().stream()
.flatMap(contentBlock -> contentBlock.toolUse().stream())
.forEach(toolUseBlock -> createParamsBuilder
// Add a message indicating that the tool use was requested.
.addAssistantMessageOfBetaContentBlockParams(
List.of(BetaContentBlockParam.ofToolUse(BetaToolUseBlockParam.builder()
.name(toolUseBlock.name())
.id(toolUseBlock.id())
.input(toolUseBlock._input())
.build())))
// Add a message with the result of the requested tool use.
.addUserMessageOfBetaContentBlockParams(
List.of(BetaContentBlockParam.ofToolResult(BetaToolResultBlockParam.builder()
.toolUseId(toolUseBlock.id())
.contentAsJson(callTool(toolUseBlock))
.build()))));
client.beta().messages().create(createParamsBuilder.build()).content().stream()
.flatMap(contentBlock -> contentBlock.text().stream())
.forEach(textBlock -> System.out.println(textBlock.text()));
private static Object callTool(BetaToolUseBlock toolUseBlock) {
if (!"get_weather".equals(toolUseBlock.name())) {
throw new IllegalArgumentException("Unknown tool: " + toolUseBlock.name());
}
GetWeather tool = toolUseBlock.input(GetWeather.class);
return tool != null ? tool.execute() : new Weather("unknown");
}In the code above, an execute() method encapsulates each tool's logic. However, there is no
requirement to follow that pattern. You are free to implement your tool's logic in any way that
best suits your use case. The pattern above is only intended to suggest that a suitable pattern
may make the process of tool use simpler to understand and implement.
The tool names are derived from the camel case tool class names (e.g., GetWeather) and converted
to snake case (e.g., get_weather). All characters are converted to lower-case and underscores are
inserted on word boundaries. Word boundaries begin where the current character is not the first
character, is upper-case, and either the preceding character is lower-case, or the following
character is lower-case. For example, MyJSONParser becomes my_json_parser and ParseJSON
becomes parse_json. This conversion can be overridden using the @JsonTypeName annotation (see
Annotating tool classes)
Like for Structured Outputs, you can perform local validation to check that the JSON schema
derived from your tool class respects the restrictions imposed by Anthropic on such schemas. Local
validation is enabled by default, but it can be disabled by adding JsonSchemaLocalValidation.NO to
the call to addTool.
MessageCreateParams.Builder createParamsBuilder = MessageCreateParams.builder()
.model(Model.CLAUDE_SONNET_4_20250514)
.maxTokens(2048)
.addTool(GetWeather.class, JsonSchemaLocalValidation.NO)
.addUserMessage("What's the temperature in New York?");See Local JSON schema validation for more details on local schema validation and under what circumstances you might want to disable it.
You can use annotations to add further information about tools to the JSON schemas that are derived from your tool classes, or to control which fields or getter methods will be used as parameters to the tool. Details from annotations captured in the JSON schema may be used by the AI model to improve its response. The SDK supports the use of Jackson Databind annotations.
- Use
@JsonClassDescriptionto add a description to a tool class detailing when and how to use that tool. - Use
@JsonTypeNameto set the tool name to something other than the simple name of the class converted to snake case, which is used by default. - Use
@JsonPropertyDescriptionto add a detailed description to a tool parameter (a field or getter method of a tool class). Where JSON schema constraints are not supported, these might be added as textual descriptions using this annotation. - Use
@JsonIgnoreto exclude apublicfield or getter method of a class from the generated JSON schema for a tool's parameters. - Use
@JsonPropertyto include a non-publicfield or getter method of a class in the generated JSON schema for a tool's parameters.
Anthropic provides some Best practices for defining tools that may help you to understand how to use the above annotations effectively for your tools.
See also Defining JSON schema properties for more details on how to use fields and getter methods and combine access modifiers and annotations to define the parameters of your tools. The same rules apply to tool classes and to the structured output classes described in that section.
The SDK defines methods that accept files, the main interface for which is exposed through MultipartField:
import com.anthropic.core.MultipartField;
import com.anthropic.models.beta.files.FileMetadata;
import com.anthropic.models.beta.files.FileUploadParams;
import com.anthropic.models.beta.AnthropicBeta;
import java.io.InputStream;
import java.nio.file.Paths;
FileUploadParams params = FileUploadParams.builder()
.file(MultipartField.<InputStream>builder()
.value(Files.newInputStream(Paths.get("/path/to/file.pdf")))
.contentType("application/pdf") // content type must be manually specified
.build())
.addBeta(AnthropicBeta.FILES_API_2025_04_14)
.build();
FileMetadata fileMetadata = client.beta().files().upload(params);Or an arbitrary InputStream:
import com.anthropic.core.MultipartField;
import com.anthropic.models.beta.files.FileMetadata;
import com.anthropic.models.beta.files.FileUploadParams;
import com.anthropic.models.beta.AnthropicBeta;
import java.io.InputStream;
import java.net.URL;
FileUploadParams params = FileUploadParams.builder()
.file(MultipartField.<InputStream>builder()
.value(new URL("https://example.com/path/to/file").openStream())
.filename("document.pdf")
.contentType("application/pdf")
.build())
.addBeta(AnthropicBeta.FILES_API_2025_04_14)
.build();
FileMetadata fileMetadata = client.beta().files().upload(params);Or a byte[] array:
import com.anthropic.core.MultipartField;
import com.anthropic.models.beta.files.FileMetadata;
import com.anthropic.models.beta.files.FileUploadParams;
import com.anthropic.models.beta.AnthropicBeta;
FileUploadParams params = FileUploadParams.builder()
.file(MultipartField.<byte[]>builder()
.value("content".getBytes())
.filename("document.txt")
.contentType("text/plain")
.build())
.addBeta(AnthropicBeta.FILES_API_2025_04_14)
.build();
FileMetadata fileMetadata = client.beta().files().upload(params);Note that you can also pass certain values directly, however this is not recommended as the files API will not infer the correct content-type for you.
FileUploadParams params = FileUploadParams.builder()
.file(Paths.get("/path/to/file"))
.addBeta(AnthropicBeta.FILES_API_2025_04_14)
.build();The SDK defines methods that return binary responses, which are used for API responses that shouldn't necessarily be parsed, like non-JSON data.
These methods return HttpResponse:
import com.anthropic.core.http.HttpResponse;
import com.anthropic.models.beta.files.FileDownloadParams;
HttpResponse response = client.beta().files().download("file_id");To save the response content to a file, use the Files.copy(...) method:
import com.anthropic.core.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
try (HttpResponse response = client.beta().files().download(params)) {
Files.copy(
response.body(),
Paths.get(path),
StandardCopyOption.REPLACE_EXISTING
);
} catch (Exception e) {
System.out.println("Something went wrong!");
throw new RuntimeException(e);
}Or transfer the response content to any OutputStream:
import com.anthropic.core.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Paths;
try (HttpResponse response = client.beta().files().download(params)) {
response.body().transferTo(Files.newOutputStream(Paths.get(path)));
} catch (Exception e) {
System.out.println("Something went wrong!");
throw new RuntimeException(e);
}The SDK defines methods that deserialize responses into instances of Java classes. However, these methods don't provide access to the response headers, status code, or the raw response body.
To access this data, prefix any HTTP method call on a client or service with withRawResponse():
import com.anthropic.core.http.Headers;
import com.anthropic.core.http.HttpResponseFor;
import com.anthropic.models.messages.Message;
import com.anthropic.models.messages.MessageCreateParams;
import com.anthropic.models.messages.Model;
MessageCreateParams params = MessageCreateParams.builder()
.maxTokens(1024L)
.addUserMessage("Hello, Claude")
.model(Model.CLAUDE_SONNET_4_20250514)
.build();
HttpResponseFor<Message> message = client.messages().withRawResponse().create(params);
int statusCode = message.statusCode();
Headers headers = message.headers();You can still deserialize the response into an instance of a Java class if needed:
import com.anthropic.models.messages.Message;
Message parsedMessage = message.parse();For more information on debugging requests, see the API docs.
When using raw responses, you can access the request-id response header using the requestId() method:
import com.anthropic.core.http.HttpResponseFor;
import com.anthropic.models.messages.Message;
import java.util.Optional;
HttpResponseFor<Message> message = client.messages().withRawResponse().create(params);
Optional<String> requestId = message.requestId();This can be used to quickly log failing requests and report them back to Anthropic.
The SDK throws custom unchecked exception types:
-
AnthropicServiceException: Base class for HTTP errors. See this table for which exception subclass is thrown for each HTTP status code:Status Exception 400 BadRequestException401 UnauthorizedException403 PermissionDeniedException404 NotFoundException422 UnprocessableEntityException429 RateLimitException5xx InternalServerExceptionothers UnexpectedStatusCodeExceptionSseExceptionis thrown for errors encountered during SSE streaming after a successful initial HTTP response. -
AnthropicIoException: I/O networking errors. -
AnthropicRetryableException: Generic error indicating a failure that could be retried by the client. -
AnthropicInvalidDataException: Failure to interpret successfully parsed data. For example, when accessing a property that's supposed to be required, but the API unexpectedly omitted it from the response. -
AnthropicException: Base class for all exceptions. Most errors will result in one of the previously mentioned ones, but completely generic errors may be thrown using the base class.
The SDK defines methods that return a paginated lists of results. It provides convenient ways to access the results either one page at a time or item-by-item across all pages.
To iterate through all results across all pages, use the autoPager() method, which automatically fetches more pages as needed.
When using the synchronous client, the method returns an Iterable
import com.anthropic.models.messages.batches.BatchListPage;
import com.anthropic.models.messages.batches.MessageBatch;
BatchListPage page = client.messages().batches().list();
// Process as an Iterable
for (MessageBatch batch : page.autoPager()) {
System.out.println(batch);
}
// Process as a Stream
page.autoPager()
.stream()
.limit(50)
.forEach(batch -> System.out.println(batch));When using the asynchronous client, the method returns an AsyncStreamResponse:
import com.anthropic.core.http.AsyncStreamResponse;
import com.anthropic.models.messages.batches.BatchListPageAsync;
import com.anthropic.models.messages.batches.MessageBatch;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
CompletableFuture<BatchListPageAsync> pageFuture = client.async().messages().batches().list();
pageFuture.thenRun(page -> page.autoPager().subscribe(batch -> {
System.out.println(batch);
}));
// If you need to handle errors or completion of the stream
pageFuture.thenRun(page -> page.autoPager().subscribe(new AsyncStreamResponse.Handler<>() {
@Override
public void onNext(MessageBatch batch) {
System.out.println(batch);
}
@Override
public void onComplete(Optional<Throwable> error) {
if (error.isPresent()) {
System.out.println("Something went wrong!");
throw new RuntimeException(error.get());
} else {
System.out.println("No more!");
}
}
}));
// Or use futures
pageFuture.thenRun(page -> page.autoPager()
.subscribe(batch -> {
System.out.println(batch);
})
.onCompleteFuture()
.whenComplete((unused, error) -> {
if (error != null) {
System.out.println("Something went wrong!");
throw new RuntimeException(error);
} else {
System.out.println("No more!");
}
}));To access individual page items and manually request the next page, use the items(),
hasNextPage(), and nextPage() methods:
import com.anthropic.models.messages.batches.BatchListPage;
import com.anthropic.models.messages.batches.MessageBatch;
BatchListPage page = client.messages().batches().list();
while (true) {
for (MessageBatch batch : page.items()) {
System.out.println(batch);
}
if (!page.hasNextPage()) {
break;
}
page = page.nextPage();
}This SDK also provides support for the
Anthropic Bedrock API. This support
requires the anthropic-java-bedrock library dependency.
implementation("com.anthropic:anthropic-java-bedrock:2.11.1")<dependency>
<groupId>com.anthropic</groupId>
<artifactId>anthropic-java-bedrock</artifactId>
<version>2.11.1</version>
</dependency>To use Anthropic on Bedrock, create the Anthropic client with the
BedrockBackend.
Usage of the API is otherwise the same.
import com.anthropic.bedrock.backends.BedrockBackend;
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(BedrockBackend.fromEnv())
.build();BedrockBackend.fromEnv() automatically resolves the AWS credentials using the
AWS default credentials provider chain
and resolves the AWS region using the
AWS default region provider chain.
See those AWS documents for details on how to configure the AWS credentials and
AWS region for resolution by those provider chains.
Instead of resolving the AWS credentials and AWS region using the default AWS
provider chains, you can resolve them independently using any provider, or any
scheme of your choice, and pass them directly to the BedrockBackend during
building. For example, you can resolve the AWS credentials directly from
environment variables and hard-code the AWS region:
import com.anthropic.bedrock.backends.BedrockBackend;
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.regions.Region;
AwsCredentials awsCredentials = AwsBasicCredentials.create(
System.getenv("AWS_ACCESS_KEY_ID"),
System.getenv("AWS_SECRET_ACCESS_KEY"));
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(BedrockBackend.builder()
.awsCredentials(awsCredentials)
.region(Region.US_EAST_1)
.build())
.build();You can also create and configure your own AWS credentials provider and set it when building a
BedrockBackend. For example, you can use the AWS DefaultCredentialsProvider, but enable
automatic asynchronous refreshing of credentials:
import com.anthropic.bedrock.backends.BedrockBackend;
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
AwsCredentialsProvider awsCredentialsProvider =
DefaultCredentialsProvider.builder()
.asyncCredentialUpdateEnabled(true)
.build();
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(BedrockBackend.builder()
.fromEnv(awsCredentialsProvider)
.build())
.build();The AWS classes used above are included automatically as transitive dependencies
of the anthropic-java-bedrock library dependency. For other resolution
schemes, you may need additional AWS dependencies.
Currently, the Bedrock backend does not support the following:
- Anthropic Batch API
- Anthropic Token Counting API
The BedrockBackend can also use an API key instead of AWS credentials for request authorization.
See the AWS documentation
for details on API keys and how to generate them.
You can set the AWS_BEARER_TOKEN_BEDROCK environment variable to the value of your API key and
call BedrockBackend.fromEnv() to authorize requests using that API key. An API key will be used in
preference to AWS credentials if both are set in the environment. If calling
BedrockBackend.Builder.fromEnv(AwsCredentialsProvider) with a non-null provider instance, that
provider's credentials will take precedence over any API key set in the environment.
The API key can also be passed directly to the backend, so you can resolve it from a source other than an environment variable, if preferred:
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(BedrockBackend.builder()
.apiKey(myApiKey)
.region(Region.US_EAST_1)
.build())
.build();An error will occur if you set both an API key and an AWS credentials provider.
This SDK also provides support for Anthropic models on the
Google Cloud Vertex AI platform.
This support requires the anthropic-java-vertex library dependency.
implementation("com.anthropic:anthropic-java-vertex:2.11.1")<dependency>
<groupId>com.anthropic</groupId>
<artifactId>anthropic-java-vertex</artifactId>
<version>2.11.1</version>
</dependency>To use Anthropic on Vertex AI, create the Anthropic client with the
VertexBackend. Usage of the API is otherwise the same.
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import com.anthropic.vertex.backends.VertexBackend;
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(VertexBackend.fromEnv())
.build();VertexBackend.fromEnv() automatically resolves the Google OAuth2 credentials
from your configured Google Cloud
Application Default Credentials
(ADC), the Google Cloud region from the CLOUD_ML_REGION environment variable,
and the Google Cloud project ID from ANTHROPIC_VERTEX_PROJECT_ID environment
variable. See the Google documentation for details on how to configure your ADC.
Instead of resolving the Google ADC, region and project ID automatically using
fromEnv(), you can resolve them independently using an alternative Google
Cloud facility, or any scheme of your choice, and pass them directly to the
VertexBackend during building. For example, you could resolve the Google
credentials and project ID directly from environment variables and hard-code the
Google Cloud region:
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import com.anthropic.vertex.backends.VertexBackend;
import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.GoogleCredentials;
String accessToken = System.getenv("GOOGLE_APPLICATION_CREDENTIALS");
String project = System.getenv("ANTHROPIC_VERTEX_PROJECT_ID");
GoogleCredentials googleCredentials = GoogleCredentials.create(
AccessToken.newBuilder().setTokenValue(accessToken).build());
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(VertexBackend.builder()
.googleCredentials(googleCredentials)
.region("us-central1")
.project(project)
.build())
.build();The Google Cloud classes used above are included automatically as transitive
dependencies of the anthropic-java-vertex library dependency. For other
resolution schemes, you may need additional Google Cloud dependencies.
Currently, the Vertex backend does not support the following:
- Anthropic Batch API
The SDK uses the standard OkHttp logging interceptor.
Enable logging by setting the ANTHROPIC_LOG environment variable to info:
$ export ANTHROPIC_LOG=infoOr to debug for more verbose logging:
$ export ANTHROPIC_LOG=debugAlthough the SDK uses reflection, it is still usable with ProGuard and R8 because anthropic-java-core is published with a configuration file containing keep rules.
ProGuard and R8 should automatically detect and use the published rules, but you can also manually copy the keep rules if necessary.
The SDK depends on Jackson for JSON serialization/deserialization. It is compatible with version 2.13.4 or higher, but depends on version 2.18.2 by default.
The SDK throws an exception if it detects an incompatible Jackson version at runtime (e.g. if the default version was overridden in your Maven or Gradle config).
If the SDK threw an exception, but you're certain the version is compatible, then disable the version check using the checkJacksonVersionCompatibility on AnthropicOkHttpClient or AnthropicOkHttpClientAsync.
Caution
We make no guarantee that the SDK works correctly when the Jackson version check is disabled.
The SDK automatically retries 2 times by default, with a short exponential backoff between requests.
Only the following error types are retried:
- Connection errors (for example, due to a network connectivity problem)
- 408 Request Timeout
- 409 Conflict
- 429 Rate Limit
- 5xx Internal
The API may also explicitly instruct the SDK to retry or not retry a request.
To set a custom number of retries, configure the client using the maxRetries method:
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
AnthropicClient client = AnthropicOkHttpClient.builder()
.fromEnv()
.maxRetries(4)
.build();Requests time out after 10 minutes by default.
However, for methods that accept maxTokens, if you specify a large maxTokens value and are not streaming, then the default timeout will be calculated dynamically using this formula:
Duration.ofSeconds(
Math.min(
60 * 60, // 1 hour max
Math.max(
10 * 60, // 10 minute minimum
60 * 60 * maxTokens / 128_000
)
)
)Which results in a timeout of up to 60 minutes, scaled by the maxTokens parameter, unless overridden.
To set a custom timeout, configure the method call using the timeout method:
import com.anthropic.models.messages.Message;
Message message = client.messages().create(
params, RequestOptions.builder().timeout(Duration.ofSeconds(30)).build()
);Or configure the default for all method calls at the client level:
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import java.time.Duration;
AnthropicClient client = AnthropicOkHttpClient.builder()
.fromEnv()
.timeout(Duration.ofSeconds(30))
.build();To route requests through a proxy, configure the client using the proxy method:
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import java.net.InetSocketAddress;
import java.net.Proxy;
AnthropicClient client = AnthropicOkHttpClient.builder()
.fromEnv()
.proxy(new Proxy(
Proxy.Type.HTTP, new InetSocketAddress(
"https://example.com", 8080
)
))
.build();Note
Most applications should not call these methods, and instead use the system defaults. The defaults include special optimizations that can be lost if the implementations are modified.
To configure how HTTPS connections are secured, configure the client using the sslSocketFactory, trustManager, and hostnameVerifier methods:
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
AnthropicClient client = AnthropicOkHttpClient.builder()
.fromEnv()
// If `sslSocketFactory` is set, then `trustManager` must be set, and vice versa.
.sslSocketFactory(yourSSLSocketFactory)
.trustManager(yourTrustManager)
.hostnameVerifier(yourHostnameVerifier)
.build();The SDK consists of three artifacts:
anthropic-java-core- Contains core SDK logic
- Does not depend on OkHttp
- Exposes
AnthropicClient,AnthropicClientAsync,AnthropicClientImpl, andAnthropicClientAsyncImpl, all of which can work with any HTTP client
anthropic-java-client-okhttp- Depends on OkHttp
- Exposes
AnthropicOkHttpClientandAnthropicOkHttpClientAsync, which provide a way to constructAnthropicClientImplandAnthropicClientAsyncImpl, respectively, using OkHttp
anthropic-java- Depends on and exposes the APIs of both
anthropic-java-coreandanthropic-java-client-okhttp - Does not have its own logic
- Depends on and exposes the APIs of both
This structure allows replacing the SDK's default HTTP client without pulling in unnecessary dependencies.
Customized OkHttpClient
Tip
Try the available network options before replacing the default client.
To use a customized OkHttpClient:
- Replace your
anthropic-javadependency withanthropic-java-core - Copy
anthropic-java-client-okhttp'sOkHttpClientclass into your code and customize it - Construct
AnthropicClientImplorAnthropicClientAsyncImpl, similarly toAnthropicOkHttpClientorAnthropicOkHttpClientAsync, using your customized client
To use a completely custom HTTP client:
- Replace your
anthropic-javadependency withanthropic-java-core - Write a class that implements the
HttpClientinterface - Construct
AnthropicClientImplorAnthropicClientAsyncImpl, similarly toAnthropicOkHttpClientorAnthropicOkHttpClientAsync, using your new client class
The SDK is typed for convenient usage of the documented API. However, it also supports working with undocumented or not yet supported parts of the API.
To set undocumented parameters, call the putAdditionalHeader, putAdditionalQueryParam, or putAdditionalBodyProperty methods on any Params class:
import com.anthropic.core.JsonValue;
import com.anthropic.models.messages.MessageCreateParams;
MessageCreateParams params = MessageCreateParams.builder()
.putAdditionalHeader("Secret-Header", "42")
.putAdditionalQueryParam("secret_query_param", "42")
.putAdditionalBodyProperty("secretProperty", JsonValue.from("42"))
.build();These can be accessed on the built object later using the _additionalHeaders(), _additionalQueryParams(), and _additionalBodyProperties() methods.
Warning
The values passed to these methods overwrite values passed to earlier methods.
For security reasons, ensure these methods are only used with trusted input data.
To set undocumented parameters on nested headers, query params, or body classes, call the putAdditionalProperty method on the nested class:
import com.anthropic.core.JsonValue;
import com.anthropic.models.messages.MessageCreateParams;
import com.anthropic.models.messages.Metadata;
MessageCreateParams params = MessageCreateParams.builder()
.metadata(Metadata.builder()
.putAdditionalProperty("secretProperty", JsonValue.from("42"))
.build())
.build();These properties can be accessed on the nested built object later using the _additionalProperties() method.
To set a documented parameter or property to an undocumented or not yet supported value, pass a JsonValue object to its setter:
import com.anthropic.core.JsonValue;
import com.anthropic.models.messages.MessageCreateParams;
import com.anthropic.models.messages.Model;
MessageCreateParams params = MessageCreateParams.builder()
.maxTokens(JsonValue.from(3.14))
.addUserMessage("Hello, Claude")
.model(Model.CLAUDE_SONNET_4_20250514)
.build();The most straightforward way to create a JsonValue is using its from(...) method:
import com.anthropic.core.JsonValue;
import java.util.List;
import java.util.Map;
// Create primitive JSON values
JsonValue nullValue = JsonValue.from(null);
JsonValue booleanValue = JsonValue.from(true);
JsonValue numberValue = JsonValue.from(42);
JsonValue stringValue = JsonValue.from("Hello World!");
// Create a JSON array value equivalent to `["Hello", "World"]`
JsonValue arrayValue = JsonValue.from(List.of(
"Hello", "World"
));
// Create a JSON object value equivalent to `{ "a": 1, "b": 2 }`
JsonValue objectValue = JsonValue.from(Map.of(
"a", 1,
"b", 2
));
// Create an arbitrarily nested JSON equivalent to:
// {
// "a": [1, 2],
// "b": [3, 4]
// }
JsonValue complexValue = JsonValue.from(Map.of(
"a", List.of(
1, 2
),
"b", List.of(
3, 4
)
));Normally a Builder class's build method will throw IllegalStateException if any required parameter or property is unset.
To forcibly omit a required parameter or property, pass JsonMissing:
import com.anthropic.core.JsonMissing;
import com.anthropic.models.messages.MessageCreateParams;
import com.anthropic.models.messages.Model;
MessageCreateParams params = MessageCreateParams.builder()
.addUserMessage("Hello, world")
.model(Model.CLAUDE_SONNET_4_20250514)
.maxTokens(JsonMissing.of())
.build();To access undocumented response properties, call the _additionalProperties() method:
import com.anthropic.core.JsonValue;
import java.util.Map;
Map<String, JsonValue> additionalProperties = client.messages().create(params)._additionalProperties();
JsonValue secretPropertyValue = additionalProperties.get("secretProperty");
String result = secretPropertyValue.accept(new JsonValue.Visitor<>() {
@Override
public String visitNull() {
return "It's null!";
}
@Override
public String visitBoolean(boolean value) {
return "It's a boolean!";
}
@Override
public String visitNumber(Number value) {
return "It's a number!";
}
// Other methods include `visitMissing`, `visitString`, `visitArray`, and `visitObject`
// The default implementation of each unimplemented method delegates to `visitDefault`, which throws by default, but can also be overridden
});To access a property's raw JSON value, which may be undocumented, call its _ prefixed method:
import com.anthropic.core.JsonField;
import java.util.Optional;
JsonField<Long> maxTokens = client.messages().create(params)._maxTokens();
if (maxTokens.isMissing()) {
// The property is absent from the JSON response
} else if (maxTokens.isNull()) {
// The property was set to literal null
} else {
// Check if value was provided as a string
// Other methods include `asNumber()`, `asBoolean()`, etc.
Optional<String> jsonString = maxTokens.asString();
// Try to deserialize into a custom type
MyClass myObject = maxTokens.asUnknown().orElseThrow().convert(MyClass.class);
}In rare cases, the API may return a response that doesn't match the expected type. For example, the SDK may expect a property to contain a String, but the API could return something else.
By default, the SDK will not throw an exception in this case. It will throw AnthropicInvalidDataException only if you directly access the property.
If you would prefer to check that the response is completely well-typed upfront, then either call validate():
import com.anthropic.models.messages.Message;
Message message = client.messages().create(params).validate();Or configure the method call to validate the response using the responseValidation method:
import com.anthropic.models.messages.Message;
Message message = client.messages().create(
params, RequestOptions.builder().responseValidation(true).build()
);Or configure the default for all method calls at the client level:
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
AnthropicClient client = AnthropicOkHttpClient.builder()
.fromEnv()
.responseValidation(true)
.build();Java enum classes are not trivially forwards compatible. Using them in the SDK could cause runtime exceptions if the API is updated to respond with a new enum value.
Using JsonField<T> enables a few features:
- Allowing usage of undocumented API functionality
- Lazily validating the API response against the expected shape
- Representing absent vs explicitly null values
Why don't you use data classes?
It is not backwards compatible to add new fields to a data class and we don't want to introduce a breaking change every time we add a field to a class.
Checked exceptions are widely considered a mistake in the Java programming language. In fact, they were omitted from Kotlin for this reason.
Checked exceptions:
- Are verbose to handle
- Encourage error handling at the wrong level of abstraction, where nothing can be done about the error
- Are tedious to propagate due to the function coloring problem
- Don't play well with lambdas (also due to the function coloring problem)
This package generally follows SemVer conventions, though certain backwards-incompatible changes may be released as minor versions:
- Changes to library internals which are technically public but not intended or documented for external use. (Please open a GitHub issue to let us know if you are relying on such internals.)
- Changes that we do not expect to impact the vast majority of users in practice.
We take backwards-compatibility seriously and work hard to ensure you can rely on a smooth upgrade experience.
We are keen for your feedback; please open an issue with questions, bugs, or suggestions.