"The model proposes; the runtime disposes." — Separation of LLM text from tool execution
At a Glance
| Duration | 8–10 hours |
| Difficulty | Intermediate (Python; JSON; optional APIs) |
| Prerequisites | Prompt design; basic function arguments |
Learning Objectives
By the end of this module, you will be able to:
- Define an agent as a policy mapping state + perception → actions over time, often via tools.
- Contrast marketing “autonomy” with tool-using agents where your code enforces permissions.
- Explain ReAct-style traces vs function calling as two faces of one pattern.
- Implement a minimal loop with mock or live APIs and stop conditions.
- Identify runaway loops, injection via tool output, cost, unsafe side effects, integrity issues.
- Sketch guardrails: allowlists, human approval, logging, budgets, sandboxes.
Concept Section 1 — From Chatbot to Agent
Loop: Observe (user + tool results) → Decide (reply or tool call) → Act (your code runs tool) → Observe → Stop on answer, max steps, or budget.
Single-shot vs agentic
| Single-shot | Agent loop | |
|---|---|---|
| Steps | One call | Many |
| Failure | Incomplete | Loops, misuse, cost |
| Debug | Easier | Needs traces |
Concept Section 2 — Autonomous vs tool-using
| Narrative | Engineering default |
|---|---|
| “AI browses freely” | Allowlisted domains or local files |
| “AI sends email” | Draft-only until human sends |
| “AI runs shell” | No (except isolated honors labs) |
Concept Section 3 — ReAct, function calling, planners
| Pattern | Model emits | Your code does |
|---|---|---|
| ReAct (text) | Thought / Action lines | Parse (fragile) |
| Function calling | JSON tool call | Dispatch + validate schema |
| Planner | Plan then subcalls | More checks |
Course stance: Draw the loop on a whiteboard before adopting LangChain-sized abstractions.
Concept Section 4 — Safety, cost, integrity
| Control | Purpose |
|---|---|
max_iterations | Stop ping-pong |
| Token budget | Cap cost |
| Tool allowlist | No arbitrary URLs |
| Human approval | Writes / sends / money |
| Structured logs | Debug + review |
Agents that complete graded work from hidden sources usually violate syllabi—design for tutor / quiz / cite, not substitution.
Agent loop vs scripted pipeline
| Pipeline | Agent | |
|---|---|---|
| Flow | Fixed | Model branches |
| Cost | Predictable | Variable |
| Debug | Stack traces | Prompt + tool traces |
Hands-On Example — Pedagogical Python Loop (Stub Model)
Key Example: Replace
fake_model_planwith a realchat.completionscall that returnstool_calls; feed tool results back per provider docs.
[object Object], json
,[object Object], typing ,[object Object], ,[object Object],
,[object Object], ,[object Object],(,[object Object],) -> ,[object Object],[,[object Object],, ,[object Object],]:
hours = {
,[object Object],: {,[object Object],: ,[object Object],, ,[object Object],: ,[object Object],},
,[object Object],: {,[object Object],: ,[object Object],, ,[object Object],: ,[object Object],},
}
,[object Object], hours.get(building.lower(), {,[object Object],: ,[object Object],})
,[object Object], ,[object Object],(,[object Object],) -> ,[object Object],:
,[object Object], name == ,[object Object],:
,[object Object], get_campus_building_hours(**arguments)
,[object Object], ValueError(,[object Object],)
,[object Object], ,[object Object],(,[object Object],) -> ,[object Object],[,[object Object],, ,[object Object],]:
,[object Object], ,[object Object], ,[object Object], user_text.lower():
,[object Object], {
,[object Object],: ,[object Object],,
,[object Object],: ,[object Object],,
,[object Object],: {,[object Object],: ,[object Object],},
}
,[object Object], {,[object Object],: ,[object Object],, ,[object Object],: ,[object Object],}
,[object Object], ,[object Object],(,[object Object],) -> ,[object Object],:
plan = fake_model_plan(user_text)
,[object Object], plan[,[object Object],] == ,[object Object],:
,[object Object], plan[,[object Object],]
result = run_tool(plan[,[object Object],], plan[,[object Object],])
,[object Object], ,[object Object],
,[object Object],(demo_loop(,[object Object],))Narrative extensions: Wrap with MAX_STEPS and an explicit timeout string; treat tool output as untrusted (never promote to system content blindly); estimate token cost as steps × average tokens × price for back-of-envelope budgets.
Try This! Feed a tool result containing “ignore previous instructions…” and verify your app isolates or blocks it.
Fun Fact: LangChain/LangGraph speed prototypes but add API surface—pin versions and print traces in class.
Try This Now
- State diagram for a sandboxed research agent with human gates.
- List 10 allowlisted vs 10 denied tools with one-line risks.
- 250 words: LangChain vs raw SDK for your capstone—pick one.
Practice Exercises
State-machine diagram; allow/deny tool list with safe substitutes; injection policy paragraph; token cost estimate; ReAct abstract bullets (empirical vs aspirational).
Mini-Project
Bounded campus info agent: Local JSON/YAML ground truth; ≥3 tools; no general web browse; tests for routing and errors; README threat model. Stub loop OK if APIs disallowed.
Key Takeaways
- Agents = policies around models; governance lives in tools.
- Prefer structured calls over free-form shell from the model.
- Start narrow, logged, human-in-the-loop for real side effects.
Key Takeaway
- Structured tool calls beat parsing fragile “Thought:” text for student projects.
- Cap iterations, cost, and tool surface before you cap “intelligence.”
- Tool output is untrusted—mind prompt-injection from retrieved or external text.
- Frameworks accelerate prototypes; threat modeling is still your job.
- Academic integrity constraints are features of good agent design, not annoyances.
Resources
- Yao et al., ReAct · Vendor function-calling guides (version-aware)
- OWASP LLM Top 10 — injection, insecure output handling
- Russell & Norvig — agent vocabulary (PEAS)
Align live API labs with campus key policy; prefer mocks for exams.