Library
Claude Code Mastery · Chapter 5 of 10
Chapter 05

Your First
AI Agent

Design, build, and ship a working agent — end to end — in one afternoon.

AgentsHands-onEnd-to-endProject
A Handbook For Builders
Your First
AI Agent
Chapter 05
The Claude Code Handbook
Prologue

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.

Section 5.0

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.
Part I · Concepts

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.

Part I · Concepts

2. Anatomy of an Agent

diagramclaude-code
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)
Claude Code — Terminal
Screenshot placeholder
Handbook page showing the six components of an agent as labeled boxes wired together.
Figure — Handbook page showing the six components of an agent as labeled boxes wired together.
Part I · Concepts

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.

pseudoclaude-code
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)
Part II · Design

4. Designing Tools

Great tools are small, well-named, and typed. Aim for verbs: search_web, read_file, write_file, save_note.

jsonclaude-code
{
  "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"]
  }
}
Part II · Design

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.

mdclaude-code
# 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.
Part II · Design

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).
Claude Code — Terminal
Screenshot placeholder
A logged agent trace with each tool call color-coded green (approved) or amber (paused for review).
Figure — A logged agent trace with each tool call color-coded green (approved) or amber (paused for review).
Part III · Build

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.

Part III · Build

8. Scaffolding with Claude Code

bashclaude-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."
Claude Code — Terminal
Screenshot placeholder
Claude Code creating the project structure, package.json scripts, and a starter AGENTS.md.
Figure — Claude Code creating the project structure, package.json scripts, and a starter AGENTS.md.
Part III · Build

9. Running the First Loop

bashclaude-code
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"
Claude Code — Terminal
Screenshot placeholder
Terminal streaming the loop: search_web → fetch_url × 3 → write_file report.md → done.
Figure — Terminal streaming the loop: search_web → fetch_url × 3 → write_file report.md → done.

Open report.md. You should see clean sections with URLs at the end of each bullet.

Part IV · Polish

10. Making It Reliable

  1. Reflect step: after writing, re-open the file and check every bullet has a URL — if not, loop back and add one.
  2. Deduplicate: track seen URLs so the agent does not re-fetch the same page.
  3. Cap tokens: summarize fetched pages before adding to context to avoid context bloat.
  4. Human review flag: mark any bullet with low confidence [review].
Part IV · Polish

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.