Build Your First AI Agent · Lesson 4 of 8

Lesson 4 — State, memory, and context

60 minutes. How the agent remembers what it's done — within a task, across tasks, across days.

---

Three flavors of memory

1. In-task memory — the `messages` array. Every turn (user, assistant, tool result) accumulates. Claude sees the whole history within a task. This is automatic.

2. Cross-task memory — what the agent needs to remember between runs. Today's run shouldn't reprocess yesterday's emails. This is a database problem, not an AI problem.

3. World context — facts about the business the agent operates in. Customer list, product catalog, policies. Loaded into the system prompt or retrieved on-demand via tools.

The cross-task pattern

For our customer-reply agent: a `processed_emails` table.

```sql CREATE TABLE processed_emails ( message_id TEXT PRIMARY KEY, processed_at INTEGER NOT NULL, action_taken TEXT ); ```

The `read_inbox` tool filters out anything whose `message_id` is in the table. Each completed action writes a row. The agent only sees fresh work.

The system prompt

Your system prompt is where world context lives. For the customer-reply agent:

``` You are a customer service agent for Acme HVAC, a residential heating + cooling company in Pennsylvania. The owner is John. The team is 4 people. We serve a 50-mile radius around Scranton.

Voice: warm, direct, no fluff. We sound like a small family business because we are.

Pricing rules:

  • Never quote a specific price in writing; always say "we'll send a written quote."
  • Service call minimum is $95.
  • Emergencies (no heat in winter, no AC over 90°F) get prioritized.

When you don't know something, request human approval before guessing. ```

The model now has a voice, constraints, and a clear escalation path. Most agent quality issues trace back to a weak system prompt.

→ Lesson 5 — Hooking the agent to real services

Previous: Lesson 3 — The tool-use loop · Next: Lesson 5 — Hooking the agent to real services