Skip to content

Java SDK

SDKMAX is fully OpenAI-compatible. Java projects can use the official OpenAI Java SDK (com.openai:openai-java) directly, pointing it at SDKMAX via a custom baseUrl — no separate SDKMAX SDK required.

Install (Maven)

xml
<dependency>
  <groupId>com.openai</groupId>
  <artifactId>openai-java</artifactId>
  <version>2.6.0</version>
</dependency>

Gradle:

groovy
implementation("com.openai:openai-java:2.6.0")

Version number

The version above is illustrative — check Maven Central for the latest release.

Initialize the client

java
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;

OpenAIClient client = OpenAIOkHttpClient.builder()
    .apiKey("sk-your-key")
    .baseUrl("https://api.sdkmax.com/v1")
    .build();

Or configure via environment variables and build with no arguments:

bash
export OPENAI_API_KEY="sk-your-key"
export OPENAI_BASE_URL="https://api.sdkmax.com/v1"
java
OpenAIClient client = OpenAIOkHttpClient.fromEnv();

Chat completions

java
import com.openai.models.chat.completions.ChatCompletion;
import com.openai.models.chat.completions.ChatCompletionCreateParams;

ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
    .model("gpt-4o")
    .addUserMessage("Explain SDKMAX's core capabilities.")
    .build();

ChatCompletion completion = client.chat().completions().create(params);
System.out.println(completion.choices().get(0).message().content().orElse(""));

Streaming

java
import com.openai.core.http.StreamResponse;
import com.openai.models.chat.completions.ChatCompletionChunk;

ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
    .model("gpt-4o")
    .addUserMessage("Write a short poem about an AI gateway.")
    .build();

try (StreamResponse<ChatCompletionChunk> stream = client.chat().completions().createStreaming(params)) {
    stream.stream().forEach(chunk ->
        chunk.choices().forEach(choice ->
            choice.delta().content().ifPresent(System.out::print)
        )
    );
}

Switching models

java
for (String model : new String[] { "gpt-4o", "claude-opus-4-8", "deepseek-chat" }) {
    ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
        .model(model)
        .addUserMessage("What's 1+1?")
        .build();
    ChatCompletion completion = client.chat().completions().create(params);
    System.out.println(model + " -> " + completion.choices().get(0).message().content().orElse(""));
}

Error handling

java
import com.openai.errors.RateLimitException;
import com.openai.errors.OpenAIServiceException;

try {
    client.chat().completions().create(params);
} catch (RateLimitException e) {
    // 429: quota exhausted or rate-limited — back off and retry
} catch (OpenAIServiceException e) {
    System.err.println(e.statusCode() + " " + e.getMessage());
}

Full error code reference: Error Codes.

Not using the official SDK?

Any HTTP client that can set custom headers and a JSON body works — build requests manually with OkHttp / HttpClient following Making Requests.

SDKMAX — Enterprise AI Gateway, Aggregating Global AI Resources