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:
- 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.
- 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.
- 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.
- 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.
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,EmbeddingStoreandEmbedding. 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.
Features
Models and stores:
- Integration with 20+ LLM providers
- Integration with 40+ embedding (vector) stores
- Integration with 25+ embedding models, including models that run inside your JVM
- Integration with 15+ chat memory stores
- Integration with 10+ image generation models
- Integration with 5+ scoring (re-ranking) models
- Integration with moderation models (OpenAI, Mistral AI, watsonx and more)
- Multimodal inputs: text, images, audio, video and PDF, depending on the model and the integration (see the comparison table)
Building applications:
- AI Services: a high-level API where you declare a Java interface and LangChain4j implements it
- Tools (function calling)
- Model Context Protocol (MCP): use the tools of MCP servers, or build your own server
- Structured outputs: answers as Java objects
- Chat memory: message window and token window, in memory or persisted
- Prompt templates
- Streaming of responses and non-blocking and reactive calls
- Guardrails for inputs and outputs, and tool guardrails for the calls and the results of tools
- Skills: reusable instructions that the model loads when it needs them
- Classification of text
- Model routing: send each request to the model that fits it
- Decision services and decision models: typed questions such as yes/no or one of several options
- Code execution of code generated by the model
- Tokenization and estimation of token counts
- Kotlin extensions with coroutine support
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:
- Observability: listeners for model calls, tools, RAG and AI Service events, and Micrometer metrics
- Logging of requests and responses
- Testing with mock models and model-based checks, and evaluation of answer quality
- A custom HTTP client
Library structure
LangChain4j has a modular design:
- The
langchain4j-coremodule defines the core abstractions (such asChatModelandEmbeddingStore) and their APIs. - The main
langchain4jmodule 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 mainlangchain4jmodule. - The
langchain4j-bom(bill of materials) keeps the versions of these modules in line, including the Spring Boot starters. Community integrations have their ownlangchain4j-community-bom. See Get Started.
Repositories
- Main repository
- Community integrations
- Quarkus extension
- Spring Boot integration
- Micronaut integration
- Jakarta EE (CDI) integration
- Examples
- Awesome LangChain4j: projects, articles and examples from the community
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.