AI Agents Explained: Tool Use, Workflows and How to Build Your First Agent
Updated 2 Nov 2026 2 min read
On this page 9 sections
What an agent is
An AI agent is a program where a language model decides what to do next and can call tools (search, a database, an API) to get things done, repeating until the task is finished. A normal chatbot only produces text. An agent can take actions.
Workflow or agent?
This distinction saves a lot of pain.
- A workflow follows steps you wrote: classify the email, then look up the order, then draft a reply. The path is fixed, even if a model does a step.
- An agent lets the model choose the steps and tools at run time.
Start with a workflow. It is cheaper, faster and easier to test. Use an agent only when the number of steps or the order cannot be known in advance, and when you can tolerate some variation.
Building blocks
- Instructions: the agent's job, rules and what it must never do.
- Tools: functions the model may call, each with a clear name, description and typed inputs, for example search_orders(order_id) or create_ticket(summary).
- The loop: ask the model, run the tool it chose, give back the result, repeat until it answers or hits a limit.
- State and memory: what happened so far, plus anything worth saving for later.
- Guardrails: limits and checks that keep it safe.
- Tracing: a log of every step, so you can debug.
The loop in plain code
def run_agent(task: str, max_steps: int = 6) -> str:
messages = [{"role": "user", "content": task}]
for _ in range(max_steps): # hard limit on steps
step = model(messages, tools=TOOLS) # model answers or asks for a tool
if step.tool_call is None:
return step.text # finished
result = run_tool(step.tool_call) # your code runs the tool, with checks
messages += [step.as_message(), tool_message(step.tool_call, result)]
return "I could not finish within the step limit."
Write this loop yourself once before using a framework. You will understand what frameworks like LangGraph are doing for you.
Keeping agents safe
- Least privilege: give each tool only the access it needs. Prefer read-only tools.
- Confirm risky actions: refunds, deletions and emails need a person's approval.
- Validate inputs: check tool arguments in code, not only in the prompt.
- Limit steps and cost: cap loops, tokens and tool calls.
- Treat outside text as untrusted: web pages and documents can contain instructions meant to trick the model (prompt injection).
- Log everything: a record of each step and decision.
Your first agent project
Support helper for an online shop. Tools: look up an order, check the return policy (use RAG), and draft a reply. Rules: never issue a refund; hand over to a person with a summary.
Build it, then test it with 30 scenarios, including tricky ones: missing order number, an angry customer, and an attempt to make it ignore the rules.
How to test agents
Check the final answer and also the path: did it call the right tool, with the right arguments, in a sensible number of steps? Track success rate, steps, cost and time. See LLM evals explained.
Common mistakes
- Using an agent when a simple workflow would do
- Giving broad tools like "run any SQL" or "send any email"
- No step limit, so loops run up the bill
- Judging success from one lucky demo
Next steps
- Study plan: AI engineer study plan
- Portfolio ideas: AI engineer portfolio projects
- Where this is used with customers: forward deployed engineer
Found this useful? Share it with a friend.