01. What is an AI Agent?
An AI agent is a system where a language model actively decides which actions to take, executes those actions, and uses the results to inform the next decision. Unlike a chatbot that responds once to a single prompt, an agent operates in a loop. It observes state, reasons about what to do next, calls external tools, receives results, and repeats until the task is complete.
The key components are three: an LLM that functions as the brain, a set of tools that extend what the model can do, and a loop that ties reasoning to action. This architecture covers everything from simple calculator-wielding bots to complex multi-agent pipelines.
The core loop
The minimal agent loop looks like this:
- Receive a task from the user
- The LLM decides whether to call a tool or respond directly
- Execute the tool and return results to the LLM
- Repeat until the LLM signals completion
This is called the "agent loop," and it is the foundation every agent framework builds on.
Agents vs. chain prompting
You might wonder: why not just use chain-of-thought prompting to get everything done in one shot? The answer is that complex tasks require information the model does not have at inference time. The model cannot know current weather, private file contents, or real-time numbers without calling tools to fetch them. Agents solve this data problem by combining reasoning with retrieval.
A minimal example
def agent_loop(model, tools, user_message, max_turns=10):
messages = [{"role": "user", "content": user_message}]
for turn in range(max_turns):
response = model.chat(messages, tools=tools)
if not response.tool_calls:
return response.content
for call in response.tool_calls:
result = tools[call.name](**call.arguments)
messages.append({"role": "assistant", "content": response.content,
"tool_calls": [call]})
messages.append({"role": "tool", "tool_call_id": call.id,
"content": str(result)})
return "Max turns reached"
The code above shows the pattern without any framework. The LLM responds with either text or a tool_calls field. Each tool call returns a result that gets appended back to the message history as a tool role message.
Failure modes
The most common failure is the LLM calling a tool that does not exist or passing arguments that do not match the schema. Set a hard limit on turns to prevent infinite loops. Watch for models that refuse to use tools at all—this usually means the system prompt failed to explain the tool availability clearly.
Run the minimal agent loop with a dummy tool that returns a fixed string. Verify that the model correctly identifies when to call the tool versus when to answer directly.