Skip to content
mentoor

Topic 1 of 5 · 4 min read

Letting an agent drive your notebook

Goal: Review an agent's proposed notebook cells and decide which to run, edit or reject before they touch the data.

Let the agent propose one cell at a time, and read each cell before it runs: a notebook cell executes against your real data the moment you press run.

Check yourself · 1 of 3

An assistant proposes a notebook cell that ends with df.to_csv('customers.csv'). What is the main thing to check before running it?

Choose one answer

Why it matters

Notebook assistants in Jupyter AI, Colab, Hex and marimo can write, run and fix cells for you. That speed is useful for exploration. The risk is that a notebook is a live session: a cell can overwrite a dataframe, delete a file, write to a database or call a paid API. A bad suggestion in a chat is just text. A bad cell is an action.

How to do it

  1. Give the agent the shape, not the data. Share column names, types and a few rows. State the question in one line: "monthly revenue per region, refunds excluded."
  2. Ask for small steps. One cell to load, one to clean, one to aggregate. Small cells can be checked by eye; a 60-line cell cannot.
  3. Read before you run. For every cell, check four things:
    • Inputs: which files, tables or variables does it read?
    • Writes: does it save, overwrite, drop, delete or call out to a network service?
    • Filters: which rows does it silently remove (dropna, drop_duplicates, a WHERE)?
    • Names: does it reuse a variable you already have and replace it?
  4. Run reads freely, gate writes. Loading and previewing are safe. Anything that writes or deletes needs your explicit yes, ideally pointed at a scratch copy first.
  5. Print the evidence. After each step ask for df.shape, df.head() and a count of nulls so you see what actually happened.

Before and after

Weak request: "Clean the sales data and save it."

The agent returns one cell that drops every row with any null, overwrites sales.csv and prints "Done". You never see how many rows went.

Better request: "Load sales.csv into raw. In a separate cell, make clean from raw, do not modify raw, and print the row count before and after. Do not write any files."

Now the original stays intact, the loss is visible, and saving is a later, deliberate step.

What goes wrong

  • Run-all on an unread notebook. Cells run in order and one of them contains a delete or an overwrite.
  • Stale state. The agent's fix cell assumes a variable that exists only because of a cell you ran earlier and later edited.
  • Confident output, wrong data. The agent says "revenue is 1.2M" but computed it from a dataframe you had already filtered.
  • Credentials in cells. Generated code pastes a connection string or token into a cell that you then share.

Examples

Reviewing a proposed cell

The agent proposes:

df = df.dropna()
df.to_csv('orders.csv', index=False)

Two problems before running: dropna() removes any row with a null in any column, including optional ones like coupon_code, and to_csv overwrites the source file. Edit it to clean = df.dropna(subset=['order_id', 'amount']), print len(df) and len(clean), and write to orders_clean.csv.

A scoped request

Prompt: "I have a table payments(payment_id, user_id, amount_cents, created_at). Write one cell that computes daily totals in a new dataframe daily. Read only. Do not change payments. Print daily.shape." The constraints tell the agent what it may not touch and give you a check to compare against.

Key points

  • A notebook cell is an action on live data, so read each generated cell before you run it.
  • Give the agent column names, types and a few rows instead of the full dataset.
  • Ask for one small step per cell and request shape and null counts after each step.
  • Gate every write, delete or network call behind your explicit approval, ideally on a scratch copy.
  • Keep the raw dataframe untouched and build cleaned versions under new names.

Common mistakes

  • Pressing run-all on a notebook whose generated cells you have not read.
  • Letting the agent overwrite the source file or table instead of writing a new one.
  • Accepting a cleaned dataframe without checking how many rows were removed.
  • Pasting real tokens or connection strings into a cell, then sharing the notebook.

Check yourself · 2 of 3

You want an agent to clean a dataframe. Which request keeps you able to see what changed?

Choose one answer

Check yourself · 3 of 3

What should you share with a hosted notebook agent so it can write correct code for a table?

Choose one answer

Practice items, not counted towards your mastery.

Start here