Lesson 208 of 2116
Capstone — Python CLI That Summarizes With Claude
Tie it all together. A command-line tool that reads a file, calls Claude, and prints a summary. Real code, real errors, real polish.
Lesson map
What this lesson covers
Learning path
The main moves in order
- 1From Zero to Useful Tool
- 2CLI
- 3argparse
- 4retry
Concept cluster
Terms to connect while reading
Section 1
From Zero to Useful Tool
A good capstone is a script you'll actually use. This one reads any text file and streams a Claude summary to your terminal, with sensible errors and retries.
One dependency, one env var. That is the setup.
pip install anthropic
export ANTHROPIC_API_KEY=sk-ant-...argparse for args, streaming for UX, exponential backoff on 429/529, size guard on input. Production-ish in 40 lines.
# summarize.py
import argparse
import sys
import time
from pathlib import Path
import anthropic
def read_text(path: Path) -> str:
if not path.exists():
sys.exit(f"error: {path} does not exist")
if path.stat().st_size > 500_000:
sys.exit(f"error: {path} is larger than 500 KB")
return path.read_text(encoding="utf-8")
def summarize(text: str, bullets: int, tries: int = 3) -> None:
client = anthropic.Anthropic()
prompt = (
f"Summarize the following text in exactly {bullets} bullet points. "
f"Each bullet under 20 words.\n\n{text}"
)
for attempt in range(1, tries + 1):
try:
with client.messages.stream(
model="claude-opus-4-7",
max_tokens=1024,
messages=[{"role": "user", "content": prompt}],
) as stream:
for chunk in stream.text_stream:
print(chunk, end="", flush=True)
print()
return
except anthropic.APIStatusError as e:
if e.status_code in (429, 529) and attempt < tries:
wait = 2 ** attempt
print(f"\n[retry in {wait}s: {e.status_code}]", file=sys.stderr)
time.sleep(wait)
continue
raise
def main() -> None:
p = argparse.ArgumentParser(description="Summarize a text file with Claude.")
p.add_argument("file", type=Path)
p.add_argument("--bullets", type=int, default=5)
args = p.parse_args()
text = read_text(args.file)
summarize(text, bullets=args.bullets)
if __name__ == "__main__":
main()One script, many files. The kind of tool you keep on your PATH.
# usage
python summarize.py README.md --bullets 3
python summarize.py meeting-notes.txt --bullets 7Key terms in this lesson
The big idea: programming with AI is still programming. The skills you built across this track — types, tests, error handling, prompting — all show up in one small script that actually does something useful.
End-of-lesson quiz
Check what stuck
15 questions · Score saves to your progress.
Tutor
Curious about “Capstone — Python CLI That Summarizes With Claude”?
Ask anything about this lesson. I’ll answer using just what you’re reading — short, friendly, grounded.
Progress saved locally in this browser. Sign in to sync across devices.
Related lessons
Keep going
Creators · 50 min
Deploying an AI App to Vercel
Streaming AI chat to production takes one framework and three env vars. Learn the deploy path that actually ships.
Creators · 50 min
The Landscape: Copilot vs. Cursor vs. Windsurf vs. Claude Code
The AI coding tool market fragmented fast. Let's map the 2026 landscape honestly: who is for autocomplete, who is for agents, who wins on cost, and what the tradeoffs actually feel like.
Creators · 40 min
Agents vs. Autocomplete — the Mental Model Shift
Autocomplete is a suggestion. An agent is an actor. The mental model you bring to each is different, and conflating them is the number-one reason teams trip over AI coding.
