# LangChain4j

> Idiomatic Java library for building LLM-powered apps on the JVM

- Category: Agents & Automation / Agent Frameworks & Builders
- Language: Java
- Licence: Apache-2.0
- Repository: https://github.com/langchain4j/langchain4j
- Website: https://docs.langchain4j.dev
- Canonical page: https://ai-tldr.dev/tools/langchain4j/

## In short

LangChain4j is an open-source Java library that makes it easier to add large language models to Java applications. It sits in the Agent Frameworks & Builders category of the Agents & Automation tools and is open-source, written in Java and licensed Apache-2.0. To get started: add the dependencies.

## Overview

LangChain4j is an open-source Java library that makes it easier to add large language models to Java applications. It gives you a single, unified API so you can call many different LLM providers and embedding (vector) stores without learning each provider's own API. If you want to try a different model or store, you can switch with minimal code changes.

Despite the name, it is not a Java port of the Python LangChain project. It is built from the ground up for Java, using familiar conventions like type safety, POJOs, annotations, interfaces, and fluent builders. It ships first-class integrations for Quarkus, Spring Boot, Helidon, and Micronaut, and offers ready-made building blocks for prompt templating, chat memory, function calling, RAG, and agents.

## What it does

- One unified API across 20+ popular LLM providers (such as OpenAI and Google Vertex AI), so you can swap models without rewriting your code
- Built-in support for 30+ embedding (vector) stores like Pinecone and Milvus for retrieval workloads
- A toolbox covering low-level pieces (prompt templating, chat memory, function calling) up to high-level patterns like RAG and agents
- Designed around Java conventions: type safety, POJOs, annotations, interfaces, and fluent APIs
- First-class integrations with Quarkus, Spring Boot, Helidon, and Micronaut
- A large set of runnable examples in the separate langchain4j-examples repository to help you start quickly

## Getting started

LangChain4j is published to Maven Central. It needs JDK 17 or newer. Add the core library and the integration module for the provider you want to use (here, OpenAI), then call a model.

1. Add the dependencies

   Add the core LangChain4j library and the OpenAI integration module to your Maven project. Use the latest version shown on Maven Central.

```xml
<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j</artifactId>
    <version>1.11.8</version>
</dependency>
<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-open-ai</artifactId>
    <version>1.11.8</version>
</dependency>
```

2. Call a model

   Build a chat model with your API key and model name, then send a message. The chat method returns the model's reply as a String.

```java
String apiKey = System.getenv("OPENAI_API_KEY");

OpenAiChatModel model = OpenAiChatModel.builder()
    .apiKey(apiKey)
    .modelName("gpt-4o-mini")
    .build();

String answer = model.chat("Say 'Hello World'");
System.out.println(answer);
```

3. Explore more examples

   The langchain4j-examples repository contains runnable samples in plain Java and for Quarkus, Spring Boot, Helidon, and Micronaut, covering chatbots, RAG, and more. The full docs live at docs.langchain4j.dev.

## When to use it

- Building chatbots and assistants inside Java and Spring Boot or Quarkus applications
- Creating RAG pipelines that ground answers in your own documents using embedding stores
- Adding function calling and agent-style workflows so an LLM can use tools and take actions
- Staying portable across LLM providers by switching models behind one unified API
