<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>BitSketch Academy blog</title>
    <link>https://bitsketchacademy.com/blog</link>
    <atom:link href="https://bitsketchacademy.com/blog/rss.xml" rel="self" type="application/rss+xml" />
    <description>One idea about how AI agents work, explained with a sketch and a bit of code. Every week or two, no hype.</description>
    <language>en</language>
    <lastBuildDate>Wed, 04 Nov 2026 00:00:00 GMT</lastBuildDate>
    <item>
      <title>An AI agent is just a while-loop</title>
      <link>https://bitsketchacademy.com/blog/ai-agent-is-a-while-loop</link>
      <guid isPermaLink="true">https://bitsketchacademy.com/blog/ai-agent-is-a-while-loop</guid>
      <pubDate>Wed, 04 Nov 2026 00:00:00 GMT</pubDate>
      <description>The whole thing in 20 lines of Python, and the three places it breaks.</description>
      <content:encoded><![CDATA[<p><em>Welcome to the BitSketch Academy blog. Every week or two I take one idea about how AI agents work and explain it with a sketch and a bit of code. No hype, no framework of the month. This is the first one, and it starts at the bottom.</em></p>
<p>Agent frameworks make it look complicated. There are planners, executors, memory modules, orchestrators, graphs of nodes. Read the docs for a weekend and you can still come away unsure what actually <em>happens</em> when you press run.</p>
<p>Here&#39;s the secret: underneath every one of them is the same small loop.</p>
<ol>
<li>Send the conversation so far to the model.</li>
<li>If the model asks to use a tool, run it and add the result to the conversation.</li>
<li>If it doesn&#39;t ask, it has answered. Stop.</li>
</ol>
<p>That&#39;s an agent. Planning, memory, sub-agents and guardrails are all things bolted onto this loop.</p>
<figure class="lesson-figure"><a href="https://bitsketchacademy.com/img/blog/ai-agent-is-a-while-loop/the-loop.webp" target="_blank" rel="noopener"><img src="https://bitsketchacademy.com/img/blog/ai-agent-is-a-while-loop/the-loop.webp" alt="The agent loop: the model decides whether it wants a tool; if yes, your code runs it and adds the result to messages; if no, that's the answer." width="1456" height="816" loading="lazy" decoding="async"></a></figure>
<h2>The whole thing</h2>
<p>Here it is in plain Python. <code>call_model</code> stands for whatever model API you use: it takes a list of messages and returns the model&#39;s reply.</p>
<div class="code-block"><button type="button" class="copy-btn" data-copy>Copy</button><pre><code class="language-python">def run_agent(task, tools, call_model):
    messages = [{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: task}]

    while True:
        reply = call_model(messages, tools=tools)
        messages.append(reply)

        if not reply.tool_calls:          # no tool requested: this is the answer
            return reply.content

        for call in reply.tool_calls:     # the model asked for one or more tools
            result = tools[call.name](**call.arguments)
            messages.append({
                &quot;role&quot;: &quot;tool&quot;,
                &quot;tool_call_id&quot;: call.id,
                &quot;content&quot;: str(result),
            })</code></pre></div>
<p>It&#39;s under 20 lines, and it&#39;s a real agent. Give it a tool that reads files and one that writes them, and you have the core of a coding assistant.</p>
<h2>One run, step by step</h2>
<p>Say the task is <em>&quot;What&#39;s 17% of the price of order #4411?&quot;</em> and the agent has one tool, <code>get_order</code>.</p>
<figure class="lesson-figure"><a href="https://bitsketchacademy.com/img/blog/ai-agent-is-a-while-loop/one-run.webp" target="_blank" rel="noopener"><img src="https://bitsketchacademy.com/img/blog/ai-agent-is-a-while-loop/one-run.webp" alt="One run, step by step: 1. you add the task. 2. the model asks for get_order(id=4411). 3. you add the result {&quot;price&quot;: 250}. 4. the model answers &quot;17% of $250 is $42.50.&quot; with no tool call, so the loop ends." width="1456" height="816" loading="lazy" decoding="async"></a></figure>
<p>Two model calls and one tool call. Notice what the model <em>didn&#39;t</em> do: it never touched your database. It asked, and your code did the work.</p>
<figure class="lesson-figure"><a href="https://bitsketchacademy.com/img/blog/ai-agent-is-a-while-loop/phone.webp" target="_blank" rel="noopener"><img src="https://bitsketchacademy.com/img/blog/ai-agent-is-a-while-loop/phone.webp" alt="The model asks for get_order(id=4411) over the phone; your code, which has the database, reads back {&quot;price&quot;: 250}." width="1456" height="816" loading="lazy" decoding="async"></a></figure>
<p>The best way I know to think about it: <strong>the model is a very smart colleague on the phone.</strong> They can&#39;t see your screen. They can only tell you what to do, and they only know what you read back to them.</p>
<h2>Three places it breaks</h2>
<p>The loop is simple, and it&#39;s also fragile. Read the code again and you&#39;ll find three problems hiding in it.</p>
<p><strong>1. The model has no memory.</strong> It remembers nothing between calls. The <code>messages</code> list <em>is</em> its memory, and you resend all of it every time. Long tasks make that list grow, so every step gets slower and more expensive, until the context is full of old tool output and the model starts losing the plot.</p>
<p><strong>2. The model decides when it&#39;s done.</strong> The loop ends only when the model stops asking for tools. You&#39;re trusting it to know when the job is finished. Sometimes it quits early and says &quot;done&quot; when nothing was done. Sometimes it never stops.</p>
<p><strong>3. There is no brake.</strong> <code>while True</code> will happily run all night if the model keeps asking for tools. Nothing in that code stops a loop that calls the same failing tool 400 times and burns through your API budget.</p>
<p>Every serious agent framework is, in large part, a set of fixes for these three problems. &quot;Max iterations&quot; is a brake on <code>while True</code>. &quot;Memory&quot; is code that edits <code>messages</code>. &quot;Tool schema&quot; is a description of <code>tools</code> sent to the model. Once you can see the loop, the framework stops being a black box.</p>
<figure class="lesson-figure"><a href="https://bitsketchacademy.com/img/blog/ai-agent-is-a-while-loop/three-bugs.webp" target="_blank" rel="noopener"><img src="https://bitsketchacademy.com/img/blog/ai-agent-is-a-while-loop/three-bugs.webp" alt="The same loop with three warnings: messages grows forever, the model decides when it's done, and while True has no brake." width="1456" height="816" loading="lazy" decoding="async"></a></figure>
<h2>Where to go from here</h2>
<p>I turned this into a short, free text course: <strong><a href="https://bitsketchacademy.com/courses/agent-loop-from-scratch">The Agent Loop, From Scratch</a>.</strong> It builds this loop one piece at a time, fixes each of the three problems above with code you can copy, and ends with a working agent you can point at your own codebase:</p>
<ol>
<li>The loop in 30 lines</li>
<li>Tools: how the model acts</li>
<li>Stopping on purpose: step limits, cost budgets and done-checks</li>
<li>Where the agent&#39;s state lives</li>
<li>Debugging from the trace</li>
<li>Project: a codebase Q&amp;A agent</li>
<li>Finding the loop in any framework</li>
</ol>
<p>👉 <a href="https://bitsketchacademy.com/courses/agent-loop-from-scratch/the-loop">Start reading, free</a></p>
<p>If you&#39;d rather watch than read, the same ideas come as hand-drawn Shorts on <a href="https://bitsketchacademy.com/youtube">YouTube</a>.</p>
<p>Next time: the contract between the model and your tools, which is where most beginner agent bugs live. Leave your email below so you don&#39;t miss it.</p>
]]></content:encoded>
      <category>AI agents</category>
      <category>LLM</category>
      <category>Python</category>
    </item>
  </channel>
</rss>
