code_output_guide.md

Created about 12 hours ago

attributes
source_pathreferences/code_output_guide.md
source_sha256b23f0b1c607029e374cdf2ccc8f9a7fc080f200a2d5405a87d35dec8a2ebb138
source_urlhttps://github.com/aws/agent-toolkit-for-aws/blob/903433d67cc4eeab334355620373f33a0c162dd4/skills/core-skills/aws-ai-ml/references/code_output_guide.md
supporting_skill_id40202
upstream_revision903433d67cc4eeab334355620373f33a0c162dd4

Code Output Guide

Mode Selection

Ask the user once before generating code: "Would you like me to generate a Jupyter notebook or a Python script?"

Some steps need the code to execute to have any effect (e.g. eval-criteria Step 6, which registers the criteria in Hub). The mode stays the user's choice — but such a step is not finished when the file is written, only when the code has run and the step's own verification has passed. This applies only where not executing would silently skip the side effect the step exists to produce; a step that generates code for the user to run later is finished when the code is written, so don't extend the rule to it. In Notebook Mode that means executing the cells; if no execution tool is available and the user would rather not run them, offer to emit the same template as a script and run that, instead of reporting the step done.

Shared Rules (Both Modes)

  • Use EXACTLY the imports shown in each code template — do not add extras
  • Replace [PLACEHOLDER] values with user-specific configuration
  • Include set_attribution(Attribution.SAGEMAKER_AGENT_PLUGIN) in the setup cell/section (only when generating code that uses from sagemaker.* imports)

Reading Code Templates

Templates use # Cell N: Label markers to delimit sections. # NOTEBOOK_ONLY skips a line in script mode; # NOTEBOOK_ONLY_SECTION on a # Cell N: line skips the entire section.

Notebook Mode

Write a .ipynb file in <project-dir>/notebooks/.

Naming and appending:

  • Notebook path: <project-dir>/notebooks/<project-name>.ipynb
  • If the notebook already exists → ask: "Would you like me to append cells to the existing notebook, or create a new one?"
  • If it doesn't exist → create it
  • When appending, use the template's # Cell 0 [markdown]: cell as the section divider before the new cells

Formatting:

  • Use your file write tool to create the complete notebook JSON, OR use notebook MCP tools (create_notebook, add_cell) if available
  • Do NOT use bash commands, shell scripts, or echo/cat piping
  • 2-space JSON indentation
  • Each source line is a separate string ending with \n (except the last)
  • Escape quotes: \"
  • No trailing commas

Structure:

  • Wrap cells in {"cells": [...], "metadata": {...}, "nbformat": 4, "nbformat_minor": 4}
  • Code cells: cell_type, execution_count: null, metadata: {}, outputs: [], source: [...]
  • Markdown cells: cell_type: "markdown", no execution_count or outputs
  • # Cell 0 [markdown]: becomes a markdown cell; all others become code cells

Execution:

  • If notebook execution tools are available (e.g., run_cell MCP), offer to run cells for the user. If not available, tell the user to run cells themselves. When the step needs the code to execute (see Mode Selection), do not report it complete until the cells have actually run.
  • Do NOT use bash commands or inline scripts to execute notebook cells.

Script Mode

Write a numbered .py file in <project-dir>/scripts/.

Naming:

  • Format: NN_<descriptive_name>.py (e.g., 01_sft_finetuning.py) — use the next available number in <project-dir>/scripts/

Formatting:

  • Plain Python file, standard text
  • Use # %% cell markers to preserve logical sections (IDE-compatible)
  • Include a docstring at the top describing what the script does
  • # Cell 0 [markdown]: → a comment block or docstring

Dependencies:

  • Install any required pip packages directly (e.g., pip install 'sagemaker>=3.7.1,<4.0') before writing or running the script. Do not embed install commands in the script itself.

Execution:

  • After generating the script, ask the user to choose how it runs:
    • Leave it to you: the user runs python3 <script>.py. Done. Wait for user to come back.
    • Run it and wait: execute the script directly via python3 <script>.py. Report final status when complete.
    • Start it but don't wait: execute the script in background, or submit the job and keep its name for status checks after submission.

If the conversation context makes it clear the user wants the agent to execute (e.g., "just run it", "do it", or the plan says "run directly"), skip the question and execute immediately (run it and wait).

Resumption After Interruption

If the conversation was interrupted while a job was running (e.g., context compaction, user stopped and restarted, connection drop), do NOT re-run the script. Instead, check for an existing job by name or ARN from the conversation context or the job's launch entry in <project-dir>/run-log.jsonl (directory-management/overview.md § Run Log), and monitor its status rather than launching a duplicate.

Children

No children yet.

Like how this is organised?

Build your own Memory tree on knowen.md: notes, decisions and tasks in one connected markdown graph, shared with your team and your AI agents.