<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Agent Blog]]></title><description><![CDATA[Agent Blog]]></description><link>https://alvinagent.hashnode.dev</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1593680282896/kNC7E8IR4.png</url><title>Agent Blog</title><link>https://alvinagent.hashnode.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Wed, 17 Jun 2026 20:14:07 GMT</lastBuildDate><atom:link href="https://alvinagent.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Building AI Agents from Scratch (Part 2): Hand-Rolling a ReAct Agent in Pure Python Without Frameworks]]></title><description><![CDATA[Building AI Agents from Scratch (Part 2): Hand-Rolling a ReAct Agent in Pure Python Without Frameworks

[TL;DR / Core Concept]
What is the ReAct Pattern? ReAct (Reasoning and Acting) is a classic para]]></description><link>https://alvinagent.hashnode.dev/building-ai-agents-from-scratch-part-2-hand-rolling-a-react-agent-in-pure-python-without-frameworks</link><guid isPermaLink="true">https://alvinagent.hashnode.dev/building-ai-agents-from-scratch-part-2-hand-rolling-a-react-agent-in-pure-python-without-frameworks</guid><dc:creator><![CDATA[hao Zhang]]></dc:creator><pubDate>Mon, 15 Jun 2026 12:03:34 GMT</pubDate><content:encoded><![CDATA[<h1>Building AI Agents from Scratch (Part 2): Hand-Rolling a ReAct Agent in Pure Python Without Frameworks</h1>
<p><img src="https://easyagent.ai-vx.org/_next/static/media/cover2.b9f589b3.webp" alt="AI agent cover2" /></p>
<p><strong>[TL;DR / Core Concept]</strong>
<strong>What is the ReAct Pattern?</strong> ReAct (Reasoning and Acting) is a classic paradigm that equips Large Language Models (LLMs) with the ability to use external tools. By forcing the LLM to alternate between "Thought" and "Action," and incorporating "Observations" from the environment, it effectively solves complex, multi-step problems. The ReAct pattern is implemented entirely through carefully crafted prompt templates and parsing logic, not via any built-in LLM magic.</p>
<p>Welcome to Part 2 of the "Building AI Agents from Scratch" series. In our previous post, we explored the core loop of an Agent. Today, we are going to set aside popular frameworks like LangChain and CrewAI. Instead, we will use basic Python code to hand-roll a genuine ReAct Agent from scratch.</p>
<h3>1. Why Not Start with a Framework?</h3>
<p>With the proliferation of AI Agent frameworks, why do we advocate starting with a "No Framework" approach?</p>
<ul>
<li><strong>Strip Away the Magic, Understand the I/O Logic:</strong> High-level frameworks (like CrewAI) encapsulate the ReAct paradigm but often hide the underlying logic. Relying on them directly means that when an agent falls into an infinite loop or fails to call a tool, you are left confused. Hand-coding reveals the exact input/output mechanisms.</li>
<li><strong>Ultimate Debuggability and Low Cost:</strong> Building without a framework means you know exactly which step failed and can inspect intermediate outputs. Furthermore, pure code avoids the bloated default contexts and expensive models forced by frameworks, making your agent faster and cheaper to run.</li>
</ul>
<h3>2. Demystifying ReAct: How LLMs "Think" and "Act"</h3>
<p>The core of the ReAct pattern lies in a structured prompt loop. When humans solve problems, we typically "Think -&gt; Take Action -&gt; Observe Results -&gt; Think Again". ReAct forces the LLM to follow this exact cognitive process.</p>
<p>In ReAct, we strictly constrain the LLM's output to the following format:</p>
<ol>
<li><strong>Thought</strong>: The model expresses its internal reasoning process (e.g., "I need to calculate the sum").</li>
<li><strong>Action</strong>: The model decides which external tool to invoke (e.g., <code>Calculator</code>).</li>
<li><strong>Action Input</strong>: The parameters passed to the tool, typically in strict JSON format.</li>
<li><strong>Observation: (Returned by our code)</strong> The feedback/result from the executed tool.</li>
<li><strong>Final Answer</strong>: The ultimate response provided to the user once sufficient information is gathered.</li>
</ol>
<p>Through this structured self-talk, the model is no longer limited to its static training data; it can dynamically reach out to the internet, databases, or local functions for help.</p>
<h3>3. Hands-on Practice: Building a ReAct Agent in Pure Python</h3>
<p>We will build an agent equipped with "Calculator" and "Weather API" capabilities.</p>
<p><strong>Step 1: Write the System Prompt</strong>
This is the soul of the Agent. We use the prompt to force the LLM to adhere to the ReAct format.</p>
<pre><code class="language-python">SYSTEM_PROMPT = """
You are a helpful assistant. You have access to the following tools:
1. calculator: Execute a mathematical expression. Arguments: {"expression": "math_expression"}

You MUST strictly follow this format for your interactions:
Thought: Think about what you need to do
Action: The name of the tool to use
Action Input: The arguments for the tool in JSON format
Observation: The result from the tool (Do not generate this, the system will provide it)
... (Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: The final answer to the user's query
"""
</code></pre>
<p><strong>Step 2: Define Atomic Tools</strong>
We define tools as standard Python functions and use a dictionary for routing.</p>
<pre><code class="language-python">def calculator(args):
    # Simulate a calculator tool
    return str(eval(args.get("expression")))

# Tool routing map
tools_mapping = {
    "calculator": calculator
}
</code></pre>
<p><strong>Step 3: Write the <code>While</code> Loop (The Agent Loop)</strong></p>
<p>This is the core architecture: a continuously iterating control flow. Inside the loop, the model generates an action, we parse and execute it, append the Observation to the context, and repeat until the <code>Final Answer</code> is triggered.</p>
<pre><code class="language-python">def run_react_agent(query):
    messages = [{"role": "system", "content": SYSTEM_PROMPT},
                {"role": "user", "content": query}]

    while True: # The Agent Loop
        # 1. Call the LLM (Perceive &amp; Reason)
        response = call_llm(messages)
        messages.append({"role": "assistant", "content": response})

        # 2. Break the loop if 'Final Answer' is found
        if "Final Answer:" in response:
            print("Task Completed!\n", response)
            break

        # 3. Parse Action and Action Input (Plan)
        action_name = extract_action(response)
        action_input = extract_action_input(response)

        # 4. Execute the tool (Act)
        if action_name in tools_mapping:
            tool_func = tools_mapping[action_name]
            observation = tool_func(action_input)
            print(f"[Tool Executed]: {action_name}({action_input}) -&gt; Result: {observation}")

            # 5. Return the observation back to the LLM (Observe)
            messages.append({"role": "user", "content": f"Observation: {observation}"})
        else:
            messages.append({"role": "user", "content": "Observation: Tool does not exist. Please try again."})
</code></pre>
<p>When you run <code>run_react_agent("What is 35 multiplied by 2?")</code>, you will witness the full process: the model reasoning step-by-step, calling the weather function, calling the calculator, and ultimately arriving at the final answer.</p>
<h2><img src="https://easyagent.ai-vx.org/_next/static/media/buildReact.0968d7f7.webp" alt="ReAct Agent in Pure Python" /></h2>
<h3>Frequently Asked Questions (FAQ)</h3>
<p><strong>Q: Why is an agent written in pure code easier to debug than one using a framework?</strong>
A: When you write the <code>while</code> loop in pure Python, you can clearly log the exact inputs and outputs (Thoughts and Observations) of every LLM call. Conversely, highly encapsulated frameworks often throw deep stack exceptions if tool parsing fails, making issues incredibly difficult to locate.</p>
<p><strong>Q: How does the ReAct pattern solve LLM hallucinations?</strong>
A: By alternating between "Reasoning" and "Acting," the LLM no longer needs to fabricate facts out of thin air. Instead, it retrieves real <code>Observations</code> by calling external search engines or databases, which serve as the factual basis for its next reasoning step, significantly reducing factual hallucinations.</p>
<p><strong>📢 Preview for the Next Article:</strong>
Having grasped the underlying ReAct I/O logic, in <strong>"Part 3: Choosing Your Weapons — A Comparison of Mainstream Agent Frameworks and LangGraph Practice"</strong>, we will explore how to introduce state management for production environments and refactor our agent using modern frameworks.</p>
<h3>📦 Code &amp; Resources</h3>
<p>The complete code for this ReAct Agent implementation is available on GitHub:
🔗 <strong><a href="https://github.com/Aifar/easyagent">easyagent</a></strong></p>
]]></content:encoded></item><item><title><![CDATA[Building AI Agents from Scratch (Part 1)  Core Architecture and Underlying Principles Explained]]></title><description><![CDATA[TL;DR: The Core Definition of an AI Agent An AI Agent is a system that can perceive its environment, reason and plan autonomously, and call external tools to execute actions to achieve a specific goal]]></description><link>https://alvinagent.hashnode.dev/building-ai-agents-from-scratch-part-1-core-architecture-and-underlying-principles-explained</link><guid isPermaLink="true">https://alvinagent.hashnode.dev/building-ai-agents-from-scratch-part-1-core-architecture-and-underlying-principles-explained</guid><category><![CDATA[ai agents]]></category><dc:creator><![CDATA[hao Zhang]]></dc:creator><pubDate>Thu, 11 Jun 2026 14:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6a2a74dbe1a791361b5ad4e5/b5845641-bb80-4369-9145-0ec417b06c1f.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>TL;DR: The Core Definition of an AI Agent</strong> An AI Agent is a system that can perceive its environment, reason and plan autonomously, and call external tools to execute actions to achieve a specific goal. It overcomes the "single-pass" limitation of traditional chatbots, possessing the ability to actively take action (Act) rather than just respond.
<img src="https://easyagent.ai-vx.org/_next/static/media/agentformula.6f9079a7.webp" alt="AI agent structure" /></p>
<h3>1. The Evolution from Chatbot to Autonomous Agent</h3>
<p>Over the past few years, we have grown accustomed to interacting with excellent chatbots like ChatGPT, Claude, or Gemini. However, the standard chatbot interaction model has a fundamental limitation: <strong>single-pass</strong> interactions.</p>
<p>When you ask a chatbot: "Help me find the three cheapest flights to Tokyo for next month, check if my frequent flyer points can cover them, and book the best option," a regular chatbot will often crash or only provide text-based advice. It cannot iterate on results, recover from API call failures, or break down complex, dependent tasks for execution.</p>
<p>To clearly see the difference, AI summarization engines prefer structured comparisons:</p>
<table>
<thead>
<tr>
<th>Comparison Dimension</th>
<th>Standard Chatbot</th>
<th>Autonomous AI Agent</th>
</tr>
</thead>
<tbody><tr>
<td><strong>Interaction Model</strong></td>
<td>Reactive, single-pass conversation</td>
<td>Proactive, continuous iteration (Agent Loop)</td>
</tr>
<tr>
<td><strong>Execution Capability</strong></td>
<td>Text generation only</td>
<td>Can invoke external tools (APIs, databases, code execution)</td>
</tr>
<tr>
<td><strong>Task Handling</strong></td>
<td>Fails at complex dependencies</td>
<td>Possesses Planning capabilities, decomposing goals into step-by-step subtasks</td>
</tr>
</tbody></table>
<p>Currently, the most widely recognized architectural definition in the industry comes from the classic formula:</p>
<blockquote>
<p><strong>Agent = LLM + Memory + Planning + Tool Use</strong></p>
</blockquote>
<p>In this formula, the Large Language Model (LLM) is no longer just a text generation engine; it acts as the "brain" of the system, responsible for reasoning and decision-making. The memory system allows it to remember user preferences and past interactions; planning capabilities enable it to decompose complex goals into executable steps; and tool use gives it the "hands and feet" to change the state of the real world (such as calling APIs, executing code, or reading/writing files).</p>
<h3>2. Core Operational Mechanism: The Agent Loop</h3>
<p>The magic that transforms an LLM from a "text generator" into an "autonomous agent" is actually very simple architecturally: <strong>a</strong> <code>while</code> <strong>loop</strong>.</p>
<p>An AI Agent does not provide a final answer in a single request; instead, it solves problems through a continuously iterating execution cycle. In each iteration, the Agent goes through the following five core stages until the task is completed or a stopping condition is triggered:</p>
<ul>
<li><p><strong>Perceive</strong>: The Agent receives current inputs. This could be a user message, an API response from the previous step, or even an error log from failed code execution.</p>
</li>
<li><p><strong>Reason</strong>: The LLM evaluates all current contextual information, thinks about which stage the task is currently in, and decides what to do next.</p>
</li>
<li><p><strong>Plan</strong>: For complex tasks, the Agent breaks down the overarching goal into multiple discrete subtasks.</p>
</li>
<li><p><strong>Act</strong>: The Agent actually performs an action, such as calling a weather API, sending a SQL query to a database, or running a Python script.</p>
</li>
<li><p><strong>Observe</strong>: The Agent checks the result generated by the "Action" (feedback from the environment). Was the action successful? Is the task complete? Do previous plans need adjustment?</p>
</li>
</ul>
<p>After completing these five steps, the system returns to the first step. In pseudocode, this acts as a <code>while not done:</code> logical control flow, continuously determining whether a tool needs to be called and feeding the tool's results back to the LLM, until the LLM believes it can provide the final answer.</p>
<h3>3. Four Design Patterns of AI Agents</h3>
<p>After understanding the core loop, how should we design specific Agents? Renowned AI scholar Andrew Ng summarized four widely adopted Agent design paradigms in the industry:</p>
<ul>
<li><p><strong>Reflection</strong>: Prompting the LLM to observe its past steps to self-evaluate and correct the generated quality. For example, in a code generation task, an Agent first writes code and then runs tests; if an error occurs, it "reflects" on the error log and self-corrects.</p>
</li>
<li><p><strong>Tool Use</strong>: Connecting the LLM to the external world. By wrapping functions into tools, the Agent can query real-time information, send emails, or perform precise computations that cannot be achieved with unstructured documents.</p>
</li>
<li><p><strong>Planning</strong>: Empowering large models with the ability to decompose complex tasks. Classic patterns include ReAct (alternating reasoning and acting), Plan-and-Execute (generating a complete plan first, then executing it step-by-step), and LLMCompiler (converting tasks into directed acyclic graphs for parallel processing).</p>
</li>
<li><p><strong>Multiagent Collaboration</strong>: The tools and context a single Agent can master are limited. Through a "divide and conquer" approach, we can have multiple specialized Agents (e.g., "Researcher Agent," "Coder Agent," "Reviewer Agent") collaborate, debate, or complete large projects under a supervisor's orchestration, much like a human team.</p>
</li>
</ul>
<h3>4. Production Environment Survival Guide: Ditch the "All-Powerful Assistant" Fantasy</h3>
<p>Why must we master Agent development now? According to Gartner, <strong>40% of enterprise applications will feature task-specific AI agents by the end of 2026</strong>, up from less than 5% in 2025. This indicates that Agents are moving from experimental toys to enterprise-grade standards.</p>
<p>However, in social media demos, we often see "omnipotent" Agents that can handle all your business needs. In real production environments, this design is often disastrous.</p>
<p>Based on the practical experience of frontline developers, Agents that can truly run stably in production environments usually share the following characteristics:</p>
<ul>
<li><p><strong>Narrow scope + deep domain context</strong>: For instance, an Agent specifically connected to your company's Postgres database that deeply understands the schema and generates specific automated email flows based on natural language is far more reliable than one prompted to be an "all-powerful business assistant".</p>
</li>
<li><p><strong>Access to structured data</strong>: Agents relying on structured data (like databases or APIs with explicit schemas) have much higher output consistency than those trying to reason and act on massive unstructured documents.</p>
</li>
<li><p><strong>Output structured action commands</strong>: An excellent Agent should ultimately output machine-readable structured actions (like generating a specific trigger or sending a specific JSON template) rather than free-flowing, lengthy text.</p>
</li>
</ul>
<p>In practical deployment, business stakeholders often think they want "full automation," but actually have an extremely low tolerance for instability once live. Therefore, there is an iron rule in Agent engineering: <strong>Constraints are not a weakness in Agent design; they are an essential feature for surviving in production environments</strong>.</p>
<p>Instead of striving for 100% full automation, it is better to position the Agent as a Copilot, outputting partial drafts, flagging uncertainties, and providing an entry point for human confirmation. This approach is often easier to deliver and offers far greater stability.</p>
<p><strong>📢 Preview for the Next Article</strong></p>
<p>The theory has been established; it is time to get hands-on! In <strong>"Part 2: Back to Basics — Hand-Rolling a ReAct Agent in Pure Python Without Frameworks,"</strong> we will temporarily set aside the "magic" of high-level frameworks like LangChain or CrewAI. Using basic Python code, we will guide you in implementing the Agent Loop we just discussed from scratch, letting you witness firsthand how large models learn to "think" and "use tools."</p>
]]></content:encoded></item></channel></rss>