Lesson 10 / 26

Scripts: Deterministic Work Inside a Skill

Put exact, repeatable logic in code and let Claude call it.

Let code do what code is good at

Models are excellent at judgement and language, and unreliable at exact arithmetic, parsing and repetitive checks. A skill can bundle scripts (Python, Node, shell) that do such steps deterministically: validate a format, call an API, parse a log, compute a number, take a screenshot. Claude runs the script and reads its output; the script's source never needs to enter the context. Design scripts for an agent reader: accept clear arguments, print concise, structured output (JSON or short labelled lines), use meaningful exit codes, write helpful error messages that say how to fix the problem, avoid interactive prompts, and be idempotent when possible. List the exact command in SKILL.md, including the path relative to the skill folder, and say what the output means.

A script a skill could call, run

I ran this with plain Python 3 (standard library only). The function checks a commit message against a team format and returns structured JSON with a list of problems. Valid messages return ok true; a missing type and a capitalised summary each produce a specific problem. A skill would tell Claude to run this instead of eyeballing the format.

import json, re, sys

def check_commit_message(msg):
    """Deterministic helper a skill can call instead of asking the model to eyeball the format."""
    first = msg.splitlines()[0] if msg.strip() else ""
    problems = []
    m = re.match(r"^(feat|fix|docs|refactor|test|chore)(\([a-z0-9-]+\))?: (.+)$", first)
    if not m: problems.append("first line must look like 'type(scope): summary'")
    elif len(first) > 72: problems.append(f"first line is {len(first)} characters (max 72)")
    elif m.group(3)[0].isupper(): problems.append("summary should start lowercase")
    return {"ok": not problems, "problems": problems}

for msg in ("fix(cart): round discount to paise", "Fixed the bug", "feat: Add pagination"):
    print(json.dumps(check_commit_message(msg)))

Output:

{"ok": true, "problems": []}
{"ok": false, "problems": ["first line must look like 'type(scope): summary'"]}
{"ok": false, "problems": ["summary should start lowercase"]}

Return errors Claude can act on

"Missing --repo: pass the repository as owner/name" helps; a bare stack trace does not. Print the fix, not just the failure.

Quick check: What does a skill's script give you that free-form model output does not?

  • Deterministic, repeatable results and context-free logic
  • Better creative writing
  • A larger context window
  • Automatic approval of all actions
Answer

Deterministic, repeatable results and context-free logic — Code is predictable and its source does not consume context when run.