AI Agents by HandAll problemsAbout

All problems › Agent problem · hard · ~20 min

Resumable run with idempotent writes

resumability idempotency persistence json SDK blocks

Solve it in your browser →

Make the restock run resumable (~20 min)

resumable_run.py is a purchasing agent: two read tools (list_low_stock, get_supplier) and one write tool, create_purchase_order, which sends a real order to a supplier (PURCHASE_ORDERS). Its loop works, but a restock run takes several model calls, and when the process dies halfway (an API outage, a deploy, a crash) the operator reruns it from scratch. Orders that were already sent get sent again.

Goal: run_agent(prompt, client=None, max_turns=10, run_id=None, store_dir=STORE_DIR) -> str can be stopped at any point and rerun with the same run_id without losing progress or sending an order twice, except in one window: after the supplier accepts an order and before your save records it. No local design can close that window; you only need to keep it as small as possible (save right after the write returns). The tests crash the process at a model call or as a write starts (before the supplier sees it), never inside that window.

  1. Save progress. With a run_id, keep the run's state in store_dir / f"{run_id}.json": the conversation so far, plus a record of the writes already executed. Save it after each completed turn (the assistant message and its tool results), and also right after each write executes, since that's the moment a crash would cost an order. The file must be plain JSON, so convert the SDK's content blocks to dicts (they have model_dump()).
  2. Resume. If that file already exists when run_agent starts, load it and carry on from the saved conversation instead of starting over: the next model call gets exactly the saved messages (don't add the prompt again).
  3. Never repeat a write. Within a run, a write tool called with the same name and the same input as one already executed is not executed again: the model gets the saved result of the first call. This holds whether the repeat comes later in the same process or after a resume.
  4. Without a run_id nothing is saved, but rule 3 still applies within the run.
  5. Errors from the model call propagate as today (the saved file stays for the rerun). Tool failures are is_error results, max_turns raises RuntimeError, and read tools are never deduplicated.

You can assume a saved run never ended with a final answer (resuming a finished run is out of scope), and that ordering the same SKU, quantity and supplier twice in one run is always a duplicate.

Be ready to explain: which crash points lose what with your design, why the dedupe key is the input and not the tool_use id, how a real supplier API would close the remaining window (idempotency keys), and what changes if two processes resume the same run at once.

Try it: python -m resumable_run demo (needs a key; saves to runs/) Check it: pytest tests/test_resumable_run.py

Starter code and hidden tests open in the editor. Nothing to install.