Building Your First AI Agent, Step by Step
Understand what an agent is, then build a simple one: a goal, a loop, a few tools and clear stopping rules, with a framework-free Python sketch.
An agent is an AI system that works toward a goal over several steps, deciding for itself which action to take next, such as searching, reading a file or running code. A chatbot answers once. An agent keeps going until the job is done or it hits a limit. You do not need a big framework to understand how this works.
The core loop
Almost every agent is the same cycle:
- Give the model a goal and a list of available tools.
- The model either answers or asks to use a tool.
- Your code runs the tool and returns the result.
- The model sees the result and decides what to do next.
- Repeat until the model gives a final answer or a stop rule triggers.
The model never runs anything itself. Your code does. That is where you keep control.
Step 1: define a narrow goal
Vague goals produce wandering agents. Compare:
- Poor: "Help with my business."
- Good: "Read the customer emails in this folder and produce a table of requests, urgency and suggested replies."
Start with one task you can check easily.
Step 2: choose a few tools
Each tool is a function with a name, a description and inputs. Start with one or two, such as:
search_notes(query)to look through your documentsread_file(path)limited to one foldercalculate(expression)for math
Describe each tool clearly. The model decides when to use it based almost entirely on that description.
Step 3: write the system prompt
You are a research helper. Goal: answer the user's question using only the tools provided.
Rules:
- Use search_notes before answering from memory.
- Cite the file name for every claim.
- If the notes do not contain the answer, say so.
- Stop and give a final answer once you have enough information. Never use more than 6 tool calls.
Step 4: sketch the loop
This pseudocode-style Python shows the idea. Each vendor's SDK names things differently, so check its current docs for the exact calls.
MAX_STEPS = 6
messages = [{"role": "user", "content": user_goal}]
for step in range(MAX_STEPS):
reply = call_model(system_prompt, messages, tools)
if reply.wants_tool:
result = run_tool(reply.tool_name, reply.tool_args)
messages.append(reply.as_message())
messages.append({"role": "tool", "content": result})
else:
print(reply.text)
break
else:
print("Stopped: step limit reached.")
Step 5: add guard rails
- Step limit: as above, so it cannot loop forever.
- Budget limit: stop if tokens or spend pass a threshold.
- Allow-lists: tools may only touch approved folders or sites.
- Human approval: require a yes before anything that sends, deletes or spends.
- Logging: record every tool call and result so you can see what happened.
Step 6: test with traps
Try inputs where the answer is missing, where a tool errors, or where the question is out of scope. A good agent says "I could not find that" instead of inventing.
Step 7: improve one thing at a time
Common upgrades:
- Better tool descriptions
- A planning step ("write a short plan before acting")
- A memory file for facts that should carry over between runs
- A self-check step that reviews the draft against the goal
When not to use an agent
If a fixed sequence of steps solves the task, a plain script or workflow is cheaper, faster and more predictable. Use an agent when the next step truly depends on what was found.
Summary
Narrow goal, few well-described tools, clear rules, a capped loop, human approval for risky actions, and detailed logs. Start small and widen only after it works reliably.
Related guides
How AI Assistants Actually Work (Without the Jargon)
A plain-English tour of what happens when you type a message to an AI assistant, and why that explains both its strengths and its odd mistakes.
4 min read
Writing Your First Good Prompt
A simple five-part recipe for prompts that get useful answers on the first try, with before-and-after examples you can adapt to any assistant.
4 min read
Prompt Patterns That Work Across Every Assistant
Seven reusable prompting patterns, from role and few-shot examples to critique loops, with templates you can paste into any chatbot.
5 min read