Skip to content

Courses / GenAI & LLM / Module 7

Module 7: Intro to agents and tool use (continues in Agentic AI)

6 h · theory 2 · hands-on 2 · project 1 · revision 1

You will be able to

  • Explain the agent loop: model, tools, observations, stop condition
  • Build a two-tool agent with step limits
  • Decide when a fixed workflow beats an agent
  • Know what the full Agentic course adds: LangGraph, MCP, multi-agent, approvals, evals

Capstone increment: An agent that looks up a vendor and drafts a follow-up for low-confidence extractions, behind a step limit.

Job-description skills: AI agents, Tool calling · Lab tools: openai, langgraph

This module is an introduction. Agentic AI covers it in full.

1. An agent is a loop

An agent is a model called in a loop with tools: it reads the task and everything so far, either calls a tool or answers, and every tool result is appended to the context before the next call. You built the core of this in Module 2. The new part is that the model, not your code, decides the sequence of steps.

The loop needs a stop condition beyond “the model answered”: a step limit, a token or cost budget, and detection of repeated identical calls. Without them, a confused model loops until your bill stops it.

Task + contextsystem, tools, historyModel calltool call or answer?toolApproval gateonly for risky actionsRun toolresult or errorappend the observation to the context, then call the model againanswer → stopAlso stop on: step limit, token or cost budget, repeated identical calls (a loop)
An agent is this loop. Everything else in the course (state, memory, MCP, multi-agent, evals) makes the loop safer or smarter.

2. Step through a real run

Use the stepper below. Each step is one model call, and each call resends the whole conversation, so input tokens and cost grow with every step. Break a tool and watch the model recover from an error message; make it loop and watch the step limit stop it.

Agent loop stepperTask: “The checkout API is slow. Find out why.” Step through the loop, then break it.
0 model calls · context now 640 tokens · run cost $0.00000
  1. User: The checkout API is slow. Find out why.
Guided experiments
  1. Run the happy path. How many model calls did it take, and why does each call cost more input tokens than the one before?
  2. Turn on “Log tool is down”. The error goes back to the model as an observation. What does it do next? What would happen if the loop crashed on tool errors instead?
  3. Turn on “Model keeps re-checking”. Raise the step limit to 12. What happens to context size and cost? This is why every agent needs a step limit and a budget (covered in depth in the Agentic AI course).
  4. Set the step limit to 3 on the happy path. The agent is stopped one step short of the answer. How would you pick a limit for a real task?
  5. The last step proposes a rollback but doesn’t do it. Why should that action wait for a human approval?
In production: Return tool errors to the model as observations (“ERROR 503: log service unavailable”) instead of raising. Models recover surprisingly well when they can see what went wrong.

3. Workflow or agent?

If you can write the steps down in advance, write them as code: a fixed workflow with an LLM call at the points that need language. It's cheaper, faster, deterministic and easy to test. Use an agent only when the path genuinely depends on what's discovered along the way: investigation, open-ended research, multi-step troubleshooting.

Many production “agents” are workflows with one or two decision points. Start there. You can always give the model more freedom later; taking freedom away after users depend on it is harder.

Common misconception: “Agents are the more advanced solution, so they’re better.” They trade predictability for flexibility. For a task with known steps, a workflow beats an agent on cost, latency and reliability.

4. Tools are your attack surface

Every tool is something the model can do on your behalf, including when it has been manipulated by text in a document (prompt injection, Module 12). Give tools the least privilege possible, validate their arguments like any API input, and require a human approval for anything irreversible or outward-facing: sending email, paying, deleting.

Design tools for the model: clear names, short descriptions that say when to use them, typed arguments, and results that are concise and readable. A tool that returns 20,000 tokens of raw JSON wrecks the context budget.

5. What the Agentic AI course adds

The Agentic AI course takes this loop to production: LangGraph state machines with checkpoints and human-in-the-loop interrupts, memory, planning, MCP servers to share tools across apps, multi-agent designs, agent evaluation, and the cost and safety controls that make agents safe to deploy.

Hands-on: coding window

The coding window and the full VS Code lab are available after you sign in.

Sign in to code

Labs

Lab 7.1: A real two-tool agent

Replace the scripted model in the starter with a real tool-calling model (local or hosted). Give it lookup_vendor and draft_email, a 6-step limit and loop detection.

Expected: A transcript for a normal run, a run where lookup_vendor fails, and a run stopped by the step limit.

Lab 7.2: Follow-up agent behind a flag (capstone increment)

For extractions with low-confidence fields, add an agent that looks up the vendor and drafts a confirmation email. Drafts go to the review queue; nothing is sent without a human click.

Expected: Drafts appear in the review queue with the agent’s trace; a test proves the agent can’t send email.

Lab 7.3: Agent versus workflow

Implement the follow-up as a fixed workflow too. Run both on 20 cases and compare success rate, model calls, latency and cost.

Expected: A comparison table and a one-paragraph recommendation for which to ship.

Go deeper

Videos

Further reading

GitHub repositories

Checkpoint quiz

1. Why does each step of an agent cost more input tokens than the last?
2. A tool call fails with a 503. What should the agent loop do?
3. When is a fixed workflow better than an agent?
4. Which action should require human approval?
5. Which is NOT a stop condition an agent loop needs?

Interview questions

  1. What is an AI agent, technically? Draw the loop.
  2. How do you stop an agent from running up a large bill?
  3. Give an example where you would deliberately not use an agent.
  4. How would you make an agent with an email tool safe to deploy?
  5. How do you test an agent whose sequence of steps changes between runs?

Ask the professor

Sign in to ask questions about this module.

Discussion

Community discussion appears here once giscus is configured (see README).