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
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.
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.
- User: The checkout API is slow. Find out why.
Guided experiments
- Run the happy path. How many model calls did it take, and why does each call cost more input tokens than the one before?
- 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?
- 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).
- 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?
- The last step proposes a rollback but doesn’t do it. Why should that action wait for a human approval?
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.
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 codeLabs
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
- How We Build Effective Agents: Barry Zhang, Anthropic · AI Engineer · ~15 min. When not to build an agent, and how to keep agents simple when you do.
Further reading
- Paper ReAct: Synergizing Reasoning and Acting in Language Models · Yao et al., ICLR 2023. The paper behind the reason, act, observe loop. The trajectories in its appendix are worth reading.
- Article Building effective agents · Anthropic. The clearest guide to when you need an agent at all, and the workflow patterns to try first.
- Article LLM Powered Autonomous Agents · Lilian Weng. A structured survey of planning, memory and tool use, with the key papers for each.
GitHub repositories
- langchain-ai/langgraph: The quickstart’s ReAct-style agent and the human-in-the-loop interrupt examples.
- anthropics/claude-cookbooks: The patterns/agents folder: workflow patterns (chaining, routing, orchestrator-workers) next to agents.
- openai/openai-python: The function-calling examples: the raw message flow that agent frameworks wrap.
Checkpoint quiz
Interview questions
- What is an AI agent, technically? Draw the loop.
- How do you stop an agent from running up a large bill?
- Give an example where you would deliberately not use an agent.
- How would you make an agent with an email tool safe to deploy?
- 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).