AI for Java Architects – Part 9

LangChain and LangGraph for Java Architects: Building Production AI Workflows

“LLMs provide intelligence. Frameworks provide engineering.”


Introduction

In the previous articles, we learned:

  • Part 1 — LLM Fundamentals
  • Part 2 — Embeddings and Vector Databases
  • Part 3 — RAG
  • Part 4 — Spring AI RAG Applications
  • Part 5 — Prompt Engineering
  • Part 6 — Spring AI Deep Dive
  • Part 7 — AI Agents
  • Part 8 — Multi-Agent Systems

At this point, you may ask:

Why do we need frameworks like LangChain and LangGraph?

After all, can’t we simply call the LLM directly?

Yes.

Just like you can write JDBC code directly instead of using Spring Data.

Frameworks provide:

  • Reusability
  • Orchestration
  • Memory
  • State management
  • Tool integration
  • Agent workflows

This article explains these frameworks from a Java architect’s perspective.


The Problem with Raw LLM Calls

Simple application:

String answer =
    chatClient.prompt()
        .user(question)
        .call()
        .content();

Works fine.

But real applications need:

  • Memory
  • RAG
  • Tool calls
  • Validation
  • Retries
  • Multiple steps
  • Agent workflows

Suddenly:

Question
 ↓
Search
 ↓
Analyze
 ↓
Call API
 ↓
Review
 ↓
Respond

This becomes difficult to manage manually.


What is LangChain?

LangChain is a framework for building AI applications.

Its purpose:

Connect LLMs with data, memory, tools, and workflows.

Think of it as:

JavaAI
Spring FrameworkLangChain
Spring DataRetrievers
Spring BatchChains
Spring State MachineLangGraph
Spring IntegrationTools

Core LangChain Components

ComponentPurpose
ModelsLLMs
PromptsInstructions
ChainsWorkflows
MemoryConversation history
RetrieversRAG
ToolsExternal actions
AgentsAutonomous decisions

The Chain Concept

The original idea behind LangChain:

Connect multiple AI operations.

Example:

Question
    ↓
Retrieve Documents
    ↓
Summarize
    ↓
Analyze Risks
    ↓
Generate Report

Example Chain

Step 1:

Retrieve architecture documents.

Step 2:

Summarize them.

Step 3:

Identify risks.

Step 4:

Generate recommendations.


Traditional Programming

step1();
step2();
step3();

AI Chains

LLM
 ↓
Retriever
 ↓
LLM
 ↓
Parser

Prompt Templates

LangChain introduced reusable prompts.

Example:

You are a cloud architect.

Analyze:
{architecture}

This enables:

  • Versioning
  • Reuse
  • Testing

Output Parsers

Models produce text.

Applications require:

  • DTOs
  • JSON
  • Objects

Example:

{
  "risk": "Database bottleneck"
}

Parser:

Risk risk;

Retrievers

Retrievers provide RAG.

Question
     ↓
Retriever
     ↓
Documents

The LLM receives:

  • User question
  • Retrieved context

Memory

Without memory:

User:
My project uses Kafka.

User:
Recommend improvements.

AI:
What messaging platform?

With memory:

AI:
Since your project uses Kafka...

Tool Calling

Tools extend AI capabilities.

Examples:

  • Databases
  • REST APIs
  • Jira
  • Email
  • Calendar

Example

User:

What is my ticket status?

Agent:

  • Calls Jira.
  • Retrieves tickets.
  • Summarizes.

The Evolution

Generation:

Question
     ↓
LLM

RAG:

Question
     ↓
Retriever
     ↓
LLM

Agent:

Question
     ↓
Planner
     ↓
Tools
     ↓
LLM

Why LangGraph?

Chains work well for linear workflows.

But agents need:

  • Decisions
  • Loops
  • Branches
  • State

Example:

Search
   ↓
Found?
   ↓
Yes → Answer

No → Search Again

This is where LangGraph enters.


What is LangGraph?

LangGraph is a graph-based agent framework.

Instead of:

step1();
step2();
step3();

You build:

Node
   ↓
Decision
   ↓
Next Node

Graph Example

START
   ↓
Retrieve
   ↓
Analyze
   ↓
Need More Data?
   ↓
YES → Retrieve Again

NO → Respond

Why Graphs?

Real-world workflows are rarely linear.

Examples:

  • Retry.
  • Approval.
  • Escalation.
  • Loops.

Agent State

State is one of LangGraph’s biggest ideas.

Example:

Question:
Review architecture.

Status:
Searching.

Results:
2 documents.

Risk:
High.

Every node can read and update state.


State Example

{
    "question": "...",
    "documents": [],
    "risks": [],
    "complete": false
}

Java Analogy

Think:

WorkflowContext

Shared across steps.


LangGraph Architecture

User
   ↓
State
   ↓
Node
   ↓
Decision
   ↓
Node
   ↓
END

Example Workflow

Architecture Review.

START
   ↓
Search Documents
   ↓
Analyze Architecture
   ↓
Analyze Security
   ↓
Generate Report
   ↓
END

Conditional Routing

Example:

Security Risk?

YES → Security Agent

NO → Skip

This makes workflows dynamic.


Loops

Example:

Search

No Results

Search Again

No Results

Ask User

Human Approval

Deploy?

YES → Deploy

NO → Stop

Very important for enterprise systems.


Multi-Agent Graph

Supervisor
    ↓
----------------
Architecture
Security
Cost
----------------
    ↓
Report

LangChain vs LangGraph

LangChainLangGraph
ChainsGraphs
LinearDynamic
Simple workflowsAgents
Prompt pipelinesStateful execution
RAGMulti-agent systems

Example: Incident Agent

Workflow:

Alert
   ↓
Logs Agent
   ↓
Metrics Agent
   ↓
Root Cause Agent
   ↓
Report

Example: Architecture Agent

Question
   ↓
Retrieve HLD
   ↓
Analyze
   ↓
Security Review
   ↓
Recommendations

Example: Deployment Agent

Code
   ↓
Review
   ↓
Security Check
   ↓
Approval
   ↓
Deploy

Why Java Developers Should Learn These Concepts

Even if using Spring AI:

The concepts matter.

Because Spring AI is adopting many ideas:

  • Memory
  • Tools
  • Advisors
  • Agents
  • Workflows

LangChain Concepts in Spring AI

LangChainSpring AI
PromptPromptTemplate
RetrieverVectorStore
MemoryChatMemory
AgentTool Calling
ChainAdvisors
StateConversation Context

Enterprise Architecture Example

Suppose your company builds:

Deadline Processing Agent

Steps:

  1. Retrieve holidays.
  2. Calculate working days.
  3. Validate deadlines.
  4. Notify systems.

This is effectively an agent graph.


Cost Considerations

Complex workflows mean:

  • Multiple LLM calls.
  • More tokens.
  • Increased latency.

Optimization becomes important.


Observability

Monitor:

  • Nodes executed.
  • Time spent.
  • Tokens consumed.
  • Failures.
  • Retries.

Error Handling

What if:

  • API fails?
  • Vector search fails?
  • LLM fails?

Graphs can:

  • Retry.
  • Escalate.
  • Ask humans.

Interview Questions

What is LangChain?

A framework for AI applications.


What is a chain?

A sequence of AI operations.


Why use LangGraph?

To build stateful agent workflows.


What is agent state?

Shared information across steps.


What are nodes?

Units of work in a graph.


Hands-On Exercise

Build:

Architecture Review Workflow

Nodes:

  • Search.
  • Analyze.
  • Security review.
  • Recommendations.

Capstone Project

Enterprise Architecture Copilot

Graph:

User
   ↓
Retriever
   ↓
Architecture Agent
   ↓
Security Agent
   ↓
Cost Agent
   ↓
Final Report

Key Takeaways

✔ LangChain introduced AI workflows.

✔ LangGraph enables stateful agents.

✔ Graphs support loops and decisions.

✔ State enables complex workflows.

✔ Multi-agent systems use graph architectures.

✔ Java developers already understand many of these concepts.


What’s Next?

Part 10 — Model Context Protocol (MCP) and Tool Integration

Topics:

  • What is MCP?
  • Why tool standards matter.
  • MCP servers.
  • Databases as tools.
  • APIs as tools.
  • Enterprise integrations.
  • Spring AI + MCP.
  • The future of AI interoperability.

Because the future of AI may not depend on one model, but on how models interact with the systems we already build.


“Frameworks do not make AI intelligent. They make intelligence manageable.”

Leave a Reply

Your email address will not be published. Required fields are marked *