AI Engineering

An AI agent is just an LLM,
some functions, and a loop.

By Manoj SainiΒ· 31 July 2026Β· 6 min readΒ· Pilani, India

Every second sales deck in 2026 has the word "agent" on it, priced like it's alien technology. So let's ruin the mystique: an AI agent is a language model, a list of functions it's allowed to call, and a while loop. That's the whole trick.

We run one in production. It's written in plain PHP β€” no framework, no orchestration platform, no "agentic mesh". Once you see the three parts, you'll be able to judge every agent pitch you hear, including ours.

The three parts

🧠An LLM Any capable model behind an API. It reads the task and decides what to do next.
πŸ”§Functions Ordinary code you already have: look up an order, query a database, send an email.
πŸ”A loop Call the model, run what it asks for, feed the result back. Repeat until done.

That's genuinely it. The model never touches your database. It can only request one of the functions you defined, with the arguments it thinks are right. Your code runs the function β€” or refuses to β€” and hands the result back.

What one turn of the loop looks like

Say a parent messages your support inbox: "I paid yesterday but my child's subscription still shows expired." A chatbot would generate a sympathetic paragraph. The agent does this:

// turn 1 β€” the model reads the task and your function list, then answers with:
{ "call": "find_payment", "args": { "parent_id": 4182, "days": 2 } }

// your code runs find_payment(), returns the result to the model:
{ "status": "captured", "subscription_updated": false, "webhook_received": false }

// turn 2 β€” now it knows the webhook was missed, so it answers with:
{ "call": "replay_webhook", "args": { "payment_id": "pay_9f3k..." } }

// turn 3 β€” replay succeeded, subscription now active. Model drafts the reply,
// which queues for human approval because outbound messages are gated.

Three turns. The model diagnosed a missed webhook and fixed it β€” not because it's magic, but because you gave it a diagnostic function and a repair function, and the loop let it use one after the other.

The difference from a chatbot in one line: a chatbot's output is text you read. An agent's output is a decision about which of your functions to run next.

The whole loop, honestly

while ($steps++ < $max_steps) {
    $reply = call_model($conversation, $available_functions);

    if ($reply->is_final_answer) break;          // done β€” respond to the user

    if (needs_approval($reply->call)) {
        queue_for_human($reply);                   // payments, deletions, outbound
        break;
    }

    $result = run_function($reply->call);          // your ordinary code
    log_action($reply->call, $result);             // every step, with reasoning
    $conversation[] = $result;                     // feed it back, loop again
}

Fifteen lines of pseudocode. Our production version is longer only because of what surrounds it β€” and that surrounding part is where agents actually succeed or fail.

The parts that actually matter

The loop is a weekend project. These four things are the engineering:

πŸ›‘
An approval gate on anything irreversible

Payments, deletions, messages leaving the company β€” the agent proposes, a human taps approve. The loop breaks and waits. An agent that can be wrong safely is an agent you can actually deploy.

πŸ“œ
A log of every step and its reasoning

When someone asks in six weeks why the agent refunded order #8817, the answer is in the log, not in anyone's memory. Without this you don't have an agent, you have a liability.

⏱️
A hard cap on iterations

Every turn of the loop is a model call, which is money and latency. Uncapped agents wander. Ours resolves most tasks in three to six steps and gives up gracefully past ten.

🎯
Fewer, better functions

Twelve well-named functions with clear descriptions beat forty vague ones. The model chooses from what you describe β€” describe badly and it chooses badly.

When you don't need an agent

Here's the part vendors skip. If the steps are known in advance, you don't need an agent β€” you need a script. "Every night, pull unpaid invoices and send reminders" has a fixed path; a cron job does it deterministically, for free, forever.

An agent earns its cost only when the path is different every time β€” support triage where every complaint needs different lookups, research where the next step depends on what the last one found. We wrote a whole post on drawing that line: Do You Actually Need AI, or Just a Cron Job?

So why hire anyone, if it's this simple?

Fair question, since we just gave away the architecture.

Because the loop was never the product. The product is: functions that expose enough to be useful and little enough to be safe, an approval queue your staff will actually use, evaluation that catches the model confidently doing the wrong thing, and the judgement to say "this part shouldn't be an agent at all." That's engineering and taste, and it's the part that takes the weeks. The same architecture described here runs inside a patent-filed platform live on both app stores.

Frequently asked questions

What's the difference between an AI agent and a chatbot?+
A chatbot answers. An agent acts. A chatbot's output is text you read; an agent's output is a decision to call a function, look at the result, and decide what to do next. The loop and the tools are the difference β€” not the intelligence.
Do I need LangChain or a framework to build one?+
No. Frameworks package the loop for you, which helps prototyping β€” but the core is an API call, a set of functions, and a while loop in whatever language you already use. Ours runs in plain PHP. Understand the loop first; adopt a framework only if it genuinely saves work after that.
How much does an agent cost to run?+
Each loop turn is a model call, so cost scales with steps per task, not conversations. A well-scoped agent resolves most tasks in three to six steps. Cap the iterations, log token usage per task, and the bill stays predictable.
When should I NOT use an agent?+
When the steps are known in advance. A fixed sequence is a script, and a script is cheaper, faster and deterministic. An agent earns its cost only when the path to the answer is different every time.

Keep reading

What's the task in your business
where the path changes every time?

That's where an agent belongs. Free 30-minute call β€” and if a script would do it cheaper, that's what we'll tell you.

support@thetechnosquare.com  Β·  Pilani, India  Β·  Working worldwide 🌍