AI Agents

An LLM with hands. What could go wrong?

▶ the lesson · narrated sketch video · tap for sound

the intro

An agent is an LLM in a loop with tools: the model decides which tool to call, sees the result, and continues until the task is done. The failure modes are loops and hallucinated arguments, so real agents need iteration limits, logging, and validation.

fun fact ✦

Most “AI agents” in production are one prompt, one tool, and a very confident marketing page.

the docs

the deep dive

the ideas behind the video — concepts, diagrams, a walkthrough and the traps to avoid

A chatbot answers and stops. An agent answers, calls a function, reads the result, and decides what to do next — a while-loop wrapped around the model, with tools as its hands. The loop is where both the magic and the money live: every turn costs tokens, and a confused agent will happily orbit forever. The craft is designing tools and stop conditions it cannot get lost in.

the loop is where the magic and the money are

LLM thinkstool runsresult readcap reached?
the loop that makes it an agent — and the exit that makes it safe

key terms

tool

A function the model may call: a clear name, a description it can read, typed parameters. The description is the documentation.

search_orders(email: string)

agent loop

Model decides, tool runs, result returns, model continues until done. One call is a chatbot; the loop is the agent.

one pass = chatbot, loop = agent

MCP

Model Context Protocol: an open standard for plugging tools and data into any compliant model client.

write once, use with any client

iteration cap

A max on loop turns. Without it, a confused agent orbits until your budget does.

5-10 turns plus a failure path

how it works

  1. 1
    Write tools like an API

    Clear names, honest descriptions, typed parameters. A vague description produces hallucinated arguments.

  2. 2
    Run the loop

    The model picks a tool, you execute it, and the result goes back into the conversation history.

  3. 3
    Log every step

    Prompt, tool call, result, decision. When it derails, the log is the only witness.

  4. 4
    Cap and bail out

    Max iterations and max tokens, plus a failure path that returns something useful instead of throwing.

the code, explained

the agent loop, no framework required

1for (let i = 0; i < 10; i++) {
the cap: a stuck agent stops burning money
2 const plan = await llm(history, tools)
3 if (plan.done) return plan.answer
the only clean exit is the model deciding it is done
4 const r = await run(plan.tool, plan.args)
5 history.push({ role: "tool", data: r })
the result goes back in so the model can react
6}
7throw new Error("iteration cap hit")

side by side

one-call chatbot

  • one prompt, one reply
  • predictable cost per user
  • cannot act, only talk
VS

agent loop

  • plans multi-step work
  • calls tools mid-answer
  • cost scales with turns
  • needs caps, logs, retries
⚠️ watch out

The unbounded loop is the classic agent incident: a confused model retries a failing tool forever, at token prices, overnight. Cap iterations at 5-10, cap tokens per turn, and make the failure path return something useful instead of throwing.

quick answers

Do I need an agent framework?

No. The loop is a while-statement and an array. Frameworks help with orchestration, but start hand-rolled so you understand every step.

What is MCP, in one line?

An open protocol so a tool you build once works with any compliant model client, instead of one integration per vendor.

When is a plain LLM call enough?

More often than the hype says. No state, no side effects, one step — skip the loop and ship the call.

quick hits about AI Agents

short answers · interviews, tips, trivia & hidden gems

🎤 interview

What actually makes it an agent?watch ↗

📚 tutorial

How do you design a tool?watch ↗

🔧 useful

Log every agent step?watch ↗

💎 deep

Why cap agent loops?watch ↗

🧠 trivia

Are agents everywhere?watch ↗

🆕 update

What is MCP?watch ↗