Return to site

🕸️☕ LangGraph4J in 5 PRACTICAL STEPS

· jakartaee

Want to build a stateful AI workflow in Java instead of hiding everything behind one giant prompt?

Meet LangGraph4j.

🔸 TLDR

LangGraph4j brings graph-based agent orchestration to Java.

Think:

STATE → NODE → DECISION → NODE → ... → END

Instead of letting an LLM control everything, you explicitly model how your AI application can move through its workflow.

Section image

🔸 WHAT IS LANGGRAPH4J?

LangGraph4j is a Java library for building stateful, graph-based AI applications and agentic workflows.

You model the application as a graph:

▪️ State → shared data flowing through the workflow

▪️ Nodes → Java code, LLM calls, agents or tools doing the work

▪️ Edges → decide what happens next

▪️ Checkpoints → persist state and resume execution

It is designed to work with both LangChain4j and Spring AI.

Let's build the mental model in 5 steps. 👇

Let's build the mental model in 5 steps. 👇

🔸 1. DEFINE THE STATE

class MyState extends AgentState {
    static final String QUESTION = "question";
    static final String ANSWER = "answer";

    MyState(Map<String, Object> init) {
        super(init);
    }
}

The state is the workflow's shared memory. Each node can read it and return updates that are passed to the following nodes.

🔸 2. CREATE YOUR NODES

var research = node_async(state -> {
    var question = state.value("question", "");
    return Map.of("research", search(question));
});

A node performs one unit of work. It can execute normal Java code, call an LLM, query a database or invoke a tool.

🔸 3. CONNECT THE WORKFLOW

graph.addNode("research", research)
     .addNode("answer", answer)
     .addEdge(START, "research")
     .addEdge("research", "answer")
     .addEdge("answer", END);

Edges define the execution flow.

And they can be conditional, allowing the current state to determine which node executes next. 🔀

🔸 4. COMPILE THE GRAPH

var app = graph.compile();

Compilation turns your definition into an executable graph and validates its structure before execution.

🔸 5. RUN IT 🚀

var result = app.invoke(
    Map.of("question", "What is Project Valhalla?")
);

Give the graph its initial state and let it execute the workflow until it reaches END.

You can also stream intermediate states and add checkpoint persistence for long-running workflows.

🔸 LANGCHAIN4J VS LANGGRAPH4J

They overlap, but they solve the problem at different levels.

▪️ LangChain4j is primarily an AI application toolkit: connect to LLMs, manage prompts and memory, call tools, build RAG pipelines and create AI Services. It now also includes its own agentic APIs.

▪️ LangGraph4j focuses on orchestration: model a complex AI workflow explicitly as a graph of states, nodes and edges, with conditional routing, checkpoints, pause/resume and streaming.

A simple way to picture it:

LangChain4j → gives your Java application AI capabilities. 🧠

LangGraph4j → coordinates how those capabilities work together over time. 🕸️

And they are not necessarily competitors.

LangGraph4j is explicitly designed to work with LangChain4j (or Spring AI) inside its graph nodes.

For example:

LangChain4j LLM + Tools + RAG + Memory

LangGraph4j START → Research Agent → Decision → Tool → Review → END

👉 Use LangChain4j when the main challenge is integrating AI capabilities into Java.

👉 Consider LangGraph4j when the main challenge becomes controlling a multi-step, stateful workflow involving those capabilities.

🔸 WHICH KIND OF "GRAPH" ARE WE TALKING ABOUT? 🕸️

The word graph appears in several technologies, but it does not mean the same thing.

▪️ 🔴LangGraph4j → EXECUTION GRAPH

In LangGraph4j, the graph describes how an AI workflow executes.

START
  ↓
RESEARCH
  ↓
DECISION
 ↙     ↘
TOOL   ANSWER
         ↓
        END

A node is a computation step: Java code, an LLM call, an agent or a tool.

An edge determines which step can execute next.

👉 The graph represents control flow and state transitions.

▪️ 🟠Neo4j → KNOWLEDGE / DATA GRAPH

Neo4j is a graph database.

Here, nodes represent stored entities and relationships represent how those entities are connected.

(Java) ──USED_BY──> (Spring)
   │
   └──HAS_FEATURE──> (Virtual Threads)

The graph is the data itself.

👉 Neo4j answers questions such as: "How are these things related?"

▪️🟡GraphQL → QUERY LANGUAGE FOR CONNECTED DATA

Despite its name, GraphQL does not store a graph.

It is an API query language that lets clients request exactly the structured and related data they need.

user {
  posts {
    comments
  }
}

👉 GraphQL answers: "What data do I want from this API?"

So remember:

▪️ Neo4j → graph of knowledge and relationships 🧠

▪️ GraphQL → query related API data 🔎

▪️ LangGraph4j → graph of execution and decisions 🤖

Same word: graph. Three very different abstractions.

Neo4j stores the graph. GraphQL queries data. LangGraph4j executes the graph.

🔸 LANGGRAPH4J VS EMBABEL

Both build agentic applications on the JVM, but their orchestration philosophy is quite different.

▪️🟢 LangGraph4j → YOU DESIGN THE GRAPH

With LangGraph4j, you explicitly define the workflow:

START
  ↓
RESEARCH
  ↓
DECISION
 ↙     ↘
TOOL   ANSWER
         ↓
        END 

You define the nodes, edges, state and routing rules.

That gives you strong control over how execution moves through the application, including conditional paths, checkpoints, pause/resume and human-in-the-loop workflows.

Think: "Here is the workflow. Execute it."

▪️ 🔵Embabel → YOU DEFINE THE GOAL

Embabel takes a more goal-oriented approach.

You define things such as:

@Action
Research research(Topic topic) { ... }

@Action
Article write(Research research) { ... }

@AchievesGoal
Article finishedArticle(...) { ... }

Instead of explicitly wiring every transition, Embabel can determine which actions are needed to reach the goal.

Its planner can dynamically create a sequence of actions and replan after each action when the situation changes.

Think: "Here are the actions available and the goal. Find a way to achieve it."

So the mental model is:

▪️ LangGraph4j → explicit workflow orchestration 🕸️

▪️ Embabel → goal-oriented agent planning 🧠

Or even shorter:

LangGraph4j: you draw the road.

Embabel: you define the destination.

Both can produce sophisticated agentic systems; but LangGraph4j emphasizes explicit control flow, while Embabel emphasizes dynamic planning toward goals.

🔸 TAKEAWAYS

▪️ LangGraph4j is about orchestration, not another LLM.

▪️ Nodes perform work; edges control where execution goes next.

▪️ State travels across the entire workflow.

▪️ Conditional edges enable dynamic agent behavior.

▪️ Checkpoints make long-running and resumable workflows possible.

▪️ It integrates with Spring AI and LangChain4j.

Java AI is moving beyond "send prompt, get answer."

We're starting to build actual workflows. 🕸️🤖☕

#Java #LangGraph4j #AI #AgenticAI #JavaAI #SpringAI #LangChain4j #LLM #SoftwareEngineering #GenerativeAI

Go further with Java certification:

Java👇

Spring👇

SpringBook👇

JavaFullstackBook👇