Using LLM4S from Java

Call a model from Java with llm4s-java-api: a client, a prompt, a conversation, and failures you can read without Scala.

Table of contents

  1. What llm4s-java-api is
  2. Add the dependency
  3. Configure a provider
    1. Configure in code
    2. Recording metrics
    3. Behind a proxy or a corporate CA
  4. Your first call
    1. The snippets’ imports
  5. A conversation
  6. Completion options
  7. The whole reply
  8. Streaming the reply
  9. Structured output
  10. Asynchronous calls
  11. Reading a result
  12. Handling a failure
  13. An agent turn
  14. Tools
  15. An agent with a system prompt, guardrails and a context window
  16. The Kotlin DSL
    1. An agent block
  17. Embeddings
  18. AI services
  19. Testing your code
  20. What is not here yet
  21. Where next

What llm4s-java-api is

LLM4S is written in Scala. Its core API returns Scala types (Either, Option, Seq) that are awkward to use from Java. llm4s-java-api is a small layer in front of it for Java callers: Llm4s creates a client, JLlmClient calls the model, ConversationBuilder builds a conversation, and every call returns an LlmResult, a result type modelled on java.util.Optional and CompletableFuture. A failed call is a value you check, not an exception you have to catch.

It does not hide every Scala type. Conversation and ProviderConfig are Scala classes that still appear in its signatures, as does LLMError behind LlmException.error(), though its kind reads as a Java enum, and so does core’s CompletionOptions, in an overload that JCompletionOptions makes unnecessary. What is not here yet says which of them gets in your way.

The values it hands you to read - JCompletion, JAgentResult, JMessage, PendingInterrupt, JEmbeddings and the rest - are Java classes whose constructors are package-private: you receive them, you do not build them, so a later release can add a field without breaking code compiled against this one. The classes that do the work come from a factory: Llm4s makes clients and agents, JCompletionOptions.builder() and ConversationBuilder.create() start a builder, LlmResult.success and failure make a result, and the Answer factories an answer. Their constructors are not for you either: each takes a first parameter of a type only the package can name, and throws UnsupportedOperationException when called from anywhere else. To call a Scala LLMClient you built yourself - a ReliableClient around a provider’s client, your own implementation, a test double - wrap it with Llm4s.wrapClient(client); a Scala EmbeddingClient, with Llm4s.wrapEmbeddingClient(client, model).

llm4s-java-api is on Maven Central from 0.5.0 on; the current release is 0.6.0. The gradle-java sample is a complete Java project you can copy. It needs JDK 21: it depends on llm4s-agent, which is compiled for JDK 21 and starts virtual threads (see Scala and JDK support). Whether some artifacts get a lower floor before 1.0 is tracked in #1493.

Add the dependency

The artifact is org.llm4s:llm4s-java-api_3. The _3 is the Scala binary version, and LLM4S is Scala 3 only. sbt adds it for you (%%); Maven and Gradle do not, so write it yourself.

Maven

1
2
3
4
5
6
7
8
9
10
11
<dependency>
    <groupId>org.llm4s</groupId>
    <artifactId>llm4s-java-api_3</artifactId>
    <version>0.6.0</version>
</dependency>
<!-- and the provider module you call: llm4s-openai_3, llm4s-anthropic_3, llm4s-gemini_3, ... -->
<dependency>
    <groupId>org.llm4s</groupId>
    <artifactId>llm4s-ollama_3</artifactId>
    <version>0.6.0</version>
</dependency>

Gradle (Kotlin DSL)

1
2
3
4
dependencies {
    implementation("org.llm4s:llm4s-java-api_3:0.6.0")
    implementation("org.llm4s:llm4s-ollama_3:0.6.0")   // the provider you call
}

sbt

1
2
libraryDependencies += "org.llm4s" %% "llm4s-java-api" % "0.6.0"
libraryDependencies += "org.llm4s" %% "llm4s-ollama"   % "0.6.0"   // the provider you call

What arrives with it:

  • llm4s-core and llm4s-agent, and no provider module: declare the ones you call, llm4s-openai (OpenAI and Azure OpenAI), llm4s-anthropic, llm4s-gemini, llm4s-ollama or llm4s-openai-compatible (DeepSeek, Mistral, OpenRouter and any server with a chat-completions API), at the same version. A Java user who calls only Ollama gets no vendor SDK, OkHttp or Kotlin standard library (the dependency footprint). A typed builder whose module is missing, Llm4s.anthropic() without llm4s-anthropic say, fails with a CONFIGURATION result naming the artifact to add; a configured section whose provider is not on the classpath fails the same way. Up to 0.5.x the facade brought all five (#1496).
  • Scala’s standard library (scala3-library_3 and scala-library). If your build pins or excludes it, see the Gradle guide for the recipes that keep the two Scala libraries aligned.
  • No logging backend. LLM4S logs through SLF4J; add one (for example Logback), or SLF4J prints a warning and drops the logs.

Configure a provider

Java code does not build a provider configuration. It comes from an application.conf on your classpath, the same file Scala users write: a named section per provider, and one section named as the default.

1
2
3
4
5
6
7
8
9
10
11
# src/main/resources/application.conf
llm4s {
  providers {
    provider = "openai-main"          # the default: the name of a section below

    openai-main {
      provider = "openai"
      model    = "gpt-4o-mini"
    }
  }
}

With OPENAI_API_KEY set in the environment, that is the whole configuration: the OpenAI module binds the variable to the shared credential for openai, so the section needs only provider and model. Other providers work the same way; anthropic reads ANTHROPIC_API_KEY, and ollama needs no key but does need a baseUrl:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
llm4s {
  providers {
    provider = "claude"

    claude {                              # key from ANTHROPIC_API_KEY
      provider = "anthropic"
      model    = "claude-sonnet-4-20250514"
    }

    ollama-local {
      provider = "ollama"
      model    = "llama3.2"
      baseUrl  = "http://localhost:11434"
    }
  }
}

The configuration guide lists every key and every provider. Nothing in the library reads a LLM_MODEL variable; the model is the model key of the section.

Configure in code

To build a client in code instead of from a file, Llm4s has a builder for each provider this module brings: openAi(), anthropic(), gemini(), ollama(), openAiCompatible() (vLLM, LM Studio, LiteLLM, a gateway: any server that speaks OpenAI’s chat-completions API) and azureOpenAi(). Each takes plain Java values and builds an LlmResult<JLlmClient>:

1
2
3
4
5
6
7
8
9
10
11
12
LlmResult<JLlmClient> openai = Llm4s.openAi()
    .apiKey(apiKey)
    .model("gpt-4o-mini")
    .build();
LlmResult<JLlmClient> local = Llm4s.ollama()
    .model("llama3.2")
    .build();
LlmResult<JLlmClient> gateway = Llm4s.openAiCompatible()
    .baseUrl("http://localhost:8000/v1")
    .model("qwen2.5-coder")
    .header("X-Team-Id", "platform")
    .build();

What each provider needs is required - an API key and a model, for Ollama the model alone, for an OpenAI-compatible server its base URL and model, for Azure the endpoint, key and deployment - and everything else has the default application.conf would give it: a builder makes the same config as a section with the same values. That means the provider’s own base URL (http://localhost:11434 for Ollama), and the context window and reply reserve the model catalogue lists for the model; for an OpenAI-compatible server, the catalogue’s window for the provider its base URL’s host names (Groq, Together, Fireworks, xAI, Perplexity), else 8192 tokens. baseUrl, contextWindow, reserveCompletion, requestTimeout and streamTimeout (each a java.time.Duration, at most about 292 years) override them, as do organization for OpenAI, apiVersion for Azure and header for an OpenAI-compatible server. A context window set without a reserve keeps a quarter of itself for the reply, up to the reserve the provider would otherwise use, as a section’s contextWindow does. metrics(MetricsCollector) makes the client report every call, as createClient(config, metrics) does.

build() sends no request and does not throw. A required field left out - or set to null or a blank string, as System.getenv returns for a variable that is not set - is a failed result of kind VALIDATION whose message names it (Field 'apiKey' is required), as is a value out of range, such as a context window of zero. A builder is immutable, so one can be kept and forked: base.model("gpt-4o") returns a new builder and leaves base as it was. Its toString prints the API key as ***, an OpenAI-compatible server’s headers by name only, and a base URL or endpoint without its userinfo, query or fragment, so a builder can be logged.

From Kotlin, build the client the same way and hand it to llm4s-kotlin-api’s Llm4s.wrapClient, which returns an LLMClientKt with suspend functions, Flow streaming and LLMException (#1850). wrapClient takes any JLlmClient, so a client from createClient(ProviderConfig) works the same way:

1
2
3
4
5
6
7
8
9
10
import org.llm4s.kotlin.Llm4s
import org.llm4s.javaapi.Llm4s as JLlm4s

val client = Llm4s.wrapClient(
    JLlm4s.ollama()
        .model("llama3.2")
        .build()
        .get(),   // throws the facade's LlmException on a VALIDATION failure; check isSuccess to handle it
)
client.use { println(it.complete("What is 2 + 2?")) }   // complete is a suspend function: call it in a coroutine

For any other provider - one from its own module, such as llm4s-bedrock - Llm4s.createClient(ProviderConfig) takes the provider’s config object. OpenAI, Anthropic and Ollama have one with a Java entry point too, which takes the essentials and fills in the rest (the default base URL, and a context window and completion reserve guessed from the model name):

1
2
3
LlmResult<JLlmClient> openai = Llm4s.createClient(OpenAIConfig.apply(Secret.apply(apiKey), "gpt-4o-mini"));
LlmResult<JLlmClient> anthropic = Llm4s.createClient(AnthropicConfig.apply(Secret.apply(apiKey), "claude-sonnet-4-20250514"));
LlmResult<JLlmClient> local = Llm4s.createClient(OllamaConfig.apply("llama3.2", "http://localhost:11434"));

(The imports are listed under The snippets’ imports.) Reading the key is up to you; creating the client sends no request. The with methods of each config (withContextWindow, withReserveCompletion, and for OpenAI withBaseUrl and withOrganization) change one field at a time; some take a Scala Option, which the builders above avoid.

Recording metrics

Llm4s.createClient(ProviderConfig, MetricsCollector) builds a client that reports every call (its outcome, duration, tokens and cost) to a MetricsCollector:

1
LlmResult<JLlmClient> measured = Llm4s.createClient(OllamaConfig.apply("llama3.2", "http://localhost:11434"), metrics);

For metrics, use new MicrometerMetrics(meterRegistry) from llm4s-observability-micrometer to record into the application’s Micrometer MeterRegistry (see Micrometer metrics), or PrometheusMetrics from llm4s-observability-prometheus. A null collector records nothing. The Spring Boot starter does this for you when the application has a MeterRegistry.

A client builder takes the collector too: Llm4s.ollama().model("llama3.2").metrics(metrics).build().

Behind a proxy or a corporate CA

Every client builder takes the proxy and TLS settings that llm4s.http gives a configured provider (Corporate networks): an HTTP proxy, its Basic credentials, the hosts reached directly, a trust store whose CAs are trusted in addition to the JDK’s, and, for a gateway that requires mutual TLS, a key store with the client certificate (keyStore(path, password)):

1
2
3
4
5
6
7
8
LlmResult<JLlmClient> corporate = Llm4s.openAi()
    .apiKey(apiKey)
    .model("gpt-4o")
    .proxy("http://proxy.corp:3128")
    .proxyCredentials(proxyUser, proxyPassword)
    .nonProxyHosts("localhost", "*.internal")
    .trustStore(java.nio.file.Path.of("/etc/pki/corp-ca.p12"), trustStorePassword)
    .build();

A proxy URL the builder cannot use, or a store it cannot open, makes build() return a failed result of kind VALIDATION naming the field; toString prints every password as ***.

Your first call

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.io.PrintStream;

import org.llm4s.javaapi.JLlmClient;
import org.llm4s.javaapi.Llm4s;
import org.llm4s.javaapi.LlmResult;

public final class HelloLlm4s {

    private HelloLlm4s() {}

    public static void main(String[] args) {
        System.exit(run(System.out, System.err));
    }

    static int run(PrintStream out, PrintStream err) {
        LlmResult<JLlmClient> created = Llm4s.createDefaultClient();
        if (created.isFailure()) {
            err.println("Could not create a client: " + created.getError().getMessage());
            return 1;
        }

        // JLlmClient is AutoCloseable: it holds an HTTP client, so close it.
        try (JLlmClient client = created.get()) {
            LlmResult<String> answer = client.complete("What is a monad? Answer in one sentence.");
            answer
                .ifSuccess(text -> out.println(text))
                .ifFailure(error -> err.println("The call failed: " + error.getMessage()));
            return answer.isSuccess() ? 0 : 1;
        }
    }
}

Llm4s.createDefaultClient() loads the default provider section and builds a client from it. It does not throw: a missing section, a missing API key or an unknown provider is a failed LlmResult, and its message says what to fix (for a missing key it names the environment variable and the section). JLlmClient is AutoCloseable and closing it closes the underlying client, so use try-with-resources.

ifSuccess and ifFailure each take a lambda and return the result, so they chain. The run method is separate from main so that a test can pass its own streams.

The gradle-java sample is this program in a complete Gradle project.

The snippets’ imports

The snippets below leave out their imports. Together they use these:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Flow;

import org.llm4s.javaapi.CompletionStream;
import org.llm4s.javaapi.ConversationBuilder;
import org.llm4s.javaapi.FakeLlmClient;
import org.llm4s.javaapi.JCompletion;
import org.llm4s.javaapi.JCompletionOptions;
import org.llm4s.javaapi.JContextWindow;
import org.llm4s.javaapi.JEmbeddingClient;
import org.llm4s.javaapi.JEmbeddingPurpose;
import org.llm4s.javaapi.JEmbeddings;
import org.llm4s.javaapi.JGuardrails;
import org.llm4s.javaapi.JLlmClient;
import org.llm4s.javaapi.JReasoningEffort;
import org.llm4s.javaapi.JTool;
import org.llm4s.javaapi.JToolArgs;
import org.llm4s.javaapi.JToolCall;
import org.llm4s.javaapi.Llm4s;
import org.llm4s.javaapi.LlmErrorKind;
import org.llm4s.javaapi.LlmException;
import org.llm4s.javaapi.LlmResult;
import org.llm4s.javaapi.Param;
import org.llm4s.javaapi.Tool;
import org.llm4s.llmconnect.config.AnthropicConfig;
import org.llm4s.llmconnect.config.OllamaConfig;
import org.llm4s.llmconnect.config.OpenAIConfig;
import org.llm4s.llmconnect.model.Conversation;
import org.llm4s.metrics.MetricsCollector;
import org.llm4s.security.Secret;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

A conversation

A conversation is a list of messages. ConversationBuilder builds one without Scala syntax:

1
2
3
4
5
6
7
Conversation conversation = ConversationBuilder.create()
    .system("You answer in one short sentence.")
    .user("What is a monad?")
    .build();

LlmResult<String> answer = client.complete(conversation);
System.out.println(answer.get());

The builder has system, user and assistant, and messages are sent in the order you add them. It is immutable: each method returns a new builder, so one can be shared and extended. A null message throws NullPointerException at once.

Completion options

JCompletionOptions sets how the model answers one request, built with a builder and passed with the conversation:

1
2
3
4
5
6
7
8
JCompletionOptions options = JCompletionOptions.builder()
    .temperature(0.2)
    .maxTokens(512)
    .reasoning(JReasoningEffort.MEDIUM)
    .build();

LlmResult<String> answer = client.complete(conversation, options);
System.out.println(answer.get());

The builder has temperature, topP, maxTokens, presencePenalty, frequencyPenalty, reasoning (the Java enum JReasoningEffort: MINIMAL, LOW, MEDIUM, HIGH, XHIGH or MAX, how hard to think) and thinking (a JThinking: JThinking.off(), JThinking.adaptive() or JThinking.budget(n), whether to think and within what budget). JThinking.budget throws IllegalArgumentException for n <= 0; a provider fits a positive budget to its own range (Anthropic: at least 1024 and below the token limit) and reports that it did. An explicit thinking wins over reasoning; see Cost controls for what each provider sends. A setting you leave out keeps the library’s default: temperature 0.7, top-p 1.0, no penalties, and no token limit, reasoning level or thinking mode. The built options have an accessor of the same name for each setting. maxTokens() returns an OptionalInt, reasoning() an Optional<JReasoningEffort> and thinking() an Optional<JThinking>, empty when unset. Their setters also take an OptionalInt or Optional, so an empty one clears the value.

The builder is immutable like ConversationBuilder: each setter returns a new builder, and options.toBuilder() starts one from existing options. A value no provider accepts throws IllegalArgumentException at once: a negative or non-finite temperature, a top-p outside 0 to 1, a non-finite penalty, or a token count below 1. A null throws NullPointerException. A value only some models reject, such as a temperature above 1 for some models, is left to the provider. A model that does not reason ignores reasoning. Tools and response formats are not options here yet.

The whole reply

complete returns the reply’s text. completion takes the same arguments - a query, a conversation, or a conversation and JCompletionOptions - and returns the whole reply as a JCompletion: the model that answered, the tokens it used, its estimated cost and the tool calls it asked for, alongside the text:

1
2
3
4
5
6
7
8
9
JCompletion reply = client.completion(conversation).get();

System.out.println(reply.model() + ": " + reply.content());
reply.usage().ifPresent(usage ->
    System.out.println(usage.promptTokens() + " tokens in, " + usage.completionTokens() + " out"));
reply.estimatedCost().ifPresent(cost -> System.out.println("about $" + cost.toPlainString()));
for (JToolCall call : reply.toolCalls()) {
    System.out.println("wants " + call.name() + " " + call.argumentsJson());
}

model() is the model as the provider names it, which can be more specific than the one you configured. usage() is a JTokenUsage with promptTokens(), completionTokens(), totalTokens() and thinkingTokens(), and the prompt-cache counts cachedTokens() (input read from the provider’s prompt cache, billed at the cheaper cache-read rate) and cacheCreationTokens() (input written into it, typically billed above the normal input rate) - each an int, zero when the model reported none - or empty when the provider reported no usage. estimatedCost() is a java.math.BigDecimal in USD, empty when the cost is not known; it is the figure an agent adds to its usage().totalCost(). toolCalls() lists JToolCalls, the same type an agent’s messages use, with the arguments as JSON text; thinking() is the model’s reasoning text when it reported one. servedTier() is the service tier the provider reports it served the call at - "auto", "default", "flex" or "priority", a JServiceTier’s lower-case name, or the provider’s own name for a tier those do not cover, such as OpenAI’s "scale" - and empty when it reported none. A JCompletion is a value with equals, hashCode and a toString that prints the full text.

The method is completion, a noun, because it returns the reply rather than performs a request for its text; complete keeps returning a String. There is no completion overload for core’s Scala CompletionOptions, so client.completion(conversation, null) compiles, and returns a failed result like any other null argument.

Streaming the reply

stream sends the same arguments as complete - a query, a conversation, or a conversation and JCompletionOptions - with a CompletionStreamListener, and returns at once. The listener receives each piece of the reply’s text as it arrives (onPartial), then the whole reply (onComplete, a JCompletion) or the error (onError); only onPartial is abstract, so a lambda or a method reference is a listener:

1
2
3
4
5
6
7
JCompletion reply;
try (CompletionStream stream = client.stream(conversation, System.out::print).get()) {
    reply = stream.await().get();
}
System.out.println();

Flow.Publisher<String> pieces = client.streamAsPublisher(conversation);

The returned CompletionStream awaits the reply (await(), the same outcome the listener heard) and cancels the call (cancel(), or close(), so try-with-resources stops a call its block leaves). Cancelling interrupts the provider’s thread, which is how llm4s providers stop, and the listener then hears onError with a CANCELLED error. Listener calls are made one at a time, never on your thread; one that throws stops the call, and onError receives what it threw.

streamAsPublisher returns the text as a java.util.concurrent.Flow.Publisher<String> instead: it is in the JDK, so Reactor (JdkFlowAdapter), RxJava and Mutiny adapt it without another llm4s dependency, and llm4s-reactor returns it as a Flux<String> (Java Reactor). Each subscription makes its own call. A subscriber slower than the model holds the provider’s thread rather than losing text, and cancelling the subscription cancels the call. A failed call ends with onError (an LlmException) after every piece that streamed before the failure. Streaming yields text only; tool-call deltas are not streamed.

Structured output

Pass a record class after the query, or after the conversation (and options), and complete returns an instance of it instead of text. The record’s components become a JSON Schema that is sent as the structured-output format, and the answer is read back into the record:

1
2
3
4
record City(String name, String country, int population) {}

City city = client.complete("The largest city in Japan, with its population", City.class).get();
System.out.println(city.name() + ", " + city.country() + ": " + city.population());

A component can be a String, an int, long, short or byte (boxed or not), a double or float, a boolean, an enum (the model answers with a constant’s name), another record, a java.util.List of any of these, or an Optional of any of these. Optional is the only way to let the model answer null or leave a field out; every other field is required. A Map, an array, a raw List, an interface or a record that contains itself is refused before any call, and an answer that does not fit - a missing field, the wrong JSON type, a fraction for an int, an unknown enum name - is a failed result whose message names the field, such as field 'previous[0].city': expected a string, got a number. Numbers pass through a double, so ask for an identifier above 2^53 as a String.

How firmly the schema binds the model is the provider’s: OpenAI-style providers send it as a native JSON Schema, Anthropic adds it to the system prompt, and watsonx and Bedrock ignore it, so the answer is still checked when it comes back. See Structured output for each provider.

Asynchronous calls

Every blocking call has an *Async variant that returns a CompletableFuture at once and makes the call on another thread: completeAsync and completionAsync on JLlmClient (with the same arguments as complete and completion, a record class for structured output included), runAsync, continueConversationAsync, resumeAsync and recoverAsync on JAgent, and embedAsync on JEmbeddingClient. Fan several calls out and wait for all of them with allOf, or bound one with orTimeout:

1
2
3
4
5
6
7
8
CompletableFuture<String> first = client.completeAsync("Summarise the first report");
CompletableFuture<String> second = client.completeAsync("Summarise the second report");
CompletableFuture<JAgentResult> turn = agent.runAsync("Plan the release");

CompletableFuture.allOf(first, second, turn).join();
System.out.println(first.join() + " / " + second.join() + " / " + turn.join().answer().orElse(""));

CompletableFuture<String> answer = client.completeAsync("A long question").orTimeout(30, TimeUnit.SECONDS);

A failed call completes its future exceptionally with the LlmException that the blocking call’s LlmResult would hold, of the same LlmErrorKind, so join() throws a CompletionException and get() an ExecutionException whose cause is that LlmException. A future you cancel throws CancellationException, as any CompletableFuture does.

cancel(true) interrupts the call in flight, which is how llm4s providers stop; an agent turn cancelled this way ends as the blocking call interrupted does: cancelled, the conversation of a continued, resumed or recovered turn left for recover. Completing the future yourself - orTimeout, completeOnTimeout, complete - interrupts the call too, since its answer can no longer be delivered. cancel(false) lets the call finish and discards its answer. Cancel the future the method returned: a future derived from it with thenApply does not pass its cancellation back.

By default each call runs on a new virtual thread. A JLlmClient has an executor of its own for that, which close() shuts down: a call still running is then interrupted, and one made after it fails with kind OTHER. withAsyncExecutor runs the calls on an executor of yours instead - a bounded pool, a container’s managed executor:

1
2
ExecutorService pool = Executors.newFixedThreadPool(8);
JLlmClient pooled = client.withAsyncExecutor(pool);   // shares client's connection; never shuts pool down

The pooled client shares the connection of the client it came from, so closing either closes it, and it never shuts the pool down. JAgent and JEmbeddingClient have withAsyncExecutor too. Any executor works, a ForkJoinPool included: cancelling a future interrupts the thread running its call whatever the executor. An executor that refuses a call - shut down, or its queue full - fails that call’s future at once with kind OTHER, the executor’s RejectedExecutionException its cause. The threading guide says which thread runs what.

Reading a result

Every complete call returns an LlmResult<String>, and every completion call an LlmResult<JCompletion>. There are several ways to take the value out, depending on how you want to treat a failure:

1
2
3
4
5
6
7
LlmResult<String> result = client.complete("What is 2+2?");

String text = result.get();                       // the value, or throws LlmException
String orNull = result.getOrNull();               // the value, or null when the call failed
Optional<String> optional = result.toOptional();  // Optional.empty() when the call failed
LlmResult<Integer> length = result.map(String::length);
CompletableFuture<String> future = result.toCompletableFuture();

isSuccess() and isFailure() test it, getError() returns the LlmException of a failure (or null), and map transforms a success and passes a failure through. toCompletableFuture() returns a future that is already complete: it makes a result fit an API that expects a future, and does not make the call asynchronous (see Asynchronous calls).

Handling a failure

A call that fails does not throw. JLlmClient turns a failure of the provider, the network or the library into a failed LlmResult, and a null argument into a failed result too. Only get() throws, and it throws LlmException, an unchecked exception that says what went wrong in Java types:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
try {
    String text = client.complete("What is 2+2?").get();
    System.out.println(text);
} catch (LlmException e) {
    System.err.println(e.getMessage());
    switch (e.getKind()) {
        case AUTHENTICATION, CONFIGURATION -> System.err.println("check the API key and the provider section");
        case RATE_LIMIT -> System.err.println("rate limited; wait "
            + e.getRetryAfter().map(d -> d.toSeconds() + " s").orElse("a while"));
        case SERVICE -> System.err.println("the provider answered HTTP "
            + (e.getStatusCode().isPresent() ? e.getStatusCode().getAsInt() : "?"));
        default -> { }
    }
    if (e.isRecoverable()) {
        System.err.println("a retry may succeed");
    }
}

getKind() is the Java enum LlmErrorKind:

Kind The failure Recoverable
AUTHENTICATION the provider rejected the API key no, unless it is a provider’s 401 or 403 reported as a service error
RATE_LIMIT the provider, or a local limiter, refused the call for now yes, unless it is an embedding provider’s error
TIMEOUT the call did not finish in time yes
NETWORK the provider could not be reached yes
SERVICE the provider answered with an error yes, unless it is an embedding provider’s error
VALIDATION the request is wrong: a missing or invalid argument no, unless it is a provider’s 400 reported as a service error
CONFIGURATION a setting is missing or invalid no
CANCELLED the call was cancelled, for example by an interrupt no
OTHER anything else, including an error from another llm4s module or your own code depends on the error

A provider’s error response with status 400, 401/403 or 429 has the kind that status means (VALIDATION, AUTHENTICATION, RATE_LIMIT) even when a provider client reported it as a generic service error. Such an error keeps the recoverability of its class: a ServiceError or an APIError is recoverable whatever its status, so a 403 reported as one reads as AUTHENTICATION and isRecoverable() is true. The table’s column is what the kind’s own error classes say; to decide whether to try again, always call isRecoverable() rather than infer it from the kind. Every error class in org.llm4s.error has a kind, and a test fails when a new one is added without one. An embedding provider’s error response has the kind of its HTTP status in the same way, but llm4s does not call any embedding error recoverable, so isRecoverable() is false for it whatever its kind.

isRecoverable() says whether the call may succeed if tried again, perhaps after you do something first: wait out a rate limit, or correct a request the provider rejected. getRetryAfter() is the Optional<Duration> the provider asked you to wait (its Retry-After header), empty when it did not say, and getStatusCode() the OptionalInt HTTP status of a provider’s error response. If the error carries a Throwable, it is the exception’s getCause().

e.error() is still the llm4s error itself, a Scala LLMError whose message() and formatted() (the message plus its code and context) are plain methods, and whose classes - RateLimitError, ServiceError and the rest - you can test with instanceof for a detail the kind does not carry. The error handling guide has the full list and the recovery tools, written for Scala.

If the thread blocked in complete is interrupted, the call returns a failed result whose error is a CancelledError, with the thread’s interrupt flag still set. InterruptedException is never thrown, so the method does not declare it and Java will not let you write catch (InterruptedException e) around the call; test the result for a CancelledError instead. The threading and cancellation guide has the details.

An agent turn

Llm4s.createAgent(client) wraps a client in a JAgent. run and continueConversation block, like complete, and return an LlmResult<JAgentResult>. A JAgentResult is read with JDK types and this module’s own: answer() is an Optional<String>, messages() a java.util.List<JMessage>, and status().kind() the Java enum AgentStatusKind, so a switch covers every way a turn ends:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
JAgent agent = Llm4s.createAgent(client);
JAgentResult result = agent.run("What is 2+2?").get();

switch (result.status().kind()) {
    case COMPLETED -> System.out.println(result.answer().orElseThrow());
    case TRUNCATED -> System.out.println("Cut off: " + result.status().partialAnswer().orElseThrow());
    case BLOCKED -> System.out.println("Blocked by " + result.status().guardrail().orElseThrow());
    case STEP_LIMIT_REACHED -> System.out.println("Hit the step limit");
    case TOKEN_LIMIT_REACHED -> System.out.println("Hit the token budget");
    case SUSPENDED -> System.out.println("Waiting for " + result.status().pending().size() + " answers");
}
for (JMessage message : result.messages()) {
    System.out.println(message.role() + ": " + message.content());
}
JUsageSummary usage = result.usage();
System.out.println(usage.inputTokens() + " tokens in, " + usage.outputTokens() + " out");

JAgentResult next = agent.continueConversation(result, "And 3+3?").get();

Each status’s data has its own accessor, empty for the other kinds: answer() for COMPLETED, partialAnswer() for TRUNCATED - an answer the model’s output limit cut off, which answer() never returns - guardrail() and reason() for BLOCKED, and pending() for SUSPENDED - the approvals and questions the turn waits for, which the agent guide’s suspended turns section answers. A JMessage has a role() (the Java enum JMessageRole), its content(), the toolCalls() an assistant message asked for, with their arguments as JSON text (an object, as a model sends them; a call built with a ujson.Str renders as a JSON string literal), and the toolCallId() a tool message answers. usage() counts tokens as longs, the cost as a java.math.BigDecimal (two usages are equal when their costs are numerically equal, whatever the scale), and byModel() breaks both down per model. No accessor returns a Scala or ujson type.

These types are values, but not Serializable. Their toString prints the full text - a message’s content, a tool call’s arguments, an answer or a guardrail’s reason - as the Scala types do, so mind what you log.

Tools

An agent can call tools you write in Java, in either of two styles. Annotate the public methods of a class with @Tool, and each parameter with @Param (its name is what the model passes; without one, the compiled name is used, which needs javac -parameters):

1
2
3
4
5
6
class WeatherTools {
    @Tool(description = "The current weather in a city")
    public String weather(@Param(name = "city", description = "City name") String city) {
        return "Sunny in " + city;
    }
}

or build one from a lambda with JTool.builder, then give the tools to Llm4s.createAgentWithTools:

1
2
3
4
5
6
7
8
9
List<JTool> weather = JTool.fromAnnotated(new WeatherTools()).get();
LlmResult<JTool> add = JTool.builder("add", "Adds two numbers")
    .param("a", long.class, "First")
    .param("b", long.class, "Second")
    .handler(args -> args.getLong("a") + args.getLong("b"))
    .build();

JAgent agent = Llm4s.createAgentWithTools(client, List.of(weather.get(0), add.get())).get();
JAgentResult result = agent.run("What is the weather in Paris?").get();

A parameter is a String, an int, long, short or byte (boxed or not), a double or float, a boolean, or an enum (the model passes a constant’s name); its JSON Schema is derived from the type, and optionalParam (or @Param(required = false), with a boxed type) lets the model leave it out. The model’s arguments are read into those Java values before your code runs, with no JSON library: a missing or mistyped argument is a tool error the model sees, and so is an exception your method or lambda throws - the agent keeps running and the model can try again. A result is passed to the model as text if it is a String; a number, boolean, enum, Map or Collection becomes JSON. A tool that cannot be built (no handler, an unsupported parameter type, a parameter declared twice) and an agent given two tools with one name are failed results, not exceptions.

An agent runs a tool at least once, not exactly once: a retry, an approval, an answered question or a recover can run the same call again (see Tool side effects). A tool with an external side effect therefore passes the call’s idempotency key to the system it reaches, which then does the work once. A lambda reads it from its arguments, as args.idempotencyKey(); an annotated method declares a JToolArgs parameter, which the model does not see, and reads it there:

1
2
3
4
@Tool(description = "Charges a card once")
public String charge(@Param(name = "cents", description = "Amount in cents") long cents, JToolArgs call) {
    return billing.charge(cents, call.idempotencyKey().orElse(null));   // empty outside an agent run
}
1
2
3
4
LlmResult<JTool> refund = JTool.builder("refund", "Refunds a charge once")
    .param("charge", String.class, "The charge to refund")
    .handler(args -> billing.refund(args.getString("charge"), args.idempotencyKey().orElse(null)))
    .build();

idempotencyKey() and toolCallId() (the id the model gave the call) are Optional<String>s: present for every call an agent runs, the key the same in every run of one call, and empty when the tool runs outside an agent.

An agent with a system prompt, guardrails and a context window

Llm4s.agentBuilder(id, client) sets the rest of an agent from Java (#2476): its system prompt, its tools, guardrails on each query and on each final answer, how much of the conversation each model call is sent, and streaming. createAgent and createAgentWithTools are the builder with none of these set.

1
2
3
4
5
6
7
8
9
10
11
12
13
LlmResult<JAgent> built = Llm4s.agentBuilder("support", client)
    .systemPrompt("You are a support assistant. Answer in JSON.")
    .tools(List.of(add))
    .inputGuardrails(List.of(JGuardrails.length(1, 10_000), JGuardrails.profanity()))
    .outputGuardrails(List.of(JGuardrails.json()))
    .contextWindow(JContextWindow.lastMessages(20))
    .streaming(true)
    .build();

JAgent agent = built.get();
JAgentResult result = agent.run("What is 2 + 3?").get();
result.status().guardrail().ifPresent(g ->
    System.out.println("Blocked by " + g + ": " + result.status().reason().orElse("")));
  • Guardrails. JGuardrails has the built-in ones: length(min, max), profanity() (a deliberately minimal word list, an example rather than a production filter), regex(pattern) and regex(pattern, errorMessage), which check a query or an answer, and json(), which checks only an answer - inputGuardrails does not compile with it. Each list runs in order. A guardrail that fails ends the turn BLOCKED, with the guardrail’s name in status().guardrail() and its reason in status().reason(); a blocked query stores nothing of the turn, a blocked answer is not kept, and the conversation stays usable. Guardrails written in Java are not available yet.
  • Context window. JContextWindow.lastMessages(n) sends each model call at most the n most recent messages, dropping the oldest first. The system prompt is always sent and not counted; the current turn is always sent whole; a tool call is never sent without its result. Only what is sent is pruned: messages() of a result still has the whole conversation.
  • Streaming. streaming(true) makes the agent’s model calls stream, so a streamed turn carries the answer’s text deltas.

The builder is immutable - each method returns a new one, and a setting replaces the one set before - and nothing in it throws: a null argument, an invalid guardrail or window (length(5, 1), lastMessages(0)), a blank id or two tools with one name is a failed result of build(). For what it does not set - approvals, handoffs, subagents, another runtime - build the agent with the Scala Agent.builder and wrap it with Llm4s.wrapAgent. From Kotlin, the agent { ... } block sets the same.

The Kotlin DSL

llm4s-kotlin-api declares the same tools, options and clients with a Kotlin DSL (#1495). Each block builds the Java facade’s object - a JTool, a JCompletionOptions, a provider’s client builder - so it behaves exactly as the Java code above, and a failure throws LLMException (an option out of range, IllegalArgumentException) instead of returning a failed LlmResult:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import org.llm4s.javaapi.JReasoningEffort
import org.llm4s.kotlin.Llm4s
import org.llm4s.kotlin.completionOptions
import org.llm4s.kotlin.tools
import kotlin.time.Duration.Companion.seconds

val client = Llm4s.openAi {                // also anthropic, gemini, ollama, openAiCompatible, azureOpenAi
    apiKey = System.getenv("OPENAI_API_KEY")
    model = "gpt-4o-mini"
    requestTimeout = 30.seconds
}

val reply = client.complete("Name a colour", completionOptions {
    temperature = 0.2
    maxTokens = 200
    reasoning = JReasoningEffort.LOW
})

val agent = Llm4s.createAgent(client, tools {
    tool("add", "Adds two numbers") {
        param<Long>("a", "First")
        param<Long>("b", "Second")
        handler { args -> args.get<Long>("a") + args.get<Long>("b") }
    }
    tool("refund", "Refunds a charge once") {
        param<String>("charge", "The charge to refund")
        handler { args -> billing.refund(args.get<String>("charge"), args.idempotencyKey) }
    }
})
val result = agent.run("What is 2 + 3?")   // a suspend function: call it in a coroutine

A client block takes the fields of its Java builder (see Configure in code), with a kotlin.time.Duration for each timeout; completionOptions(base) { ... } starts from existing options. tool declares one tool and tools a list of them, to which add appends one built with JTool.builder or JTool.fromAnnotated. param<T> and optionalParam<T> take the types JTool.builder does - String, Int, Long, Short, Byte, Double, Float, Boolean or an enum - and the handler reads them with args.get<T>(name), or args.getOrNull<T>(name) for an optional one the model may leave out. args.idempotencyKey and args.toolCallId describe the call (#1740), null outside an agent run; args.java is the JToolArgs underneath.

The handler is a suspend function. The agent runs a tool on a thread of its own, which waits for the handler in a coroutine of its own (runBlocking), not in the caller’s. Cancelling the coroutine that runs the turn - the caller of run, a collector of stream, a withTimeout - cancels the turn, which interrupts that thread and so cancels the handler: a suspended delay, withContext or other cancellable call throws CancellationException.

An agent block

agent(id, client) { ... } is Llm4s.agentBuilder in Kotlin (#2534): a system prompt, tools, guardrails on each query and on each final answer, a context window and streaming, each meaning what it means in the Java builder.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import org.llm4s.javaapi.JGuardrails
import org.llm4s.kotlin.AgentKt
import org.llm4s.kotlin.agent

val built: Result<AgentKt> = agent("support", client) {
    systemPrompt("You are a support assistant. Answer in JSON.")
    tools {
        tool("add", "Adds two numbers") {
            param<Long>("a", "First")
            param<Long>("b", "Second")
            handler { args -> args.get<Long>("a") + args.get<Long>("b") }
        }
    }
    inputGuardrails(JGuardrails.length(1, 10_000), JGuardrails.profanity())
    outputGuardrails(JGuardrails.json())   // inputGuardrails(JGuardrails.json()) does not compile
    contextWindow(lastMessages = 20)
    streaming()
}

val support = built.getOrThrow()          // or built.onFailure { e -> log(e.message) }
val result = support.run("What is 2 + 3?")
result.status().guardrail().ifPresent { g ->
    println("Blocked by $g: ${result.status().reason().orElse("")}")
}

Unlike the other blocks, agent does not throw: as with the Java builder, a blank id, an invalid guardrail or window (JGuardrails.length(5, 1), lastMessages = 0), two tools with one name, or a tool in tools { ... } that cannot be built gives a failed Kotlin Result holding an LLMException that names the first problem. tools also takes JTools (tools(add, refund), or a list), and each guardrail setter a list. A setting given twice replaces the first. The agent is an AgentKt, so its turns are the suspend functions and Flows of Llm4s.createAgent’s: run, continueConversation, stream and the rest.

Embeddings

An embedding model turns a text into a vector, a float[], and texts that mean similar things get vectors that point in similar directions: the basis of semantic search. Llm4s.createDefaultEmbeddingClient() creates a JEmbeddingClient for the model application.conf names, as createDefaultClient() does for chat. The model is provider/model under llm4s.embeddings, or the EMBEDDING_MODEL environment variable:

1
2
3
4
5
llm4s {
  embeddings {
    model = "openai/text-embedding-3-small"   # or set EMBEDDING_MODEL
  }
}

The key comes from the vendor’s variable, OPENAI_API_KEY here, as a chat section’s does. The embedding providers are in llm4s-openai (openai) and llm4s-ollama (ollama); Voyage, Jina and Cohere come with their own modules (llm4s-voyage, llm4s-jina, llm4s-cohere). Creating the client sends no request, and does not throw:

1
2
3
LlmResult<JEmbeddingClient> created = Llm4s.createDefaultEmbeddingClient();
created.ifFailure(error -> System.err.println("Could not create an embedding client: " + error.getMessage()));
JEmbeddingClient embedder = created.getOrNull();   // null when it failed

No model configured, a provider that is not on the classpath or a missing key is a failed result of kind CONFIGURATION, whose message says what to set. embedder.model() is the model as configured, and embedder.dimensions() an OptionalInt: its vector length as the provider’s module declares it, or empty for a model the module does not list, such as an Ollama model you pulled yourself - that model still works, and its vectors are as long as the provider makes them.

To embed with a model other than the one application.conf names - two models side by side, say - Llm4s.createEmbeddingClient takes it as provider/model. The provider’s key, base URL and timeouts still come from configuration, as for the default client, so llm4s.embeddings.model need not be set at all:

1
2
JEmbeddingClient large = Llm4s.createEmbeddingClient("openai/text-embedding-3-large").get();
JEmbeddingClient local = Llm4s.createEmbeddingClient("ollama/bge-m3").get();   // dimensions() is empty

A null model is a failed result of kind VALIDATION; one not in the form provider/model, a provider that is not on the classpath or a missing key, one of kind CONFIGURATION.

embed takes a java.util.List<String> and returns a JEmbeddings: one vector per text, in the order of the texts, from a single request. JEmbeddings.cosineSimilarity compares two of them:

1
2
3
4
5
6
7
JEmbeddings embeddings = embedder.embed(List.of(
    "The cat sat on the mat.",
    "A kitten was sitting on the rug.")).get();

List<float[]> vectors = embeddings.vectors();
double similarity = JEmbeddings.cosineSimilarity(vectors.get(0), vectors.get(1));
System.out.println(embeddings.model() + ", " + embeddings.dimensions() + " dimensions: similarity " + similarity);

model() is the model as the provider named it in its reply, and dimensions() the length of every vector. vectors() returns a new unmodifiable list of new arrays on each call, so changing one changes nothing; call it once and keep the list. A JEmbeddings is a value, with equals and hashCode over its vectors’ components. Core’s vectors are doubles; these are their nearest floats, the precision vector stores keep.

cosineSimilarity goes from -1 (opposite) through 0 (unrelated) to 1 (the same direction). A zero vector has no direction, so its similarity to anything is 0. Two vectors of different lengths, which come from different models, throw IllegalArgumentException, and a null throws NullPointerException.

Several embedding models embed a search query differently from the documents it searches, and comparing a query embedded as a document quietly finds worse matches. Say which side a text is on with JEmbeddingPurpose:

1
2
3
4
5
6
7
8
9
10
List<float[]> documents = embedder.embed(texts, JEmbeddingPurpose.DOCUMENT).get().vectors();
float[] query = embedder.embed(List.of("Where did the cat sit?"), JEmbeddingPurpose.QUERY).get().vectors().get(0);

int best = 0;
for (int i = 1; i < documents.size(); i++) {
    if (JEmbeddings.cosineSimilarity(query, documents.get(i)) > JEmbeddings.cosineSimilarity(query, documents.get(best))) {
        best = i;
    }
}
System.out.println("closest: " + texts.get(best));

embed(texts) without a purpose embeds documents. Voyage and Cohere send the purpose as input_type and Jina as task; OpenAI and Ollama embed both alike and ignore it, so code that says which side it is on works with any of them.

Like complete, embed blocks and never throws: an empty list returns no vectors without a request, a null list, text or purpose is a failed result of kind VALIDATION, and an interrupt one of kind CANCELLED. A provider’s error response has the kind of its HTTP status - 401 reads as AUTHENTICATION, 429 as RATE_LIMIT, 400 as VALIDATION, any other as SERVICE, with getStatusCode(). A provider that cannot be reached is NETWORK, and one that does not answer in time TIMEOUT; any other failure with no status, such as a reply that cannot be read, is OTHER. To keep the vectors in a store, search them and answer questions from what they find, see Java RAG (llm4s-java-rag).

AI services

Declare an interface, annotate its methods with their prompts, and Llm4s.aiService(MyService.class).client(client).build() implements it: @SystemMessage and @UserMessage templates filled from @V parameters, answers returned as String, an enum, a record, JCompletion, LlmResult<T>, CompletableFuture<T> or a streamed Flow.Publisher<String>, per-conversation memory through @MemoryId, and tools run in an agent turn. Mistakes in the interface fail at build(). It is experimental; Java AI services is its guide, with compiled examples.

Testing your code

For a complete runnable example, see the Java offline ticket classifier. It injects a JLlmClient, handles unknown replies and provider failures, and checks recorded prompts in its test.

FakeLlmClient is a fake model for your own tests: it answers from a script, records every prompt your code sent, and needs no network or API key. Its client() is a real JLlmClient, so the code under test needs no interface or mock - give it the fake’s client wherever it takes one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class CalculatorTest {

    @Test
    void answersFromTheModel() {
        FakeLlmClient fake = FakeLlmClient.replying("4");

        String answer = new Calculator(fake.client()).ask("What is 2+2?");

        assertEquals("4", answer);
        assertEquals(List.of("What is 2+2?"), fake.prompts());
    }

    @Test
    void reportsAProviderFailure() {
        FakeLlmClient fake = FakeLlmClient.builder()
            .failWith(LlmErrorKind.RATE_LIMIT, "slow down")
            .build();

        LlmResult<String> result = fake.client().complete("What is 2+2?");

        assertTrue(result.isFailure());
        assertEquals(LlmErrorKind.RATE_LIMIT, result.getError().getKind());
    }
}

Calculator stands for your own class: here, one whose ask returns client.complete(question).getOrNull(). FakeLlmClient.replying("a", "b") answers the first call with a and the second with b; builder() mixes replies (reply) and failures (failWith, with the LlmErrorKind that getKind() then reports). A call beyond the script is a failed result whose message names the call and its prompt, so a test that makes more calls than it scripted says why. callCount(), prompts() (the last user message of each call, in order) and lastPrompt() show what your code sent.

The fake is in llm4s-java-api itself, so a test needs no other dependency. It answers complete, completion and structured output; for an agent with tools from Scala, llm4s-testkit’s ScriptedLLMClient scripts tool calls too (Testing with the Testkit), and Llm4s.wrapClient turns it into a JLlmClient.

What is not here yet

llm4s-java-api covers a client, a conversation, the whole reply, the kind of a failure and embeddings. These are not available from Java yet, each with the issue that tracks it:

You may expect State today
Agents beyond a turn An agent turn reads a result; the agent guide covers streaming a turn and suspended turns from Java. Tools are defined from Java in Tools

Where next

  • Java AI services: an annotated interface that llm4s implements - prompt templates, typed answers, memory and tools.
  • Moving a Java app from LangChain4j or Spring AI: each LangChain4j and Spring AI concept mapped to this API, with compiled before-and-after code and provider configuration.
  • Java RAG: ingest documents and ask questions about them, with llm4s-java-rag.
  • Java Reactor: a streamed reply as a Reactor Flux<String>, with llm4s-reactor.
  • Spring Boot: auto-configuration, a template bean and a health indicator on top of this module.
  • Quarkus: the client as a CDI bean, configured from application.properties, behind a REST endpoint.
  • Basic usage and providers: the same concepts in Scala, and every provider’s configuration.
  • Gradle integration: dependency recipes for Gradle builds.
  • The gradle-java sample: a Java project you can run.