Skip to content

A Practical Guide to AI Agents and Function Calling

An agent is a language model that can call your code. Learn the tool-calling loop, how to design safe tools, and when you do not need an agent at all.

Muhammad Yasir

@YasirAwan4831

Published Updated 2 min read

A Practical Guide to AI Agents and Function Calling

On this page

The word agent is used loosely, so here is a working definition: an agent is a language model that can decide to call functions you provide, look at the results, and continue until the task is done. That is all function calling is.

The tool-calling loop

  1. You describe the available tools (name, purpose, parameters) to the model.

  2. The model replies either with an answer or with a request to call a tool.

  3. Your code runs the tool and returns the result to the model.

  4. The loop repeats until the model produces a final answer or you stop it.

The model never executes anything itself. Your application is always the one running code, which is exactly where you enforce safety.

while steps < MAX_STEPS:
    response = model.generate(messages, tools=TOOLS)
    if response.tool_call is None:
        return response.text            # final answer

    result = run_tool(response.tool_call)   # your code, your rules
    messages.append(tool_result(response.tool_call, result))
    steps += 1

raise RuntimeError("Agent exceeded step limit")

Designing good tools

Tool quality matters more than agent cleverness. A model chooses tools from their names and descriptions, so treat those as an interface for a very literal colleague.

  • One clear job per tool, such as search_jobs or save_note, not do_everything.

  • Precise descriptions that say when to use the tool and when not to.

  • Typed, validated parameters: never trust arguments blindly.

  • Small, readable results: return what the model needs, not a raw database dump.

Safety by construction

Because the model can be wrong or be manipulated by text it reads, limit what any tool can do.

  • Give tools the minimum permissions they need.

  • Require explicit human confirmation before irreversible actions such as sending email or deleting data.

  • Set a maximum number of steps and a time limit.

  • Log every tool call with its arguments for later review.

Treat model output as untrusted input to your own system. Validate it like you would validate a form submission.

Do you actually need an agent?

Agents add flexibility and also add unpredictability, cost and latency. If the steps of a task are known in advance, a fixed workflow that calls the model at specific points is easier to test and cheaper to run. Reach for an agent when the path genuinely depends on what the model discovers along the way.

A good first agent

Start with two or three read-only tools, such as searching notes and fetching a web page, and a hard step limit. Once that behaves predictably, add tools that change things, one at a time, each behind a confirmation step.

Enjoyed this article?

Share it with someone who might find it useful.