AI Agents · workshop

Workshop · Build an AI agent in one Python file

A chatbot talks.
An agent acts.

Today we build Tidy, a small agent that cleans a messy Downloads folder. One file, two tools, one loop. Then you use the same recipe for your own daily tasks.

Chatbot

answers

You ask: “Clean my Downloads folder.”

Sure! Here is how to do it:
  1. Create folders called Images, Documents, Audio…
  2. Drag each file into the right folder.
  3. Check that nothing is left.

Result: you still have 10 files to move yourself.

Agent

does the job

You ask: “Clean my Downloads folder.”

  • actlist_files()
  • see10 loose files
  • actmove_file() × 10
  • actlist_files()
  • seenothing left

Result: the folder is clean. You did nothing.

01

The recipe: four parts

Every agent, small or huge, is built from the same four parts. Tidy uses the simplest version of each.

1

Goal

What you want, in plain English.

GOAL = "Clean up my Downloads folder…"
2

Model

The brain. It reads the goal and decides the next move.

MODEL = "claude-opus-5-5"
3

Tools

The hands. Normal Python functions the model is allowed to ask for.

list_files() · move_file()
4

Loop

Repeat think, act, see until the goal is done.

for round_number in range(1, 16):
repeat until the goal is done no tool needed ANSWER THINK ACT SEE
Indigo = the model thinks. Orange = a tool runs. Green = the result comes back. The same colors are used everywhere on this page.
02

Watch Tidy work, step by step

This is a replay of a real run of the code below. Press Next step and read the line under the screens.

~/my_downloads/
Loose files
No loose files left
Sub-folders
python agent.py

    Step 0 / 9 Round – Tool calls 0 Files sorted 0 / 10

    Keyboard: → next step · R reset

    03

    The code, part by part

    The whole agent is one Python file. Read the left column first, then the code.

    File: agent.py · Python 3.10 or newer · library: anthropic

    04

    Run it on your laptop

    Save the code as agent.py, open a terminal in the same folder, then do these three steps.

    1. Install the library

    One time only.

    pip install anthropic

    2. Add your API key

    Create a key at platform.claude.com. Never paste it inside the code.

    macexport ANTHROPIC_API_KEY="sk-ant-..."
    win$env:ANTHROPIC_API_KEY="sk-ant-..."

    3. Run the agent

    The first run creates my_downloads with 10 demo files, then sorts them.

    python agent.py

    Each run is paid per token on your Anthropic account. For a cheaper run, change MODEL to "claude-haiku-5-5". To start again with a messy folder, delete my_downloads and run again.

    Example output · the wording changes on every run
    --- Round 1 ---
    [THINK] I need to see what is in the folder before I sort anything.
    [ACT]   list_files({})
    [SEE]   beirut_sunset.jpg
    budget_november.xlsx
    cv_2026.pdf
    …7 more files
    
    --- Round 2 ---
    [THINK] Sort by extension: images, documents, audio, video, other.
    [ACT]   move_file({'filename': 'beirut_sunset.jpg', 'folder': 'Images'})
    [SEE]   Moved beirut_sunset.jpg -> Images/
    …9 more moves
    
    --- Round 3 ---
    [ACT]   list_files({})
    [SEE]   (no loose files left)
    
    --- Round 4 ---
    [AGENT] Sorted 10 files into 5 folders.
    Images 2, Documents 4, Audio 2, Videos 1, Other 1.
    Nothing was deleted.
    
    Stopped after 4 rounds. Reason: end_turn
    05

    Same loop, any daily task

    Tidy is the training example. For a real daily task you change three things: what starts the agent, its goal and its tools. The loop stays exactly the same.

    An agent is not always a chat

    A fifth part makes an agent useful every day: the trigger, the thing that starts it. A chat message is only one of four.

    Message

    You send it something: a text, a photo of a receipt, a voice note.

    a Telegram bot receives it
    Schedule

    It wakes up on its own, every morning at 8 or every Sunday night.

    cron (Mac, Linux) · Task Scheduler (Windows)
    Event

    Something happens: a new email, a new file, a form is sent.

    check every 5 minutes, or a webhook
    Button

    You click once or type one command, and it does the whole job.

    python agent.py

    The most useful daily agents never chat. They wake up, do the job and leave the result where you already look: a sheet, your calendar, a draft, a notification. No code? n8n, Make and Zapier each have an AI agent block built from the same parts.

    Six agents for daily life

    06

    Agent, script or chatbot?

    Not every task needs an agent. Answer four questions before you build anything. Try the examples first, then your own task.

    Examples

    Does it repeat?Every day, every week, every month.

    Does it need judgment?Reading, understanding, choosing, writing.

    Can you give it the tools?Email, calendar, files, web, a sheet, an app with an API.

    Is a mistake easy to undo?A wrong label is easy. A wrong payment is not.

    About Tidy: sorting by file extension is a fixed rule, so a ten-line script could do the same job. We used it because you can watch every step. An agent earns its place when the job needs reading and judgment, for example “put each PDF in the folder of its course”.

    07

    Four rules for a safe agent

    An agent acts on real things. These four choices in the code keep it under control.

    Few tools

    Two tools are enough for this job. Every extra tool is one more way to make a mistake.

    A fence

    The tools only work inside my_downloads. A name with / or .. is refused.

    A stop

    MAX_ROUNDS = 15. If the agent gets stuck, the loop ends anyway.

    A human says yes

    Tidy has no delete tool, so it cannot delete. In general the agent prepares, and a human approves anything that sends, pays or deletes.

    08

    Your turn: design your own agent

    Pick one task from your week and fill the card. The start of your agent.py writes itself on the right. The loop is the one you already have.

    Your agent.py starts like this
    
            

    Practice on Tidy first

    Easy · change 1 sentence

    A new folder

    Edit GOAL so spreadsheets (.xlsx) go to a folder called Spreadsheets. Delete my_downloads and run again.

    Medium · add 1 tool

    Rename files

    Write rename_file(old, new), add it to TOOLS and run_tool. Goal: put today's date at the start of every screenshot name.

    Hard · a new agent

    Note summarizer

    Keep the loop. Replace the tools with read_file and write_file. Goal: read every .txt note and write one summary.md.