LLM Agents & Tool Use
An LLM agent is a language model that can do things , not just talk. You give it tools (search, code, APIs) and run it in a loop : it reasons about what to do, emits a structured call to a tool, your code runs that tool and returns the result, and the model continues until the task is done. The formula is simple: agent = LLM + tools + a loop .
Learn LLM Agents & Tool Use in our free AI & Machine Learning course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…
Part of the free AI & Machine Learning course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll learn function/tool calling, the ReAct (reason + act) loop, planning and multi-step execution, memory, reflection, and the guardrails that keep an agent safe — the same building blocks used by frameworks like LangChain, LlamaIndex, and the native tool APIs from OpenAI and Anthropic.
What You'll Learn in This Lesson
1 Agent = LLM + tools + a loop
A plain chatbot answers once and stops. An agent can take actions in the world and react to what it sees. The three ingredients:
Great for a single answer, but it can't look anything up or act on the result.
The loop is what lets it break a task into steps and adapt as it learns more.
2 Function / tool calling — who runs what
This is the most misunderstood part: the model never runs your code . You describe each tool to the model, and when it wants one, it emits a structured call . Your application executes it and returns the result. The steps:
Step
Who does it
🧰 Describe tools (name + JSON schema)
You, once, up front.
🧠 Decide to call a tool
The model — it emits a structured call.
⚙️ Run the real function
Your code (the model can't execute anything).
🔁 Return the result
You feed it back into the conversation.
✅ Use the result
The model, on the next step of the loop.
Tip: a tool's schema (clear name, description, typed arguments) is part of the prompt — well-described tools get called correctly far more often.
3 The ReAct loop — reason, act, observe
ReAct stands for Reason + Act . Instead of answering in one shot, the agent interleaves thinking and doing, then observes and repeats:
- Reason — “To answer this I need today's exchange rate.”
- Act — call the get_rate tool.
- Observe — read the returned rate.
- Repeat — reason again with the new fact, or finish.
Run this tiny agent. The “model” is faked so you can watch the reason → act → observe loop without an API key:
And here's what tool calling really looks like — the model emits a structured call, your code runs it:
4 Memory, reflection, and guardrails
A useful agent needs more than a loop. Three capabilities turn a toy into something dependable:
Together these give an agent persistence (memory), reliability (reflection), and safety (guardrails) — the difference between a demo and something you'd let run unattended.
📋 The agent loop in pseudocode
Common Agent Mistakes (and the fix)
- Expecting the model to run your code. — It only emits a structured call; your application executes the tool and returns the result.
- No stopping condition. — Always cap steps and budget, or an agent can loop forever and burn tokens.
- Vague tool descriptions. — A clear name, purpose, and typed argument schema dramatically improves correct tool use.
- No guardrails on actions. — Allow-list tools and require confirmation before anything destructive or irreversible.
- Forgetting to feed results back. — The observation must return to the model, or it can't reason about what happened.
- No reflection. — Have the agent verify its own work against tool results before finalising the answer.
Frequently Asked Questions
⏱ Test Yourself — Timed Quiz
10 quick questions, 12 seconds each. Instant feedback — beat the clock!
🎉 Lesson Complete
- ✅ An agent is an LLM + tools + a loop
- ✅ In tool calling, the model emits a structured call ; your code runs it and returns the result
- ✅ The ReAct loop interleaves reason, act, and observe over multiple steps
- ✅ Memory persists context; reflection catches mistakes
- ✅ Guardrails and stopping conditions keep an agent safe and bounded
Practice quiz
What is the simplest way to describe an LLM agent?
- An LLM plus tools plus a loop
- A faster GPU
- A bigger training dataset
- A spreadsheet macro
Answer: An LLM plus tools plus a loop. An agent is an LLM given tools and run in a loop so it can decide, act, observe, and continue until the task is done.
In tool (function) calling, who actually runs the tool?
- Nobody — the call is only logged
- The language model executes the code itself
- Your application code runs it and returns the result to the model
- The user runs it by hand
Answer: Your application code runs it and returns the result to the model. The model only emits a structured request to call a tool; your code executes it and feeds the result back into the conversation.
What does the model emit when it decides to use a tool?
- A random number
- A structured call naming the tool and its arguments
- A compiled binary
- A new model weight
Answer: A structured call naming the tool and its arguments. Tool calling makes the model output a structured object (tool name plus JSON arguments) that your code can parse and execute.
What do the two parts of the ReAct loop stand for?
- Retry and Accept
- Read and Act
- React and Acquire
- Reason and Act
Answer: Reason and Act. ReAct interleaves Reasoning (thinking about what to do) and Acting (calling a tool), then observing the result and repeating.
Why give an agent a loop rather than a single model call?
- So it can act, observe results, and adjust over multiple steps
- Loops are required by HTTP
- To use more tokens for billing
- To slow it down deliberately
Answer: So it can act, observe results, and adjust over multiple steps. Multi-step tasks need the agent to observe each tool result and decide the next action — a loop enables planning and correction.
Which of these is a typical agent tool?
- The training loss
- A web search function
- The model's softmax layer
- The GPU driver
Answer: A web search function. Tools are external capabilities you expose — web search, code execution, database queries, or third-party APIs.
What role does memory play in an agent?
- It speeds up matrix multiplication
- It compresses the model weights
- It replaces the need for tools
- It lets the agent recall earlier steps, facts, or past tasks
Answer: It lets the agent recall earlier steps, facts, or past tasks. Memory (conversation history or a vector store) lets the agent carry context across steps and sessions instead of forgetting.
What is reflection (self-correction) in an agent?
- The agent deletes its tools
- The agent restarts the GPU
- The agent reviews its own output and fixes mistakes before continuing
- The agent copies the user's last message
Answer: The agent reviews its own output and fixes mistakes before continuing. Reflection has the agent critique or verify its own intermediate work, catching errors before they compound.
Why are stopping conditions and guardrails important?
- They make the model larger
- They prevent infinite loops, runaway costs, and unsafe actions
- They improve image quality
- They are only for training
Answer: They prevent infinite loops, runaway costs, and unsafe actions. Without a max-step limit, budget cap, or action allow-list, an agent can loop forever or take harmful actions.
Which of these is a framework commonly used to build agents?
- LangChain
- Photoshop
- PostgreSQL
- FFmpeg
Answer: LangChain. LangChain, LlamaIndex, and the native tool-calling APIs from providers like OpenAI and Anthropic are common ways to build agents.
Continue this course
- Previous: Model Interpretability (SHAP & LIME)
- Next: Handling Imbalanced Data