Art Of Debugging logo

Art Of Debugging

CommunityPopular
stas00
art-of-debugging

Systematic methodology and concrete tool recipes for debugging Unix, Python, and PyTorch programs - crashes, hangs, segfaults, wrong output, CUDA OOM, NaN/Inf, slowness, and multi-node/multi-GPU issues. Use when a program crashes, hangs, deadlocks, segfaults, runs out of memory (OOM), produces NaN/Inf or wrong numbers, runs too slowly, or when the user mentions gdb, strace, py-spy, core files, CUDA_LAUNCH_BLOCKING, ldd/nm/LD_PRELOAD, cProfile, or distributed training hangs. Distilled from "The Art of Debugging", the latest version of which can be found at https://github.com/stas00/the-art-of-debugging The latest SKILL.md version can be found at https://github.com/stas00/the-art-of-debugging/blob/master/SKILL.md

Overview

Publisherstas00
Repositorythe-art-of-debugging
Skill nameart-of-debugging
Stars
1.7K
Forks
108
Bundled files
53
LicenseCC-BY-SA-4.0
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 53 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by stas00 on GitHub. Read the source before you install it.

Installation

Install the Art Of Debugging AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/stas00/the-art-of-debugging.git \
  .claude/skills/art-of-debugging
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Art Of Debugging in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Art Of Debugging on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Art Of Debugging is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

The Art of Debugging

Distilled from The Art of Debugging Open Book by Stas Bekman - source: https://github.com/stas00/the-art-of-debugging (CC BY-SA 4.0). This skill is a condensed index; each section links back to the full chapter for depth.

Actionable methodology + copy-paste recipes for debugging Unix / Python / PyTorch programs. Apply the general loop first; then jump to the domain cheatsheet for the failure at hand. For scaling this up to large-model training/inference on real clusters (compute/storage/network, SLURM, throughput/memory, instabilities, fault tolerance, inference), pair this with Machine Learning Engineering.

The debugging loop

The single most important idea: most of the effort is in locating the cause; once you truly understand it, the fix is usually easy. Optimize everything for reaching understanding faster.

  1. Reproduce reliably. Get one command that triggers the bug every time. If it's flaky, pin the nondeterminism (seeds, ordering, timing, network, uninitialized memory) first - you can't debug what you can't repeat.
  2. Shrink the payload. Make the repro fast: fewer layers, tiny model/data, one process, one CPU/GPU, one node. A 2-second repro beats a 2-minute one - you'll run it hundreds of times. See methodology.
  3. Localize. Confirm you're editing the code that actually runs (the die trick), then bisect the search space: which commit, which file/function, which line, which input, which rank.
  4. Get a usable signal. Turn a cryptic failure into a precise one: a real traceback (sync mode), a stack dump (py-spy/gdb), a syscall trace (strace), a printed value at the boundary, or a min/max/NaN check on a tensor.
  5. Change one thing, re-run, verify. Fix on the fast repro, confirm, then re-widen to the full payload. Revert anything that didn't help.

Make the loop fast and reliable

Localization techniques

  • Am I editing the right file/class? Insert a guaranteed break where you think execution goes; if the program doesn't die, you're in the wrong file/class/env:
    python
    def suspect():
        die   # NameError -> proves this code runs; the traceback also names the caller
    traceback.print_stack() shows callers without stopping (useful when the same function is reached via many paths). See am I editing the right file and the right class?.
  • Bisect a regression. git bisect start / bad / good <rev> walks commits automatically to the one that broke things - script the test for git bisect run. See finding a breaking commit by bisecting.
  • Small/synthetic payload first. Use tiny or synthetic inputs; switch to real data only when the bug is data-dependent. See real vs random vs synthetic data.
  • Race conditions. Reordering/timing bugs hide under async; forcing synchronous execution can expose (or mask) them - note which. See avoiding race conditions and async vs sync mode.

Reproducing resource & environment issues

  • Cap resources on purpose to test failure paths: emulate a nearly-full disk, limited CPU RAM, or limited GPU memory. See running out of resources.
  • Watch resources live. watch -n1 nvidia-smi / free -h / df -h in a second visible terminal to correlate a hang/OOM with what the machine is doing. See watching and reproducing resource issues.
  • Inject sleep to freeze a program at the interesting moment so you can attach a debugger or snapshot state. See uses for sleep.
  • HPC/SLURM: keep the allocation and re-run with srun instead of re-sbatch-ing to cut per-iteration overhead. See SLURM salloc and srun fast debug combo.

Unix / shell

Full chapter: Unix Tools for Debugging.

  • Make shell scripts fail loudly and traceably:
    bash
    set -e          # abort on first error
    set -o pipefail # a failing command anywhere in a pipe fails the whole pipe
    set -u          # abort on undefined variables (catches typos)
    set -x          # trace: print each command with expanded values as it runs
    set +x          # turn tracing back off around a noisy region
    Combine as set -euo pipefail. See controlling script execution.
  • strace - trace system calls to see what a program actually does (files, network, why it's stuck):
    bash
    strace python -c "print('hi')"                 # trace from the start
    strace --pid PID                               # attach to a running/stuck process
    strace -o log.txt -f torchrun ...  # -f follows forked children
    strace -e trace=open,openat,read python prog.py           # filter to specific syscalls
    strace -e trace=network -p PID                            # is it stuck on a socket?
    Classic use: a process at 100% CPU with no output, or hung on I/O/network. See strace.
  • nohup - survive logout/disconnect (don't lose a long run to a dropped SSH):
    bash
    nohup ./long-running-command > log.txt &
    See nohup.
  • make - after editing compiled sources, rebuild before re-testing, or you'll debug a stale binary. See make.
  • Terminal ergonomics: search long scrollback and copy multi-line commands cleanly; keep an informative prompt (host, path, git branch, last exit code) so you always know where/what ran. See shell environment.

Compiled programs (C/C++, extensions, shared libraries)

Full chapter: Debugging Compiled Programs. Compile with -g for debug symbols.

  • Segfault -> backtrace from a core file:
    bash
    ulimit -c unlimited                                       # allow core dumps in this shell
    sudo sysctl -w kernel.core_pattern=/tmp/core-%e.%p.%h.%t  # control where cores go
    ./program                                                 # crash -> core file written
    gdb ./program /tmp/core-...                               # or: gdb -c core ./program
    At the (gdb) prompt:
    bt                    # backtrace (read bottom-up: outermost caller -> crash site)
    bt full               # + local variable values at each frame
    thread apply all bt   # backtrace for every thread (essential for multithreaded crashes)
    See segmentation fault, core files and gdb.
  • No core? Run it under gdb and step to the crash:
    bash
    gdb ./program
    (gdb) run            # then: bt / break FILE:LINE / next / step / print VAR / continue
    See run the program under gdb.
  • Inspect / snapshot a running process:
    bash
    sudo gdb --pid=PID    # attach; then: thread apply all bt
    gcore PID             # force a core dump without killing (or: kill -ABRT PID)
    See get the backtrace from the still running process.
  • "symbol not found" / wrong library loaded:
    bash
    ldd ./program                             # which shared libs resolve, and to what paths
    LD_LIBRARY_PATH=/path/to/libs ./program   # prepend a search dir
    nm -D libfoo.so | grep symbol             # is the symbol actually exported? (T=defined, U=undefined)
    LD_PRELOAD=/path/to/shim.so ./program     # force-load / override a library
    See debugging shared libraries and symbol resolution (ldd, nm).

Python

Full chapter: Debugging Python Programs.

  • Print effectively instead of scattering bare print:
  • Run the code you think you're running. Edits not taking effect? Wrong copy is imported:
    bash
    pip install -e .               # run from the source tree, not a copied install
    PYTHONPATH=src python prog.py  # or point Python straight at the source
    python -c "import pkg; print(pkg.__file__)"   # confirm which file is actually loaded
    See ensuring the Python package you edit is the one that is run and make tests use the git repo's packages.
  • Who called this? traceback.print_stack() or the die trick to reveal the caller in complex codebases. See who is calling?.
  • Diagnose a hang (process alive but stuck) with py-spy - no code changes, attaches live:
    bash
    pip install py-spy
    py-spy dump -n -p PID          # -n also shows native (C/C++ extension) frames
    # all Python subprocesses at once (skip the launcher):
    pgrep -P $(pgrep -o python) | xargs -I {} py-spy dump --pid {}
    No sudo? echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope. The first line of each dump is where it's stuck. See py-spy.
  • Slow code -> profile before optimizing (measure, don't guess):
    bash
    python -m cProfile -s cumtime prog.py     # what dominates cumulative time
    kernprof -l -v prog.py                     # line_profiler: per-line timing of @profile funcs
    For sub-ms functions, bump pstats precision (e.g. pstats.f8 = lambda x: f"{x:6.3f}") so timings aren't all 0.000. See profilers and cProfile.

PyTorch (incl. CUDA / multi-GPU / multi-node)

Full chapter: Debugging PyTorch Programs.

Debug fast

Shrink the model, not the problem - make a full run finish in seconds:

Cryptic CUDA errors

CUDA is async, so the reported line is usually wrong. Force a real traceback:

bash
CUDA_LAUNCH_BLOCKING=1 python prog.py   # sync CUDA -> accurate Python traceback
CUDA_VISIBLE_DEVICES="" python prog.py  # run on CPU (if feasible) for the clearest traceback

See dealing with async CUDA bugs.

CUDA / CPU OOM

NaN/Inf & wrong numbers

python
torch.autograd.set_detect_anomaly(True)   # pinpoint the op that first produced NaN/Inf in backward

Find where bad values first appear; watch fp underflow/overflow (especially fp16/bf16); expect small, benign cross-device numeric differences. Inspect tensors compactly (shape/device/dtype/stats) and use lovely-tensors for one-line summaries that surface bad tensors fast. See detecting problematic tensor values, underflow and overflow detection, floating point discrepancies across devices, dumping tensor values, and auto-dumping tensor attributes.

Segfault in a PyTorch/NCCL extension

Same core-file + gdb flow as compiled programs, but activate the exact python env that produced the core or gdb can't unpack it:

bash
conda activate my-env
gdb python core-python-...      # then: bt / thread apply all bt

See segfaults and getting a backtrace from a core file.

Multi-GPU / multi-node hang or deadlock

  1. Verify comms first with a minimal all-reduce test (torch-distributed-gpu-test.py); rule out network/NCCL before app code. See getting nodes to talk to each other and InfiniBand connection.
  2. Dump every rank's stack at once with py-spy (recipes for python/deepspeed/accelerate, across nodes via srun/pdsh). Ranks stuck at different lines reveal the desync (a mismatched collective). See diagnosing crashes, hangs and tracing execution.
  3. Make distributed output legible: prefix every log line with node:rank, and target pdb at one rank. See prefixing logs, pdb on a specific rank.
  4. Narrow further: check for a network-level hang, isolate a bad GPU, or trace line-by-line with the python trace module. On AMD, a slow/hung run may be IOMMU-related.

For the cluster-level context around these bugs (verifying node connectivity, NCCL/InfiniBand tuning, network benchmarking, checkpointing/fault tolerance), see Machine Learning Engineering.

Performance

  • Time regions precisely. For GPU work use CUDA events (CPU timers lie because kernels are async):
    python
    s, e = torch.cuda.Event(enable_timing=True), torch.cuda.Event(enable_timing=True)
    s.record(); run(); e.record(); torch.cuda.synchronize()
    ms = s.elapsed_time(e)
    See measuring durations.
  • Profile ops with torch.profiler (CPU+GPU, op-level, small overhead); when it's not enough, drop to cProfile for pure-Python hot spots. See performance and profiling.

Pick the tool by symptom

SymptomReach for
Stuck / 100% CPU / no outputpy-spy dump (Python), strace --pid (syscalls), gdb --pid (native)
Multi-GPU/node hangminimal collective test -> py-spy across all ranks -> node:rank logs
Segfault / crash in C or extensioncore file + gdb (bt, bt full, thread apply all bt)
Cryptic CUDA error / wrong lineCUDA_LAUNCH_BLOCKING=1, or run on CPU
CUDA/CPU OOMforward vs backward; fragmentation (PYTORCH_ALLOC_CONF); memory profiler
NaN/Inf / wrong numbersset_detect_anomaly, under/overflow detection, per-tensor stats, lovely-tensors
"my edits do nothing"the die trick; pip install -e . / PYTHONPATH; check pkg.__file__
Who calls this?traceback.print_stack() / die
Wrong/missing shared libldd, nm -D, LD_LIBRARY_PATH, LD_PRELOAD
Too slow (Python)cProfile -s cumtime, line_profiler
Too slow (PyTorch/GPU)CUDA events, torch.profiler
Regression appearedgit bisect run
Script fails silentlyset -euo pipefail, set -x
Flaky / non-deterministicpin seeds/order/timing; force sync; check race conditions
Long run dies on disconnectnohup ... > log & (or tmux/screen)

Notes for AI agents

  • Observe before guessing: obtain a stack dump / traceback / syscall trace / boundary value / tensor stat before proposing a cause; don't speculate from the error string alone.
  • Secure a fast, reliable repro first, then optimize its speed - iteration count matters more than any single clever idea.
  • Change one variable at a time, re-run the repro, and revert changes that don't move the needle.
  • Confirm you're running the code you edited (pkg.__file__, the die trick) before deeper investigation - a huge share of "impossible" bugs are wrong-file/wrong-env.
  • Read the linked chapter section before applying an unfamiliar recipe - each has worked examples, caveats, and copy-paste scripts.
  • Prefer built-in, low-overhead tools (py-spy, strace, gdb, env vars) that need no source changes and work on already-running processes.
  • For large-scale ML training/inference engineering (bottleneck analysis, throughput/memory, distributed hangs at cluster scale, fault tolerance), use the companion skill: Machine Learning Engineering.

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Art Of Debugging AI skill do?

Systematic methodology and concrete tool recipes for debugging Unix, Python, and PyTorch programs - crashes, hangs, segfaults, wrong output, CUDA OOM, NaN/Inf, slowness, and multi-node/multi-GPU issues. Use when a program crashes, hangs, deadlocks, segfaults, runs out of memory (OOM), produces NaN/Inf or wrong numbers, runs too slowly, or when the user mentions gdb, strace, py-spy, core files, CUDA_LAUNCH_BLOCKING, ldd/nm/LD_PRELOAD, cProfile, or distributed training hangs. Distilled from "The Art of Debugging", the latest version of which can be found at https://github.com/stas00/the-art-o...

Why use Art Of Debugging on TypingMind?

Because you install it once and use it with any model. Art Of Debugging is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Art Of Debugging in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/stas00/the-art-of-debugging/tree/master. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Art Of Debugging?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Art Of Debugging?

As many as you like. As long as a model supports skills, you can use Art Of Debugging with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Art Of Debugging AI skill free?

Yes. It is published on GitHub by stas00 under the CC-BY-SA-4.0 license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇