<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Shafiq Ur Rehman</title>
    <description>The latest articles on Forem by Shafiq Ur Rehman (@im-shafiqurehman).</description>
    <link>https://forem.com/im-shafiqurehman</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2818455%2Ffe66a016-1e91-4ff2-931d-f2f9a9fc110e.png</url>
      <title>Forem: Shafiq Ur Rehman</title>
      <link>https://forem.com/im-shafiqurehman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/im-shafiqurehman"/>
    <language>en</language>
    <item>
      <title>From Simple LLMs to Reliable AI Systems: Building Reflexion, Based Agents with LangGraph</title>
      <dc:creator>Shafiq Ur Rehman</dc:creator>
      <pubDate>Sun, 19 Apr 2026 12:44:55 +0000</pubDate>
      <link>https://forem.com/im-shafiqurehman/from-simple-llms-to-reliable-ai-systems-building-reflexion-based-agents-with-langgraph-1a5n</link>
      <guid>https://forem.com/im-shafiqurehman/from-simple-llms-to-reliable-ai-systems-building-reflexion-based-agents-with-langgraph-1a5n</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;"An LLM that cannot reflect on its mistakes is not an agent, it is an autocomplete on steroids."&lt;br&gt;
— Common wisdom in modern AI engineering&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Introduction: Why "Just Prompting" Is No Longer Enough&lt;/p&gt;

&lt;p&gt;You have seen this happen. You give an LLM a hard task. It writes a report. It fixes code. It plans something step by step. The answer sounds right. But small things are wrong. Sometimes big things are wrong.&lt;/p&gt;

&lt;p&gt;The model does not stop to check itself. It does not ask if it made a mistake. It does not try again in a better way.&lt;/p&gt;

&lt;p&gt;This is the gap between a simple LLM call and a system you can trust.&lt;/p&gt;

&lt;p&gt;This article shows how to close that gap. You will learn two ideas: Reflexion, where the AI checks its own work and tries again, and LangGraph, a tool to build workflows with memory and clear steps.&lt;/p&gt;




&lt;h2&gt;
  
  
  Section 1: The Reliability Problem with Bare LLMs
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhdyiocrr9mb0f5uep3wc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhdyiocrr9mb0f5uep3wc.png" alt=" " width="800" height="245"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Large language models are extraordinarily powerful pattern completers. Given a well-formed prompt, they can write poetry, generate code, summarize documents, and reason through logic puzzles. But they have a structural weakness that every practitioner eventually hits:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;They do not know when they are wrong.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The Core Failure Modes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Hallucination&lt;/strong&gt; &lt;em&gt;(making up information that sounds plausible but is factually incorrect)&lt;/em&gt;: An LLM asked to cite sources may invent URLs, author names, or statistics that feel authoritative but do not exist.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Premature convergence&lt;/strong&gt;: The model "settles" on its first reasonable-sounding answer without exploring whether a better one exists. This is especially damaging in multi-step reasoning tasks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Context blindness at scale&lt;/strong&gt;: As tasks grow spanning multiple documents, steps, or tool calls, the model loses track of earlier constraints, leading to contradictions deep in a workflow.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Silent failure&lt;/strong&gt;: Unlike a software crash, a wrong LLM output looks identical to a correct one. There is no error message. The system "succeeds" by returning something.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Counter-view:&lt;/strong&gt; Some researchers argue that sufficiently large models with good prompting (chain-of-thought, self-consistency) can sidestep many reliability issues. This is partially true for isolated reasoning tasks, but it breaks down when tasks are long-horizon, multi-step, or require external tool use, where real-world feedback is necessary.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Real-World Case: The Air Canada Chatbot Incident (2024)
&lt;/h3&gt;

&lt;p&gt;Air Canada deployed an LLM-powered chatbot that confidently told a customer they could apply for a bereavement fare &lt;em&gt;after&lt;/em&gt; their trip and receive a refund retroactively, which was false. The chatbot hallucinated a policy that did not exist. Air Canada was held legally liable. The system had no feedback loop, no validation layer, and no ability to catch its own mistakes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This is not a prompt engineering failure. It is an architectural failure.&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;📖 &lt;strong&gt;Further Reading:&lt;/strong&gt; &lt;em&gt;[Search: "Reliability of LLMs in production systems 2024"]&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;blockquote&gt;
&lt;h3&gt;
  
  
  📌 Background: What Is a "Forward Pass"?
&lt;/h3&gt;

&lt;p&gt;When you send a prompt to an LLM, it runs a &lt;strong&gt;single forward pass&lt;/strong&gt;, meaning it reads your input from left to right through billions of parameters and generates tokens one by one until it stops. There is no internal loop, no checking, no going back. It is a one-way function. This is why LLMs cannot self-correct without external scaffolding.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Section 2: Enter Reflexion: Teaching AI to Think Twice
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F91jnkk7zj80vv28br6oy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F91jnkk7zj80vv28br6oy.png" alt=" " width="800" height="290"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Reflexion&lt;/strong&gt; is a framework introduced in a 2023 research paper by Shinn et al. at Northeastern University. The core idea is elegant:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Instead of training a model to be better (which requires compute and data), give it the ability to reflect on its own failures in natural language, store that reflection as memory, and try again.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is significant because it requires &lt;strong&gt;no weight updates&lt;/strong&gt;, no fine-tuning, no retraining. It is a pure inference-time technique that turns a static model into a self-improving agent.&lt;/p&gt;
&lt;h3&gt;
  
  
  The Three Components of Reflexion
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Actor&lt;/strong&gt; The LLM that actually &lt;em&gt;does&lt;/em&gt; the task. It takes the current task description + any memory from past attempts and generates an output (text, code, a plan, a tool call, etc.).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Evaluator&lt;/strong&gt; &lt;em&gt;(also called the "Critic")&lt;/em&gt;  A scoring function that judges the Actor's output. This can be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Another LLM call that critiques the output&lt;/li&gt;
&lt;li&gt;A deterministic function (e.g., unit test pass/fail, a factuality checker, a code linter)&lt;/li&gt;
&lt;li&gt;A human-in-the-loop signal&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reflector&lt;/strong&gt; The component that reads the Actor's output &lt;em&gt;and&lt;/em&gt; the Evaluator's feedback, then produces a &lt;strong&gt;verbal self-critique&lt;/strong&gt;, a natural language paragraph explaining what went wrong and how to do better. This critique is stored in a &lt;strong&gt;persistent episodic memory&lt;/strong&gt; and injected into the Actor's next attempt.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Why Verbal Reflection Works
&lt;/h3&gt;

&lt;p&gt;The brilliant insight is that LLMs are good at &lt;em&gt;talking about&lt;/em&gt; their mistakes even when they make them. By externalizing the critique into language (rather than gradient updates), you leverage the very skill LLMs are best at. "I failed because I did not account for edge case X. Next time, I should check for X first." This verbalized lesson, fed back into the context window, measurably improves next-attempt quality.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Counter-view:&lt;/strong&gt; Critics point out that Reflexion can get "stuck"  if the Actor's initial attempt is wrong in a way the Evaluator cannot detect, the reflection loop simply reinforces the error. The quality of the Evaluator is the ceiling of the entire system. A bad judge produces bad feedback.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Example: HotpotQA Multi-Hop Reasoning
&lt;/h3&gt;

&lt;p&gt;In the original Reflexion paper, the technique was benchmarked on &lt;strong&gt;HotpotQA&lt;/strong&gt;, a dataset of questions requiring reasoning across multiple Wikipedia articles. A plain GPT-4 agent answered correctly ~30% of the time on hard questions. The same model with Reflexion reached ~60% accuracy after three reflection cycles, without any fine-tuning. The improvement came purely from the agent saying: &lt;em&gt;"I missed that the question asked about the founding date, not the founding country. Let me re-read the passage with that in mind."&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;📖 &lt;strong&gt;Further Reading:&lt;/strong&gt; &lt;em&gt;[Search: "Reflexion: Language Agents with Verbal Reinforcement Learning Shinn et al. 2023"]&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;



&lt;blockquote&gt;
&lt;h3&gt;
  
  
  ⚠️ CRITICAL NOTE: Token Budget and Cost
&lt;/h3&gt;

&lt;p&gt;Every reflection cycle is an additional LLM call. On a 3-cycle Reflexion loop with a GPT-4-class model, you are paying for 3–6× the tokens of a single call. For high-volume production systems, this cost must be budgeted explicitly. Always add a &lt;strong&gt;max_iterations&lt;/strong&gt; guard and use cheaper models for the Evaluator when possible.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;h2&gt;
  
  
  Section 3: LangGraph, Stateful Agents as Executable Graphs
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftv0olkgzlsr1a7qrf8fv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftv0olkgzlsr1a7qrf8fv.png" alt=" " width="800" height="264"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LangGraph&lt;/strong&gt; is a library built on top of LangChain that lets you define agent workflows as &lt;strong&gt;directed graphs&lt;/strong&gt;  where nodes are functions (or LLM calls) and edges are transitions between them, which can be conditional.&lt;/p&gt;

&lt;p&gt;This is a fundamentally better model for complex agents than a simple chain or a while-loop in Python, for three reasons:&lt;/p&gt;
&lt;h3&gt;
  
  
  Why Graphs Beat Chains for Agents
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Explicit state management&lt;/strong&gt;: LangGraph makes the agent's "working memory" what it knows, what it has tried, what it is doing into a typed, inspectable Python object called the &lt;strong&gt;State&lt;/strong&gt;. You always know what data is flowing through your system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Conditional branching&lt;/strong&gt;: Edges in LangGraph can be conditional. After the evaluator runs, you can route: "If score is good enough → END; else → reflect_node." This is the architectural backbone of the retry loop.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Built-in persistence&lt;/strong&gt;: LangGraph supports checkpointing, saving the agent's state to a database between steps. This means long-running agents can be paused, resumed, debugged, or even handed off to a human mid-execution.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Counter-view:&lt;/strong&gt; Some engineers prefer simpler approaches, a while-loop in Python with direct API calls, arguing that LangGraph adds abstraction overhead. This is valid for simple use cases. The graph model truly pays off when you have branching logic, human-in-the-loop steps, or parallel sub-agents that need to join results.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Example: LangGraph vs. LangChain Sequential Chain
&lt;/h3&gt;

&lt;p&gt;Imagine an agent that writes code, runs it, and fixes errors.&lt;/p&gt;

&lt;p&gt;With a &lt;strong&gt;LangChain sequential chain&lt;/strong&gt;, you predefine the steps: write → run → fix → done. But what if it needs 3 fix cycles? Or what if the code is correct on the first try? The chain cannot dynamically decide.&lt;/p&gt;

&lt;p&gt;With &lt;strong&gt;LangGraph&lt;/strong&gt;, you define: &lt;code&gt;write_node → run_node → conditional_edge(pass? → END, fail? → fix_node) → run_node&lt;/code&gt;. The graph &lt;em&gt;routes itself&lt;/em&gt; based on runtime results. This is the difference between a flowchart and a script.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;📖 &lt;strong&gt;Further Reading:&lt;/strong&gt; &lt;em&gt;[Search: "LangGraph documentation stateful agents 2024"]&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;



&lt;blockquote&gt;
&lt;h3&gt;
  
  
  📌 Key Term: Conditional Edges
&lt;/h3&gt;

&lt;p&gt;In LangGraph, a &lt;strong&gt;conditional edge&lt;/strong&gt; is a function that inspects the current State and returns the name of the next node to visit. This is how you implement decision logic: "if the evaluator score is above 0.8, go to END; otherwise, go to reflect_node." Without conditional edges, you have a chain, not an agent.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Section 4: Architecting the Reflexion Agent Design Deep Dive
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp4s2xg09ua2ag1we5y81.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp4s2xg09ua2ag1we5y81.png" alt=" " width="800" height="302"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we get into the engineering. A Reflexion agent in LangGraph is built around three decisions that determine everything else: &lt;strong&gt;what the state looks like&lt;/strong&gt;, &lt;strong&gt;what each node does&lt;/strong&gt;, and &lt;strong&gt;how the conditional router decides when to stop&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  4.1 Designing the State
&lt;/h3&gt;

&lt;p&gt;The State is the agent's "working memory." Every node reads from it and writes to it. A well-designed State captures:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The task&lt;/strong&gt; is immutable, set at the start&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;All past attempts&lt;/strong&gt; so the Actor can see what it has already tried&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;All past reflections&lt;/strong&gt;  so the Actor has accumulated lessons&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scores per attempt&lt;/strong&gt;  for the router to decide stop/continue&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Iteration counter&lt;/strong&gt; is the safety valve against infinite loops&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Final answer&lt;/strong&gt; populated when done&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A common mistake is storing only the &lt;em&gt;latest&lt;/em&gt; attempt and reflection, discarding history. This strips the agent of its learning advantage. The whole point is that accumulated reflections compound across cycles.&lt;/p&gt;
&lt;h3&gt;
  
  
  4.2 The Actor Node
&lt;/h3&gt;

&lt;p&gt;The Actor prompt is the most important in the system. It should include:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;actor_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ReflexionState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ReflexionState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Build context from accumulated memory
&lt;/span&gt;    &lt;span class="n"&gt;memory_context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reflection&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nf"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;attempts&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;reflections&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;memory_context&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="s"&gt;--- Attempt &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; ---&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="n"&gt;memory_context&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;--- Self-Critique &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; ---&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;reflection&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

    &lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    Task: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;task&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;

    &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Your previous attempts and self-critiques&lt;/span&gt;&lt;span class="si"&gt;:{&lt;/span&gt;&lt;span class="n"&gt;memory_context&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; if memory_context else &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="n"&gt;This&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;your&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;

    Now produce your best answer, learning from any past mistakes.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;llm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;attempts&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;attempts&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;iteration&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;iteration&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how the full history of attempts and reflections is injected. This is &lt;strong&gt;episodic memory&lt;/strong&gt;, the agent is literally given its autobiography.&lt;/p&gt;

&lt;h3&gt;
  
  
  4.3 The Evaluator Node
&lt;/h3&gt;

&lt;p&gt;This is the most context-dependent part. The right evaluator depends entirely on your task:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Task Type&lt;/th&gt;
&lt;th&gt;Best Evaluator&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Code generation&lt;/td&gt;
&lt;td&gt;Unit test runner (deterministic)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Factual Q&amp;amp;A&lt;/td&gt;
&lt;td&gt;Another LLM with a fact-check prompt&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Essay writing&lt;/td&gt;
&lt;td&gt;Rubric-based LLM judge&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;API calls&lt;/td&gt;
&lt;td&gt;HTTP response status + schema validation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Math&lt;/td&gt;
&lt;td&gt;Python &lt;code&gt;eval()&lt;/code&gt; or symbolic solver&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A &lt;strong&gt;deterministic evaluator&lt;/strong&gt; (like running tests) is always preferable when available, because it is objective and cheap. LLM-as-judge is useful but introduces its own biases.&lt;/p&gt;

&lt;h3&gt;
  
  
  4.4 The Reflector Node
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;reflect_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ReflexionState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ReflexionState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;last_attempt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;attempts&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;last_score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;scores&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    You attempted this task: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;task&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;

    Your output was:
    &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;last_attempt&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;

    The evaluator gave it a score of &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;last_score&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; out of 1.0.

    Write a concise, specific self-critique (3–5 sentences):
    - What specifically went wrong?
    - What did you overlook or misunderstand?
    - What concrete change will you make next time?

    Do not be vague. Be precise and actionable.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;reflection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;llm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;reflections&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;reflections&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;reflection&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The prompt instructs the LLM to be &lt;strong&gt;specific and actionable&lt;/strong&gt;, not vague. "I should do better" is useless. "I failed to handle the case where the input list is empty, causing an IndexError. Next time, I will add a guard clause at line 1." is useful.&lt;/p&gt;

&lt;h3&gt;
  
  
  4.5 The Conditional Router
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;should_continue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ReflexionState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;scores&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;actor&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;  &lt;span class="c1"&gt;# First iteration, no score yet
&lt;/span&gt;
    &lt;span class="n"&gt;last_score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;scores&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;iteration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;iteration&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;last_score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.85&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;END&lt;/span&gt;  &lt;span class="c1"&gt;# Good enough
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;iteration&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;max_iterations&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;END&lt;/span&gt;  &lt;span class="c1"&gt;# Safety stop
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;reflect&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;  &lt;span class="c1"&gt;# Not done yet, reflect and retry
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The threshold (0.85 here) is a hyperparameter &lt;em&gt;(a design-time setting that you tune rather than the model learns)&lt;/em&gt; that you tune per domain. For medical or legal agents, set it close to 1.0. For creative writing suggestions, 0.7 may suffice.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example (Real-World Case): Reflexion for Competitive Programming
&lt;/h3&gt;

&lt;p&gt;DeepMind's AlphaCode 2 and similar code-agent research use Reflexion-like loops where the actor writes code, a test suite evaluates it, and failure messages are reflected into the next attempt. On LeetCode Hard problems, this pattern lifted solve rates from ~15% (single pass) to ~45% (5 reflection cycles) in published ablations. The key: tests provided a perfect, deterministic evaluator with no LLM-as-judge ambiguity.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;📖 &lt;strong&gt;Further Reading:&lt;/strong&gt; &lt;em&gt;[Search: "LangGraph Reflexion agent code tutorial LangChain 2024"]&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;blockquote&gt;
&lt;h3&gt;
  
  
  ⚠️ WARNING: Reflection Can Degrade Quality
&lt;/h3&gt;

&lt;p&gt;There is a known failure mode called &lt;strong&gt;"reflection poisoning"&lt;/strong&gt; where a poor reflection actually steers the actor &lt;em&gt;away&lt;/em&gt; from a correct answer it found. If your evaluator has a bug or blind spot, a correct output might be scored low, causing the reflector to critique something that was actually right. Always log and inspect all intermediate states, especially on tasks where correctness is hard to verify.&lt;/p&gt;
&lt;/blockquote&gt;







&lt;h2&gt;
  
  
  Section 5: Pros, Cons, and When to Use This Pattern
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Reflexion + LangGraph: Honest Trade-offs
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Pros&lt;/th&gt;
&lt;th&gt;Cons&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Quality&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Measurably higher accuracy on complex tasks&lt;/td&gt;
&lt;td&gt;Quality ceiling is set by the evaluator's accuracy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Cost&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;No fine-tuning needed; inference-only&lt;/td&gt;
&lt;td&gt;Multiple LLM calls per task; 3–10× base cost&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Flexibility&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Works with any LLM; swappable components&lt;/td&gt;
&lt;td&gt;Adds significant engineering complexity vs. one-shot&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Debuggability&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;State is fully inspectable at every step&lt;/td&gt;
&lt;td&gt;More surface area for bugs; harder to trace failures&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Latency&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Best answer given time budget&lt;/td&gt;
&lt;td&gt;Latency scales with iterations; not for real-time apps&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Reliability&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Handles task types that single-pass fails at&lt;/td&gt;
&lt;td&gt;Can loop indefinitely without a hard iteration cap&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h3&gt;
  
  
  When TO Use Reflexion-Based Agents
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Tasks where errors are &lt;strong&gt;catchable and measurable&lt;/strong&gt; (code, math, structured outputs)&lt;/li&gt;
&lt;li&gt;Workflows where &lt;strong&gt;cost of a wrong answer&lt;/strong&gt; exceeds the cost of extra API calls (legal, medical, financial drafting)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Asynchronous&lt;/strong&gt; or batch tasks where latency is not the primary constraint&lt;/li&gt;
&lt;li&gt;Tasks involving &lt;strong&gt;tool use&lt;/strong&gt; where real-world feedback naturally forms the evaluation signal&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  When NOT To Use
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Real-time, low-latency applications (chatbots with &amp;lt;2s response requirement)&lt;/li&gt;
&lt;li&gt;Tasks where the evaluator itself would need to be an expensive LLM call, the economics may not hold&lt;/li&gt;
&lt;li&gt;Simple, well-scoped tasks where a single well-crafted prompt already performs well&lt;/li&gt;
&lt;li&gt;Domains where you cannot define a reliable evaluation metric at all&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Counter-view:&lt;/strong&gt; With inference costs falling ~50% every 12–18 months historically, the cost argument against multi-cycle agents is weakening. By 2026 standards, what costs $0.10 per task today may cost $0.01. Cost-based objections have a short half-life.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Example: When NOT to Use It: The Customer Service Case
&lt;/h3&gt;

&lt;p&gt;A retail company tested Reflexion for their live chat customer support bot. The latency of 3 reflection cycles (avg. 12 seconds per loop) made conversations feel broken. Customers expected responses in 2–3 seconds. The agent was technically more accurate, but user satisfaction scores dropped because of perceived slowness. &lt;strong&gt;Architecture must match use-case constraints&lt;/strong&gt;, not just quality targets.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;📖 &lt;strong&gt;Further Reading:&lt;/strong&gt; &lt;em&gt;[Search: "LLM agent latency optimization production 2024"]&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;







&lt;h2&gt;
  
  
  Section 6: Production Hardening What Research Papers Don't Tell You
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm8j6nd2vnjene326syp7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm8j6nd2vnjene326syp7.png" alt=" " width="800" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Research papers show the happy path. Production systems face messier realities. Here is what you must address:&lt;/p&gt;

&lt;h3&gt;
  
  
  Critical Production Concerns
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Context window overflow&lt;/strong&gt;: By iteration 3, the state contains the original task + 3 attempts + 3 reflections. On long tasks, this can exceed the model's context window &lt;em&gt;(the maximum text length a model can process at once)&lt;/em&gt;. Implement a compression step that summarizes older reflections into a brief "lessons learned" paragraph.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Checkpointing for resilience&lt;/strong&gt;: LangGraph's &lt;code&gt;SqliteSaver&lt;/code&gt; and &lt;code&gt;RedisSaver&lt;/code&gt; let you persist state between steps. If your agent is doing a 10-step task and fails at step 8, you can resume from step 8 without rerunning the first 7 steps. This is non-negotiable for long-running agents.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Observability&lt;/strong&gt;: Use LangSmith (or equivalent tracing tools) to visualize every node's inputs and outputs in real time. Reflexion agents that fail silently are far harder to debug than a simple chain, because the error may be in the evaluator logic, the reflection prompt, or the routing condition.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Human-in-the-loop escalation&lt;/strong&gt;: If after &lt;code&gt;max_iterations&lt;/code&gt; the agent has not reached a satisfactory score, route to a human review queue instead of silently returning the best-so-far. This is the most important reliability upgrade for production.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example: The GitHub Copilot Workspace Model
&lt;/h3&gt;

&lt;p&gt;GitHub Copilot Workspace (released 2024) uses a multi-step agentic loop that resembles Reflexion: it generates a plan, the user can review/edit it (human evaluator), then it generates code, runs tests, and iterates on failures. The "human-as-evaluator" in the planning step is a deliberate design choice that combines automated iteration with human judgment the best of both worlds.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;📖 &lt;strong&gt;Further Reading:&lt;/strong&gt; &lt;em&gt;[Search: "GitHub Copilot Workspace agent architecture 2024"]&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;blockquote&gt;
&lt;h3&gt;
  
  
  ⚠️ SECURITY NOTE: Prompt Injection in Agentic Loops
&lt;/h3&gt;

&lt;p&gt;When the agent's tool outputs (e.g., web search results, code execution stdout) are fed back into the prompt, &lt;strong&gt;malicious content in those results can hijack the agent's behavior&lt;/strong&gt;. This is called prompt injection. Always sanitize tool outputs before injecting them into prompts, and consider running evaluators and reflectors on a separate, sandboxed model invocation.&lt;/p&gt;
&lt;/blockquote&gt;







&lt;h2&gt;
  
  
  Section 7: The Bigger Picture Where Reflexion Fits in the AI Stack
&lt;/h2&gt;

&lt;p&gt;Reflexion is one pattern in a growing taxonomy of agent architectures. Understanding where it sits helps you choose the right tool:&lt;/p&gt;

&lt;h3&gt;
  
  
  Agent Architecture Taxonomy
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Single-pass LLM&lt;/strong&gt; One prompt, one response. Fast. No self-correction.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chain-of-thought&lt;/strong&gt; &lt;em&gt;(prompting the model to "think step by step" before answering)&lt;/em&gt; Better reasoning, but still single-pass.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ReAct&lt;/strong&gt; &lt;em&gt;(Reasoning + Acting: the model alternates between thinking and calling tools)&lt;/em&gt; Good for tool use, but no explicit self-correction loop.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reflexion&lt;/strong&gt; Adds a verbal self-correction cycle on top of any base agent pattern.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multi-agent systems&lt;/strong&gt; Multiple specialized agents (planner, executor, critic), each running independently, coordinated by an orchestrator. Reflexion can live inside each agent.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RLHF / fine-tuning&lt;/strong&gt; &lt;em&gt;(Reinforcement Learning from Human Feedback training the model's weights to be better using human preferences)&lt;/em&gt;  Bakes improvements into the model permanently, but requires data and compute. Reflexion is the inference-time alternative.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Reflexion sits at a sweet spot: &lt;strong&gt;more reliable than ReAct, cheaper than fine-tuning, easier to implement than multi-agent systems&lt;/strong&gt;. It is the right starting point when single-pass quality is insufficient, but you cannot yet justify the infrastructure cost of a full multi-agent system.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Counter-view:&lt;/strong&gt; Some teams argue that investing engineering time in Reflexion scaffolding would be better spent curating fine-tuning data. For domain-specific, high-volume tasks, a fine-tuned small model often outperforms a Reflexion-looped large model at a fraction of the cost. This is a genuine trade-off worth modeling quantitatively before committing.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Example: Cognition AI's Devin (2024)
&lt;/h3&gt;

&lt;p&gt;Devin, marketed as the first "AI software engineer," uses a multi-step loop where the agent writes code, runs it in a sandboxed terminal, observes the output (evaluator), and iterates on failures. A Reflexion-like architecture at its core. The real innovation was the deterministic evaluator: actual code execution. Devin's benchmark scores (14% on SWE-bench) became meaningful precisely because the evaluation was objective, not LLM-based.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;📖 &lt;strong&gt;Further Reading:&lt;/strong&gt; &lt;em&gt;[Search: "Cognition AI Devin architecture evaluation 2024"]&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;







&lt;h2&gt;
  
  
  Conclusion: The Engineering Mindset Shift
&lt;/h2&gt;

&lt;p&gt;The move from simple LLMs to reliable AI systems is not about finding a better model. It is about changing your &lt;strong&gt;architectural mindset&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From &lt;strong&gt;one-shot generation&lt;/strong&gt; to &lt;strong&gt;iterative refinement&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;From &lt;strong&gt;static prompts&lt;/strong&gt; to &lt;strong&gt;stateful, memory-carrying agents&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;From &lt;strong&gt;hoping the model is right&lt;/strong&gt; to &lt;strong&gt;building systems that verify and retry&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Reflexion and LangGraph together give you the building blocks for this shift. Reflexion provides the &lt;em&gt;cognitive loop&lt;/em&gt;, the ability to criticize and improve. LangGraph provides the &lt;em&gt;execution infrastructure&lt;/em&gt;, typed state, conditional routing, persistence, and observability.&lt;/p&gt;

&lt;p&gt;Neither is magic. Both require careful engineering: a well-designed evaluator, a well-tuned reflector prompt, a sensible iteration cap, and proper production hardening. But applied correctly, they transform an LLM from a clever autocomplete into a system that can be &lt;em&gt;trusted&lt;/em&gt; with consequential tasks.&lt;/p&gt;

&lt;p&gt;The difference between a demo and a production AI system is not the model. It is the scaffolding around it.&lt;/p&gt;

</description>
      <category>llm</category>
      <category>agentsystems</category>
      <category>reflexion</category>
      <category>generativeai</category>
    </item>
    <item>
      <title>How React Achieves High Performance, Even With Extra Layers</title>
      <dc:creator>Shafiq Ur Rehman</dc:creator>
      <pubDate>Sun, 21 Sep 2025 14:10:41 +0000</pubDate>
      <link>https://forem.com/im-shafiqurehman/how-react-achieves-high-performance-even-with-extra-layers-339j</link>
      <guid>https://forem.com/im-shafiqurehman/how-react-achieves-high-performance-even-with-extra-layers-339j</guid>
      <description>&lt;p&gt;A common interview question around React is:&lt;br&gt;
&lt;strong&gt;“If DOM updates are already costly, and React adds Virtual DOM + Reconciliation as extra steps, how can it be faster?”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Many developers, including myself at one point, confidently answer: &lt;em&gt;“Because of Virtual DOM!”&lt;/em&gt;&lt;br&gt;
But that’s not the full picture. Let’s break it down properly.&lt;/p&gt;
&lt;h2&gt;
  
  
  First: How Browser Rendering Works (Brief Overview)
&lt;/h2&gt;

&lt;p&gt;Before we talk about React’s optimizations, let’s first understand how the browser renders things by default.&lt;/p&gt;

&lt;p&gt;When the browser receives HTML and CSS from the server, it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creates the &lt;strong&gt;DOM tree&lt;/strong&gt; and &lt;strong&gt;CSSOM tree&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Combines them into the &lt;strong&gt;Render Tree&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Decides the &lt;strong&gt;layout&lt;/strong&gt;, which element goes where&lt;/li&gt;
&lt;li&gt;Finally, &lt;strong&gt;paints&lt;/strong&gt; the pixels on screen&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When something changes, like text content or styles, the DOM and CSSOM are rebuilt, the Render Tree is recreated, and then comes the expensive part: &lt;strong&gt;Reflow&lt;/strong&gt; and &lt;strong&gt;Repaint&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this process:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Positions are recalculated&lt;/li&gt;
&lt;li&gt;Elements are repainted wherever styles or content have changed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both Reflow and Repaint are costly operations, and this is exactly where React tries to help.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F265b33lv87kgaf71adz7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F265b33lv87kgaf71adz7.png" alt=" " width="800" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let’s move to React and the Virtual DOM.&lt;/p&gt;
&lt;h2&gt;
  
  
  What Is Virtual DOM?
&lt;/h2&gt;

&lt;p&gt;Virtual DOM is a lightweight copy of the actual DOM, represented as a JavaScript object.&lt;/p&gt;

&lt;p&gt;Why was it needed? What was the problem with direct DOM manipulation?&lt;/p&gt;

&lt;p&gt;Let’s look at an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Normal DOM manipulation, NOT React&lt;/span&gt;
&lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;span&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;span&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toLocaleTimeString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, only the &lt;code&gt;span&lt;/code&gt;’s time is updating. But if you inspect in DevTools, you’ll see the entire &lt;code&gt;div&lt;/code&gt; re-rendering.&lt;/p&gt;

&lt;p&gt;Modern browsers are smart; if other elements existed, they wouldn’t repaint them. But even then, in this case, the browser isn’t precise enough. The entire element container is being marked for update.&lt;/p&gt;

&lt;p&gt;Now look at the same code in React:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTime&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toLocaleTimeString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;interval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toLocaleTimeString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h3&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Current Time:&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h3&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, only the &lt;code&gt;span&lt;/code&gt; updates. The &lt;code&gt;div&lt;/code&gt; doesn’t re-render. React has already optimized the process at this level.&lt;/p&gt;

&lt;p&gt;So how does React do this?&lt;/p&gt;

&lt;p&gt;React creates a Virtual DOM.&lt;/p&gt;

&lt;p&gt;First, it creates the initial Virtual DOM, a JS object tree mirroring your UI.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div
├── h3
├── form
│   └── input
└── span → "10:30 AM"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When state updates, say, time changes to “10:31 AM,”  React creates a new Virtual DOM tree:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div
├── h3
├── form
│   └── input
└── span → "10:31 AM"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then React compares the old and new Virtual DOM trees. This comparison process is called &lt;strong&gt;Diffing&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;React sees: “Only the text inside &lt;code&gt;span&lt;/code&gt; changed.” So it updates only that &lt;code&gt;span&lt;/code&gt; in the Real DOM, and triggers repaint for just that node.&lt;/p&gt;

&lt;p&gt;This comparison algorithm is called the &lt;strong&gt;Diffing Algorithm&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two Key Optimizations in Diffing
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Batching Updates&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If multiple state updates happen, React doesn’t go update the DOM each time. It batches them together and applies them in one go.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React will wait, collect all three, compute a final Virtual DOM, diff it, and update the Real DOM once.&lt;/p&gt;

&lt;p&gt;This avoids multiple reflows/repaints.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Element Type Comparison&lt;/strong&gt;
Let’s say you have a login/logout UI:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;isLoggedIn&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isLoggedIn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Welcome back, user!&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Log In&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initial Virtual DOM (logged out):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div → class="app"
└── button → "Log In"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Updated Virtual DOM (logged in):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div → class="app"
└── h1 → "Welcome back, user!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React starts comparing from the root.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;div&lt;/code&gt; → same → check props → &lt;code&gt;class="app"&lt;/code&gt; → same → move on
&lt;/li&gt;
&lt;li&gt;Now children: &lt;code&gt;button&lt;/code&gt; vs &lt;code&gt;h1&lt;/code&gt; → TYPE MISMATCH&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;React doesn’t try to “update” the button into an h1. It destroys the entire subtree and recreates it from scratch.&lt;/p&gt;

&lt;p&gt;This is efficient because trying to morph one element into another is more expensive than just replacing it.&lt;/p&gt;

&lt;p&gt;So far, this process seems optimized. Then why did React introduce Fiber?&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Was Fiber Needed?
&lt;/h2&gt;

&lt;p&gt;The original Reconciliation process had a critical flaw: &lt;strong&gt;It was synchronous and recursive&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Once started, it would run to completion, blocking the main thread.&lt;/p&gt;

&lt;p&gt;Imagine this scenario:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User is typing in an input field
&lt;/li&gt;
&lt;li&gt;Meanwhile, 10 API calls return and trigger UI updates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because React’s diffing was synchronous, it would process all 10 updates in one blocking pass, freezing the UI while the user is typing.&lt;/p&gt;

&lt;p&gt;React had no way to say: “This user input is high priority, do it first. Those API updates? Do them later.”&lt;/p&gt;

&lt;p&gt;Everything was treated equally and executed in one uninterrupted stack.&lt;/p&gt;

&lt;p&gt;This hurt user experience.&lt;/p&gt;

&lt;p&gt;So in React 16, Fiber was introduced.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is React Fiber?
&lt;/h2&gt;

&lt;p&gt;React Fiber is a new Reconciliation algorithm. All updates in modern React go through Fiber.&lt;/p&gt;

&lt;p&gt;Fiber solved the core problem: &lt;strong&gt;It made Reconciliation interruptible, prioritizable, and asynchronous&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let’s understand how.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fiber Working, Step by Step
&lt;/h2&gt;

&lt;p&gt;Consider this component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"app"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shafique&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nf"&gt;startTransition&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nf"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        Click to Update
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Profile&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Dashboard&lt;/span&gt; &lt;span class="na"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;setName("Shafique")&lt;/code&gt; → high priority update (Sync Lane)
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setLoading(true)&lt;/code&gt; wrapped in &lt;code&gt;startTransition&lt;/code&gt; → low priority (Transition Lane)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fiber will handle them differently.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fiber Architecture, Key Concepts
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Fiber Node
&lt;/h3&gt;

&lt;p&gt;Every element, component, DOM node, and text becomes a &lt;strong&gt;Fiber Node&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Example component tree:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;App&amp;gt;
  ├── &amp;lt;h2&amp;gt;
  ├── &amp;lt;Profile&amp;gt;
  └── &amp;lt;Dashboard&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Becomes a Fiber Tree where each node is a unit of work.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Current Tree vs WIP(Work-In-Progress Tree)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Current Tree&lt;/strong&gt; → The tree currently rendered on screen
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Work-In-Progress (WIP) Tree&lt;/strong&gt; → The tree being prepared for next render&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When updates happen, React builds the WIP tree and then swaps it with the Current Tree during the Commit Phase.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fiber Reconciliation Two Phases
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Phase 1: Render Phase (Interruptible)
&lt;/h3&gt;

&lt;p&gt;This phase has two sub-phases:&lt;/p&gt;

&lt;h4&gt;
  
  
  a. Begin Work
&lt;/h4&gt;

&lt;p&gt;React visits each Fiber Node starting from the root.&lt;/p&gt;

&lt;p&gt;It checks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Does this node need update?
&lt;/li&gt;
&lt;li&gt;What’s the new state/props?
&lt;/li&gt;
&lt;li&gt;Create/clone Fiber Node for WIP tree&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  b. Complete Work
&lt;/h4&gt;

&lt;p&gt;After a node’s children are processed, React:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creates the actual DOM node (if new)
&lt;/li&gt;
&lt;li&gt;Links it to the Fiber Node via &lt;code&gt;stateNode&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Adds the Fiber Node to the “Effect List”  if it needs a DOM update&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;fiber&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stateNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;h2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F788c8ce8fps0pl4wjtmo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F788c8ce8fps0pl4wjtmo.png" alt=" " width="800" height="459"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Effect List is a linked list of nodes that need DOM mutations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Traversal Order, Depth First
&lt;/h2&gt;

&lt;p&gt;Fiber doesn’t use recursion; it uses a linked list with pointers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;child&lt;/code&gt; → first child
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sibling&lt;/code&gt; → next sibling
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;return&lt;/code&gt; → parent&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Traversal order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Start at Root
&lt;/li&gt;
&lt;li&gt;Go to the child
&lt;/li&gt;
&lt;li&gt;Keep going to the child until the leaf
&lt;/li&gt;
&lt;li&gt;At leaf → go to sibling
&lt;/li&gt;
&lt;li&gt;If no sibling → go back to parent
&lt;/li&gt;
&lt;li&gt;Parents’ sibling? Go there. Otherwise, go to the grandparent.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Root
└── App
    ├── h2
    ├── Profile
    └── Dashboard
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Traversal:&lt;/p&gt;

&lt;p&gt;Root → App → h2 (leaf) → Profile (sibling) → Dashboard (sibling) → App (parent) → Root&lt;/p&gt;

&lt;p&gt;At each node, Begin Work → then, after children → Complete Work.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjdwfy7ilp4jmtnxmocio.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjdwfy7ilp4jmtnxmocio.png" alt=" " width="720" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Phase 2: Commit Phase (Synchronous)
&lt;/h2&gt;

&lt;p&gt;Once the Render Phase is done, React has:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A complete WIP Fiber Tree
&lt;/li&gt;
&lt;li&gt;An Effect List with all nodes needing DOM updates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, React enters the Commit Phase, which is &lt;strong&gt;synchronous and uninterruptible&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It walks the Effect List and performs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Insertions
&lt;/li&gt;
&lt;li&gt;Updates
&lt;/li&gt;
&lt;li&gt;Deletions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On the Real DOM.&lt;/p&gt;

&lt;p&gt;Then, it swaps WIP Tree → becomes new Current Tree.&lt;/p&gt;

&lt;h2&gt;
  
  
  Update Phase, How Priorities Work
&lt;/h2&gt;

&lt;p&gt;When state updates:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;React creates an &lt;strong&gt;Update Object&lt;/strong&gt; → { payload, timestamp, lane }
&lt;/li&gt;
&lt;li&gt;Enqueues it in the component’s update queue
&lt;/li&gt;
&lt;li&gt;Marks the Fiber Node (and all ancestors) as “needing work”
&lt;/li&gt;
&lt;li&gt;Schedules the update based on lane (priority)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// High priority&lt;/span&gt;
&lt;span class="nf"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shafique&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Sync Lane&lt;/span&gt;

&lt;span class="c1"&gt;// Low priority&lt;/span&gt;
&lt;span class="nf"&gt;startTransition&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Transition Lane&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React’s Scheduler:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Checks which updates are pending
&lt;/li&gt;
&lt;li&gt;Assigns priority: Sync, Transition, Idle
&lt;/li&gt;
&lt;li&gt;Executes high-priority first&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So when you click the button:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;React creates a WIP(work-in-progress) tree
&lt;/li&gt;
&lt;li&gt;Processes &lt;code&gt;setName("Shafique")&lt;/code&gt; → updates Profile
&lt;/li&gt;
&lt;li&gt;Skips &lt;code&gt;setLoading(true)&lt;/code&gt; for now (low priority)
&lt;/li&gt;
&lt;li&gt;Commits → UI updates immediately
&lt;/li&gt;
&lt;li&gt;Later — starts new WIP tree → processes &lt;code&gt;setLoading(true)&lt;/code&gt; → commits Dashboard update&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The user sees instant feedback, and background work occurs later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fiber’s Real Power
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Work is split into chunks → doesn’t block the main thread
&lt;/li&gt;
&lt;li&gt;High-priority work (user input) jumps the queue
&lt;/li&gt;
&lt;li&gt;Low priority work (data loading) waits — but doesn’t block
&lt;/li&gt;
&lt;li&gt;Browser gets breathing room → stays responsive&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even though Fiber adds more steps, it makes the right steps happen at the right time.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>discuss</category>
      <category>frontend</category>
    </item>
    <item>
      <title>How Node.js Achieves High Performance &amp; Scalability</title>
      <dc:creator>Shafiq Ur Rehman</dc:creator>
      <pubDate>Sat, 20 Sep 2025 06:25:36 +0000</pubDate>
      <link>https://forem.com/im-shafiqurehman/how-nodejs-achieves-high-performance-scalability-3lad</link>
      <guid>https://forem.com/im-shafiqurehman/how-nodejs-achieves-high-performance-scalability-3lad</guid>
      <description>&lt;h2&gt;
  
  
  What is Node.js?
&lt;/h2&gt;

&lt;p&gt;Node.js is an open-source JavaScript runtime environment that allows you to develop scalable web applications (accessible on the internet, without requiring installation on the user's device). This environment is built on top of Google Chrome’s JavaScript Engine V8. It uses an event-driven(waits for things to happen and then reacts to them), non-blocking I/O model(sends I/O requests and continuously does other work, notified when done), making it lightweight, more efficient, and perfect for data-intensive real-time applications running across shared devices.&lt;/p&gt;

&lt;h2&gt;
  
  
  Non-Blocking I/O: The Performance Game-Changer
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Runs on the &lt;strong&gt;side stack&lt;/strong&gt; (callback queue/microtask queue).&lt;/li&gt;
&lt;li&gt;The main thread &lt;strong&gt;does not wait&lt;/strong&gt;, async operations run in the background, and their callbacks execute later.&lt;/li&gt;
&lt;li&gt;Example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fs = require('fs');
fs.readFile('file.txt', (err, data) =&amp;gt; {     // Non-blocking
console.log(”This runs after the file reading is completes” , data);
});
console.log("This runs immediately");

 Here, console.log("This runs immediately") executes first, and the file reading happens in the background.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Node.js Architecture Overview
&lt;/h2&gt;

&lt;p&gt;This architecture is mainly based on 5 key components:&lt;/p&gt;

&lt;p&gt;1️⃣ &lt;strong&gt;Single Thread&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;2️⃣ &lt;strong&gt;Event Loop&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;3️⃣ &lt;strong&gt;Event Queue&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;4️⃣ &lt;strong&gt;Worker Pool (Libuv)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;5️⃣ &lt;strong&gt;V8 Engine&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Single Thread&lt;/strong&gt;&lt;br&gt;
Node.js operates in a single-threaded environment. This means:&lt;/p&gt;

&lt;p&gt;Only one thread executes JavaScript code.&lt;/p&gt;

&lt;p&gt;This thread handles the main event loop.&lt;/p&gt;

&lt;p&gt;This is why Node.js is lightweight.&lt;/p&gt;

&lt;p&gt;In simple terms, a single thread handles requests from multiple users, resulting in low memory usage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Event Loop: Node.js’s Secret Weapon&lt;/strong&gt;&lt;br&gt;
The event loop runs indefinitely and connects the call stack, the microtask queue, and the callback queue. The event loop moves asynchronous tasks from the microtask queue and the callback queue to the call stack whenever the call stack is empty.&lt;/p&gt;

&lt;p&gt;Callback Queue:&lt;br&gt;
 Callback functions for operations like setTimeout() are added here before moving to the call stack.&lt;/p&gt;

&lt;p&gt;Microtask Queue: &lt;br&gt;
Callback functions for Promises and MutationObserver are queued here and have higher priority.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Event Queue&lt;/strong&gt;&lt;br&gt;
When asynchronous operations (like HTTP requests, database queries) are performed:&lt;/p&gt;

&lt;p&gt;Node.js places them in the event queue.&lt;/p&gt;

&lt;p&gt;The event loop then processes this queue when the main thread is free.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Offloading Heavy Work: Libuv &amp;amp; Worker Pool&lt;/strong&gt;&lt;br&gt;
Node.js is single-threaded, but that doesn’t mean it can’t do parallel work.&lt;/p&gt;

&lt;p&gt;For blocking I/O tasks (file system, DNS, crypto, compression), Node.js uses Libuv’s Worker Pool, a pool of 4 background threads (configurable) that handle heavy lifting.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why This Matters for Performance:
Your main thread stays free to handle new requests.
I/O-bound tasks run in parallel without blocking JavaScript execution.
CPU-bound tasks? Use worker_threads or offload to microservices.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In simple terms, Node.js's single thread handles the main application logic, while heavy tasks are handled in the background by the worker pool.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;V8 Engine: Raw Speed Under the Hood&lt;/strong&gt;&lt;br&gt;
Node.js runs on Google’s V8 JavaScript Engine, the same engine that powers Chrome.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Performance Benefits:
Just-In-Time Compilation: Converts JS to optimized machine code at runtime.
Dynamic Optimization: Frequently used functions get turbocharged.
Garbage Collection: Efficient memory management prevents leaks and slowdowns.
V8 is why Node.js apps start fast, run fast, and stay fast, even under heavy load. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxazesjkkm8up1gcf1y28.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxazesjkkm8up1gcf1y28.png" alt="A simplified flowchart illustrating the non-blocking flow of a request in Node.js: Incoming Request -&amp;gt; Event Loop -&amp;gt; Immediate processing for non-blocking tasks or delegation to the Worker Pool for heavy tasks -&amp;gt; Response." width="397" height="515"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Node.js Flow Example:
&lt;/h2&gt;

&lt;p&gt;1️⃣ A user sends an API request.&lt;/p&gt;

&lt;p&gt;2️⃣ Node.js receives the request.&lt;/p&gt;

&lt;p&gt;3️⃣ If the request involves:&lt;/p&gt;

&lt;p&gt;A non-heavy CPU task is executed directly via the event loop.&lt;/p&gt;

&lt;p&gt;A heavy task (like reading a file) is sent to the worker pool.&lt;/p&gt;

&lt;p&gt;4️⃣ While the task is processing, the event loop continues to handle other requests.&lt;/p&gt;

&lt;p&gt;5️⃣ Once the task is complete, its callback function is placed in the event queue.&lt;/p&gt;

&lt;p&gt;6️⃣ The event loop picks up the callback and executes it.&lt;/p&gt;

&lt;p&gt;7️⃣ Node.js sends the response back to the user.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvuwrdzyd3flico2krww8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvuwrdzyd3flico2krww8.png" alt="Detailed diagram of the Node.js runtime architecture showing the relationship between the V8 engine, the Event Loop with its call stack, and the Libuv thread pool which handles I/O operations and delegates work to worker threads." width="691" height="479"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Pro Tips for Optimizing Node.js Performance
&lt;/h2&gt;

&lt;p&gt;Never Use Sync APIs&lt;br&gt;
→ readFileSync and writeFileSync will destroy your server's throughput.&lt;/p&gt;

&lt;p&gt;Use Async/Await or Promises&lt;br&gt;
→ Less messy, faster, and easier to debug than callbacks.&lt;/p&gt;

&lt;p&gt;Cluster Your App&lt;br&gt;
→ Utilize all CPU cores using the cluster module.&lt;/p&gt;

&lt;p&gt;Offload CPU Work&lt;br&gt;
→ Leverage worker_threads for heavy computation.&lt;/p&gt;

&lt;p&gt;Use Caching &amp;amp; Streaming&lt;br&gt;
→ Minimize I/O roundtrips. Stream large files instead of loading into memory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thought
&lt;/h2&gt;

&lt;p&gt;Node.js doesn’t achieve high performance by throwing more hardware at the problem; it does so by being intelligent with resources. Its event-driven, non-blocking model is purpose-built for modern, I/O-heavy applications.&lt;/p&gt;

&lt;p&gt;Master these concepts, avoid blocking code, and you’ll unlock Node.js’s true potential: a server that’s fast, lean, and ready to scale&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>performance</category>
      <category>javascript</category>
      <category>node</category>
    </item>
    <item>
      <title>JavaScript Execution Context Made Simple</title>
      <dc:creator>Shafiq Ur Rehman</dc:creator>
      <pubDate>Thu, 21 Aug 2025 10:19:34 +0000</pubDate>
      <link>https://forem.com/im-shafiqurehman/javascript-execution-context-made-simple-5gk0</link>
      <guid>https://forem.com/im-shafiqurehman/javascript-execution-context-made-simple-5gk0</guid>
      <description>&lt;p&gt;A JavaScript engine is a program that converts JavaScript code into a Binary Language. Computers understand the Binary Language. Every web browser contains a JavaScript engine. For example, V8 is the JavaScript engine in Google Chrome.&lt;/p&gt;

&lt;p&gt;Let's dive in!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdrlyovje7ykt4ef2d7v6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdrlyovje7ykt4ef2d7v6.png" alt="Diagram showing synchronous vs asynchronous JavaScript execution" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Execution context&lt;/strong&gt;: Execution Context is the environment in which JS code runs. It decides what variables and functions are accessible, and how the code executes. It has two types (Global &amp;amp; Function) and works in two phases (Memory Creation &amp;amp; Code Execution).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Global Execution Context (GEC)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This is created &lt;strong&gt;once&lt;/strong&gt; when your script starts. It's the outermost context where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Global variables and functions are stored&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;this&lt;/code&gt; refers to the global object (like &lt;code&gt;window&lt;/code&gt; in browsers)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. &lt;strong&gt;Function Execution Context (FEC)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When you &lt;strong&gt;call a function&lt;/strong&gt;, a &lt;strong&gt;new context&lt;/strong&gt; is created specifically for that function. It manages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The function's local variables&lt;/li&gt;
&lt;li&gt;The value of &lt;code&gt;this&lt;/code&gt; inside the function&lt;/li&gt;
&lt;li&gt;Arguments passed to the function&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. &lt;strong&gt;Memory Creation Phase&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This is the &lt;strong&gt;first phase&lt;/strong&gt; of an execution context. During this phase:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All &lt;strong&gt;variables and functions&lt;/strong&gt; are allocated in memory&lt;/li&gt;
&lt;li&gt;Functions are &lt;strong&gt;fully hoisted&lt;/strong&gt; (stored with their complete code)&lt;/li&gt;
&lt;li&gt;Variables declared with &lt;strong&gt;&lt;code&gt;var&lt;/code&gt;&lt;/strong&gt; are hoisted and initialized with &lt;code&gt;undefined&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Variables declared with &lt;strong&gt;&lt;code&gt;let&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;const&lt;/code&gt;&lt;/strong&gt; are also hoisted but remain uninitialized, staying in the &lt;strong&gt;Temporal Dead Zone (TDZ)&lt;/strong&gt; until their declaration is reached&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. &lt;strong&gt;Code Execution Phase&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This is the &lt;strong&gt;second phase&lt;/strong&gt;, where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The code &lt;strong&gt;executes line by line&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Variables receive their actual values&lt;/li&gt;
&lt;li&gt;Functions are called when invoked&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;strong&gt;Variable Environment&lt;/strong&gt; is a &lt;strong&gt;part of the Execution Context&lt;/strong&gt;.&lt;br&gt;
It is &lt;strong&gt;where all variables, functions, and arguments are stored in memory&lt;/strong&gt; as key-value pairs during the &lt;strong&gt;Memory Creation Phase&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;It includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Variable declarations&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function declarations&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function parameters&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;It is &lt;strong&gt;used internally by the JS engine&lt;/strong&gt; to track what's defined in the current scope.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Call stack&lt;/strong&gt;: The call stack is a part of the JavaScript engine that helps keep track of function calls. When a function is invoked, it is pushed to the call stack, where its execution begins. When the execution is complete, the function is popped off the call stack. It utilizes the concept of stacks in data structures, following the Last-In-First-Out (LIFO) principle.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Event loop:&lt;/strong&gt; The event loop runs indefinitely and connects the call stack, the microtask queue, and the callback queue. The event loop moves asynchronous tasks from the microtask queue and the callback queue to the call stack whenever the call stack is empty.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;In JavaScript’s event loop, microtasks always have higher priority than macrotasks (callback queue).&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Callback Queue (Macrotask Queue):   Callback functions for setTimeout() are added to the callback queue before they are moved to the call stack for execution.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;setTimeout()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;setInterval()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;setImmediate()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;I/O events&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Microtask queue:&lt;/strong&gt; Asynchronous callback functions for promises and mutation observers are queued in the microtask queue before they are moved to the call stack for execution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Includes things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Promise.then()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Promise.catch()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Promise.finally()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;MutationObserver&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Synchronous JavaScript&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;JavaScript is synchronous, blocking, and single-threaded. This means the JavaScript engine executes code sequentially—one line at a time from top to bottom—in the exact order of the statements.&lt;/p&gt;

&lt;p&gt;Consider a scenario with three &lt;code&gt;console.log&lt;/code&gt; statements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;First line&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Second line&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Third line&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nl"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="nx"&gt;First&lt;/span&gt; &lt;span class="nx"&gt;line&lt;/span&gt;
&lt;span class="nx"&gt;Second&lt;/span&gt; &lt;span class="nx"&gt;line&lt;/span&gt;
&lt;span class="nx"&gt;Third&lt;/span&gt; &lt;span class="nx"&gt;line&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's examine another example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greetUser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shafiq Ur Rehman&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;userName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;greetUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;A new &lt;strong&gt;global execution context&lt;/strong&gt; is created and pushed onto the call stack. This is the main execution context where the top-level code runs. Every program has only one global execution context, and it always stays at the bottom of the call stack.&lt;/li&gt;
&lt;li&gt;In the global execution context, the &lt;strong&gt;memory creation phase&lt;/strong&gt; starts. In this phase, all variables and functions declared in the program are allocated space in memory (called the variable environment). Since we don’t have variables declared in the global scope, only the functions will be stored in memory.&lt;/li&gt;
&lt;li&gt;The function &lt;code&gt;getName&lt;/code&gt; is stored in memory, with its reference pointing to the full function body. The code inside it isn’t executed yet—it will run only when the function is called.&lt;/li&gt;
&lt;li&gt;Similarly, the function &lt;code&gt;greetUser&lt;/code&gt; is stored in memory, with its reference pointing to its entire function body.&lt;/li&gt;
&lt;li&gt;When the &lt;code&gt;greetUser&lt;/code&gt; function is invoked, the code execution phase of the global execution context begins. A new execution context for &lt;code&gt;greetUser&lt;/code&gt; is created and pushed on top of the call stack. Just like any execution context, it first goes through the memory allocation phase.&lt;/li&gt;
&lt;li&gt;Inside &lt;code&gt;greetUser&lt;/code&gt;, the variable &lt;code&gt;userName&lt;/code&gt; is allocated space in memory and initialized with &lt;code&gt;undefined&lt;/code&gt;. (&lt;strong&gt;Note:&lt;/strong&gt; During memory creation, variables declared with &lt;code&gt;var&lt;/code&gt; are initialized with &lt;code&gt;undefined&lt;/code&gt;, while variables declared with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are set as &lt;em&gt;uninitialized&lt;/em&gt;, which leads to a reference error if accessed before assignment.)&lt;/li&gt;
&lt;li&gt;After the memory phase finishes, the code execution phase starts. The variable &lt;code&gt;userName&lt;/code&gt; needs the result of the &lt;code&gt;getName&lt;/code&gt; function call. So &lt;code&gt;getName&lt;/code&gt; is invoked, and a new execution context for &lt;code&gt;getName&lt;/code&gt; is pushed onto the call stack.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The function &lt;code&gt;getName&lt;/code&gt; allocates space for its parameter &lt;code&gt;name&lt;/code&gt;, initializes it with &lt;code&gt;undefined&lt;/code&gt;, and then assigns it the value &lt;code&gt;"Shafiq Ur Rehman"&lt;/code&gt;. Once the &lt;code&gt;return&lt;/code&gt; statement runs, that value is returned to the &lt;code&gt;greetUser&lt;/code&gt; context. The &lt;code&gt;getName&lt;/code&gt; execution context is then popped off the call stack. Execution goes back to &lt;code&gt;greetUser&lt;/code&gt;, where the returned value is assigned to &lt;code&gt;userName&lt;/code&gt;. Next, the &lt;code&gt;console.log&lt;/code&gt; statement runs and prints:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, Shafiq Ur Rehman!
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Once done, the &lt;code&gt;greetUser&lt;/code&gt; The execution context is also popped off the call stack.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, the program returns to the global execution context. Since there’s no more code left to run, the global context is popped off the call stack, and the program ends.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Asynchronous JavaScript&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Unlike synchronous operations, asynchronous operations don't block subsequent tasks from starting, even if the current task isn't finished. The JavaScript engine works with Web APIs (like setTimeout, setInterval, etc.) in the browser to enable asynchronous behavior.&lt;/p&gt;

&lt;p&gt;Using Web APIs, JavaScript offloads time-consuming tasks to the browser while continuing to execute synchronous operations. This asynchronous approach allows tasks that take time (like database access or file operations) to run in the background without blocking the execution of subsequent code.&lt;/p&gt;

&lt;p&gt;Let’s break this down with a &lt;code&gt;setTimeout()&lt;/code&gt; example. (I’ll skip memory allocation here since we already covered it earlier.)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;first&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;second&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;third&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here’s what happens when this code runs:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The program starts with a &lt;strong&gt;global execution context&lt;/strong&gt; created and pushed onto the call stack.&lt;/li&gt;
&lt;li&gt;The first line &lt;code&gt;console.log("first")&lt;/code&gt; runs. It creates an execution context, prints &lt;code&gt;"first"&lt;/code&gt; to the console, and then is popped off the stack.&lt;/li&gt;
&lt;li&gt;Next, the &lt;code&gt;setTimeout()&lt;/code&gt; function is called. Since it’s a &lt;strong&gt;Web API provided by the browser&lt;/strong&gt;, it doesn’t run fully inside the call stack. Instead, it takes two arguments: a callback function and a delay (3000ms here). The browser registers the callback function in the Web API environment, starts a timer for 3 seconds, and then &lt;code&gt;setTimeout()&lt;/code&gt; itself is popped off the stack.&lt;/li&gt;
&lt;li&gt;Execution moves on to &lt;code&gt;console.log("third")&lt;/code&gt;. This prints &lt;code&gt;"third"&lt;/code&gt; immediately, and that context is also popped off.&lt;/li&gt;
&lt;li&gt;Meanwhile, the callback function from &lt;code&gt;setTimeout&lt;/code&gt; is sitting in the Web API environment, waiting for the 3-second timer to finish.&lt;/li&gt;
&lt;li&gt;Once the timer completes, the callback doesn’t go straight to the call stack. Instead, it’s placed into the &lt;strong&gt;callback queue&lt;/strong&gt;. This queue only runs when the call stack is completely clear. So even if you had thousands of lines of synchronous code after &lt;code&gt;setTimeout&lt;/code&gt;, they would all finish first.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;event loop&lt;/strong&gt; is the mechanism that keeps watching the call stack and the queues. When the call stack is empty, the event loop takes the callback from the queue and pushes it onto the stack.&lt;/li&gt;
&lt;li&gt;Finally, the callback runs: &lt;code&gt;console.log("second")&lt;/code&gt; prints &lt;code&gt;"second"&lt;/code&gt; to the console. After that, the callback function itself is popped off, and eventually, the global execution context is cleared once everything has finished.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;JavaScript runs code synchronously but can handle async tasks using browser Web APIs. Knowing how the engine works under the hood is key to mastering the language.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;“Let me know your thoughts in the comments,”&lt;/em&gt; or &lt;em&gt;“Follow me for more JavaScript insights.”&lt;/em&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
