<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Agents on Vivek's Field Notes</title><link>https://heyyviv.github.io/tags/agents/</link><description>Recent content in Agents on Vivek's Field Notes</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Thu, 26 Nov 2026 22:20:56 +0530</lastBuildDate><atom:link href="https://heyyviv.github.io/tags/agents/index.xml" rel="self" type="application/rss+xml"/><item><title>Lang Graph</title><link>https://heyyviv.github.io/blog/lang-graph/</link><pubDate>Thu, 26 Nov 2026 22:20:56 +0530</pubDate><guid>https://heyyviv.github.io/blog/lang-graph/</guid><description>&lt;p>Short notes, added as I learn.&lt;/p>
&lt;h2 id="what-is-langgraph">What is LangGraph?&lt;/h2>
&lt;p>&lt;strong>LangGraph&lt;/strong> is a framework (from the LangChain team) for building &lt;strong>stateful LLM apps as graphs&lt;/strong>: workflows and agents that can loop, branch, pause and resume.&lt;/p>
&lt;p>You describe the app as:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>State:&lt;/strong> the shared data of one run (a &lt;code>TypedDict&lt;/code>).&lt;/li>
&lt;li>&lt;strong>Nodes:&lt;/strong> Python functions, each doing one step (call the LLM, run a tool, check output).&lt;/li>
&lt;li>&lt;strong>Edges:&lt;/strong> what runs next, either fixed or decided at runtime.&lt;/li>
&lt;/ul>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1&lt;/span>&lt;span>graph &lt;span style="color:#666">=&lt;/span> StateGraph(State)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2&lt;/span>&lt;span>graph&lt;span style="color:#666">.&lt;/span>add_node(&lt;span style="color:#4070a0">&amp;#34;generate&amp;#34;&lt;/span>, generate)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3&lt;/span>&lt;span>graph&lt;span style="color:#666">.&lt;/span>add_node(&lt;span style="color:#4070a0">&amp;#34;check&amp;#34;&lt;/span>, check)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4&lt;/span>&lt;span>graph&lt;span style="color:#666">.&lt;/span>add_edge(START, &lt;span style="color:#4070a0">&amp;#34;generate&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5&lt;/span>&lt;span>graph&lt;span style="color:#666">.&lt;/span>add_edge(&lt;span style="color:#4070a0">&amp;#34;generate&amp;#34;&lt;/span>, &lt;span style="color:#4070a0">&amp;#34;check&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6&lt;/span>&lt;span>graph&lt;span style="color:#666">.&lt;/span>add_conditional_edges(&lt;span style="color:#4070a0">&amp;#34;check&amp;#34;&lt;/span>, &lt;span style="color:#007020;font-weight:bold">lambda&lt;/span> s: &lt;span style="color:#4070a0">&amp;#34;generate&amp;#34;&lt;/span> &lt;span style="color:#007020;font-weight:bold">if&lt;/span> s[&lt;span style="color:#4070a0">&amp;#34;bad&amp;#34;&lt;/span>] &lt;span style="color:#007020;font-weight:bold">else&lt;/span> END)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7&lt;/span>&lt;span>app &lt;span style="color:#666">=&lt;/span> graph&lt;span style="color:#666">.&lt;/span>compile() &lt;span style="color:#60a0b0;font-style:italic"># turns it into a runnable app&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8&lt;/span>&lt;span>app&lt;span style="color:#666">.&lt;/span>invoke({&lt;span style="color:#4070a0">&amp;#34;question&amp;#34;&lt;/span>: &lt;span style="color:#4070a0">&amp;#34;…&amp;#34;&lt;/span>})
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The graph that code builds:&lt;/p>
&lt;pre tabindex="0">&lt;code class="language-mermaid" data-lang="mermaid">flowchart LR
 S([START]) --&amp;gt; G[generate]
 G --&amp;gt; C{check}
 C --&amp;gt;|bad| G
 C --&amp;gt;|good| E([END])
&lt;/code>&lt;/pre>&lt;p>Why it exists: plain LangChain chains are a one-way pipe. Real agents need:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Need&lt;/th>
&lt;th>LangGraph gives&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Retry / loop until good&lt;/td>
&lt;td>Cycles in the graph&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Choose the next step at runtime&lt;/td>
&lt;td>Conditional edges&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Shared memory between steps&lt;/td>
&lt;td>State + reducers&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Remember across calls, survive crashes&lt;/td>
&lt;td>Checkpointers&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Human approval&lt;/td>
&lt;td>&lt;code>interrupt()&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Many agents&lt;/td>
&lt;td>Subgraphs&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Live output&lt;/td>
&lt;td>Streaming&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>It is &lt;strong>low-level&lt;/strong>: it doesn&amp;rsquo;t hide prompts or decide your agent&amp;rsquo;s logic. It is the runtime that runs your graph reliably. It works with or without LangChain.&lt;/p>
&lt;p>One line: &lt;strong>LangChain = building blocks (LLMs, prompts, tools, retrievers). LangGraph = the runtime that wires them into a stateful, looping app.&lt;/strong>&lt;/p>
&lt;h2 id="workflow-vs-agent">Workflow vs agent&lt;/h2>
&lt;p>&lt;strong>Workflow:&lt;/strong> &lt;em>you&lt;/em> write the path. The LLM fills in steps, but the order is fixed in code.
&lt;code>input → summarize → translate → output&lt;/code>. Predictable, cheap, easy to test.&lt;/p>
&lt;p>&lt;strong>Agent:&lt;/strong> the &lt;em>LLM&lt;/em> chooses the path. It looks at the state, picks the next step or tool, and loops until it decides it&amp;rsquo;s done.
&lt;code>input → LLM → (tool? → LLM → tool? → ...) → output&lt;/code>. Flexible, but slower, costlier, and can loop.&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>&lt;/th>
&lt;th>Workflow&lt;/th>
&lt;th>Agent&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Who picks the next step&lt;/td>
&lt;td>Your code&lt;/td>
&lt;td>The LLM&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Path&lt;/td>
&lt;td>Fixed&lt;/td>
&lt;td>Decided at runtime&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Good for&lt;/td>
&lt;td>Known steps&lt;/td>
&lt;td>Open-ended tasks&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Risk&lt;/td>
&lt;td>Rigid&lt;/td>
&lt;td>Loops, cost, surprises&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>Rule: use a workflow when you can draw the steps up front. Use an agent only when you can&amp;rsquo;t.&lt;/p>
&lt;pre tabindex="0">&lt;code class="language-mermaid" data-lang="mermaid">flowchart LR
 subgraph W[Workflow: your code picks the path]
 direction LR
 i1([input]) --&amp;gt; s1[summarize] --&amp;gt; s2[translate] --&amp;gt; o1([output])
 end
 subgraph A[Agent: the LLM picks the path]
 direction LR
 i2([input]) --&amp;gt; L{LLM decides}
 L --&amp;gt;|call tool| T[tool]
 T --&amp;gt; L
 L --&amp;gt;|done| o2([output])
 end
&lt;/code>&lt;/pre>&lt;p>In LangGraph both are the same thing, a graph. A workflow uses fixed edges (&lt;code>add_edge&lt;/code>). An agent uses a conditional edge where the LLM&amp;rsquo;s output picks the next node (&lt;code>add_conditional_edges&lt;/code>).&lt;/p>
&lt;h2 id="llm-workflows-the-5-common-patterns">LLM workflows: the 5 common patterns&lt;/h2>
&lt;p>An &lt;strong>LLM workflow&lt;/strong> is a fixed series of steps where one or more steps call an LLM. Most real apps are one of these 5 patterns (or a mix):&lt;/p>
&lt;p>&lt;strong>1. Prompt chaining:&lt;/strong> the output of one LLM call is the input to the next.&lt;/p>
&lt;pre tabindex="0">&lt;code class="language-mermaid" data-lang="mermaid">flowchart LR
 T([topic]) --&amp;gt; O[LLM: outline] --&amp;gt; W[LLM: write blog] --&amp;gt; P[LLM: proofread] --&amp;gt; B([blog])
&lt;/code>&lt;/pre>&lt;p>Use when the task splits cleanly into steps. You can add a check between steps.&lt;/p>
&lt;p>&lt;strong>2. Routing:&lt;/strong> classify the input first, then send it to the right specialist.&lt;/p>
&lt;pre tabindex="0">&lt;code class="language-mermaid" data-lang="mermaid">flowchart LR
 Q([query]) --&amp;gt; C{LLM: classify}
 C --&amp;gt;|refund| R[refund handler]
 C --&amp;gt;|tech| T[tech-support handler]
 C --&amp;gt;|general| G[general handler]
&lt;/code>&lt;/pre>&lt;p>Use when inputs fall into clear types. In LangGraph: a conditional edge.&lt;/p>
&lt;p>&lt;strong>3. Parallelization:&lt;/strong> run independent LLM calls at the same time, then combine them.&lt;/p>
&lt;pre tabindex="0">&lt;code class="language-mermaid" data-lang="mermaid">flowchart LR
 E([essay]) --&amp;gt; G[LLM: grammar score]
 E --&amp;gt; C[LLM: clarity score]
 E --&amp;gt; D[LLM: depth score]
 G --&amp;gt; M[combine]
 C --&amp;gt; M
 D --&amp;gt; M
 M --&amp;gt; F([final score])
&lt;/code>&lt;/pre>&lt;p>Use for speed, or for several views of the same input (voting). In LangGraph: fan-out edges + a reducer to merge the results.&lt;/p>
&lt;p>&lt;strong>4. Orchestrator–worker:&lt;/strong> one LLM plans the subtasks at runtime, workers do them, and the results are combined.&lt;/p>
&lt;pre tabindex="0">&lt;code class="language-mermaid" data-lang="mermaid">flowchart LR
 T([task]) --&amp;gt; O[orchestrator LLM&amp;lt;br/&amp;gt;splits into N subtasks]
 O -.-&amp;gt;|Send| W1[worker 1]
 O -.-&amp;gt;|Send| W2[worker 2]
 O -.-&amp;gt;|Send| WN[worker N]
 W1 --&amp;gt; S[synthesizer]
 W2 --&amp;gt; S
 WN --&amp;gt; S
 S --&amp;gt; R([result])
&lt;/code>&lt;/pre>&lt;p>Unlike parallelization, the subtasks are &lt;strong>not known in advance&lt;/strong> (e.g. &amp;ldquo;edit every file that needs a change&amp;rdquo;). In LangGraph: the &lt;code>Send&lt;/code> API to spawn workers.&lt;/p>
&lt;p>&lt;strong>5. Evaluator–optimizer:&lt;/strong> one LLM generates, another critiques, and it loops until it&amp;rsquo;s good enough.&lt;/p>
&lt;pre tabindex="0">&lt;code class="language-mermaid" data-lang="mermaid">flowchart LR
 S([START]) --&amp;gt; G[LLM: generate]
 G --&amp;gt; E{LLM: evaluate}
 E --&amp;gt;|not good: feedback| G
 E --&amp;gt;|good, or tries &amp;gt;= 3| X([END])
&lt;/code>&lt;/pre>&lt;p>Use when there are clear criteria and iteration helps (writing, translation, SQL fixing). In LangGraph: a loop with a max-iterations guard.&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Pattern&lt;/th>
&lt;th>Shape&lt;/th>
&lt;th>LangGraph feature&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Prompt chaining&lt;/td>
&lt;td>Straight line&lt;/td>
&lt;td>&lt;code>add_edge&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Routing&lt;/td>
&lt;td>One-of-N branch&lt;/td>
&lt;td>&lt;code>add_conditional_edges&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Parallelization&lt;/td>
&lt;td>Fan-out / fan-in&lt;/td>
&lt;td>Parallel edges + reducer&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Orchestrator–worker&lt;/td>
&lt;td>Dynamic fan-out&lt;/td>
&lt;td>&lt;code>Send&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Evaluator–optimizer&lt;/td>
&lt;td>Loop&lt;/td>
&lt;td>Conditional edge back + limit&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>When even these aren&amp;rsquo;t flexible enough (the steps can&amp;rsquo;t be drawn up front), you use an &lt;strong>agent&lt;/strong>: an LLM calling tools in a loop and deciding each step.&lt;/p>
&lt;p>These patterns are from Anthropic&amp;rsquo;s &amp;ldquo;Building effective agents&amp;rdquo; post.&lt;/p>
&lt;h2 id="the-5-patterns-in-langgraph-code">The 5 patterns in LangGraph code&lt;/h2>
&lt;p>&lt;code>llm&lt;/code> is any chat model. The code shows only the graph wiring, so each snippet stays short.&lt;/p>
&lt;h3 id="1-prompt-chaining">1. Prompt chaining&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">class&lt;/span> &lt;span style="color:#0e84b5;font-weight:bold">S&lt;/span>(TypedDict):
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2&lt;/span>&lt;span> topic: &lt;span style="color:#007020">str&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3&lt;/span>&lt;span> outline: &lt;span style="color:#007020">str&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4&lt;/span>&lt;span> blog: &lt;span style="color:#007020">str&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">def&lt;/span> &lt;span style="color:#06287e">make_outline&lt;/span>(s): &lt;span style="color:#007020;font-weight:bold">return&lt;/span> {&lt;span style="color:#4070a0">&amp;#34;outline&amp;#34;&lt;/span>: llm&lt;span style="color:#666">.&lt;/span>invoke(&lt;span style="color:#4070a0">f&lt;/span>&lt;span style="color:#4070a0">&amp;#34;Outline for: &lt;/span>&lt;span style="color:#70a0d0">{&lt;/span>s[&lt;span style="color:#4070a0">&amp;#39;topic&amp;#39;&lt;/span>]&lt;span style="color:#70a0d0">}&lt;/span>&lt;span style="color:#4070a0">&amp;#34;&lt;/span>)&lt;span style="color:#666">.&lt;/span>content}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">def&lt;/span> &lt;span style="color:#06287e">write_blog&lt;/span>(s): &lt;span style="color:#007020;font-weight:bold">return&lt;/span> {&lt;span style="color:#4070a0">&amp;#34;blog&amp;#34;&lt;/span>: llm&lt;span style="color:#666">.&lt;/span>invoke(&lt;span style="color:#4070a0">f&lt;/span>&lt;span style="color:#4070a0">&amp;#34;Write a blog from:&lt;/span>&lt;span style="color:#4070a0;font-weight:bold">\n&lt;/span>&lt;span style="color:#70a0d0">{&lt;/span>s[&lt;span style="color:#4070a0">&amp;#39;outline&amp;#39;&lt;/span>]&lt;span style="color:#70a0d0">}&lt;/span>&lt;span style="color:#4070a0">&amp;#34;&lt;/span>)&lt;span style="color:#666">.&lt;/span>content}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9&lt;/span>&lt;span>g &lt;span style="color:#666">=&lt;/span> StateGraph(S)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10&lt;/span>&lt;span>g&lt;span style="color:#666">.&lt;/span>add_node(&lt;span style="color:#4070a0">&amp;#34;outline&amp;#34;&lt;/span>, make_outline)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11&lt;/span>&lt;span>g&lt;span style="color:#666">.&lt;/span>add_node(&lt;span style="color:#4070a0">&amp;#34;write&amp;#34;&lt;/span>, write_blog)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12&lt;/span>&lt;span>g&lt;span style="color:#666">.&lt;/span>add_edge(START, &lt;span style="color:#4070a0">&amp;#34;outline&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13&lt;/span>&lt;span>g&lt;span style="color:#666">.&lt;/span>add_edge(&lt;span style="color:#4070a0">&amp;#34;outline&amp;#34;&lt;/span>, &lt;span style="color:#4070a0">&amp;#34;write&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14&lt;/span>&lt;span>g&lt;span style="color:#666">.&lt;/span>add_edge(&lt;span style="color:#4070a0">&amp;#34;write&amp;#34;&lt;/span>, END)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>&lt;strong>Gate:&lt;/strong> add a check between steps (e.g. &amp;ldquo;is the outline at least 3 points?&amp;rdquo;) with a conditional edge that stops early.&lt;/li>
&lt;li>&lt;strong>Trade-off:&lt;/strong> each step is simple and accurate, but latency adds up (N calls in series).&lt;/li>
&lt;/ul>
&lt;h3 id="2-routing">2. Routing&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">def&lt;/span> &lt;span style="color:#06287e">classify&lt;/span>(s):
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2&lt;/span>&lt;span> &lt;span style="color:#007020;font-weight:bold">return&lt;/span> {&lt;span style="color:#4070a0">&amp;#34;kind&amp;#34;&lt;/span>: llm&lt;span style="color:#666">.&lt;/span>invoke(&lt;span style="color:#4070a0">f&lt;/span>&lt;span style="color:#4070a0">&amp;#34;Answer refund, tech or general: &lt;/span>&lt;span style="color:#70a0d0">{&lt;/span>s[&lt;span style="color:#4070a0">&amp;#39;q&amp;#39;&lt;/span>]&lt;span style="color:#70a0d0">}&lt;/span>&lt;span style="color:#4070a0">&amp;#34;&lt;/span>)&lt;span style="color:#666">.&lt;/span>content&lt;span style="color:#666">.&lt;/span>strip()}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4&lt;/span>&lt;span>g&lt;span style="color:#666">.&lt;/span>add_node(&lt;span style="color:#4070a0">&amp;#34;classify&amp;#34;&lt;/span>, classify)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5&lt;/span>&lt;span>g&lt;span style="color:#666">.&lt;/span>add_conditional_edges(&lt;span style="color:#4070a0">&amp;#34;classify&amp;#34;&lt;/span>, &lt;span style="color:#007020;font-weight:bold">lambda&lt;/span> s: s[&lt;span style="color:#4070a0">&amp;#34;kind&amp;#34;&lt;/span>],
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6&lt;/span>&lt;span> {&lt;span style="color:#4070a0">&amp;#34;refund&amp;#34;&lt;/span>: &lt;span style="color:#4070a0">&amp;#34;refund&amp;#34;&lt;/span>, &lt;span style="color:#4070a0">&amp;#34;tech&amp;#34;&lt;/span>: &lt;span style="color:#4070a0">&amp;#34;tech&amp;#34;&lt;/span>, &lt;span style="color:#4070a0">&amp;#34;general&amp;#34;&lt;/span>: &lt;span style="color:#4070a0">&amp;#34;general&amp;#34;&lt;/span>})
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>Use &lt;strong>structured output&lt;/strong> (&lt;code>llm.with_structured_output(...)&lt;/code> with a fixed set of labels) so the LLM can&amp;rsquo;t return a label you didn&amp;rsquo;t expect.&lt;/li>
&lt;li>A cheap model can do the routing and a strong model can handle the hard branch, which saves cost.&lt;/li>
&lt;/ul>
&lt;h3 id="3-parallelization">3. Parallelization&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">class&lt;/span> &lt;span style="color:#0e84b5;font-weight:bold">S&lt;/span>(TypedDict):
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2&lt;/span>&lt;span> essay: &lt;span style="color:#007020">str&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3&lt;/span>&lt;span> scores: Annotated[&lt;span style="color:#007020">list&lt;/span>[&lt;span style="color:#007020">int&lt;/span>], operator&lt;span style="color:#666">.&lt;/span>add] &lt;span style="color:#60a0b0;font-style:italic"># reducer: merges the parallel writes&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4&lt;/span>&lt;span> final: &lt;span style="color:#007020">float&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">def&lt;/span> &lt;span style="color:#06287e">grammar&lt;/span>(s): &lt;span style="color:#007020;font-weight:bold">return&lt;/span> {&lt;span style="color:#4070a0">&amp;#34;scores&amp;#34;&lt;/span>: [score(s[&lt;span style="color:#4070a0">&amp;#34;essay&amp;#34;&lt;/span>], &lt;span style="color:#4070a0">&amp;#34;grammar&amp;#34;&lt;/span>)]}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">def&lt;/span> &lt;span style="color:#06287e">clarity&lt;/span>(s): &lt;span style="color:#007020;font-weight:bold">return&lt;/span> {&lt;span style="color:#4070a0">&amp;#34;scores&amp;#34;&lt;/span>: [score(s[&lt;span style="color:#4070a0">&amp;#34;essay&amp;#34;&lt;/span>], &lt;span style="color:#4070a0">&amp;#34;clarity&amp;#34;&lt;/span>)]}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">def&lt;/span> &lt;span style="color:#06287e">depth&lt;/span>(s): &lt;span style="color:#007020;font-weight:bold">return&lt;/span> {&lt;span style="color:#4070a0">&amp;#34;scores&amp;#34;&lt;/span>: [score(s[&lt;span style="color:#4070a0">&amp;#34;essay&amp;#34;&lt;/span>], &lt;span style="color:#4070a0">&amp;#34;depth&amp;#34;&lt;/span>)]}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">def&lt;/span> &lt;span style="color:#06287e">combine&lt;/span>(s): &lt;span style="color:#007020;font-weight:bold">return&lt;/span> {&lt;span style="color:#4070a0">&amp;#34;final&amp;#34;&lt;/span>: &lt;span style="color:#007020">sum&lt;/span>(s[&lt;span style="color:#4070a0">&amp;#34;scores&amp;#34;&lt;/span>]) &lt;span style="color:#666">/&lt;/span> &lt;span style="color:#007020">len&lt;/span>(s[&lt;span style="color:#4070a0">&amp;#34;scores&amp;#34;&lt;/span>])}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">for&lt;/span> n, fn &lt;span style="color:#007020;font-weight:bold">in&lt;/span> [(&lt;span style="color:#4070a0">&amp;#34;grammar&amp;#34;&lt;/span>, grammar), (&lt;span style="color:#4070a0">&amp;#34;clarity&amp;#34;&lt;/span>, clarity), (&lt;span style="color:#4070a0">&amp;#34;depth&amp;#34;&lt;/span>, depth)]:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12&lt;/span>&lt;span> g&lt;span style="color:#666">.&lt;/span>add_node(n, fn)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13&lt;/span>&lt;span> g&lt;span style="color:#666">.&lt;/span>add_edge(START, n) &lt;span style="color:#60a0b0;font-style:italic"># fan-out&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14&lt;/span>&lt;span> g&lt;span style="color:#666">.&lt;/span>add_edge(n, &lt;span style="color:#4070a0">&amp;#34;combine&amp;#34;&lt;/span>) &lt;span style="color:#60a0b0;font-style:italic"># fan-in: combine waits for all three&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>All three run in the &lt;strong>same super-step&lt;/strong>, so the total time ≈ the slowest call, not the sum.&lt;/li>
&lt;li>Without the reducer, two nodes writing &lt;code>scores&lt;/code> in one step is an error.&lt;/li>
&lt;li>Two kinds: &lt;strong>sectioning&lt;/strong> (different subtasks) and &lt;strong>voting&lt;/strong> (the same task N times, then a majority or average).&lt;/li>
&lt;/ul>
&lt;h3 id="4-orchestratorworker">4. Orchestrator–worker&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">from&lt;/span> &lt;span style="color:#0e84b5;font-weight:bold">langgraph.types&lt;/span> &lt;span style="color:#007020;font-weight:bold">import&lt;/span> Send
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">class&lt;/span> &lt;span style="color:#0e84b5;font-weight:bold">S&lt;/span>(TypedDict):
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4&lt;/span>&lt;span> task: &lt;span style="color:#007020">str&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5&lt;/span>&lt;span> subtasks: &lt;span style="color:#007020">list&lt;/span>[&lt;span style="color:#007020">str&lt;/span>]
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6&lt;/span>&lt;span> results: Annotated[&lt;span style="color:#007020">list&lt;/span>[&lt;span style="color:#007020">str&lt;/span>], operator&lt;span style="color:#666">.&lt;/span>add]
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7&lt;/span>&lt;span> answer: &lt;span style="color:#007020">str&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">def&lt;/span> &lt;span style="color:#06287e">plan&lt;/span>(s): &lt;span style="color:#007020;font-weight:bold">return&lt;/span> {&lt;span style="color:#4070a0">&amp;#34;subtasks&amp;#34;&lt;/span>: split_into_subtasks(s[&lt;span style="color:#4070a0">&amp;#34;task&amp;#34;&lt;/span>])} &lt;span style="color:#60a0b0;font-style:italic"># LLM decides N&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">def&lt;/span> &lt;span style="color:#06287e">assign&lt;/span>(s): &lt;span style="color:#007020;font-weight:bold">return&lt;/span> [Send(&lt;span style="color:#4070a0">&amp;#34;worker&amp;#34;&lt;/span>, {&lt;span style="color:#4070a0">&amp;#34;sub&amp;#34;&lt;/span>: t}) &lt;span style="color:#007020;font-weight:bold">for&lt;/span> t &lt;span style="color:#007020;font-weight:bold">in&lt;/span> s[&lt;span style="color:#4070a0">&amp;#34;subtasks&amp;#34;&lt;/span>]]
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">def&lt;/span> &lt;span style="color:#06287e">worker&lt;/span>(w): &lt;span style="color:#007020;font-weight:bold">return&lt;/span> {&lt;span style="color:#4070a0">&amp;#34;results&amp;#34;&lt;/span>: [llm&lt;span style="color:#666">.&lt;/span>invoke(w[&lt;span style="color:#4070a0">&amp;#34;sub&amp;#34;&lt;/span>])&lt;span style="color:#666">.&lt;/span>content]}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">def&lt;/span> &lt;span style="color:#06287e">synth&lt;/span>(s): &lt;span style="color:#007020;font-weight:bold">return&lt;/span> {&lt;span style="color:#4070a0">&amp;#34;answer&amp;#34;&lt;/span>: &lt;span style="color:#4070a0">&amp;#34;&lt;/span>&lt;span style="color:#4070a0;font-weight:bold">\n&lt;/span>&lt;span style="color:#4070a0">&amp;#34;&lt;/span>&lt;span style="color:#666">.&lt;/span>join(s[&lt;span style="color:#4070a0">&amp;#34;results&amp;#34;&lt;/span>])}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14&lt;/span>&lt;span>g&lt;span style="color:#666">.&lt;/span>add_node(&lt;span style="color:#4070a0">&amp;#34;plan&amp;#34;&lt;/span>, plan)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15&lt;/span>&lt;span>g&lt;span style="color:#666">.&lt;/span>add_node(&lt;span style="color:#4070a0">&amp;#34;worker&amp;#34;&lt;/span>, worker)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16&lt;/span>&lt;span>g&lt;span style="color:#666">.&lt;/span>add_node(&lt;span style="color:#4070a0">&amp;#34;synth&amp;#34;&lt;/span>, synth)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17&lt;/span>&lt;span>g&lt;span style="color:#666">.&lt;/span>add_edge(START, &lt;span style="color:#4070a0">&amp;#34;plan&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18&lt;/span>&lt;span>g&lt;span style="color:#666">.&lt;/span>add_conditional_edges(&lt;span style="color:#4070a0">&amp;#34;plan&amp;#34;&lt;/span>, assign, [&lt;span style="color:#4070a0">&amp;#34;worker&amp;#34;&lt;/span>]) &lt;span style="color:#60a0b0;font-style:italic"># spawn N workers at runtime&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">19&lt;/span>&lt;span>g&lt;span style="color:#666">.&lt;/span>add_edge(&lt;span style="color:#4070a0">&amp;#34;worker&amp;#34;&lt;/span>, &lt;span style="color:#4070a0">&amp;#34;synth&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>&lt;code>Send(node, input)&lt;/code> starts one copy of &lt;code>worker&lt;/code> per subtask, each with &lt;strong>its own small input&lt;/strong>, not the full state.&lt;/li>
&lt;li>The difference from #3: there the branches are fixed in the graph; here N is decided by the LLM while running.&lt;/li>
&lt;/ul>
&lt;h3 id="5-evaluatoroptimizer">5. Evaluator–optimizer&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">class&lt;/span> &lt;span style="color:#0e84b5;font-weight:bold">S&lt;/span>(TypedDict):
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2&lt;/span>&lt;span> task: &lt;span style="color:#007020">str&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3&lt;/span>&lt;span> draft: &lt;span style="color:#007020">str&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4&lt;/span>&lt;span> feedback: &lt;span style="color:#007020">str&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5&lt;/span>&lt;span> ok: &lt;span style="color:#007020">bool&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6&lt;/span>&lt;span> tries: &lt;span style="color:#007020">int&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">def&lt;/span> &lt;span style="color:#06287e">generate&lt;/span>(s):
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9&lt;/span>&lt;span> d &lt;span style="color:#666">=&lt;/span> llm&lt;span style="color:#666">.&lt;/span>invoke(&lt;span style="color:#4070a0">f&lt;/span>&lt;span style="color:#4070a0">&amp;#34;&lt;/span>&lt;span style="color:#70a0d0">{&lt;/span>s[&lt;span style="color:#4070a0">&amp;#39;task&amp;#39;&lt;/span>]&lt;span style="color:#70a0d0">}&lt;/span>&lt;span style="color:#4070a0;font-weight:bold">\n&lt;/span>&lt;span style="color:#4070a0">Fix this feedback: &lt;/span>&lt;span style="color:#70a0d0">{&lt;/span>s[&lt;span style="color:#4070a0">&amp;#39;feedback&amp;#39;&lt;/span>]&lt;span style="color:#70a0d0">}&lt;/span>&lt;span style="color:#4070a0">&amp;#34;&lt;/span>)&lt;span style="color:#666">.&lt;/span>content
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10&lt;/span>&lt;span> &lt;span style="color:#007020;font-weight:bold">return&lt;/span> {&lt;span style="color:#4070a0">&amp;#34;draft&amp;#34;&lt;/span>: d, &lt;span style="color:#4070a0">&amp;#34;tries&amp;#34;&lt;/span>: s[&lt;span style="color:#4070a0">&amp;#34;tries&amp;#34;&lt;/span>] &lt;span style="color:#666">+&lt;/span> &lt;span style="color:#40a070">1&lt;/span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">def&lt;/span> &lt;span style="color:#06287e">evaluate&lt;/span>(s):
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13&lt;/span>&lt;span> v &lt;span style="color:#666">=&lt;/span> judge(s[&lt;span style="color:#4070a0">&amp;#34;draft&amp;#34;&lt;/span>]) &lt;span style="color:#60a0b0;font-style:italic"># LLM with structured output: ok + feedback&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14&lt;/span>&lt;span> &lt;span style="color:#007020;font-weight:bold">return&lt;/span> {&lt;span style="color:#4070a0">&amp;#34;ok&amp;#34;&lt;/span>: v&lt;span style="color:#666">.&lt;/span>ok, &lt;span style="color:#4070a0">&amp;#34;feedback&amp;#34;&lt;/span>: v&lt;span style="color:#666">.&lt;/span>feedback}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16&lt;/span>&lt;span>g&lt;span style="color:#666">.&lt;/span>add_edge(START, &lt;span style="color:#4070a0">&amp;#34;generate&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17&lt;/span>&lt;span>g&lt;span style="color:#666">.&lt;/span>add_edge(&lt;span style="color:#4070a0">&amp;#34;generate&amp;#34;&lt;/span>, &lt;span style="color:#4070a0">&amp;#34;evaluate&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18&lt;/span>&lt;span>g&lt;span style="color:#666">.&lt;/span>add_conditional_edges(&lt;span style="color:#4070a0">&amp;#34;evaluate&amp;#34;&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">19&lt;/span>&lt;span> &lt;span style="color:#007020;font-weight:bold">lambda&lt;/span> s: END &lt;span style="color:#007020;font-weight:bold">if&lt;/span> s[&lt;span style="color:#4070a0">&amp;#34;ok&amp;#34;&lt;/span>] &lt;span style="color:#007020;font-weight:bold">or&lt;/span> s[&lt;span style="color:#4070a0">&amp;#34;tries&amp;#34;&lt;/span>] &lt;span style="color:#666">&amp;gt;=&lt;/span> &lt;span style="color:#40a070">3&lt;/span> &lt;span style="color:#007020;font-weight:bold">else&lt;/span> &lt;span style="color:#4070a0">&amp;#34;generate&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">20&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">21&lt;/span>&lt;span>app&lt;span style="color:#666">.&lt;/span>invoke({&lt;span style="color:#4070a0">&amp;#34;task&amp;#34;&lt;/span>: &lt;span style="color:#4070a0">&amp;#34;...&amp;#34;&lt;/span>, &lt;span style="color:#4070a0">&amp;#34;feedback&amp;#34;&lt;/span>: &lt;span style="color:#4070a0">&amp;#34;&amp;#34;&lt;/span>, &lt;span style="color:#4070a0">&amp;#34;tries&amp;#34;&lt;/span>: &lt;span style="color:#40a070">0&lt;/span>})
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>&lt;strong>Always cap the loop&lt;/strong> (&lt;code>tries &amp;gt;= 3&lt;/code>). Otherwise a picky judge loops forever, and the recursion limit throws an error.&lt;/li>
&lt;li>Works only if the evaluator has &lt;strong>clear criteria&lt;/strong> (tests pass, the SQL runs, the word count fits).&lt;/li>
&lt;/ul>
&lt;h2 id="state-and-data-points">State and data points&lt;/h2>
&lt;p>&lt;strong>State:&lt;/strong> the shared memory of one graph run. Every node can read it, and nodes update it as the run moves forward. Think of it as a dict that travels through the graph.&lt;/p>
&lt;p>&lt;strong>Data points:&lt;/strong> the individual keys inside the state. Each one is a piece of info the workflow needs to remember.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">class&lt;/span> &lt;span style="color:#0e84b5;font-weight:bold">EssayState&lt;/span>(TypedDict):
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2&lt;/span>&lt;span> essay: &lt;span style="color:#007020">str&lt;/span> &lt;span style="color:#60a0b0;font-style:italic"># data point: the input&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3&lt;/span>&lt;span> feedback: &lt;span style="color:#007020">str&lt;/span> &lt;span style="color:#60a0b0;font-style:italic"># data point: written by the &amp;#34;review&amp;#34; node&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4&lt;/span>&lt;span> score: &lt;span style="color:#007020">int&lt;/span> &lt;span style="color:#60a0b0;font-style:italic"># data point: written by the &amp;#34;grade&amp;#34; node&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>You define the data points up front with a &lt;code>TypedDict&lt;/code>, so every node agrees on the names and types.&lt;/li>
&lt;li>A node reads the data points it needs and returns only the ones it changed, e.g. &lt;code>{&amp;quot;score&amp;quot;: 8}&lt;/code>.&lt;/li>
&lt;li>Each data point has its own merge rule (override or append, see Reducers below).&lt;/li>
&lt;/ul>
&lt;p>So: &lt;strong>state = the container, data points = what&amp;rsquo;s in it.&lt;/strong>&lt;/p>
&lt;h3 id="workflow-state-how-state-moves-through-a-run">Workflow state: how state moves through a run&lt;/h3>
&lt;p>The state is passed from node to node. Each node adds or changes its data points, so the state fills up as the workflow runs:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-text" data-lang="text">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1&lt;/span>&lt;span>START {essay: &amp;#34;…&amp;#34;, feedback: &amp;#34;&amp;#34;, score: 0}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2&lt;/span>&lt;span> ↓ review returns {feedback: &amp;#34;good flow&amp;#34;}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3&lt;/span>&lt;span> {essay: &amp;#34;…&amp;#34;, feedback: &amp;#34;good flow&amp;#34;, score: 0}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4&lt;/span>&lt;span> ↓ grade returns {score: 8}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5&lt;/span>&lt;span> {essay: &amp;#34;…&amp;#34;, feedback: &amp;#34;good flow&amp;#34;, score: 8}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6&lt;/span>&lt;span>END → this final state is what invoke() returns
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>You start a run by passing the initial state: &lt;code>app.invoke({&amp;quot;essay&amp;quot;: &amp;quot;…&amp;quot;, &amp;quot;feedback&amp;quot;: &amp;quot;&amp;quot;, &amp;quot;score&amp;quot;: 0})&lt;/code>. Keys you leave out aren&amp;rsquo;t filled with defaults; they&amp;rsquo;re simply missing until a node writes them.&lt;/li>
&lt;li>Nodes never talk to each other directly. They talk only through the state.&lt;/li>
&lt;li>The state lives for one run. To keep it across runs (e.g. chat history), you need persistence/checkpoints (a later video).&lt;/li>
&lt;/ul>
&lt;h2 id="stategraph">StateGraph&lt;/h2>
&lt;p>A graph where every node reads one shared state and returns &lt;strong>only the keys it changes&lt;/strong>.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>State:&lt;/strong> one dict (a &lt;code>TypedDict&lt;/code>) shared by all nodes.&lt;/li>
&lt;li>&lt;strong>Node:&lt;/strong> a function &lt;code>state -&amp;gt; {changed keys}&lt;/code>.&lt;/li>
&lt;li>&lt;strong>Edge:&lt;/strong> which node runs next.&lt;/li>
&lt;/ul>
&lt;h2 id="reducers-how-a-returned-key-is-merged">Reducers: how a returned key is merged&lt;/h2>
&lt;p>A &lt;strong>reducer&lt;/strong> is a function &lt;code>(old_value, new_value) -&amp;gt; merged_value&lt;/code>. A node returns an update like &lt;code>{&amp;quot;steps&amp;quot;: [&amp;quot;b&amp;quot;]}&lt;/code>, and LangGraph calls the reducer of &lt;code>steps&lt;/code> to merge it into the state. Each key has its own reducer, set in the state definition with &lt;code>Annotated[type, reducer]&lt;/code>.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Override (default):&lt;/strong> &lt;code>answer: str&lt;/code>. No reducer, so the new value replaces the old.&lt;/li>
&lt;li>&lt;strong>Append:&lt;/strong> &lt;code>steps: Annotated[list, operator.add]&lt;/code>. Merged as &lt;code>old + new&lt;/code>.&lt;/li>
&lt;/ul>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">class&lt;/span> &lt;span style="color:#0e84b5;font-weight:bold">State&lt;/span>(TypedDict):
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2&lt;/span>&lt;span> answer: &lt;span style="color:#007020">str&lt;/span> &lt;span style="color:#60a0b0;font-style:italic"># override&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3&lt;/span>&lt;span> steps: Annotated[&lt;span style="color:#007020">list&lt;/span>[&lt;span style="color:#007020">str&lt;/span>], operator&lt;span style="color:#666">.&lt;/span>add] &lt;span style="color:#60a0b0;font-style:italic"># append&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Classic bug: forget the reducer on a list and each node wipes the history. When two nodes run in parallel and write the same key, you &lt;em>need&lt;/em> a reducer, or LangGraph raises an error.&lt;/p>
&lt;h3 id="step-by-step">Step by step&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-text" data-lang="text">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1&lt;/span>&lt;span>state: {steps: [&amp;#34;a&amp;#34;]}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2&lt;/span>&lt;span>node returns: {steps: [&amp;#34;b&amp;#34;]}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3&lt;/span>&lt;span>reducer: operator.add([&amp;#34;a&amp;#34;], [&amp;#34;b&amp;#34;]) → [&amp;#34;a&amp;#34;, &amp;#34;b&amp;#34;]
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4&lt;/span>&lt;span>new state: {steps: [&amp;#34;a&amp;#34;, &amp;#34;b&amp;#34;]}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Without the reducer, the new state would be &lt;code>{steps: [&amp;quot;b&amp;quot;]}&lt;/code>, and &lt;code>&amp;quot;a&amp;quot;&lt;/code> is lost.&lt;/p>
&lt;h3 id="common-reducers">Common reducers&lt;/h3>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Reducer&lt;/th>
&lt;th>Merge&lt;/th>
&lt;th>Use for&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>none (default)&lt;/td>
&lt;td>new replaces old&lt;/td>
&lt;td>Current answer, a flag, a counter you set&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>operator.add&lt;/code>&lt;/td>
&lt;td>&lt;code>old + new&lt;/code> (lists); &lt;code>old + new&lt;/code> (numbers, so it sums)&lt;/td>
&lt;td>Logs, results from parallel workers, totals&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>add_messages&lt;/code>&lt;/td>
&lt;td>Append messages; &lt;strong>replace&lt;/strong> a message with the same id&lt;/td>
&lt;td>Chat history (&lt;code>MessagesState&lt;/code> uses it)&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>custom function&lt;/td>
&lt;td>anything you write&lt;/td>
&lt;td>Dedupe, keep the max, merge dicts&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;h3 id="custom-reducer">Custom reducer&lt;/h3>
&lt;p>Any function with two arguments works:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">def&lt;/span> &lt;span style="color:#06287e">merge_dict&lt;/span>(old: &lt;span style="color:#007020">dict&lt;/span>, new: &lt;span style="color:#007020">dict&lt;/span>) &lt;span style="color:#666">-&amp;gt;&lt;/span> &lt;span style="color:#007020">dict&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2&lt;/span>&lt;span> &lt;span style="color:#007020;font-weight:bold">return&lt;/span> {&lt;span style="color:#666">**&lt;/span>old, &lt;span style="color:#666">**&lt;/span>new} &lt;span style="color:#60a0b0;font-style:italic"># new keys win&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">def&lt;/span> &lt;span style="color:#06287e">keep_last_5&lt;/span>(old: &lt;span style="color:#007020">list&lt;/span>, new: &lt;span style="color:#007020">list&lt;/span>) &lt;span style="color:#666">-&amp;gt;&lt;/span> &lt;span style="color:#007020">list&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5&lt;/span>&lt;span> &lt;span style="color:#007020;font-weight:bold">return&lt;/span> (old &lt;span style="color:#666">+&lt;/span> new)[&lt;span style="color:#666">-&lt;/span>&lt;span style="color:#40a070">5&lt;/span>:] &lt;span style="color:#60a0b0;font-style:italic"># bounded history&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">class&lt;/span> &lt;span style="color:#0e84b5;font-weight:bold">State&lt;/span>(TypedDict):
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8&lt;/span>&lt;span> meta: Annotated[&lt;span style="color:#007020">dict&lt;/span>, merge_dict]
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">9&lt;/span>&lt;span> recent: Annotated[&lt;span style="color:#007020">list&lt;/span>[&lt;span style="color:#007020">str&lt;/span>], keep_last_5]
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="chat-state-with-add_messages">Chat state with add_messages&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">from&lt;/span> &lt;span style="color:#0e84b5;font-weight:bold">langgraph.graph&lt;/span> &lt;span style="color:#007020;font-weight:bold">import&lt;/span> MessagesState &lt;span style="color:#60a0b0;font-style:italic"># = {&amp;#34;messages&amp;#34;: Annotated[list, add_messages]}&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">def&lt;/span> &lt;span style="color:#06287e">chatbot&lt;/span>(s: MessagesState):
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4&lt;/span>&lt;span> &lt;span style="color:#007020;font-weight:bold">return&lt;/span> {&lt;span style="color:#4070a0">&amp;#34;messages&amp;#34;&lt;/span>: [llm&lt;span style="color:#666">.&lt;/span>invoke(s[&lt;span style="color:#4070a0">&amp;#34;messages&amp;#34;&lt;/span>])]} &lt;span style="color:#60a0b0;font-style:italic"># return only the new message&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="rules-to-remember">Rules to remember&lt;/h3>
&lt;ul>
&lt;li>A node returns &lt;strong>only the new part&lt;/strong>: &lt;code>{&amp;quot;steps&amp;quot;: [&amp;quot;b&amp;quot;]}&lt;/code>, not &lt;code>{&amp;quot;steps&amp;quot;: s[&amp;quot;steps&amp;quot;] + [&amp;quot;b&amp;quot;]}&lt;/code>. Doing both duplicates the list: &lt;code>[&amp;quot;a&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;]&lt;/code>.&lt;/li>
&lt;li>Parallel nodes in the same super-step writing the same key &lt;strong>need&lt;/strong> a reducer (error otherwise: &lt;code>InvalidUpdateError&lt;/code>).&lt;/li>
&lt;li>The reducer also runs on the input you pass to &lt;code>invoke()&lt;/code>.&lt;/li>
&lt;li>Keep reducers &lt;strong>pure and fast&lt;/strong>. They run on every update.&lt;/li>
&lt;li>Distributed-systems link: a reducer is a &lt;strong>merge function&lt;/strong>, the same idea as Redux reducers, MapReduce reduce, or a CRDT merge. Order-independent reducers (add to a set, take the max) are safe for parallel writes; with &lt;code>operator.add&lt;/code> on lists, the order depends on the order the nodes ran in.&lt;/li>
&lt;/ul>
&lt;h2 id="is-langchain-stateless">Is LangChain stateless?&lt;/h2>
&lt;p>Mostly, yes. A LangChain &lt;strong>chain is stateless&lt;/strong>.&lt;/p>
&lt;ul>
&lt;li>Every &lt;code>chain.invoke(...)&lt;/code> is independent. Data flows through the pipe once and nothing is kept.&lt;/li>
&lt;li>The LLM itself is stateless too. It only &amp;ldquo;remembers&amp;rdquo; what you put back into the prompt.&lt;/li>
&lt;/ul>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1&lt;/span>&lt;span>chain &lt;span style="color:#666">=&lt;/span> prompt &lt;span style="color:#666">|&lt;/span> llm &lt;span style="color:#666">|&lt;/span> parser
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2&lt;/span>&lt;span>chain&lt;span style="color:#666">.&lt;/span>invoke({&lt;span style="color:#4070a0">&amp;#34;q&amp;#34;&lt;/span>: &lt;span style="color:#4070a0">&amp;#34;My name is Vivek&amp;#34;&lt;/span>})
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3&lt;/span>&lt;span>chain&lt;span style="color:#666">.&lt;/span>invoke({&lt;span style="color:#4070a0">&amp;#34;q&amp;#34;&lt;/span>: &lt;span style="color:#4070a0">&amp;#34;What&amp;#39;s my name?&amp;#34;&lt;/span>}) &lt;span style="color:#60a0b0;font-style:italic"># doesn&amp;#39;t know&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Memory in LangChain = you keep the chat history yourself and resend it each call. The old &lt;code>ConversationBufferMemory&lt;/code> classes did this for you, and they&amp;rsquo;re deprecated now.&lt;/p>
&lt;p>Caveat: newer LangChain (v1) builds its agents (&lt;code>create_agent&lt;/code>) on the LangGraph runtime. Those agents can have state, but the state comes from LangGraph, not from the chain.&lt;/p>
&lt;h2 id="does-langchain-have-a-key-value-store">Does LangChain have a key-value store?&lt;/h2>
&lt;p>Yes, but not a shared &lt;strong>workflow state&lt;/strong>.&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Dict passed down a chain.&lt;/strong> In &lt;code>prompt | llm | parser&lt;/code>, each step gets the previous step&amp;rsquo;s output. &lt;code>RunnablePassthrough.assign(x=...)&lt;/code> adds keys to it. Key-value, but it only flows forward in one pipe.&lt;/li>
&lt;li>&lt;strong>Memory classes.&lt;/strong> Chat history only (deprecated).&lt;/li>
&lt;li>&lt;strong>&lt;code>BaseStore&lt;/code> / &lt;code>InMemoryStore&lt;/code>.&lt;/strong> A real key-value store (&lt;code>mset&lt;/code>, &lt;code>mget&lt;/code>), used for caching embeddings or storing docs. A utility, not workflow state.&lt;/li>
&lt;/ol>
&lt;h2 id="langchain-vs-langgraph-state">LangChain vs LangGraph: state&lt;/h2>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>&lt;/th>
&lt;th>LangChain chain&lt;/th>
&lt;th>LangGraph&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Data flow&lt;/td>
&lt;td>Output → next input, one direction&lt;/td>
&lt;td>One shared state that every node reads and writes&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>State inside one run&lt;/td>
&lt;td>Only the dict in the pipe&lt;/td>
&lt;td>Typed state with data points&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>State across runs&lt;/td>
&lt;td>None&lt;/td>
&lt;td>Yes, with a checkpointer&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Loops / branches&lt;/td>
&lt;td>Hard&lt;/td>
&lt;td>Built in (conditional edges)&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Merge rule per key&lt;/td>
&lt;td>None&lt;/td>
&lt;td>Reducers (override or append)&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>Main reason LangGraph exists: LangChain can store data, but it has no single state that lives through loops, branches and restarts.&lt;/p>
&lt;h2 id="sequential-vs-event-driven-execution">Sequential vs event-driven execution&lt;/h2>
&lt;p>&lt;strong>Sequential execution (LangChain chains):&lt;/strong> steps run in a fixed line, one after another. Step 2 always runs after step 1, and the order is set when you build the chain.&lt;/p>
&lt;pre tabindex="0">&lt;code class="language-mermaid" data-lang="mermaid">flowchart LR
 I([input]) --&amp;gt; P[prompt] --&amp;gt; L[llm] --&amp;gt; O[parser] --&amp;gt; X([output])
&lt;/code>&lt;/pre>&lt;p>Simple and predictable, but it can&amp;rsquo;t go back (no loops) and it can&amp;rsquo;t decide at runtime which step comes next.&lt;/p>
&lt;p>&lt;strong>Event-driven execution (LangGraph):&lt;/strong> a node runs &lt;em>because something happened&lt;/em>, not because it&amp;rsquo;s next in a list. The &amp;ldquo;event&amp;rdquo; is a state update: when a node finishes and writes to the state, that message triggers the nodes connected to it.&lt;/p>
&lt;pre tabindex="0">&lt;code class="language-mermaid" data-lang="mermaid">flowchart LR
 S([START]) --&amp;gt; G[generate_sql]
 G --&amp;gt;|writes state| V{validate}
 V --&amp;gt;|invalid: loop back| G
 V --&amp;gt;|valid| R[run_query]
 R --&amp;gt; X([END])
&lt;/code>&lt;/pre>&lt;p>How LangGraph runs it (based on Google&amp;rsquo;s &lt;strong>Pregel&lt;/strong> model):&lt;/p>
&lt;ol>
&lt;li>Run in &lt;strong>super-steps&lt;/strong>. In each step, all nodes that received a message run, possibly in parallel.&lt;/li>
&lt;li>Their state updates are merged using the reducers.&lt;/li>
&lt;li>The updates trigger the next set of nodes.&lt;/li>
&lt;li>The run stops when no node is triggered (it reaches &lt;code>END&lt;/code>) or the recursion limit is hit.&lt;/li>
&lt;/ol>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>&lt;/th>
&lt;th>Sequential&lt;/th>
&lt;th>Event-driven&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>What starts a step&lt;/td>
&lt;td>Its position in the chain&lt;/td>
&lt;td>A message / state update reaching it&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Order&lt;/td>
&lt;td>Fixed at build time&lt;/td>
&lt;td>Decided at runtime&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Loops&lt;/td>
&lt;td>No&lt;/td>
&lt;td>Yes&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Parallel nodes&lt;/td>
&lt;td>Only special cases&lt;/td>
&lt;td>Natural (same super-step)&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Used by&lt;/td>
&lt;td>LangChain chains&lt;/td>
&lt;td>LangGraph&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>Note: a &amp;ldquo;sequential workflow&amp;rdquo; in LangGraph (START → a → b → END) still runs event-driven. It just happens to have one path.&lt;/p>
&lt;h2 id="execution-model-how-a-graph-actually-runs">Execution model (how a graph actually runs)&lt;/h2>
&lt;h3 id="compile">compile()&lt;/h3>
&lt;p>&lt;code>graph.compile()&lt;/code> checks the graph (no orphan nodes, edges point to real nodes) and turns it into a &lt;strong>Pregel&lt;/strong> app: a runnable object with &lt;code>invoke&lt;/code>, &lt;code>stream&lt;/code> and &lt;code>ainvoke&lt;/code>. You can&amp;rsquo;t run a &lt;code>StateGraph&lt;/code> before you compile it.&lt;/p>
&lt;h3 id="the-pieces-inside">The pieces inside&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>Channels:&lt;/strong> each state key is a &lt;em>channel&lt;/em>. A channel holds the current value plus its reducer.&lt;/li>
&lt;li>&lt;strong>Nodes = actors:&lt;/strong> each node &lt;strong>subscribes&lt;/strong> to channels and &lt;strong>writes&lt;/strong> to channels.&lt;/li>
&lt;li>&lt;strong>Edges = triggers:&lt;/strong> an edge &lt;code>a → b&lt;/code> means &amp;ldquo;when &lt;code>a&lt;/code> finishes, &lt;code>b&lt;/code> becomes active in the next step&amp;rdquo;.&lt;/li>
&lt;/ul>
&lt;p>Nodes never call each other. They only read channels and write channels. This is message passing.&lt;/p>
&lt;h3 id="one-super-step--3-phases">One super-step = 3 phases&lt;/h3>
&lt;pre tabindex="0">&lt;code class="language-mermaid" data-lang="mermaid">flowchart LR
 P[1. PLAN&amp;lt;br/&amp;gt;which nodes are triggered?] --&amp;gt; E[2. EXECUTE&amp;lt;br/&amp;gt;run them in parallel&amp;lt;br/&amp;gt;on the same snapshot]
 E --&amp;gt; U[3. UPDATE&amp;lt;br/&amp;gt;merge writes via reducers&amp;lt;br/&amp;gt;save checkpoint]
 U --&amp;gt;|nodes triggered| P
 U --&amp;gt;|none triggered| X([return state])
&lt;/code>&lt;/pre>&lt;p>Example: &lt;code>START → a → (b, c in parallel) → d → END&lt;/code>&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Step&lt;/th>
&lt;th>Runs&lt;/th>
&lt;th>Sees&lt;/th>
&lt;th>After the update&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>0&lt;/td>
&lt;td>input&lt;/td>
&lt;td>—&lt;/td>
&lt;td>input written to the channels&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>1&lt;/td>
&lt;td>a&lt;/td>
&lt;td>input&lt;/td>
&lt;td>a&amp;rsquo;s writes applied&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>2&lt;/td>
&lt;td>b, c together&lt;/td>
&lt;td>the state after a&lt;/td>
&lt;td>b&amp;rsquo;s and c&amp;rsquo;s writes merged by the reducers&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>3&lt;/td>
&lt;td>d&lt;/td>
&lt;td>the state after b and c&lt;/td>
&lt;td>d&amp;rsquo;s writes applied&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>—&lt;/td>
&lt;td>nothing triggered&lt;/td>
&lt;td>—&lt;/td>
&lt;td>stop, return the state&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;h3 id="rules-that-come-from-this">Rules that come from this&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>Parallel nodes can&amp;rsquo;t see each other&amp;rsquo;s writes&lt;/strong> in the same step. &lt;code>b&lt;/code> doesn&amp;rsquo;t see &lt;code>c&lt;/code>&amp;rsquo;s output. They only meet in the next step.&lt;/li>
&lt;li>&lt;strong>Writes become visible only at the end of the step&lt;/strong> (a barrier, like Bulk Synchronous Parallel).&lt;/li>
&lt;li>&lt;strong>A step is all-or-nothing:&lt;/strong> if one parallel node fails, none of that step&amp;rsquo;s writes are applied. With a checkpointer, you can resume from the last good step and the nodes that succeeded won&amp;rsquo;t rerun.&lt;/li>
&lt;li>&lt;strong>Fan-in waits:&lt;/strong> &lt;code>d&lt;/code> runs once, after both &lt;code>b&lt;/code> and &lt;code>c&lt;/code> have finished.&lt;/li>
&lt;li>&lt;strong>Loops are just more steps.&lt;/strong> The &lt;strong>recursion limit&lt;/strong> (default 25 steps) stops a graph that never ends, by raising &lt;code>GraphRecursionError&lt;/code>. Change it with &lt;code>config={&amp;quot;recursion_limit&amp;quot;: 50}&lt;/code>.&lt;/li>
&lt;li>&lt;strong>Uneven branches:&lt;/strong> if &lt;code>b&lt;/code> takes one step and &lt;code>c&lt;/code> takes two, &lt;code>d&lt;/code> may run as soon as &lt;code>b&lt;/code> finishes. Use a node with &lt;code>defer=True&lt;/code> (or an explicit join) to wait for every branch.&lt;/li>
&lt;/ul>
&lt;h3 id="streaming--watching-the-steps">Streaming = watching the steps&lt;/h3>
&lt;p>&lt;code>app.stream(input, stream_mode=...)&lt;/code> emits output as the steps run:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Mode&lt;/th>
&lt;th>Emits&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>&lt;code>values&lt;/code>&lt;/td>
&lt;td>The full state after each step&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>updates&lt;/code>&lt;/td>
&lt;td>Only what each node wrote&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>messages&lt;/code>&lt;/td>
&lt;td>LLM tokens as they&amp;rsquo;re produced&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>debug&lt;/code>&lt;/td>
&lt;td>Everything (tasks, checkpoints)&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;h3 id="dist-sys-link">Dist-sys link&lt;/h3>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>LangGraph&lt;/th>
&lt;th>Distributed systems&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Super-step&lt;/td>
&lt;td>BSP superstep (Pregel, Giraph)&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>End-of-step update&lt;/td>
&lt;td>A barrier&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Reducer&lt;/td>
&lt;td>Merge function / CRDT&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Checkpoint per step&lt;/td>
&lt;td>Snapshot / write-ahead log&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Step all-or-nothing&lt;/td>
&lt;td>A transaction&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;h2 id="checkpoints">Checkpoints&lt;/h2>
&lt;p>A &lt;strong>checkpoint&lt;/strong> is a saved snapshot of the state, taken after every super-step. A &lt;strong>checkpointer&lt;/strong> is the thing that saves the snapshots (in memory, SQLite, Postgres, Redis…).&lt;/p>
&lt;p>This is how LangGraph gets state &lt;strong>across runs&lt;/strong>. Without a checkpointer, the state dies when &lt;code>invoke()&lt;/code> returns.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">from&lt;/span> &lt;span style="color:#0e84b5;font-weight:bold">langgraph.checkpoint.memory&lt;/span> &lt;span style="color:#007020;font-weight:bold">import&lt;/span> InMemorySaver
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3&lt;/span>&lt;span>app &lt;span style="color:#666">=&lt;/span> graph&lt;span style="color:#666">.&lt;/span>compile(checkpointer&lt;span style="color:#666">=&lt;/span>InMemorySaver())
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5&lt;/span>&lt;span>cfg &lt;span style="color:#666">=&lt;/span> {&lt;span style="color:#4070a0">&amp;#34;configurable&amp;#34;&lt;/span>: {&lt;span style="color:#4070a0">&amp;#34;thread_id&amp;#34;&lt;/span>: &lt;span style="color:#4070a0">&amp;#34;user-42&amp;#34;&lt;/span>}}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6&lt;/span>&lt;span>app&lt;span style="color:#666">.&lt;/span>invoke({&lt;span style="color:#4070a0">&amp;#34;messages&amp;#34;&lt;/span>: [&lt;span style="color:#4070a0">&amp;#34;My name is Vivek&amp;#34;&lt;/span>]}, cfg)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7&lt;/span>&lt;span>app&lt;span style="color:#666">.&lt;/span>invoke({&lt;span style="color:#4070a0">&amp;#34;messages&amp;#34;&lt;/span>: [&lt;span style="color:#4070a0">&amp;#34;What&amp;#39;s my name?&amp;#34;&lt;/span>]}, cfg) &lt;span style="color:#60a0b0;font-style:italic"># remembers: same thread&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>&lt;strong>thread_id&lt;/strong> = which conversation. Each thread has its own chain of checkpoints. A new thread_id starts from an empty state.&lt;/li>
&lt;li>Each checkpoint stores: the state values, which nodes run next, and metadata (the step number).&lt;/li>
&lt;/ul>
&lt;pre tabindex="0">&lt;code class="language-mermaid" data-lang="mermaid">flowchart LR
 subgraph T1[thread_id = user-42]
 direction LR
 c0[(ckpt step 0&amp;lt;br/&amp;gt;input)] --&amp;gt; c1[(ckpt step 1)] --&amp;gt; c2[(ckpt step 2)] --&amp;gt; c3[(ckpt step 3&amp;lt;br/&amp;gt;latest)]
 end
 c1 -.-&amp;gt;|time travel: fork from here| f2[(new branch)]
 subgraph T2[thread_id = user-7]
 direction LR
 d0[(ckpt step 0)] --&amp;gt; d1[(ckpt step 1&amp;lt;br/&amp;gt;latest)]
 end
&lt;/code>&lt;/pre>&lt;p>What checkpoints give you:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Feature&lt;/th>
&lt;th>How&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Memory (chatbots)&lt;/td>
&lt;td>The next invoke on the same thread loads the last state&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Fault tolerance&lt;/td>
&lt;td>A crash mid-run resumes from the last checkpoint instead of starting over&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Human-in-the-loop&lt;/td>
&lt;td>Pause before a node (&lt;code>interrupt&lt;/code>), wait for a human, resume later&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Time travel&lt;/td>
&lt;td>&lt;code>get_state_history(cfg)&lt;/code> lists old checkpoints, and you can replay or fork from any of them&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Debugging&lt;/td>
&lt;td>&lt;code>get_state(cfg)&lt;/code> shows the exact state at any point&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>Production: &lt;code>InMemorySaver&lt;/code> is lost on restart and isn&amp;rsquo;t shared between pods. Use &lt;code>SqliteSaver&lt;/code> locally and &lt;code>PostgresSaver&lt;/code> behind a FastAPI service on K8s, so any pod can resume any thread.&lt;/p>
&lt;p>Dist-sys link: this is the same idea as a write-ahead log / snapshot. Save after each step, and on failure replay from the last good point.&lt;/p>
&lt;h2 id="human-in-the-loop-hitl">Human in the loop (HITL)&lt;/h2>
&lt;p>&lt;strong>HITL&lt;/strong> = the graph &lt;strong>pauses&lt;/strong> at a point, waits for a human to approve, edit or reject, then &lt;strong>resumes&lt;/strong> from the same spot.&lt;/p>
&lt;p>Use it before anything risky or irreversible: running SQL on prod, sending an email, a payment, deleting data.&lt;/p>
&lt;p>It needs a &lt;strong>checkpointer&lt;/strong>. The pause is just &amp;ldquo;save the state and stop&amp;rdquo;. The resume loads that checkpoint, maybe hours later, maybe on another pod.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">from&lt;/span> &lt;span style="color:#0e84b5;font-weight:bold">langgraph.types&lt;/span> &lt;span style="color:#007020;font-weight:bold">import&lt;/span> interrupt, Command
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">def&lt;/span> &lt;span style="color:#06287e">approve_sql&lt;/span>(state):
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4&lt;/span>&lt;span> decision &lt;span style="color:#666">=&lt;/span> interrupt({&lt;span style="color:#4070a0">&amp;#34;sql&amp;#34;&lt;/span>: state[&lt;span style="color:#4070a0">&amp;#34;sql&amp;#34;&lt;/span>]}) &lt;span style="color:#60a0b0;font-style:italic"># pause here, send sql to the human&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5&lt;/span>&lt;span> &lt;span style="color:#007020;font-weight:bold">return&lt;/span> {&lt;span style="color:#4070a0">&amp;#34;approved&amp;#34;&lt;/span>: decision &lt;span style="color:#666">==&lt;/span> &lt;span style="color:#4070a0">&amp;#34;yes&amp;#34;&lt;/span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7&lt;/span>&lt;span>app &lt;span style="color:#666">=&lt;/span> graph&lt;span style="color:#666">.&lt;/span>compile(checkpointer&lt;span style="color:#666">=&lt;/span>checkpointer)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8&lt;/span>&lt;span>cfg &lt;span style="color:#666">=&lt;/span> {&lt;span style="color:#4070a0">&amp;#34;configurable&amp;#34;&lt;/span>: {&lt;span style="color:#4070a0">&amp;#34;thread_id&amp;#34;&lt;/span>: &lt;span style="color:#4070a0">&amp;#34;t1&amp;#34;&lt;/span>}}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10&lt;/span>&lt;span>app&lt;span style="color:#666">.&lt;/span>invoke({&lt;span style="color:#4070a0">&amp;#34;question&amp;#34;&lt;/span>: &lt;span style="color:#4070a0">&amp;#34;top 5 customers&amp;#34;&lt;/span>}, cfg) &lt;span style="color:#60a0b0;font-style:italic"># runs, stops at interrupt&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11&lt;/span>&lt;span>&lt;span style="color:#60a0b0;font-style:italic"># ... show the SQL to the human in the UI ...&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12&lt;/span>&lt;span>app&lt;span style="color:#666">.&lt;/span>invoke(Command(resume&lt;span style="color:#666">=&lt;/span>&lt;span style="color:#4070a0">&amp;#34;yes&amp;#34;&lt;/span>), cfg) &lt;span style="color:#60a0b0;font-style:italic"># continues with decision = &amp;#34;yes&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Flow:&lt;/p>
&lt;pre tabindex="0">&lt;code class="language-mermaid" data-lang="mermaid">sequenceDiagram
 participant App as FastAPI app
 participant G as Graph
 participant CP as Checkpointer
 participant H as Human
 App-&amp;gt;&amp;gt;G: invoke(question, thread_id=t1)
 G-&amp;gt;&amp;gt;G: generate_sql
 G-&amp;gt;&amp;gt;CP: interrupt() in approve_sql, save state
 G--&amp;gt;&amp;gt;App: paused, here is the SQL
 App-&amp;gt;&amp;gt;H: show SQL
 H--&amp;gt;&amp;gt;App: approve
 App-&amp;gt;&amp;gt;G: invoke(Command(resume=&amp;#34;yes&amp;#34;), thread_id=t1)
 CP--&amp;gt;&amp;gt;G: load saved state
 G-&amp;gt;&amp;gt;G: approve_sql reruns, gets &amp;#34;yes&amp;#34;
 G-&amp;gt;&amp;gt;G: run_query
 G--&amp;gt;&amp;gt;App: answer
&lt;/code>&lt;/pre>&lt;p>Common patterns:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Pattern&lt;/th>
&lt;th>Human does&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Approve / reject&lt;/td>
&lt;td>Yes or no before an action&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Edit state&lt;/td>
&lt;td>Fix the SQL or the draft, then continue&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Review tool call&lt;/td>
&lt;td>Check the tool name and args before it runs&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Give input&lt;/td>
&lt;td>Answer a question the agent asked&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>Gotchas:&lt;/p>
&lt;ul>
&lt;li>On resume the node &lt;strong>restarts from its beginning&lt;/strong>, not from the &lt;code>interrupt&lt;/code> line. Code before &lt;code>interrupt()&lt;/code> runs twice, so keep side effects after it (or make them idempotent).&lt;/li>
&lt;li>Same &lt;code>thread_id&lt;/code> to resume. A different id = a fresh run.&lt;/li>
&lt;li>Older style: &lt;code>compile(interrupt_before=[&amp;quot;run_query&amp;quot;])&lt;/code> pauses before a node without code inside it.&lt;/li>
&lt;/ul>
&lt;h2 id="subgraphs">Subgraphs&lt;/h2>
&lt;p>A &lt;strong>subgraph&lt;/strong> = a compiled graph used as a &lt;strong>node&lt;/strong> inside another graph. Like a function call: the parent graph calls it, it runs its own steps, and it returns updates.&lt;/p>
&lt;p>Why use one:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Multi-agent:&lt;/strong> each agent is its own subgraph (SQL agent, RAG agent), and a parent &amp;ldquo;supervisor&amp;rdquo; graph routes between them.&lt;/li>
&lt;li>&lt;strong>Reuse:&lt;/strong> build &amp;ldquo;validate + retry SQL&amp;rdquo; once and plug it in anywhere.&lt;/li>
&lt;li>&lt;strong>Teams / testing:&lt;/strong> each subgraph can be built and tested on its own.&lt;/li>
&lt;li>&lt;strong>Private state:&lt;/strong> a subgraph can keep scratch keys the parent never sees.&lt;/li>
&lt;/ul>
&lt;h3 id="way-1-shared-keys-added-directly-as-a-node">Way 1: shared keys, added directly as a node&lt;/h3>
&lt;p>If the parent and child share state keys, just add the compiled subgraph as a node.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">class&lt;/span> &lt;span style="color:#0e84b5;font-weight:bold">State&lt;/span>(TypedDict):
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2&lt;/span>&lt;span> question: &lt;span style="color:#007020">str&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3&lt;/span>&lt;span> sql: &lt;span style="color:#007020">str&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5&lt;/span>&lt;span>sql_agent &lt;span style="color:#666">=&lt;/span> sql_builder&lt;span style="color:#666">.&lt;/span>compile() &lt;span style="color:#60a0b0;font-style:italic"># child graph, also uses question/sql&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7&lt;/span>&lt;span>parent &lt;span style="color:#666">=&lt;/span> StateGraph(State)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8&lt;/span>&lt;span>parent&lt;span style="color:#666">.&lt;/span>add_node(&lt;span style="color:#4070a0">&amp;#34;sql_agent&amp;#34;&lt;/span>, sql_agent) &lt;span style="color:#60a0b0;font-style:italic"># the whole graph is one node&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">9&lt;/span>&lt;span>parent&lt;span style="color:#666">.&lt;/span>add_edge(START, &lt;span style="color:#4070a0">&amp;#34;sql_agent&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Shared keys flow in and out automatically. Keys only the child has stay inside the child.&lt;/p>
&lt;h3 id="way-2-different-state-called-from-a-node-function">Way 2: different state, called from a node function&lt;/h3>
&lt;p>If the schemas differ, wrap the subgraph in a node and map the state yourself.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1&lt;/span>&lt;span>&lt;span style="color:#007020;font-weight:bold">def&lt;/span> &lt;span style="color:#06287e">call_rag&lt;/span>(state: ParentState):
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2&lt;/span>&lt;span> out &lt;span style="color:#666">=&lt;/span> rag_graph&lt;span style="color:#666">.&lt;/span>invoke({&lt;span style="color:#4070a0">&amp;#34;query&amp;#34;&lt;/span>: state[&lt;span style="color:#4070a0">&amp;#34;question&amp;#34;&lt;/span>]}) &lt;span style="color:#60a0b0;font-style:italic"># parent → child&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3&lt;/span>&lt;span> &lt;span style="color:#007020;font-weight:bold">return&lt;/span> {&lt;span style="color:#4070a0">&amp;#34;context&amp;#34;&lt;/span>: out[&lt;span style="color:#4070a0">&amp;#34;docs&amp;#34;&lt;/span>]} &lt;span style="color:#60a0b0;font-style:italic"># child → parent&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;pre tabindex="0">&lt;code class="language-mermaid" data-lang="mermaid">flowchart LR
 S([START]) --&amp;gt; R{router}
 R --&amp;gt; SQL
 R --&amp;gt; RAG
 subgraph SQL[SQL agent subgraph]
 direction LR
 g[generate_sql] --&amp;gt; v{validate}
 v --&amp;gt;|retry| g
 end
 subgraph RAG[RAG agent subgraph]
 direction LR
 q[retrieve from Qdrant] --&amp;gt; a[grade docs]
 end
 SQL --&amp;gt; A[answer]
 RAG --&amp;gt; A
 A --&amp;gt; X([END])
&lt;/code>&lt;/pre>&lt;p>Notes:&lt;/p>
&lt;ul>
&lt;li>The checkpointer is set on the &lt;strong>parent&lt;/strong> only. Subgraphs use it automatically, so HITL &lt;code>interrupt()&lt;/code> inside a subgraph works.&lt;/li>
&lt;li>Streaming with &lt;code>subgraphs=True&lt;/code> also shows the events from inside the child graphs.&lt;/li>
&lt;li>Rule: use a subgraph when a part has its own loop or its own state. If it&amp;rsquo;s just one step, a plain node is enough.&lt;/li>
&lt;/ul></description></item></channel></rss>