Jahanzaib
Architecture

AI Agent

A system that uses an LLM to decide actions, call tools, and iterate toward a goal. Not just generate text.

Last updated: April 26, 2026

Definition

An AI agent is software where an LLM is the decision-making engine inside a loop. The loop pattern: receive a goal, choose a tool, observe the result, decide the next action, repeat until done. The "agent" part is the autonomous decision-making between steps. A chatbot generates text. An agent generates text AND takes actions in the world (calls APIs, queries databases, edits files, sends emails). The simplest production agent is roughly 50 lines of code wrapped around the LLM API plus tool definitions.

Code Example

python
async def run_agent(goal: str, tools: list[Tool]) -> str:
    messages = [{"role": "user", "content": goal}]
    while True:
        response = await llm.complete(messages=messages, tools=tools)
        if response.stop_reason == "end_turn":
            return response.text
        # Execute tool calls and append observations
        for tool_call in response.tool_calls:
            result = await execute_tool(tool_call)
            messages.append({"role": "tool", "content": result})

The minimum viable agent loop. Production adds error handling, max iterations, and observability.

When To Use

Use an agent when the task requires multiple steps that depend on results, when tools are needed, or when the path to the answer is unknown ahead of time. Use a workflow (not an agent) when the steps are fixed and known.

Common Questions

What is the difference between an AI agent and a chatbot?

A chatbot generates text in response to messages. An agent decides which tools to call and takes actions in the world. Most production "chatbots" today are actually agents under the hood. They retrieve context, query CRMs, send emails, etc.

Related Terms

Building with AI Agent?

I've shipped this pattern in real production systems. If you want a second pair of eyes on your architecture, that's what I do.