You have used ChatGPT. You have called an LLM from code. But agentic AI is a different beast — the model is no longer a chat partner, it is a worker that decides what to do next. If you have tried to build an agent and ended up with a pile of nested if statements wrapped around an API call, this tutorial is for you.
By the end of this post you will have built a working agent in Python, understood why the "agent loop" is the only pattern you need to learn, and avoided the three mistakes almost every beginner makes.
What you'll learn
- What actually separates an agent from a normal LLM call
- The perceive → think → act loop that every agent runs on
- How to build a task executor that plans, uses tools, and remembers
- Where memory fits in and why it's easier than it looks
- The common beginner mistakes that kill agents in production
What Is Agentic AI?
A normal LLM call is a one-shot function: input text, output text. An agentic system is a loop: the model decides what to do, executes it, observes the result, and decides again. It keeps going until the job is done.
Traditional AI predicts. Agentic AI acts.
Concretely, an agent has four things a chatbot does not:
- Tools — functions it can call (search, code execution, APIs)
- A goal — a success condition it is working toward
- State — memory of what it has already tried
- Autonomy — the authority to pick its next step, not you
Think of the difference between a search box and an intern. The search box returns matches. The intern takes a task, reads some docs, writes a draft, runs it past the spec, and comes back when it's done. That's agentic AI.
For a deeper breakdown of this shift, see our post on agentic AI vs traditional automation.
The Agent Loop: Perceive → Think → Act
Every useful agent architecture reduces to this loop:
while not goal_reached:
observation = perceive(environment)
next_action = think(observation, history)
result = act(next_action)
history.append((observation, next_action, result))
That's it. Agents look complex because frameworks pile abstractions on top, but underneath, every one of them is running that loop.
- Perceive — read the current state: user message, tool output, error
- Think — send state and history to the LLM, get back a plan or action
- Act — execute whatever the LLM chose (call a tool, write a file, respond)
The magic is not in any single step. The magic is that the model gets to see what happened last turn and decide the next turn. That is what makes it "agentic."
If you want to go deeper on the theory, Anthropic's Building effective agents post is the best primer in the field.
Your First Agent: A Simple Task Executor
Let's build one. The agent will take a natural-language task and work through it using two tools: a calculator and a note-writer. You need Python 3.11+ and an API key from your LLM provider.
import json
from anthropic import Anthropic
client = Anthropic()
# The tools the agent is allowed to use
TOOLS = [
{
"name": "calculate",
"description": "Evaluate a math expression and return the result.",
"input_schema": {
"type": "object",
"properties": {"expression": {"type": "string"}},
"required": ["expression"],
},
},
{
"name": "write_note",
"description": "Save a short note for later reference.",
"input_schema": {
"type": "object",
"properties": {"text": {"type": "string"}},
"required": ["text"],
},
},
]
notes = []
def run_tool(name, args):
if name == "calculate":
return str(eval(args["expression"], {"__builtins__": {}}))
if name == "write_note":
notes.append(args["text"])
return f"Noted: {args['text']}"
return f"Unknown tool: {name}"
Now the loop itself. We send the task, read the model's choice, execute any tool it asked for, and send the result back in. Stop when the model stops asking for tools.
def run_agent(task: str, max_steps: int = 8):
messages = [{"role": "user", "content": task}]
for step in range(max_steps):
response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
tools=TOOLS,
messages=messages,
)
if response.stop_reason == "end_turn":
return response.content[0].text
# Model asked for a tool
messages.append({"role": "assistant", "content": response.content})
tool_results = []
for block in response.content:
if block.type == "tool_use":
result = run_tool(block.name, block.input)
tool_results.append({
"type": "tool_result",
"tool_use_id": block.id,
"content": result,
})
messages.append({"role": "user", "content": tool_results})
return "Gave up after max_steps"
print(run_agent(
"I bought 3 shirts at $24.99 each with 7% tax. "
"Write a note with the total so I remember."
))
Run it. The agent will:
- Decide it needs the calculator
- Compute the total with tax
- Ask for the note-writer tool with the result
- Reply to you with confirmation
That's an agent. Under 60 lines. No framework.
Adding Memory to Your Agent
The loop above has short-term memory — the messages list. That's enough for a single session but not for an agent that should remember things across runs.
There are two memory patterns worth knowing:
Scratchpad memory — everything the agent has seen this run. You already have this: the growing messages list. Fine for tasks that complete in one session.
Long-term memory — facts the agent should remember across sessions. The simplest version is a JSON file or a SQLite table that the agent can read from and write to as tools.
import sqlite3
db = sqlite3.connect("agent_memory.db")
db.execute("CREATE TABLE IF NOT EXISTS facts (key TEXT PRIMARY KEY, value TEXT)")
def remember(key: str, value: str):
db.execute("INSERT OR REPLACE INTO facts VALUES (?, ?)", (key, value))
db.commit()
return f"Remembered: {key}"
def recall(key: str):
row = db.execute("SELECT value FROM facts WHERE key=?", (key,)).fetchone()
return row[0] if row else "Not found"
Expose remember and recall as tools and your agent now has persistent memory. When retrieval gets more complex — semantic search over documents, not key-value lookups — you want RAG instead. We cover the tradeoff in our RAG vs fine-tuning guide.
Real-World Applications
A tutorial agent is a toy. What are people actually building?
- Research assistants that take a question, search the web, read results, and return a summarized answer with citations
- Code review bots that read a pull request, run tests, check style, and post findings as comments
- Customer support agents that read tickets, look up order data, draft replies, and escalate when confidence is low
- Operations copilots that take alerts, run diagnostic commands, and either fix the issue or page a human with context
Every one of these is the same loop you just wrote, with different tools and a different goal.
Common Mistakes Beginners Make
1. Letting the agent loop forever. Always set max_steps. A model that gets confused will happily call tools in circles until your API bill is terrifying. Fail loud and early.
2. Hiding errors from the model. When a tool fails, return the error message as the tool result. Do not raise. The model can often recover if it sees the actual failure; if you swallow errors it has no way to course-correct.
3. Giving the agent 40 tools. Models degrade past roughly a dozen tools. If you need more, split into specialized sub-agents or use a router pattern where a top-level agent picks which sub-agent to invoke. Fewer, sharper tools beat a tool-dump every time.
4. Skipping system prompts. A blank system prompt means the model invents its own personality and risk tolerance. Tell it what role it is playing, what it is not allowed to do, and how to behave when uncertain.
5. Treating agents like chatbots in disguise. Agents plan, execute, and verify. If you are not checking the result of what the agent did, you do not have an agent — you have an expensive autocomplete.
Conclusion
Agentic AI is not a new algorithm. It is a loop. Once you see it that way, every framework — LangChain, CrewAI, AutoGen, Pydantic AI — just looks like a different wrapper around the same three steps.
The best way to level up from here is to build another agent this week. Pick a task you actually do (summarize your unread emails, categorize expenses, triage GitHub issues) and write the minimal agent for it. You will learn more in one weekend of shipping than in a month of reading.
When you are ready for the structured version, the MindloomHQ curriculum walks you from your first agent through production-grade systems in ten phases, and our Agentic AI Development course is the flagship path.
FAQ
What is agentic AI in simple terms?
Agentic AI is a pattern where an LLM acts in a loop — deciding what to do, doing it, observing the result, and deciding again — until a goal is reached. The LLM is the brain; tools are its hands; memory is its notebook.
Do I need a framework like LangChain to build an agent?
No. Everything in this tutorial works with only the provider SDK. Frameworks help when you want retries, streaming, tracing, or multi-agent orchestration out of the box. For learning, writing the loop yourself is much more instructive. See our honest review of LangChain.
What's the difference between an agent and a chatbot?
A chatbot replies. An agent acts — it calls tools, reads their results, and keeps working until the task is finished. Chatbots are one-shot; agents loop.
How much does it cost to run an agent?
It depends on steps per task and tokens per step. A simple 3-step task on a mid-tier model usually costs a fraction of a cent. A 20-step research agent can be several cents. Always cap max_steps and log token usage before you scale.
What should I build after my first agent?
Add tool use, then memory, then evaluation. The biggest skill jump is going from "it works on the happy path" to "I can measure whether it works." Our guide on how to evaluate AI agents covers the next step.