AI Agents and Tool Calling Explained (With a 20-Line Example)

AI Agents and Tool Calling Explained (With a 20-Line Example) featured illustration

📎 This article includes 1 downloadable practice file ↓

⏱ 2 min readUpdated 28 September 2026

An AI agent is just a language model running in a loop, allowed to use tools. Instead of answering straight away, it can say “call the exchange-rate tool with USD→INR”, read the result, and continue.

In this article
  1. How tool calling works
  2. The loop in Python (illustrative)
  3. Rules for safe agents
  4. Continue learning

How tool calling works

  1. You describe your tools to the model: name, what it does, and its inputs (usually as a JSON schema).
  2. The model replies either with normal text or with a structured request: {"name": "get_rate", "arguments": {"from": "USD", "to": "INR"}}.
  3. Your code runs the tool and sends the result back as a message.
  4. Repeat until the model gives a final answer.

The model never runs anything itself — your program decides what actually executes. That is the key safety point.

Advertisement

The loop in Python (illustrative)

def get_rate(frm, to):
    return {"USD->INR": 83.2}.get(f"{frm}->{to}")    # call a real API here

TOOLS = {"get_rate": get_rate}
messages = [{"role": "user", "content": "How much is 250 USD in rupees?"}]

for _ in range(5):                                  # hard limit on steps
    reply = llm_chat(messages, tools=TOOL_SCHEMAS)  # your provider's chat API
    messages.append(reply)
    call = reply.get("tool_call")
    if not call:
        print(reply["content"])
        break
    result = TOOLS[call["name"]](**call["arguments"])
    messages.append({"role": "tool", "name": call["name"], "content": str(result)})

Every major API (OpenAI, Anthropic, Google, and local runners like Ollama) supports this pattern; only the field names differ.

Rules for safe agents

  • Least privilege: read-only tools unless writing is truly needed.
  • Human approval for anything that sends, pays, deletes or publishes.
  • Step limits so a confused agent cannot loop forever.
  • Treat tool results as data: a web page saying “ignore your instructions” must not be obeyed. This attack is called prompt injection.
💡 Start with one tool that answers a real question from your work (e.g. “look up an invoice status”). Agents with twenty tools on day one are hard to debug.

📎 Practice files for this article

  • 🐍
    All code from this post1 examples in one file, ready to open in your editor.
    ⬇ PY · 861 B

Free to use for learning. Files with macros (.bas) are plain text — import them with Alt+F11 → File → Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free · AI can be wrong