Build Your First AI Agent from Scratch · 30 min · Python · Anthropic Python SDK
Real-World Hardening
The version of your agent that only handles the happy path isn't a smaller version of a production agent — it's a different, unfinished thing.
Hiring signal: Retry logic, tool-error handling, and graceful degradation are what separate a demo agent from one that survives being run unattended against real, messy, real-world input.
What you will learn
- Add retry-with-backoff for transient API errors (rate limits, timeouts)
- Make a failing tool call produce a useful tool_result instead of crashing the whole agent
- Distinguish errors worth retrying from errors worth surfacing immediately
Introduction
Your agent works. Now break it on purpose, three different ways, and fix each one — because "works when I demo it" and "works" are different claims, and the gap between them is entirely this lesson.
Break it: kill your Wi-Fi mid-run
Do this literally. Start a task, disconnect your network for a few seconds, reconnect. Watch what happens.
An unhandled exception here kills the whole agent, mid-task
Without retry logic, a transient network blip or a 429 rate_limit_error raises an exception straight out of messages.create, and your entire process dies — including any partial progress and every dollar of API spend already used getting to that point. The fix isn't complicated: catch the specific transient errors, wait, retry with exponential backoff (wait longer each time), and only give up after a few real attempts.
import time
def call_with_retry(client, **kwargs):
for attempt in range(4):
try:
return client.messages.create(**kwargs)
except anthropic.RateLimitError:
wait = 2 ** attempt # 1s, 2s, 4s, 8s
print(f"Rate limited, waiting {wait}s...")
time.sleep(wait)
raise RuntimeError("Gave up after 4 attempts")
Notice this only catches RateLimitError (and in the lab you'll add APIConnectionError and APITimeoutError too) — not every exception. A genuine AuthenticationError (bad API key) will never succeed no matter how many times you retry it; retrying that just wastes 15 seconds before failing anyway. Retry logic needs to know the difference between "try again, this is transient" and "this will never work, fail now."
Unlock the full lesson
You've read the first 2 sections. The rest of this lesson covers Break it: make a tool fail mid-task, Break it: give it a task with no clear finish line, What You're Building — plus a hands-on lab, quiz, and project artifact.
Create a free account to unlock Phase 0 and Phase 1 of every course — no credit card.
Browse all courses · View pricing · DeVenture Academy