<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jayasree M on Medium]]></title>
        <description><![CDATA[Stories by Jayasree M on Medium]]></description>
        <link>https://medium.com/@official.hardcodeconcepts?source=rss-343d1c58d74d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Jayasree M on Medium</title>
            <link>https://medium.com/@official.hardcodeconcepts?source=rss-343d1c58d74d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 12:14:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@official.hardcodeconcepts/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Nodes, Edges, States & Graph in LangGraph — Basics]]></title>
            <link>https://medium.com/@official.hardcodeconcepts/nodes-edges-states-graph-in-langgraph-basics-3bdc7e9954b6?source=rss-343d1c58d74d------2</link>
            <guid isPermaLink="false">https://medium.com/p/3bdc7e9954b6</guid>
            <category><![CDATA[ai-agent-tutorial]]></category>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[basics]]></category>
            <category><![CDATA[langgraph]]></category>
            <category><![CDATA[langchain]]></category>
            <dc:creator><![CDATA[Jayasree M]]></dc:creator>
            <pubDate>Wed, 26 Mar 2025 13:40:15 GMT</pubDate>
            <atom:updated>2025-03-26T13:40:15.666Z</atom:updated>
            <content:encoded><![CDATA[<h3>Nodes, Edges, States &amp; Graph in LangGraph — Basics</h3><figure><img alt="LangGraph Basics" src="https://cdn-images-1.medium.com/max/1024/0*vUQvrNo6WqSwz3co" /><figcaption>Image generated by an AI Agent, I’m working on.</figcaption></figure><p>I’ve been diving deep into AI agents lately, and one of my most frequently used frameworks is <strong>LangGraph</strong>. While many developers comment on its instability, frequent deprecations, and documentation issues, to all which I partially agree, but there is a ‘but’.</p><p>It’s tough to rely on Copilot or AI-assisted coding when working with LangGraph, ‘but’ I still believe the team will refine it over time.</p><p>Why stick with Langgraph? Simply put, I haven’t found another framework that offers this level of control. And honestly, I’m too busy to build one from scratch.</p><p>That said, I’ve decided to <strong>document my learnings</strong> here. For me, the best way to solidify knowledge is:</p><blockquote><strong><em>Learn once, you understand. Teach once, you never forget.</em></strong></blockquote><p>So, let’s cut the chatter and jump into the basics. 🚀</p><h3>Nodes in LangGraph</h3><p>Just like in any graph structure, <strong>nodes</strong> in Langgraph are <strong>entities</strong> that represent something and may be connected to other entities (or not).</p><h4>What is a Node in AI Agents?</h4><p>In simple terms, you can think of <strong>nodes as functions</strong>. Each node contains a <strong>code snippet</strong> that executes whenever the node is triggered.</p><p>Here’s a simple example:</p><pre>def node_next(input):<br>    # Get the successor  <br>    next_value = input[&#39;x&#39;] + 1  <br>    return {&#39;x&#39;: next_value}  <br><br># Sample input  <br>input_data = {&#39;x&#39;: 3}  <br><br># Calling the node  <br>result = node_next(input_data)  <br>print(result)  # Output: {&#39;x&#39;: 4}</pre><h4>How It Works</h4><ul><li>The <strong>node “Next”</strong> takes an integer as input (x).</li><li>It performs a simple <strong>operation</strong> (adds 1 to x).</li><li>It returns the <strong>new value</strong> (x + 1).</li></ul><p>So, every time this node is called, it <strong>performs some “action/magic”</strong> and gives you the next number!</p><h3>Edges in LangGraph 🚦</h3><p><strong>Edges</strong> are the <strong>connections</strong> between nodes — think of them as roads linking different destinations. In Langgraph, they serve the same purpose: <strong>guiding the flow from one node to another</strong>.</p><h4>The Need for Conditional Edges</h4><p>If all roads were straight, life would be simple. No traffic, no wrong turns, no GPS yelling at you to “make a U-turn when possible.” But that’s not how things work.</p><p>Similarly, an AI agent doesn’t just follow a single straight path — it needs to decide whether to turn left, right, or just give up and take a nap.</p><p>To handle this, <strong>LangGraph introduces Conditional Edges</strong> — which act like crossroads with a decision-making mechanism.</p><h4>Syntax for Edges &amp; Conditional Edges</h4><pre># Direct Edge<br>graph.add_edge(&quot;NodeA&quot;, &quot;NodeB&quot;)  </pre><pre># Conditioanl Edge<br>graph.add_conditional_edges(&quot;CurrentNode&quot;, condition_function) </pre><h4>How Conditional Edges Work</h4><p>Instead of blindly jumping to the next node, a <strong>conditional edge</strong> checks if the agent <em>deserves</em> to go there. <strong>In LangGraph, Conditional Edges are simple functions. They contain code snippets that check if you can go to a node or not. </strong>Basically, it’s an if-else statement wrapped in some AI magic.</p><p>Consider the below code snippet :</p><pre>def go_to_the_mall(wallet):<br>    if wallet[&#39;money&#39;] &gt; 1000:<br>        return Mall  # Congrats! You may enter the land of overpriced coffee. <br>    else:<br>        return Home  # You&#39;re broke. Go home. Or rethink your life choices. <br><br># Example input<br>wallet = {&#39;money&#39;: 500, &#39;makeup&#39;: &#39;yes&#39;, &#39;bf_photo&#39;: &#39;yes&#39;}<br>go_to_the_mall(wallet) # Output : Go Home</pre><h4>What’s Happening Here?</h4><ul><li>The function checks if you have enough <strong>money</strong>.</li><li>If you’re rolling with <strong>more than ₹1000</strong>, you get to <strong>go shopping</strong> (Mall node).</li><li>Otherwise, <strong>shut up and go home</strong> (Home node).</li></ul><p>So, <strong>edges in Langgraph aren’t just simple roads — they’re bouncers</strong> checking if you’re VIP or if you need to try again later. 🚧</p><h3>States in LangGraph 🎭</h3><p>Next up, <strong>States</strong>! What are they? 🤔</p><p>Think of <strong>states</strong> as the <strong>memory</strong> of an AI agent at a given moment. They store information about <strong>what has happened so far</strong> — like a save point in a video game or your browser tabs when you “accidentally” restart your laptop.</p><h4>Santa’s Toy Factory Analogy 🎅🏽</h4><p>Imagine Santa’s Toy Factory, where teddy bears are assembled on a production line. At every stage, a teddy bear has a <strong>state</strong>:</p><ul><li><strong>Step 1:</strong> It’s just a pile of fluff</li><li><strong>Step 2:</strong> Stuffing gets added</li><li><strong>Step 3:</strong> Cute button eyes are attached</li><li><strong>Step 4:</strong> A final hug test before it’s ready for gifting! 🎁</li></ul><p>At each stage, the teddy bear <strong>remembers</strong> what’s been done to it — because <strong>Santa’s elves don’t want to add eyes before stuffing it (creepy, right?)</strong>. Similarly, an AI agent tracks its <strong>state</strong> to know what has already been processed and what comes next.</p><h4>State in LangGraph</h4><p>In LangGraph, the <strong>state</strong> is the central mechanism for communication between nodes. It acts as a structured message that encapsulates the current snapshot of the system at any given moment.</p><p>The LangGraph doc says:</p><blockquote><em>“A state is a shared data structure that represents the current snapshot of your application.”</em></blockquote><p>States are passed along edges between nodes, carrying the output of one node to the next as input. This makes the state the backbone of any message-passing graph.</p><p><strong>States in LangGraph are akin to the evolving memory of an AI agent</strong>, enabling it to track progress, make informed decisions, and execute complex workflows effectively.</p><h4><strong>State Schema: The Blueprint</strong></h4><p>A <strong>schema</strong> defines the structure of the state, ensuring that the data being passed between nodes follows a consistent format. In Python terms, a schema can be any data type. However, it is typically represented using TypedDict or a Pydantic BaseModel. The schema helps nodes interpret the state correctly, ensuring smooth execution across the graph.</p><p>For example, consider defining a simple state schema using TypedDict:</p><pre>from typing_extensions import TypedDict<br><br>class ToyState(TypedDict):<br>    stuffing_added: bool<br>    eyes_attached: bool<br>    hug_test_passed: bool</pre><ul><li>stuffing_added: Indicates whether the stuffing has been added.</li><li>eyes_attached: Indicates whether the eyes have been attached.</li><li>hug_test_passed: Indicates whether the hug test has been passed.</li></ul><p>Each node in the LangGraph can update these state attributes as the teddy bear progresses through the assembly line.</p><h4>Are these necessary ?</h4><p>Without states, your AI agent would be as forgetful as Dory from <em>Finding Nemo</em>. It wouldn’t remember past actions or decisions, making it <strong>useless for anything requiring continuity</strong> (like conversations, workflows, or, well… anything intelligent).</p><h3>Graphs in LangGraph 📈</h3><p>A <strong>Graph</strong> in LangGraph is a beautiful <strong>combination of Nodes, Edges, and States</strong>.</p><p>When working with LangGraph, the most commonly used type of graph is the <strong>StateGraph</strong>. This structure has two special nodes:</p><ol><li><strong>START 🚦</strong> — The entry point of the graph. (Because everything needs a beginning, even bad decisions.)</li><li><strong>END 🏁</strong> — The final destination, aka where your AI agent takes a nap.</li></ol><h4>Let’s Illustrate with a Relatable Example</h4><p>Imagine this: You <strong>really</strong> want to buy some makeup. But as always, your wallet might have different opinions. 💸 So, we build a <strong>decision-making AI agent</strong> that helps you navigate life’s toughest question:</p><blockquote><em>“Do I go shopping or do I go hustle?”</em></blockquote><p>Here’s how our <strong>StateGraph</strong> makes the decision:</p><pre>from typing_extensions import TypedDict<br>from IPython.display import Image<br><br>class WalletState(TypedDict):<br>    money : float<br>    make_up : bool | None<br>    bf_photo : bool | None<br>def CarNode(state: WalletState) -&gt; WalletState:<br>    print(&quot;🚗 Inside the Car&quot;)<br>    return state<br>def MallNode(state: WalletState) -&gt; WalletState:<br>    print(&quot;🛍️ Inside the Mall&quot;)<br>    print(&quot;Buying Makeup... 💄&quot;)<br>    <br>    price_on_sale = 999  # Because discounts are a lie<br>    return {<br>        &quot;money&quot;: state[&quot;money&quot;] - price_on_sale,<br>        &quot;make_up&quot;: True<br>    }<br>def HomeNode(state: WalletState) -&gt; WalletState:<br>    print(&quot;🏠 Ditched plans and went Home&quot;)<br>    return state<br>def WorkNode(state: WalletState) -&gt; WalletState:<br>    print(&quot;💼 Went to the Part-Time Job. Need that cash for makeup!&quot;)<br>    <br>    print(&quot;💰 Collecting Tips...&quot;)<br>    tips = 500  <br>    return {<br>        &quot;money&quot;: state[&quot;money&quot;] + tips<br>    }<br>def where_to_go(state: WalletState):<br>    if state[&quot;money&quot;] &gt;= 1000:<br>        print(&quot;💰 Got Money! Yay!&quot;)<br>        return &quot;Mall&quot;<br>    <br>    else:<br>        print(&quot;😩 Ugh! Got no money!&quot;)<br>        if state[&quot;make_up&quot;] == False:<br>            print(&quot;😱 Omg, no makeup too.&quot;)<br>            return &quot;Work&quot;<br>        else:<br>            print(&quot;🙄 Guess I&#39;ll just shut up and go Home.&quot;)<br>            return &quot;Home&quot;<br>from langgraph.graph import StateGraph, START, END<br># Define the Graph<br>builder = StateGraph(WalletState)<br># Adding the Nodes<br>builder.add_node(&quot;Car&quot;, CarNode)<br>builder.add_node(&quot;Mall&quot;, MallNode)<br>builder.add_node(&quot;Home&quot;, HomeNode)<br>builder.add_node(&quot;Work&quot;, WorkNode)<br># Adding the Edges<br>builder.add_edge(START, &quot;Car&quot;)<br>builder.add_conditional_edges(&quot;Car&quot;, where_to_go)<br>builder.add_edge(&quot;Work&quot;, &quot;Car&quot;)  # Work feeds back into the cycle<br>builder.add_edge(&quot;Mall&quot;, END)<br>builder.add_edge(&quot;Home&quot;, END)<br># Compile the Graph<br>graph = builder.compile()</pre><pre># Let&#39;s run the agent, with a sample input<br><br>wallet = { <br>    &quot;money&quot;: 600,  # Ouch, not enough for makeup<br>    &quot;make_up&quot;: False,<br>    &quot;bf_photo&quot;: True  # Because priorities<br>}<br>graph.invoke(wallet)</pre><h4>What Happens?</h4><pre>🚗 Inside the Car<br>😩 Ugh! Got no money!<br>😱 Omg, no makeup too.<br>💼 Went to the Part-Time Job. Need that cash for makeup!<br>💰 Collecting Tips...<br>🚗 Inside the Car<br>💰 Got Money! Yay!<br>🛍️ Inside the Mall<br>Buying Makeup... 💄<br>{&#39;money&#39;: 101, &#39;make_up&#39;: True, &#39;bf_photo&#39;: True}</pre><h4>Visualizing the Graph</h4><pre># Display the Graph Structure<br>display(Image(graph.get_graph().draw_mermaid_png()))</pre><p>When the above script runs, it will generate a <strong>Mermaid.js-based graph</strong> that visually represents the <strong>AI agent’s decision flow</strong>.</p><figure><img alt="Mermaid Image of the Agent Graph" src="https://cdn-images-1.medium.com/max/350/1*CxqGfuZ_Sx9cY22kQekNxQ.png" /><figcaption>Mermaid Representation of the Agent Graph</figcaption></figure><h4>So…</h4><p>Graphs in LangGraph are <strong>powerful tools for AI-driven workflows</strong>, allowing complex decision-making. The <strong>StateGraph</strong> ensures that the AI remembers past decisions and takes appropriate actions.</p><p>And most importantly, <strong>LangGraph lets you build practical, decision-making AI agents</strong> — so you don’t have to suffer through <strong>“should I spend money or save money?”</strong> dilemmas alone.</p><h3>Wrapping It Up</h3><p>And that’s a wrap on the <strong>basics of LangGraph</strong>! 🎉 We covered <strong>Nodes, Edges, States, and Graphs</strong>, all while keeping things fun (because debugging AI agents is stressful enough, right?). LangGraph gives you <strong>fine control over agent workflows</strong>, and while it has its quirks, it’s one of the best frameworks out there for structured AI development.</p><p>If you’ve made it this far, congrats! 🏆 You’re now equipped to start <strong>building intelligent, decision-making AI agents </strong>using LangGraph. Go forth and create something awesome! 🚀</p><p>And remember — if your AI ever refuses to cooperate, <strong>just blame the edges.</strong> 😆 . <em>(Okay, I will stop it now)</em></p><h3>Author’s Note ✍️</h3><p>Hey there, fellow AI explorer! 🚀 If you found this guide helpful (or at least mildly entertaining), <strong>drop a like, clap, or a standing ovation</strong> — whatever Medium allows these days. 😆</p><p>Got questions, feedback, or just want to geek out about tech stuff? <strong>Drop a comment!</strong> Let’s build cool AI workflows together. 💡✨</p><p>Until next time — happy coding, and may your AI always choose the right path (unlike my online shopping habits). 🛍️😅</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3bdc7e9954b6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>