---
source: https://iinventedlight.com/library/notes/python-sys-monitoring.md
title: "Register a sys.monitoring callback for execution events"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "Public-Domain (Python PEPs); PSF-2.0 (Python docs); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python PEPs, PEP 669 – Low Impact Monitoring for CPython; Python docs, sys.monitoring — Execution event monitoring. Public-Domain; PSF-2.0."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: sys.monitoring and PEP 669"
tags: [python, sys, monitoring, pep-669, debug]
client: Librarian
sources:
  - id: S1
    title: "PEP 669 – Low Impact Monitoring for CPython"
    publisher: Python PEPs
    url: https://peps.python.org/pep-0669/
    accessed: 2026-11-19
    license: "Public-Domain"
  - id: S2
    title: "sys.monitoring — Execution event monitoring"
    publisher: Python docs
    url: https://docs.python.org/3/library/sys.monitoring.html
    accessed: 2026-11-19
    license: "PSF-2.0"
---
# Register a sys.monitoring callback for execution events
**Answer:** `sys.monitoring` is a namespace within the `sys` module, not an independent module. `import sys.monitoring` would fail with a `ModuleNotFoundError`. Import `sys` and then use `sys.monitoring`. PEP 669 proposes this API for monitoring Python programs running on CPython at low cost.

## The three parts

The monitoring API consists of three components: tool identifiers, events, and callbacks. The namespace exposes the functions and constants that activate and control event monitoring.

## Tool identifiers

A tool identifier is an integer and the associated name. Identifiers are integers in the range 0 to 5 inclusive. Before registering or activating events, a tool should choose an identifier and call `use_tool_id`. Tool identifiers are used to discourage tools from interfering with each other. Multiple tools can operate at the same time.

| sys.monitoring | events |
|---|---|
| sys.monitoring | Namespace within the sys module |
| tool identifiers | Integers in the range 0 to 5 inclusive |
| events | Execution events of interest |
| callbacks | Called when those events occur |

## See also

- [python-exceptions-hierarchy](https://iinventedlight.com/library/notes/python-exceptions-hierarchy/)
- [python-functools](https://iinventedlight.com/library/notes/python-functools/)
- [python-contextlib](https://iinventedlight.com/library/notes/python-contextlib/)

## Claims

- id: C1
  claim: PEP 669 proposes a low-cost API for monitoring programs running on CPython.
  source: S1
  evidence: "an API for monitoring Python programs running on CPython"

- id: C2
  claim: A sys.monitoring namespace holds the functions and constants.
  source: S1
  evidence: "A sys.monitoring namespace will be added"

- id: C3
  claim: sys.monitoring is a namespace in sys, not an importable module.
  source: S2
  evidence: "sys.monitoring is a namespace within the sys module"

## Sources

- [S1] PEP 669 – Low Impact Monitoring for CPython. Python PEPs. https://peps.python.org/pep-0669/ . License: Public-Domain. Accessed 2026-11-19.

- [S2] sys.monitoring — Execution event monitoring. Python docs. https://docs.python.org/3/library/sys.monitoring.html . License: PSF-2.0. Accessed 2026-11-19.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python PEPs, PEP 669 – Low Impact Monitoring for CPython; Python docs, sys.monitoring — Execution event monitoring. Public-Domain; PSF-2.0. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
