Agent Loops

You will understand what an agent loop is, why it exists, and how EngineerPrep uses one to power its AI tutor — so that next time you read or write agent code, you know exactly what each piece is doing and why.

Why This Matters

Picture a Monday morning. A student opens EngineerPrep, types "Explain the Java memory model," and the AI tutor comes back with something half-right and half-confused. You pull the logs. The model was given the question, it tried to answer from memory, noticed its own answer was shaky, fetched a relevant lesson from the database, re-read its answer, found it still wasn't precise enough, fetched one more example, and only then wrote the final response. That sequence — try, check, fetch, try again, stop — happened automatically, in a loop, driven by the model itself. A plain API call can't do that. A single prompt can't do that. Something else is going on. The question this lesson answers: what is the loop that makes an AI keep working until it's done — and how do you build one?

The Simple Idea

Think about how a chef cooks a new dish for the first time. They don't read the recipe once and then cook blindly start to finish. They taste as they go. They decide "needs more salt" or "ready to plate" at each step. They loop: taste → decide → act → taste again → decide → act → stop when done. An agent loop is that same pattern applied to an AI model. Here is the loop in plain words: 1. The model thinks — it reads the question and decides what to do next. 2. The model acts — it either calls a tool (search the database, run a function) or writes a final answer. 3. If it called a tool, the result comes back and the loop starts again from step 1. 4. If it wrote a final answer, the loop stops. That's it. Think → act → observe the result → think again → act again → stop.…

See It in Action

Imagine the screen in front of you shows a single box labeled Agent Loop . Inside it, three smaller boxes sit in a triangle: Think , Act , and Observe . An arrow connects each one to the next, forming a circle. A fourth arrow points outward from Act labeled "Final Answer" — the exit. --- Step 1 — The student's question arrives. A message drops into the top of the loop: "What is the difference between synchronized and volatile in Java?" The Think box lights up. The model reads the question and decides: "I should search EngineerPrep's lesson database before I answer this — I want to be precise." --- Step 2 — The model acts: it calls a tool. The Act box lights up. The model emits a structured request: call searchLessons("synchronized vs volatile Java") . This is not a final answer. It's a tool call — an instruction to run a function and bring back the result. --- Step 3…