AI Agents Demystified: How Simple Loops Power the Hottest Tech
AI agents are magic, but they are simpler than you think. Here’s how agentic automation works and how building for AI will make your software better for humans.
Disclaimer: This post is based on a talk I gave recently at the Munich TypeScript Meetup on May 21st and at Munich Tech Connect on May 23rd.
The talk and full code samples are available on Github at github.com/ccssmnn/agents-demystified.
AI Agents: Too hot to handle?
AI agents are everywhere right now. Every week, there’s a new demo, a new product, a new promise. But what’s really going on under the hood? Is it magic? A bird? A plane?
I had to build my own agentic coding tool that works with Helix: md-ai. What I found surprised me: the core mechanisms are almost embarrassingly simple.
Let’s break it down.
Talking to an LLM: Messages In, Text Out
At the heart of every AI agent is a language model (LLM). Calling an LLM is as simple as sending a list of messages and getting a response:
import { generateText } from "ai";import { openai } from "@ai-sdk/openai";
let response = await generateText({ model: openai("gpt-4.1-mini"), messages: [ { role: "system", content: "..." }, { role: "user", content: "..." }, ],});
console.log(response.text);
That’s it. No magic. No secret sauce. Swap models and providers thanks to the AI SDK and it’s Language Model Specification.
AI Chat: It’s a Loop
Want a chat? Wrap that in a loop:
let messages = [];while (true) { if (userIsNext(messages)) { messages = await userTurn(messages); } else { messages = await assistantTurn(messages); }}
Each turn, you append a message: user, assistant, user, assistant. This is the backbone of every AI chat app.
But chatting with an LLM has its limits. As developers, we know this pain very well. We copy code and errors into the chat, the LLM suggests a fix, and we copy-paste it back. It’s still broken, so wie copy and paste again.
Chat users are the interface between the LLM and the problem. The LLM can’t do anything. It just talks.
Tool Calling: They can Do Things
For some time now, many LLMs support tool calling. But the LLM doesn’t actually execute the tools. We do.
The AI SDK’s abstraction has us covered here. Just define a tool using their helper. Each tool can have a description, parameters and a function that should run when the model requests a tool call.
import { tool } from "ai";
let readFilesTool = tool({ description: "read files from the user's disk", parameters: z.object({ paths: z.array(z.string()) }), execute: ({ paths }) => { /* ... */ },});
Then we call the LLM with messages and the tools available. The LLM may respond with tool calls. The AI SDK executes the tool automatically and add’s the tool result to the messages list. Another LLM invocation now includes the tool result and the loop can continue.
let messages = [];let tools = initTools();while (true) { if (userIsNext(messages)) { messages = await userTurn(messages); } else { messages = await assistantTurn(messages, tools); }}
Every fancy automation you see in AI apps today? It’s tool calls. ChatGPT doing data science? That’s generating and executing Python as a tool. Web browsing? Tool. Coding Agents? Tools!
But why is this such a big deal?
No One Wants to Use Your Software
Hot take: people don’t want to use your software. They want to get something done. The UI is just a means to an end.
People look at the UI, parse the information, see possible actions (buttons, forms, links), and operate the app step by step, guided by their goal.
AI as an Automation Engine
If we give an LLM the information and tools available in our UI, we can let the AI accomplish the user’s goal. The agent becomes the user, operating the app through the tools we provide.
This is powerful. But it’s also revealing. When you build for agents, you’ll notice any place where:
- there is too much information.
- there are too many buttons.
- the flow is confusing.
Humans can learn and adapt, but the AI always starts fresh. Every run, it only knows what’s in the prompt, the context, and the tools. If it’s too much, it gets lost. If it’s clear, it gets things done.
Building for AI Makes Your App Better for Humans
With these simple building blocks, we can build automation into our software. But we keep the human in the loop. Our users have access to the same information, the same actions.
By improving the tools and information for AI, you’ll improve your app for humans too. Fewer, better tools. Relevant information. Clear documentation. Atomic, composable actions. This discipline pays off for everyone.
Users can learn which tasks they hand off to the AI and when they want to do it themselves. And with models getting better and the software improving, the level of automation increases.
Please try this at home
AI agents are not magic. They are simple loops powered by language models and tool calls. Understanding this helps us build better software for both AI and humans.