DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk6 min

How to Add LLM Features to a Java Application with LangChain4j

Start with a direct LangChain4j ChatModel call, then add AI Services, memory, and retrieval only when your Java application needs them.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Start with a direct ChatModel call to verify that your Java application can reach a model provider; then move to an AI Service interface if you want a typed application-facing API. Add conversation memory, tools, or retrieval only when the feature needs them. LangChain4j’s getting-started guide requires JDK 17 or later, and its dependency versions and example model names can change, so check the current documentation before copying them.

What LangChain4j does—and which API to start with

LangChain4j is a Java library for connecting applications to language models and related components. Its current introduction lists integrations with 20+ LLM providers and 30+ embedding stores, along with features such as AI Services, prompt templates, chat memory, streaming, output parsing, tool calling, and retrieval-augmented generation (RAG). These are project documentation claims and can change over time. See the LangChain4j introduction.

You can assemble low-level components yourself or use AI Services to reduce orchestration code. For a first connection check, the low-level ChatModel API makes the request path visible. For application code that benefits from a declarative, typed interface, AI Services can handle common input formatting and output parsing and can optionally add memory, tools, or RAG. LangChain4j implements an AI Service interface through a proxy. The project describes Chains as legacy and does not plan to expand them at this time, so AI Services are the better starting point for new higher-level examples. See Chat and Language Models and AI Services.

Make a minimal hosted-model call

The official getting-started example uses Maven, the OpenAI integration, an API key in an environment variable, and a direct chat call. The artifact version and model name below are documentation examples, not permanent recommendations. Verify the current dependency and provider instructions before using them.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Check the project runtime. LangChain4j’s getting-started page states a minimum supported JDK version of 17. Confirm the JDK used to compile and run your application.
  2. Add the provider integration. For the guide’s OpenAI example, add this Maven dependency; check the current Get Started page for the version to use:
    <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j-open-ai</artifactId>
        <version>1.21.0</version>
    </dependency>
  3. Configure credentials outside source code. Set OPENAI_API_KEY in the environment that launches the application. Read it with System.getenv("OPENAI_API_KEY"); do not hard-code a secret into a Java class or commit it to source control. LangChain4j recommends environment variables to reduce the risk of public exposure.
  4. Construct a model and send a prompt. The following follows the documentation’s basic pattern. Use a model name supported by your provider account and the current integration:
    import dev.langchain4j.model.openai.OpenAiChatModel;
    
    public class LlmExample {
        public static void main(String[] args) {
            String apiKey = System.getenv("OPENAI_API_KEY");
            if (apiKey == null || apiKey.isBlank()) {
                throw new IllegalStateException("Set OPENAI_API_KEY before running this application");
            }
    
            OpenAiChatModel model = OpenAiChatModel.builder()
                    .apiKey(apiKey)
                    .modelName("gpt-4o-mini")
                    .build();
    
            String answer = model.chat("Explain what an API is in one sentence.");
            System.out.println(answer);
        }
    }

    The example model name is time-sensitive; confirm the provider’s current model identifiers and the LangChain4j integration documentation. A successful response confirms that this basic application path works; it does not add conversation history or retrieve your application’s private data.

This example focuses on provider-specific setup. The dependency, credentials, and model identifier vary by provider; the concepts of sending chat input and receiving a response are the framework-level part. The LangChain4j documentation says the main langchain4j dependency is also needed when using the higher-level AI Services API.

Move from a direct call to an AI Service

A direct ChatModel call is useful when you want explicit control over model requests and their surrounding logic. As an application grows, you may prefer to expose a task as a Java interface rather than repeat prompt construction and response handling throughout the code. AI Services provide that declarative layer.

Approach Best fit Trade-off
Low-level ChatModel A small integration, or a flow where you want to compose model and message operations directly. More orchestration and input/output handling remains in application code.
AI Services A task that can be represented as an application-facing Java interface method. Less boilerplate for common formatting and parsing, with behavior expressed through the AI Service abstraction.

When you introduce AI Services, add the core langchain4j dependency as well as the provider integration, following the versions and setup shown in the current documentation. Define methods around product tasks—such as drafting a reply or classifying a support request—and use the documented AI Service configuration to bind the interface to a model. Consult the AI Services tutorial for current syntax and supported options; provider selection remains a separate configuration concern.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Add memory only when a feature needs prior turns

A model call is not automatically a continuing conversation. Chat memory is the context LangChain4j supplies so the model can respond as though it remembers earlier turns. Conversation history is the complete exchange your application stores or displays to the user. These are related but different responsibilities.

A memory strategy can evict messages, summarize earlier turns, remove details, or add information or instructions to the context. A bounded memory window therefore defines what the model can see, not how much transcript your product retains. If users need a complete chat transcript, store it separately according to your product’s retention and privacy requirements. See Chat Memory for the framework’s memory options and behavior.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Give the model access to application knowledge with RAG

Retrieval-augmented generation (RAG) retrieves relevant material from your data and inserts it into the model prompt before the response is generated. LangChain4j describes two broad stages:

  1. Indexing: load and prepare source documents, represent them for search, and place them in a store.
  2. Retrieval: find material relevant to a user’s request and provide it as context for the model.

Retrieval may use keyword or full-text search, vector or semantic search, or a hybrid of both. The current RAG documentation says full-text and hybrid search are supported only by the Azure AI Search and Elasticsearch integrations; verify that limitation against the current RAG tutorial before choosing an integration.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Easy RAG for a proof of concept

LangChain4j’s Easy RAG path is intended to get a proof of concept running with less setup. It combines document ingestion, an embedding store, and a chat model, with bounded memory available when needed. The documentation cautions that this simpler configuration can produce lower quality than a tailored pipeline. Treat it as a way to validate the product flow, not as evidence that a vector store by itself guarantees correct answers.

Customize retrieval when the use case demands it

For a production feature, you may need control over document loading, segmentation, embeddings, storage, retrieval, and reranking. The right choices depend on the shape and freshness of your content and on what counts as a useful result for your users. Measure retrieval quality against representative questions and source material; a fluent answer can still be unsupported if the relevant content was not indexed or retrieved.

Choose hosted inference or an optional local route

A hosted provider integration is the simplest path shown in the getting-started guide: configure that provider’s dependency and credentials, then call its chat model. If you need local inference, LangChain4j documents a Jlama integration, but it is not the same minimal setup. Its example requires the Jlama integration and a native dependency, and the documentation states that Jlama uses Java 21 preview features. That brings additional runtime and build configuration. The Jlama page does not establish a hardware recommendation or benchmark, so evaluate compatibility and performance for your own deployment rather than assuming a particular machine will be suitable. See Jlama integration documentation.

A practical implementation sequence

  • Prove provider connectivity with one direct chat call before adding abstractions.
  • Move task-specific behavior behind an AI Service interface when it reduces repeated application logic.
  • Add memory when answers genuinely depend on earlier turns, and keep full transcript storage separate if the product requires it.
  • Add RAG when the model needs your application’s private or domain-specific material; validate what is retrieved, not only how the answer reads.
  • Recheck artifact versions, provider model names, and integration support in the current LangChain4j documentation before deployment.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.