LangChain4j — LLM Integration for Java
LangChain4j integrates 20+ LLM providers and 30+ vector stores into Java apps. 11.4K+ stars. Unified API, RAG, MCP, Spring Boot. Apache 2.0.
Installation agent prête
Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.
npx -y tokrepo@latest install f7069ed9-81bd-4e67-a727-44e37eb529e7 --target codexÀ exécuter après confirmation du plan en dry-run.
What it is
LangChain4j is an open-source Java framework that brings LLM integration patterns to the Java ecosystem. It provides a unified API across 20+ LLM providers (OpenAI, Anthropic, Google, Ollama, and others) and 30+ vector store backends. The framework supports RAG pipelines, tool use, MCP protocol, structured output, and Spring Boot auto-configuration.
The project targets Java and Kotlin developers building AI-powered enterprise applications who need the same capabilities that Python developers get from LangChain, but in a Java-native form factor.
How it saves time or tokens
LangChain4j eliminates the need to write provider-specific HTTP clients for each LLM API. Switching from OpenAI to Anthropic requires changing one configuration line, not rewriting API integration code. The declarative AI Services interface lets developers define AI capabilities as Java interfaces with annotations, reducing boilerplate to near-zero. RAG pipelines are assembled from composable components rather than built from scratch.
How to use
- Add the Maven dependency:
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j</artifactId>
<version>1.0.0</version>
</dependency>
- Create a chat model and send a message:
ChatLanguageModel model = OpenAiChatModel.withApiKey("sk-...");
String answer = model.chat("Hello!");
System.out.println(answer);
- Define an AI Service interface for structured interaction:
interface Assistant {
@SystemMessage("You are a helpful coding assistant.")
String chat(String userMessage);
}
Assistant assistant = AiServices.create(Assistant.class, model);
String response = assistant.chat("Explain Java records.");
Example
// RAG pipeline with in-memory embedding store
import dev.langchain4j.data.document.Document;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
EmbeddingModel embeddingModel = OpenAiEmbeddingModel.withApiKey("sk-...");
InMemoryEmbeddingStore<TextSegment> store = new InMemoryEmbeddingStore<>();
// Ingest documents
Document doc = Document.from("LangChain4j supports 20+ LLM providers.");
EmbeddingStoreIngestor.ingest(doc, embeddingModel, store);
// Query with RAG
ContentRetriever retriever = EmbeddingStoreContentRetriever.from(store);
Assistant ragAssistant = AiServices.builder(Assistant.class)
.chatLanguageModel(model)
.contentRetriever(retriever)
.build();
String answer = ragAssistant.chat("How many providers?");
Related on TokRepo
- RAG tools directory -- Retrieval-augmented generation frameworks and tools
- AI tools for coding -- Developer tools for AI application development
Common pitfalls
- Version 1.0.0 introduced breaking API changes from the 0.x series; migration guides are available in the documentation
- The AI Services interface uses runtime proxy generation; ensure your build tool does not strip reflection metadata
- Spring Boot auto-configuration requires the separate
langchain4j-spring-boot-starterdependency, not just the core library
Questions fréquentes
LangChain4j is inspired by Python LangChain but is a separate project built natively for Java. It follows Java conventions with interfaces, annotations, and dependency injection rather than Python's decorator and chain patterns. The feature set covers similar ground: LLM integration, RAG, tool use, and agents.
LangChain4j supports OpenAI, Anthropic, Google Vertex AI, Google Gemini, Azure OpenAI, Hugging Face, Ollama, Mistral, Cohere, and more. Each provider has a dedicated module. Switching providers requires changing the model instantiation line without modifying business logic.
Yes. The langchain4j-spring-boot-starter provides auto-configuration for chat models, embedding models, and vector stores. You configure LLM providers in application.yml and inject them as Spring beans. This integrates naturally with existing Spring Boot applications.
Yes. The langchain4j-ollama module connects to a local Ollama instance. Configure the base URL and model name, and LangChain4j routes requests to your local models with the same API as cloud providers.
LangChain4j supports 30+ vector stores including Pinecone, Weaviate, Milvus, Qdrant, Chroma, pgvector, Elasticsearch, Redis, and an in-memory store for development. Each has a dedicated module with consistent API.
Sources citées (3)
- LangChain4j GitHub— LangChain4j integrates 20+ LLM providers and 30+ vector stores
- LangChain4j Documentation— AI Services interface for declarative LLM interaction in Java
- LangChain4j Spring Boot Guide— Spring Boot auto-configuration support
En lien sur TokRepo
Source et remerciements
langchain4j/langchain4j — 11,400+ GitHub stars
Fil de discussion
Actifs similaires
Apache Camel — Enterprise Integration Framework for Java
Apache Camel is an open-source integration framework that implements the Enterprise Integration Patterns. It provides a routing and mediation engine with connectors for over 300 protocols and data formats, enabling developers to integrate systems using a concise Java or YAML DSL.
AgentChat — LLM Chat + MCP Integration
AgentChat is a modern LLM chat system (FastAPI + Vue) with knowledge base, tool calling, and MCP server integration, plus a Docker Compose deploy path.
LangChain — Build LLM-Powered Applications
The most popular framework for building applications with large language models. Chains, agents, RAG, memory, tool use, and integrations with 700+ providers.
Manifest — Smart LLM Router That Cuts Costs 70%
Intelligent LLM routing that scores requests across 23 dimensions in under 2ms. Routes to the cheapest capable model among 300+ options from 13+ providers. MIT, 4,200+ stars.