A library for building stateful, multi-agents applications with LLMs
npx @tessl/cli install tessl/maven-org-bsc-langgraph4j--langgraph4j-core@1.6.0LangGraph4j is a Java library for building stateful, multi-agent applications with Large Language Models (LLMs). It provides a framework for creating cyclical graphs where different components (agents, tools, or custom logic) can interact in a stateful manner, enabling complex AI applications with memory, context awareness, and sophisticated control flow patterns.
<dependency>
<groupId>org.bsc.langgraph4j</groupId>
<artifactId>langgraph4j-core</artifactId>
<version>1.6.4</version>
</dependency>import org.bsc.langgraph4j.*;
import org.bsc.langgraph4j.action.*;
import org.bsc.langgraph4j.state.*;
import org.bsc.langgraph4j.checkpoint.*;import org.bsc.langgraph4j.*;
import org.bsc.langgraph4j.action.AsyncNodeAction;
import org.bsc.langgraph4j.state.AgentState;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
// Define a custom state class
class MyState extends AgentState {
public MyState(Map<String, Object> initData) {
super(initData);
}
}
// Create a state graph
StateGraph<MyState> workflow = new StateGraph<>(MyState::new);
// Add nodes with actions
workflow.addNode("start_node", (state) ->
CompletableFuture.completedFuture(Map.of("processed", true))
);
workflow.addNode("end_node", (state) ->
CompletableFuture.completedFuture(Map.of("finished", true))
);
// Add edges between nodes
workflow.addEdge("start_node", "end_node");
workflow.addEdge("end_node", StateGraph.END);
// Set entry point
workflow.addEdge(StateGraph.START, "start_node");
// Compile and run
CompiledGraph<MyState> app = workflow.compile();
Optional<MyState> result = app.invoke(Map.of("input", "Hello World"));LangGraph4j is built around several key components:
AgentState and Channel system for managing application state with configurable update patternsStateGraph for defining workflows and CompiledGraph for executionCompileConfig and RunnableConfigCore functionality for building and managing stateful workflow graphs. Supports nodes, edges, conditional routing, and subgraphs.
class StateGraph<State extends AgentState> {
StateGraph<State> addNode(String id, AsyncNodeAction<State> action);
StateGraph<State> addNode(String id, AsyncNodeActionWithConfig<State> action);
StateGraph<State> addEdge(String sourceId, String targetId);
StateGraph<State> addConditionalEdges(String sourceId, AsyncCommandAction<State> condition, Map<String, String> mappings);
CompiledGraph<State> compile();
CompiledGraph<State> compile(CompileConfig config);
}Execute compiled graphs with sophisticated control flow, streaming, and state management. Support for synchronous execution, streaming outputs, and state snapshots.
class CompiledGraph<State extends AgentState> {
Optional<State> invoke(Map<String, Object> inputs);
Optional<State> invoke(GraphInput input, RunnableConfig config);
AsyncGenerator<NodeOutput<State>> stream(Map<String, Object> inputs, RunnableConfig config);
AsyncGenerator<NodeOutput<State>> streamSnapshots(Map<String, Object> inputs, RunnableConfig config);
}Comprehensive state management with channels, serialization, and update strategies. Handles complex state updates with merge functions and conflict resolution.
class AgentState {
Map<String, Object> data();
<T> Optional<T> value(String key);
static Map<String, Object> updateState(Map<String, Object> state, Map<String, Object> partialState, Map<String, Channel<?>> channels);
}
interface Channel<T> {
Object update(String key, Object currentValue, Object newValue);
}Define node behavior with synchronous and asynchronous actions. Support for configuration-aware actions and complex routing logic.
@FunctionalInterface
interface AsyncNodeAction<S extends AgentState> {
CompletableFuture<Map<String, Object>> apply(S state);
static <S extends AgentState> AsyncNodeAction<S> node_async(NodeAction<S> syncAction);
}
@FunctionalInterface
interface AsyncCommandAction<S extends AgentState> {
CompletableFuture<Command> apply(S state, RunnableConfig config);
}Persistent state management with checkpointing for debugging, resuming execution, and implementing human-in-the-loop workflows.
interface BaseCheckpointSaver {
Collection<Checkpoint> list(RunnableConfig config);
Optional<Checkpoint> get(RunnableConfig config);
RunnableConfig put(RunnableConfig config, Checkpoint checkpoint) throws Exception;
Tag release(RunnableConfig config) throws Exception;
}
class Checkpoint {
String getId();
String getNodeId();
String getNextNodeId();
Map<String, Object> getState();
}Configure graph compilation and execution behavior with interrupts, thread management, and metadata handling.
class CompileConfig {
Optional<BaseCheckpointSaver> checkpointSaver();
Set<String> interruptsBefore();
Set<String> interruptsAfter();
boolean releaseThread();
static Builder builder();
}
class RunnableConfig {
Optional<String> threadId();
Optional<String> checkPointId();
Optional<String> nextNode();
StreamMode streamMode();
Optional<Object> metadata(String key);
}Ready-to-use components for common patterns like message handling and specialized state management.
class MessagesState<T> extends AgentState {
List<T> messages();
Optional<T> lastMessage();
Optional<T> lastMinus(int n);
static final Map<String, Channel<?>> SCHEMA;
}
class MessagesStateGraph<T> extends StateGraph<MessagesState<T>> {
MessagesStateGraph();
MessagesStateGraph(StateSerializer<MessagesState<T>> stateSerializer);
}// Core state type
abstract class AgentState {
static final Object MARK_FOR_REMOVAL;
static final Object MARK_FOR_RESET;
}
// Command for routing control
class Command {
String gotoNode();
Map<String, Object> update();
}
// Graph input types
interface GraphInput {}
class GraphArgs implements GraphInput {
Map<String, Object> value();
}
class GraphResume implements GraphInput {}
// Execution output
interface NodeOutput<State extends AgentState> {
String node();
State state();
}
// Stream modes
enum StreamMode {
VALUES, SNAPSHOTS
}
// Diagram generation
enum GraphRepresentation.Type {
PLANTUML, MERMAID
}
// Exception types
class GraphStateException extends RuntimeException {}
class GraphRunnerException extends RuntimeException {}