Building Your First Agent
Building Your First Agent
Theory is useful. Practice is better. Building even a simple agent from scratch teaches you more about how they work than reading about them. This lesson walks through the core loop of a basic agent using nothing but an LLM API.
The Minimal Agent Loop
A basic agent in pseudocode:
messages = [system_prompt]
messages.append(user_goal)
while true:
response = call_llm(messages, tools=available_tools)
if response is tool_call:
result = execute_tool(response.tool, response.args)
messages.append(tool_call_message)
messages.append(tool_result_message)
else:
print(response.content)
break // agent is done
That's it. The loop runs until the model generates a final response (no tool call). The messages list accumulates everything — the model's reasoning, tool calls, tool results — and the model uses all of it to decide what to do next.
Your First Agent in Practice
Start with something simple: a research assistant that can search Wikipedia and return a summary.
Define one tool: search(query: string) → string (returns a Wikipedia snippet).
Give the agent a goal: "Find out who invented the World Wide Web and when."
The agent will:
- Call search("inventor of World Wide Web")
- Get back text about Tim Berners-Lee
- Call search("Tim Berners-Lee biography") if it wants more detail
- Generate a summary from the results
Complexity to add incrementally:
- Add a second tool (web search, not just Wikipedia)
- Add memory (store facts the agent discovers)
- Add a reviewer agent that checks the output
- Give it more complex goals that require multiple searches
Each step teaches you something new about how agents behave.
What to Watch For
When you build and run your first agent, observe:
Hallucination in tool calls: the model might call tools with nonsensical arguments, or try to call tools that don't exist. Validate inputs.
Looping: without a clear stopping condition, agents sometimes loop — taking the same action repeatedly. Add a maximum iteration count.
Over-tooling: with many tools available, agents sometimes call tools unnecessarily. Fewer, well-scoped tools often work better than many overlapping ones.
Context growth: each tool result adds tokens. A long agent run can push the context window. Monitor token usage.
The aha moment: there's a moment when you watch an agent work through a problem — searching, reasoning, adjusting — and it feels qualitatively different from a chatbot. That's the thing worth building towards.
Sign up free to take the quiz, earn XP, and track your progress.