How AI agents work
It has been three and a half years since the release of ChatGPT, and the AI world has seen immense changes. We have moved from the initial version of ChatGPT—where you could only ask questions and receive answers—to powerful agents that can perform tasks for you, such as writing code, sending emails, and creating presentations.
The first time I used an AI coding agent, I was amazed by its ability to understand my requests, navigate my codebase, and implement changes. Driven by an inquiring mind, I wanted to understand how it works from a technical perspective. Here is what I discovered.
This blog does not require a technical background; I will do my best to explain these concepts so that everyone can understand.
From text-to-text
Most LLMs (Large Language Models) like GPT, Claude (Opus), and Gemini are text-to-text models. This means they are designed to take text as an input and provide text as an output.
You: Hello
LLM: Hi, how can I help you?
So, how can an LLM perform actions like this?
You: Hey, send mail to imduchuyyy@gmail.com, tell him not to forget to write a new blog post about AI agents.
LLM: ...
To text-to-action
This is where the agent comes in. Fundamentally, an agent is software that wraps around the LLM. The agent's job is to translate a user's request into a structured format containing specific actions. It sends this to the LLM, receives the response, and then executes the code associated with that action.
Let me give you a very simple example. Let's say you want an agent to help you send email.
If you just send the message to the LLM like this:
You: Hey, send mail to imduchuyyy@gmail.com, tell him not to forget to write a new blog post about AI agents.
LLM: (All the LLM can do is generate text based on what you provide, so it will give you something like this)
Sure, here is the email you can send to your co-workers.....
How does the agent intervene?
Instead of sending your message directly, the agent wraps your request with instructions (a prompt) that guide the LLM to respond in a specific format (like JSON) that a computer can understand.
You will receive a request from user.
Your task is to understand the user request and then translate it into a structured format with specific action and parameters.
The structured format should be in JSON format and should include the action you want to perform and the parameters needed for that action.
1. Send an email: to send an email, just respond with the following format:
{
"action": "send_email",
"parameters": {
"recipients": ["email1", "email2", ...],
"subject": "email subject",
"body": "email body"
}
}
2. Read an email: to read an email, just respond with the following format:
{
"action": "read_email",
"parameters": {
"email_id": "email id"
}
}
3. Other action: if you want to perform other action, just respond with the following format:
{
"action": "other_action",
"parameters": {
// parameters needed for the action
}
}
Here is the user message: Hey, send mail to imduchuyyy@gmail.com, tell him not to forget to write a new blog post about AI agents.
ONLY respond with the JSON format, do not include any other text or explanation.
The agent then passes this message to the LLM. The LLM understands the instruction and responds something like this:
{
"action": "send_email",
"parameters": {
"recipients": ["imduchuyyy@gmail.com"],
"subject": "Reminder: Write new blog about AI agent",
"body": "Hi, just a reminder to write a new blog about AI agent. Don't forget to share it with everyone once it's done. Thanks!"
}
}
The agent's code parses this JSON and automatically triggers an Email API to send the message.
Modern LLM APIs support this natively as "tool calling": you pass a list of tools with a JSON schema, and the model returns a structured tool call instead of free text. The idea is exactly the same as the prompt above.
The agent loop
One action is not an agent yet. What makes an agent useful is the loop: after running an action, it feeds the result back to the LLM and asks what to do next.
messages = [user_request]
loop:
response = llm(tools, messages)
if response is a final answer: return it
result = run(response.tool_call)
messages += [response, result]
For example, "reply to the last email from my boss" becomes several steps: search_email to find the email, read_email to see what it says, then send_email with the reply. The LLM decides each step after seeing the result of the previous one. This is how coding agents work too: read a file, edit it, run the tests, read the errors, and try again until the tests pass.
Summary
Essentially, an AI agent acts as a translator between natural language and computer language. It allows the computer to understand your intent and execute real-world actions.
This is a simplified look at the process. In reality, agents can be far more complex. I will share more about the evolving world of AI agents in upcoming posts, so stay tuned!