A prompt asks. An agent acts.
Up to now every prompt you wrote ended with words. In this chapter, the model will end with an action — it will read files, search the web, and write results back to disk. That single leap is what makes AI feel less like a chatbot and more like a coworker.
Learning Objectives
- Define what does and does not count as an "agent."
- Explain the four-step loop every agent runs.
- Design a small set of well-named tools and their JSON schemas.
- Add memory using a plain Markdown file the agent reads and writes.
- Write guardrails that keep an agent safe and predictable.
- Ship a personal research agent that produces a citations-included report.
1. What Counts as an Agent?
An agent is a program that: (1) has a goal, (2) can choose from a set of tools, (3) observes results, and (4) iterates until the goal is met or a stopping condition triggers. Everything else is just a chatbot in a fancy costume.
2. Anatomy of an Agent
Agent
├─ Identity (system prompt)
├─ Goal (what to achieve)
├─ Tools (functions with typed inputs)
├─ Memory (files it re-reads each turn)
├─ Loop (observe → think → act → reflect)
└─ Stop rule (max steps, done marker, human review)3. The Loop
The loop from Chapter 1 becomes code here. Each iteration the agent picks one tool call, we execute it, feed the result back into the context, and repeat.
while not done and steps < MAX:
step = model.next_action(context)
if step.type == "final_answer":
break
result = run_tool(step.tool, step.args)
context.append(result)4. Designing Tools
Great tools are small, well-named, and typed. Aim for verbs: search_web, read_file, write_file, save_note.
{
"name": "search_web",
"description": "Search the public web and return top 5 results with titles, URLs and snippets.",
"input_schema": {
"type": "object",
"properties": { "query": { "type": "string" } },
"required": ["query"]
}
}5. Memory Files
Store persistent facts about the project in a file like MEMORY.md. The agent reads it at the top of every task and appends to it at the end.
# MEMORY.md
## Project
Research digest generator for AI infra topics.
## Style
- Cite every claim with a URL.
- Prefer primary sources over blog posts.
## Recent runs
- 2026-07-10: focused on inference cost trends.
- 2026-07-17: focused on agent evals.6. Guardrails
- Cap loop iterations (e.g. 20) so a runaway agent cannot spin forever.
- Whitelist which tools the agent may call.
- Restrict file writes to a single sandbox folder.
- Require a human "approve" step for anything irreversible (delete, deploy, send email).
7. Project — Personal Research Assistant
We will build an agent that, given a topic, produces report.md with sections, bullet points, and cited URLs. Tools: search_web, fetch_url, write_file.
8. Scaffolding with Claude Code
mkdir research-agent && cd research-agent
git init && npm init -y
claude "Scaffold a TypeScript agent in this folder.
- Use the Anthropic SDK.
- Implement three tools: search_web (Tavily), fetch_url (Firecrawl), write_file.
- Read AGENTS.md at start and append a run log at end.
- Provide 'npm run agent -- --topic "AI eval trends"' as the entrypoint."9. Running the First Loop
export ANTHROPIC_API_KEY=sk-ant-...
export TAVILY_API_KEY=tvly-...
export FIRECRAWL_API_KEY=fc-...
npm run agent -- --topic "State of AI inference costs 2026"Open report.md. You should see clean sections with URLs at the end of each bullet.
10. Making It Reliable
- Reflect step: after writing, re-open the file and check every bullet has a URL — if not, loop back and add one.
- Deduplicate: track seen URLs so the agent does not re-fetch the same page.
- Cap tokens: summarize fetched pages before adding to context to avoid context bloat.
- Human review flag: mark any bullet with low confidence [review].
11. Ship It
Push the repo to GitHub, add a README, and schedule it as a nightly cron on Railway. Congratulations — you now own an autonomous research agent.