You are watching v1 from 5 Oct 2026, an earlier version of this video. Back to the current version, v4

The Loop

Created by Abhyuday DesaiPublished , v1 of 4Runs 11 min 16 sec
1 view

About this video

The first build of Building AI Agents. The loop is a while loop: the task and the system prompt go to the model, our code runs whatever tool the model asks for, adds the result to the conversation and sends it back. When the model replies without asking for a tool, that reply is the answer. The whole agent is one file, agent.py, and we walk it part by part.

You'll see:

  • The one tool the loop needs: bash, how much a single shell command can do, and why you run an agent like this in a container
  • agent.py in five parts: the setup, the bash tool, the prompt and the task, run_agent, and main
  • The bash tool: a subprocess in the sandbox, a thirty-second limit, trimmed output, and the schema the model actually sees
  • The system prompt: the verification rules the agent carries into every task, and the change hygiene it asks for
  • run_agent: the conversation list, the call to the model, running the tool calls, and the condition that ends the loop
  • A real run on GLM 5.2: the table-of-contents feature the agent added to md2html, the token bill, and all thirty-two tool calls in the order it made them
  • The finished feature on a real document: the agent's md2html renders a contributor guide to a page, with a nested table of contents whose links jump to their sections

The next video replaces the one bash tool with proper tools for working with files.

What changed

  1. v406 Oct 2026abhydesai12 min 39 secbe9e224

    Pacing improvements through the walk of agent.py and the ending. The narration is unchanged.

    Same chapters and transcript.

    0 views0 likes on this version
  2. v306 Oct 2026abhydesai12 min 37 sec17b5f24

    The video now ends by showing the feature actually work. The agent's md2html renders a real contributor guide to a page, and the page opens with a nested table of contents whose links jump to their sections. It closes on what the agent did unaided: found its way around a codebase it had never seen, wrote the extension, updated the two fixtures the change affected, and tested the result. With the payoff carrying that, the sign-off is now short: the agent works, and the next video gives it better tools. The code is shown at a newer commit, wrapped to 86 columns, with render.py and the sample document in the tree. Narration and pacing polish: a number of lines reworded for tone and clarity, and the walk through agent.py now moves between definitions by the file's outline.

    Chapters: one added. Transcript: 21 of 102 lines changed. 30 sec longer.

    Show the differencesHide the differences
    Chapter added
    The page
    1. So on your own repositories, run it inside a Docker container, or on a machine you're willing to wipe.

      So with your own repositories, run it inside a Docker container or a virtual machine, kept apart from the rest of your computer.

    2. Here's the main user task: the job we hand the agent to complete.

      Okay, that was our system prompt. Now here's the main user task: the job we hand the agent to complete.

    3. Here's the task. It asks for a real feature in md2html: a table of contents. Every heading gets an id, and a marker line becomes a list of links to those headings.

      Here's the task. It asks for a real feature in md2html: a table of contents. Every heading gets an ID, and a marker line becomes a list of links to those headings.

    4. Because every heading now gets an id, the expected output of two older fixtures changed, and it updated both.

      Because every heading now gets an ID, the expected output of two older fixtures changed, and it updated both.

    5. Then two older fixtures failed, because every heading now gets an id. It rewrote both expected files and ran the tests again.

      Then two older fixtures failed, because every heading now gets an ID. It rewrote both expected files and ran the tests again.

    6. That's the whole agent for this video.

      The loop worked, even with one blunt tool. It added a real feature to md2html.

      So the next video is tools. We keep the same loop and give it better tools. I'll see you there.

      Now let's try the table of contents on a real document.

      This is a short contributor guide, written in Markdown, with headings at three levels.

      This marker near the top is where the table of contents should go.

      So we expect a nested list of links to these headings.

      Let's render it with the agent's version of md2html.

      This runs md2html from the agent's sandbox on our guide, and writes the page next to it.

      The new page is in the examples folder. Let's open it.

      At the top is the table of contents, nested the same way as the headings.

      Each link jumps to its section, through the ID its heading was given.

      Our agent built all of this on its own.

      It found its way around a codebase it had never seen.

      It wrote the extension, and updated the two fixtures the change affected.

      And it tested its work: it added tests, ran the whole suite, and all sixty-one pass.

      So now, we have a working agent.

      In the next video, we give our agent better tools so it can carry out harder tasks.

      I'll see you there.

    0 views0 likes on this version
  3. v206 Oct 2026abhydesai12 min 7 sec424bd83

    Restructured so the task is explained right before the run, not three scenes earlier. A new bookkeeping scene points at the task, defers it and covers the telemetry; the task scene itself now sits after main and ends by starting the run; the run scene reviews the finished output. Thirteen scenes became fifteen, renumbered to match the order. Cut: the walk through the sample Markdown file - its headings, the marker line and the repeated headings - since the task box already states what it showed. Added: main now says the agent works on a copy, so the original project never changes; and a silent "Try it yourself" card closes the video. Narration: the test files are named as fixtures where the task first points at them, the close drops its "Not a new loop, just better tools for the same one" fragment for a plain line, and over-long sentences are split. Visuals and timing: the terminal runs are re-transcribed on the capture's own clock at 1x, visible clicks keep their click sound, scroll targets land clear of the top border, the big terminal boxes scroll to fit whole, the bill carries a note that your own run will differ, and the Popen box covers the whole call.

    Chapters: two renamed, one added. Transcript: 12 of 102 lines changed. 51 sec longer.

    Show the differencesHide the differences
    Chapter renamed
    The task The task, for later, and the telemetry
    Chapter renamed
    The run The task and the run
    Chapter added
    The run's result
    1. It asks for a real feature in md2html: a table of contents. Every heading gets an id, and a marker line becomes a list of links to those headings.

      It points the agent at a pair of test files with the expected output, and asks for new tests with the old ones still passing.

      It's a short guide with a few headings, and the last two have the same text.

      This is the marker. The table of contents goes where it is, and the repeated headings need ids that differ.

      We'll come back to it just before we run the agent. For now, let's continue with the rest of the script.

    2. And now, the loop. The same loop from the opening scene, now in Python.

      And now, the loop. The same loop from the start of this video, now in Python.

    3. And then we append the result back into the messages list. That one line is what makes this a loop, rather than a one-shot call.

      And then we append the result back into the messages list.

      That one line is what makes this a loop, rather than a one-shot call.

    4. Then the agent is supposed to do all its work inside sandbox.

      The agent works on this copy, so the original project never changes, and all its work is meant to stay in the sandbox.

    5. Now let's run it.

      Before we run it, let's look at the task we'll give it.

      Here's the task. It asks for a real feature in md2html: a table of contents. Every heading gets an id, and a marker line becomes a list of links to those headings.

      It points the agent at a pair of test files, called fixtures, with the expected output. It also asks for new tests, with the old ones still passing.

    6. So the next video is tools. Not a new loop, just better tools for the same one. I'll see you there.

      So the next video is tools. We keep the same loop and give it better tools. I'll see you there.

    0 views0 likes on this version
  4. v105 Oct 2026abhydesai11 min 16 secfe885a1

    First published cut

    1 view1 like on this version