﻿{
    "cells":  [
                  {
                      "cell_type":  "markdown",
                      "metadata":  {

                                   },
                      "source":  [
                                     "# Module 4 Self-Driven Lab\n",
                                     "\n",
                                     "Use this notebook as your workspace for **Scaling with Message Batches**. The answer-key notebook sits next to this file. Work through each checkpoint before comparing against the completed version.\n"
                                 ]
                  },
                  {
                      "cell_type":  "markdown",
                      "metadata":  {

                                   },
                      "source":  [
                                     "# Module 4 â€” Scaling with Message Batches\n",
                                     "\n",
                                     "The Message Batches API runs high-volume, non-time-sensitive work asynchronously at **50% off** standard pricing. Most batches finish in under an hour; the API guarantees completion within 24 hours.\n",
                                     "\n",
                                     "\u003e **ZDR warning:** Batches are **not ZDR-eligible**. Inputs and results are stored server-side for up to 29 days. Don\u0027t batch PHI when ZDR is required."
                                 ]
                  },
                  {
                      "cell_type":  "markdown",
                      "metadata":  {

                                   },
                      "source":  [
                                     "## Setup"
                                 ]
                  },
                  {
                      "cell_type":  "code",
                      "execution_count":  null,
                      "metadata":  {

                                   },
                      "outputs":  [

                                  ],
                      "source":  [
                                     "# Setup cell. Run this before the exercise cells below.\n",
                                     "import anthropic\n",
                                     "from dotenv import load_dotenv\n",
                                     "\n",
                                     "load_dotenv()\n",
                                     "client = anthropic.Anthropic()"
                                 ]
                  },
                  {
                      "cell_type":  "markdown",
                      "metadata":  {

                                   },
                      "source":  [
                                     "## 1. Submit a batch\n",
                                     "\n",
                                     "Each request needs a unique `custom_id` â€” that\u0027s the only way to map results back to inputs, since batch results don\u0027t return in submission order. The first request also enables the 300k-token output beta header."
                                 ]
                  },
                  {
                      "cell_type":  "markdown",
                      "metadata":  {

                                   },
                      "source":  [
                                     "## Checkpoint 1\n",
                                     "\n",
                                     "Implement this step yourself. Use the preceding explanation and the module page as your guide, then compare your approach with the answer key after you have a working version.\n"
                                 ]
                  },
                  {
                      "cell_type":  "code",
                      "execution_count":  null,
                      "metadata":  {

                                   },
                      "outputs":  [

                                  ],
                      "source":  [
                                     "# TODO: Implement checkpoint 1 for Module 4.\n",
                                     "# Keep the cell focused, run it, inspect the output, then move to the next checkpoint.\n",
                                     "\n",
                                     "raise NotImplementedError(\u0027Complete checkpoint 1\u0027)\n"
                                 ]
                  },
                  {
                      "cell_type":  "markdown",
                      "metadata":  {

                                   },
                      "source":  [
                                     "## 2. Poll for completion\n",
                                     "\n",
                                     "`processing_status` flips from `\"in_progress\"` to `\"ended\"` once every request has finished (succeeded, errored, expired, or canceled)."
                                 ]
                  },
                  {
                      "cell_type":  "code",
                      "execution_count":  null,
                      "metadata":  {

                                   },
                      "outputs":  [

                                  ],
                      "source":  [
                                     "# Setup cell. Run this before the exercise cells below.\n",
                                     "import time\n",
                                     "\n",
                                     "BATCH_ID = message_batch.id\n",
                                     "\n",
                                     "while True:\n",
                                     "    status_update = client.messages.batches.retrieve(BATCH_ID)\n",
                                     "    if status_update.processing_status == \"ended\":\n",
                                     "        print(\"Batch processing complete!\")\n",
                                     "        break\n",
                                     "    counts = status_update.request_counts\n",
                                     "    print(f\"Still processing... (Succeeded: {counts.succeeded}, Errored: {counts.errored})\")\n",
                                     "    time.sleep(60)"
                                 ]
                  },
                  {
                      "cell_type":  "markdown",
                      "metadata":  {

                                   },
                      "source":  [
                                     "## 3. Stream and map results\n",
                                     "\n",
                                     "Always key on `custom_id`. You\u0027re only billed for `succeeded` results â€” `errored`, `expired`, and `canceled` requests are free."
                                 ]
                  },
                  {
                      "cell_type":  "markdown",
                      "metadata":  {

                                   },
                      "source":  [
                                     "## Checkpoint 2\n",
                                     "\n",
                                     "Implement this step yourself. Use the preceding explanation and the module page as your guide, then compare your approach with the answer key after you have a working version.\n"
                                 ]
                  },
                  {
                      "cell_type":  "code",
                      "execution_count":  null,
                      "metadata":  {

                                   },
                      "outputs":  [

                                  ],
                      "source":  [
                                     "# TODO: Implement checkpoint 2 for Module 4.\n",
                                     "# Keep the cell focused, run it, inspect the output, then move to the next checkpoint.\n",
                                     "\n",
                                     "raise NotImplementedError(\u0027Complete checkpoint 2\u0027)\n"
                                 ]
                  },
                  {
                      "cell_type":  "markdown",
                      "metadata":  {

                                   },
                      "source":  [
                                     "## Reflection\n",
                                     "\n",
                                     "Before opening the answer key, write down what worked, what failed, and which API behavior or agent-design rule you would rely on in production.\n"
                                 ]
                  }
              ],
    "metadata":  {
                     "kernelspec":  {
                                        "display_name":  "Python 3",
                                        "language":  "python",
                                        "name":  "python3"
                                    },
                     "language_info":  {
                                           "name":  "python",
                                           "version":  "3.9"
                                       }
                 },
    "nbformat":  4,
    "nbformat_minor":  5
}
