Skip to main content

Introduction

LangChain4j is an open-source Java library for building applications and agents powered by large language models (LLMs). It helps you add AI features to a Java application, from a single question to a model up to agents that use tools, your own documents and other agents.

Here is what it gives you:

  1. One API for many providers. LLM providers (such as OpenAI, Anthropic or Google Gemini) and embedding (vector) stores (such as PostgreSQL with pgvector, or Milvus) each have their own API. LangChain4j gives you one API for all of them, so you can try or switch providers without rewriting your code. It supports 20+ LLM providers and 40+ embedding stores.
  2. Building blocks for common patterns. Prompt templates, chat memory, tools, structured outputs, retrieval-augmented generation (RAG), guardrails and agents each come as a Java interface with ready-to-use implementations. You can start with a few lines of code and customize each part when you need to.
  3. A library made for Java. You work with interfaces, annotations, POJOs and records, dependency injection and fluent builders. LangChain4j works in plain Java and integrates with Quarkus, Spring Boot, Helidon, Micronaut and Jakarta EE.
  4. Many examples. The examples repository shows how to build different kinds of applications, in plain Java and with the frameworks above. For Quarkus, see also the Quarkus LangChain4j samples.

To write your first code, go to Get Started.

Not a port of LangChain (Python)

Despite the name, LangChain4j is not a Java port of LangChain (Python) — it is built for Java, not ported to it. It is designed from the ground up around Java conventions: type safety, POJOs, annotations, interfaces, dependency injection and fluent APIs. Its API, internals, and release cycle are independent of the Python LangChain project.

Two levels of abstraction​

LangChain4j offers two levels of abstraction:

  • Low level. Here you work directly with the building blocks, such as ChatModel, UserMessage, AiMessage, EmbeddingStore and Embedding. You have full control over how to combine them, but you write more glue code.
  • High level. Here you use APIs such as AI Services and Agents. You declare what you need, for example as a Java interface, and LangChain4j does the work behind it. You can still adjust every part of the behavior.

LangChain4j at two levels: AI Services and Agents on top of low-level building blocks for basics and RAG

Features​

Models and stores:

Building applications:

Agents:

  • Agents and agentic AI: workflows (sequence, loop, parallel, conditional), supervisor agents, more agentic patterns, human-in-the-loop steps, agent-to-agent (A2A) communication, and runs that can be suspended, persisted and resumed

RAG (Retrieval-Augmented Generation):

  • Ingestion:
    • Loading documents of many types (TXT, PDF, DOC, PPT, XLS, etc.) from many sources (file system, URL, GitHub, Azure Blob Storage, Amazon S3, etc.)
    • Splitting documents into smaller segments using several splitting algorithms
    • Post-processing of documents and segments
    • Embedding segments using embedding models
    • Storing embeddings in an embedding (vector) store
  • Retrieval (simple and advanced):
    • Transformation of queries (expansion, compression)
    • Routing of queries
    • Retrieving from a vector store and/or any custom sources
    • Re-ranking
    • Reciprocal Rank Fusion
    • Customization of each step in the RAG flow

Running in production:

Library structure​

LangChain4j has a modular design:

  • The langchain4j-core module defines the core abstractions (such as ChatModel and EmbeddingStore) and their APIs.
  • The main langchain4j module contains high-level features such as AI Services, as well as useful tools like document loaders and chat memory implementations.
  • Many langchain4j-{integration} modules each integrate one LLM provider, embedding store or other service. You can use an integration module on its own. For the high-level features, also add the main langchain4j module.
  • The langchain4j-bom (bill of materials) keeps the versions of these modules in line, including the Spring Boot starters. Community integrations have their own langchain4j-community-bom. See Get Started.

Repositories​

Use cases​

Some examples of what you can build:

  • Assistants that know your data and can act. For example, a customer support assistant that answers questions from your documentation, and looks up, changes or cancels orders through your own services.
  • Agents for multi-step tasks. Agents that plan and carry out a task with tools and other agents, and ask a person for approval when needed.
  • Extraction of structured data. Turn unstructured text, documents and images (invoices, CVs, support tickets, customer reviews) into Java objects.
  • Classification and routing. Sort support tickets, moderate content, or decide which workflow should handle a request.
  • Generation and transformation of text. Write emails tailored to each customer, summarize, proofread, rewrite or translate.

Community integrations​

Some integrations are maintained in the community repository. They work with the same APIs as the integrations in the main repository. Their artifact and package names contain community (for example langchain4j-community-xinference). The community repository makes it easier to maintain more integrations, separately from the main repository.