AI Engineer Roadmap All lessons Interactive Quiz MernStackDev GitHub

What is an AI Agent?

Play the interactive lesson: The agent loopRead on GitHub
On this page
  1. Explain it like I'm new
  2. Watch it
  3. How it works
  4. Code example
  5. When to use it / when not to
  6. Common mistakes
  7. Recap
  8. Interview questions
  9. Go deeper

Level: Beginner · Reading time: 7 min · Phase: 4 · Agentic AI

In one line: An AI agent is an LLM running in a loop that decides which actions (tool calls) to take, sees the results, and keeps going until the goal is done.

Before you start: Nothing, this is a starting point. You only need to know that an LLM is a program that takes text in and writes text back.

Explain it like I'm new

A plain chatbot is like asking a friend a question by text message: you ask, they reply, done. An agent is like giving an assistant a task: "Find three flights to Delhi under ₹8,000 and add the cheapest to my calendar." The assistant searches, reads results, compares, then uses the calendar, deciding each next step based on what they just learned.

Words to know:

The model never runs code itself. It asks for a tool; your code runs it. That boundary is where you put safety checks.

Watch it

The agent loop: An agent is a model plus tools plus memory, in a loop.
The agent loop: An agent is a model plus tools plus memory, in a loop.

It plays on a loop. Read the caption in the box, one step at a time. Want to go at your own pace? Step through it with Previous and Next.

How it works

  1. Send the model the goal, the conversation so far, and a list of available tools. Each tool has a name, a description, and an input schema (a description of the arguments it accepts).
  2. The model replies with either a final answer or one or more tool calls (tool name + arguments).
  3. Your code validates and runs each tool call.
  4. You send the tool results back as new messages.
  5. Repeat until the model gives a final answer or you hit a step limit.
flowchart LR
    G[goal] --> M[LLM decides]
    M -- tool call --> T[your code runs tool]
    T -- result --> M
    M -- final answer --> D[done]
The agent loop
The agent loop

Agent vs workflow. Anthropic's guide Building effective agents draws a useful line:

Workflows are more predictable and cheaper. Agents are more flexible when you cannot predict the steps in advance.

Worked example

Goal: *"What is 12*7+5? Save the answer as a note."* This is the real output of python 04-agentic-ai/code/agent.py (the model is a scripted stand-in, so no API key is needed):

step 1: calculator({"expression": "12*7+5"}) -> 89
step 2: save_note({"text": "12*7+5 = 89"}) -> Saved note #1.
12*7+5 is 89, and I saved it as a note.

Trace it:

  1. Your code sends the goal and the tool list to the model.
  2. The model replies with a tool call: calculator with expression = "12*7+5". It does no maths itself.
  3. Your code runs calculator and gets 89. It sends 89 back to the model as a tool result.
  4. The model sees the answer, but the goal also says to save it. So it asks for save_note with the text 12*7+5 = 89.
  5. Your code runs save_note, which returns Saved note #1., and sends that back.
  6. The model now has nothing left to do. It replies with plain text and no tool call. That reply is the final answer, and the loop ends.

Notice that the model picked the second step because of the first result. That choosing is what makes it an agent.

Code example

The core loop from agent.py (runs offline with a scripted model):

def run_agent(llm, user_goal, max_steps=6):
    messages = [{"role": "user", "content": user_goal}]
    for step in range(1, max_steps + 1):
        reply = llm.step(messages, TOOL_SPECS)
        messages.append({"role": "assistant", "content": reply["raw"]})
        if not reply["tool_calls"]:
            return reply["text"]                         # model says it is done
        results = []
        for call in reply["tool_calls"]:
            fn = TOOLS.get(call["name"])
            out = fn(**call["input"]) if fn else f"Error: unknown tool {call['name']}"
            results.append({"type": "tool_result", "tool_use_id": call["id"], "content": out})
        messages.append({"role": "user", "content": results})
    return "Stopped: reached max steps without finishing."   # always cap the loop

Run it: python 04-agentic-ai/code/agent.py. With ANTHROPIC_API_KEY set and pip install anthropic, it also runs against Claude.

When to use it / when not to

SituationBetter choice
Steps are known and fixed (extract → classify → store)Workflow
One question, answer from documentsRAG, no agent
Open-ended, steps depend on results (research, debugging, multi-tool tasks)Agent
High-risk actions (payments, deleting data)Agent only with approvals and strict guardrails

Common mistakes

Recap

If you remember only three things:

Interview questions

1. What is the difference between an LLM call and an agent? <details><summary>Answer</summary> An LLM call is one request and one response. An agent puts the LLM in a loop with tools: it decides an action, observes the result, and decides again until the goal is met or a limit is reached. </details>

2. When would you choose a workflow over an agent? <details><summary>Answer</summary> When the steps are known in advance. Workflows are more predictable, cheaper, easier to test, and easier to debug. Use an agent when the path depends on intermediate results. </details>

3. Who executes the tools in an agent, the model or your code? <details><summary>Answer</summary> Your code. The model only outputs a structured request (tool name and arguments). This is why validation, permissions, and approvals belong in your code. </details>

Go deeper

Prev: Phase 4 overview · Next: Tool Calling · Back to phase: Phase 4