LineLapse BlogMake your own →
← All posts

What are AI agents, and how do they work?

#ai#agents#llm

Ask a chatbot a question and it answers once, from memory. Ask an AI agent to "find the cheapest flight and book it" and it behaves differently: it looks things up, tries something, checks what happened, and keeps going until the job is done. That difference is the whole idea behind AI agents.

What is an AI agent? It is a program that wraps a language model in a loop. The model decides what to do next, the program carries out that action with a tool (a search, a file edit, an API call), and the result is fed back to the model so it can decide the next step. The loop repeats until the model says it is finished.

What are AI Agents?model · tools · loopRegular LLMAgent👁️ Perceive1Reason2⚡ Act3Observe4ReAct LoopToolsWeb SearchCode RunMemoryLLM CoordinatorContext Windowgoaltool resultspast reasoningFinal AnswerReasonsActsLoops to doneevolves intoReAct loopreads & writesgoal met
▶ Watch it draw

Why a plain chatbot isn't an agent

A language model, on its own, takes text in and produces text out (see how large language models like ChatGPT work). It cannot open a web page, run code, or check whether its answer was right. It gets one turn.

An agent gives the model two things it lacks: tools it can call, and a loop that lets it see the outcome of each call. The model is still just predicting text. The surrounding program turns some of that text into real actions.

How AI agents work: the loop, step by step

Here is the typical cycle, in order:

  1. A goal arrives. "Find out why the nightly build fails." This goes into the prompt along with instructions and a list of the tools available, each with a short description of what it does and what inputs it takes.
  2. The model chooses. It reads the prompt and either writes a final answer or writes a structured request, such as "call read_file with this path."
  3. The program acts. The surrounding code notices the request, runs the real tool, and captures the output. The model never executes anything itself.
  4. The result goes back in. The tool's output is appended to the conversation, so the model's next turn can see what happened.
  5. Repeat or stop. The model picks the next action based on everything so far. The loop ends when the model produces a final answer, or when the program hits a limit such as a maximum number of steps.

Each pass through the loop gives the agent fresh evidence. If a search returns nothing useful, it can try different words. If a command errors, it can read the error and adjust.

Memory and context

An agent's "memory" is mostly the growing conversation: the goal, each action, and each result. That text has to fit inside the model's context window, so long tasks need housekeeping, such as summarising older steps or storing notes outside the conversation and fetching them when needed. Fetching relevant text on demand is the same trick used in RAG, and many agents use retrieval as one of their tools.

What can go wrong

Agents inherit every weakness of the model, and the loop can compound them:

  • Compounding errors. A wrong early step can send every later step in the wrong direction.
  • Loops that don't end. Without a step limit, an agent can repeat the same failing action.
  • Cost and time. Every turn is a full model call, so many-step tasks add up.
  • Risky actions. An agent with the power to send email or delete files can do real damage if it misjudges. Sensible designs limit its permissions and ask a human to approve anything irreversible.

Agent or workflow?

Not everything needs an agent. If the steps are known in advance (summarise this document, then translate it), a fixed workflow that calls the model at set points is simpler, cheaper, and more predictable. An agent earns its place when the path to the goal isn't known up front and the model has to choose the steps as it goes.

Quick recap

  • An AI agent is a language model inside a loop, with tools it can request.
  • The model decides; the surrounding program executes and returns the result.
  • The conversation so far acts as the agent's working memory.
  • Loops can drift, run up costs, or take risky actions, so limits and human approval matter.
  • If the steps are fixed, a plain workflow is often the better choice.

Make your own → Type any topic and watch it drawn