Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: pub@towardsai.net
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab VeloxTrend Ultrarix Capital Partners Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Free: 6-day Agentic AI Engineering Email Guide.
Learnings from Towards AI's hands-on work with real clients.
LLM & AI Agent Applications with LangChain and LangGraph — Part 23: Introduction to LangGraph
Latest   Machine Learning

LLM & AI Agent Applications with LangChain and LangGraph — Part 23: Introduction to LangGraph

Last Updated on January 5, 2026 by Editorial Team

Author(s): Michalzarnecki

Originally published on Towards AI.

LLM & AI Agent Applications with LangChain and LangGraph — Part 23: Introduction to LangGraph

Hi! Welcome to the next article of the LLM-based application development series. In this part we will jump into LangGraph and build a simple graph showed below.

So far we’ve learned about chains in LangChain. Chains work great in simple scenarios where we have a sequential flow of data:

prompt → model → parser → result

But what happens when we want something more complex?

A chain is a straight line — we go from point A to B and then to C.
A graph is a network of connections. In a graph we can add:

  • conditions — deciding which path should be chosen,
  • loops — repeating steps until we reach a correct result,
  • branches — different routes depending on state or input data.

It’s a bit like the difference between a single subway line and the full public transport map of a big city.

When is a chain no longer enough?

Chains are perfect for simple linear flows, but they are limited to a fixed sequence of steps. In real applications we often need additional scenarios, such as:

  • retry — repeated attempts if the model returns an invalid output,
  • fallback — an alternative path if the primary model fails,
  • multi-agent — multiple agents that communicate depending on the current state,
  • conditional control — different paths driven by application logic.

This is exactly where LangGraph starts to shine.

Core concepts in LangGraph

To build these kinds of flows, LangGraph uses a few fundamental concepts:

  • Graph — the overall workflow structure, basically your “plan of action”,
  • Node — a single step in the graph, usually a function that processes state,
  • State — the data that moves between nodes,
  • Edge — a connection between nodes that defines order and transitions,
  • Conditional edge — a special edge that chooses the next path based on state.

Let’s build a simple graph

We’ll start with something minimal:

  • Node A adds 1 to the value x.
  • Node B multiplies the result by 2.
  • Node C returns the final output.

This still looks like a chain — but the moment we have LangGraph in place, we can easily add conditions, loops, and branching without rewriting everything from scratch.

1. Install libraries and dependencies

!pip install -q langgraph langchain langchain-openai python-dotenv
!apt install libgraphviz-dev
!pip install pygraphviz

2. Define State

class State(TypedDict):
x: int

3. Define nodes

def add_one(state: State) -> State:
return {"x": state["x"] + 1}

def multiply_by_two(state: State) -> State:
return {"x": state["x"] * 2}

def finish(state: State) -> State:
print(f"Result {state['x']}")
return state

4. Create graph

graph = StateGraph(State)

# define nodes
graph.add_node("A", add_one)
graph.add_node("B", multiply_by_two)
graph.add_node("C", finish)

# define edges
graph.set_entry_point("A")
graph.add_edge("A", "B")
graph.add_edge("B", "C")
graph.add_edge("C", END)

5. Compile and run the graph

app = graph.compile()

result = app.invoke({"x": 1})
print(result)
Result 4
{'x': 4}

Visualizing the workflow

What’s also nice is that LangGraph can draw your graph as a diagram using draw_png().

from IPython.display import Image, display

png_bytes = app.get_graph().draw_png()
display(Image(png_bytes))

This lets you see the structure of your application like a process map — which is incredibly helpful once graphs grow beyond a few nodes.

Summary

Chains are great for simple linear scenarios. But when you need more flexibility, you reach for graphs.

LangGraph lets you describe processes as graphs with nodes, edges, state, and conditional logic. In the next parts, I’ll show how to combine graphs with language models, tools, and loops.

That’s all int this chapter dedicated to LangGraph introduction. In the next chapter we will combine graph with LLM and extend flow with loop and conditional edge.

see next chapter

see previous chapter

see the full code from this article in the GitHub repository

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.

Published via Towards AI


Towards AI Academy

We Build Enterprise-Grade AI. We'll Teach You to Master It Too.

15 engineers. 100,000+ students. Towards AI Academy teaches what actually survives production.

Start free — no commitment:

6-Day Agentic AI Engineering Email Guide — one practical lesson per day

Agents Architecture Cheatsheet — 3 years of architecture decisions in 6 pages

Our courses:

AI Engineering Certification — 90+ lessons from project selection to deployed product. The most comprehensive practical LLM course out there.

Agent Engineering Course — Hands on with production agent architectures, memory, routing, and eval frameworks — built from real enterprise engagements.

AI for Work — Understand, evaluate, and apply AI for complex work tasks.

Note: Article content contains the views of the contributing authors and not Towards AI.