LangGraph for Absolute Beginners: Build Your First AI Workflow (BMI Calculator)

langgraph

If you're new to AI development and have just started with LangChain, you might be wondering: "How do I build applications that have multiple steps and remember information?"

That’s exactly where LangGraph comes in.

In this beginner-friendly tutorial, we’ll build a simple sequential workflow — a BMI Calculator — using LangGraph. It’s one of the easiest and best ways to understand how LangGraph works before moving to more advanced AI agents.

What is LangGraph?

LangGraph is a powerful library built on top of LangChain that lets you create stateful, graph-based workflows.

Think of it like this:

  • In normal programming, you write code line by line.
  • In LangGraph, you build a graph (like a flowchart) where each box (node) does one job, and arrows (edges) show the order of execution.

It’s perfect for building reliable, multi-step AI applications such as:

Why Start with a BMI Calculator?

Because it’s simple and teaches the core concepts without needing any AI model (LLM). You’ll learn:

  • How to define State
  • How to create Nodes
  • How to connect them using Edges
  • How to run the workflow

Let’s get started!

Step 1: Install Required Packages

Run these commands in your Google Colab or Jupyter Notebook:

!pip install langgraph langchain


Step 2: Define the State

The State is like a shared notebook that travels through your workflow. All nodes can read from it and update it.

from typing import TypedDict class BMI(TypedDict): weight_kg: float height_m: float bmi: float # This will be calculated


Step 3: Create a Node (Function)

A node is simply a Python function that takes the current state and returns an updated state.

def calculate_bmi(state: BMI) -> BMI: # Calculate BMI using formula: weight / (height²) state['bmi'] = state['weight_kg'] / (state['height_m'] ** 2) return state


Step 4: Build the Graph

Now let’s create the workflow using LangGraph:

from langgraph.graph import StateGraph, START, END # Create a new graph with our BMI state graph = StateGraph(BMI) # Add our calculation node graph.add_node("Calculate_bmi", calculate_bmi) # Define the flow: Start → Calculate_bmi → End graph.add_edge(START, "Calculate_bmi") graph.add_edge("Calculate_bmi", END) # Compile the graph into a runnable workflow workflow = graph.compile()


Step 5: Run Your First Workflow!

# Execute the workflow with input values
result = workflow.invoke({
    'weight_kg': 70,
    'height_m': 1.75
})

print(result)

Output:

{ 'weight_kg': 70, 'height_m': 1.75, 'bmi': 22.857142857142858 }

Congratulations! You just built and ran your first LangGraph workflow.


LangGraph can automatically generate a nice diagram of your workflow using Mermaid:

from IPython.display import Markdown mermaid_code = workflow.get_graph().draw_mermaid() Markdown(f"""```mermaid {mermaid_code} ```""")

Output:

--- config: flowchart: curve: linear --- graph TD; __start__([<p>__start__</p>]):::first Calculate_bmi(Calculate_bmi) __end__([<p>__end__</p>]):::last __start__ --> Calculate_bmi; Calculate_bmi --> __end__; classDef default fill:#f2f0ff,line-height:1.2 classDef first fill-opacity:0 classDef last fill:#bfb6fc

This is a clean flowchart:



You can also save it as a file:

mermaid_code = workflow.get_graph().draw_mermaid() with open('bmi_graph.mmd', 'w') as f: f.write(mermaid_code) print("Diagram saved as bmi_graph.mmd")



What Did We Learn?

  • State: Holds all the data (weight, height, bmi)
  • Node: A function that performs one task (calculate_bmi)
  • Edges: Define the sequence (START → node → END)
  • Graph: The complete workflow structure
  • Compile & Invoke: Turn the graph into something you can run

Even though this example is very simple, this is the exact same pattern used in complex LangGraph applications.

You’ve just taken your first big step into building structured AI workflows with LangGraph. This simple BMI calculator might look basic, but you now understand the foundation that powers modern AI agents and multi-step applications. LangGraph makes AI development more organized, reliable, and visual — exactly what beginners need.

bicky

CSE student writing practical guides on AI agents, LLM security and bug bounty. Everything is tested in labs first.

Comments