Applied AIAI agentsLLMsAutomationArchitecture

AI agents: what they are, when to use them and how to build one

Pablo Alcalde García

“AI agent” is the buzzword of the moment, and like every buzzword it is used for almost anything. In this guide I explain what an agent really is, how it differs from a chatbot or an automated workflow, how to build one with a Python example and, above all, when you don’t need one.

What an AI agent is

An agent is a system in which a language model decides what to do next inside a loop: it picks a tool, runs it, observes the result and decides whether to continue or whether it can already answer.

The difference from other things is easy to see:

SystemWho decides the stepsExample
ChatbotNobody: it just answersAnswering frequent questions
WorkflowThe developer, in code”Extract data → validate → save”
AgentThe model, at runtime”Investigate this incident and propose a fix”

If the steps are always the same, you don’t need an agent: you need a workflow. It is cheaper, faster and easier to debug.

The basic agent loop

Every agent, however sophisticated it looks, repeats the same thing:

  1. It receives a goal and the conversation history.
  2. The model decides: do I answer, or do I use a tool?
  3. If it uses a tool, your code runs it and returns the result to the model.
  4. This repeats until the model answers or a step limit is reached.

Minimal example in Python

This code uses Anthropic’s SDK (the pattern is equivalent with other providers). It defines one tool and a loop that runs whatever tools the model asks for:

import anthropic

client = anthropic.Anthropic()
MODEL = "..."  # use the current model identifier on your account

tools = [{
    "name": "lookup_order",
    "description": "Returns the status of an order from its id.",
    "input_schema": {
        "type": "object",
        "properties": {"id": {"type": "string"}},
        "required": ["id"],
    },
}]

def run_tool(name: str, args: dict) -> str:
    if name == "lookup_order":
        return f"Order {args['id']}: shipped, arrives tomorrow."
    return "unknown tool"

messages = [{"role": "user", "content": "Where is my order A-123?"}]

for _ in range(6):  # step limit: never infinite loops
    resp = client.messages.create(
        model=MODEL, max_tokens=1024, tools=tools, messages=messages
    )
    if resp.stop_reason != "tool_use":
        print(resp.content[0].text)
        break

    messages.append({"role": "assistant", "content": resp.content})
    results = []
    for block in resp.content:
        if block.type == "tool_use":
            output = run_tool(block.name, block.input)
            results.append({
                "type": "tool_result",
                "tool_use_id": block.id,
                "content": output,
            })
    messages.append({"role": "user", "content": results})

Three details that separate a demo from something usable:

  • A step limit (range(6)): without it, a reasoning error turns into an expensive loop.
  • Small, well-described tools: the model chooses based on the description. A vague description produces wrong calls.
  • Validating the arguments before running anything with real effects.

When an agent DOES make sense

  • Open-ended tasks where the path depends on what is found along the way (investigating an incident, debugging a failure, preparing a report from several data sources).
  • When there are many possible tools and the right combination can’t be fixed in advance.
  • When the cost of a mistake is low or a person reviews the result.

When NOT to use an agent

  • The process is fixed and repeatable: use a workflow in code.
  • You need deterministic answers (billing calculations, regulatory compliance).
  • You can’t tolerate variable latency or cost: every turn of the loop is a model call.
  • You have no way to evaluate whether the agent is doing well (see how to evaluate LLMs in production).

Real risks

  1. Actions with side effects: deleting, paying, sending emails. Ask for human confirmation on anything irreversible.
  2. Instruction injection: if the agent reads external content, that content can try to give it orders. I cover it in security in LLM applications.
  3. Cost: an agent can make dozens of calls per task. See how much it really costs to implement AI and prompt caching.
  4. Reliability: the probability of getting it right drops with every chained step. Few steps, clear tools.

How to start without overcomplicating it

  1. Write the process as a workflow. If it works, you’re done.
  2. If there is a point where the decision is genuinely open, turn only that point into an agent.
  3. Log every tool call (what it asked for, what it returned).
  4. Create 20 real test cases before taking anything for granted.

Conclusion

An agent is a powerful tool for open-ended problems, not a modern name for automation. Start simple, limit steps and permissions, and measure before expanding.

If you are considering automating a process with agents in your company, you can ask me to call you and we’ll review it with your case.

Call me now