ghosttrap-sdk
Error reporting for Python apps, built for AI agents. Part of ghosttrap.
Install
pip install ghosttrap-sdk
Use
import ghosttrap
ghosttrap.init("t_your_token_here")
Get your token by running ghosttrap setup from ghosttrap-cli.
Optional kwargs
ghosttrap.init(
"t_your_token_here",
server="https://ghosttrap.io", # override only if self-hosting
send_user=False, # see "User context" below
trap_logs=False, # see "Bare log errors" below
)
What it hooks into
sys.excepthook— unhandled exceptionsthreading.excepthook— unhandled exceptions in background threads (these never reachsys.excepthook)- Python logging —
logger.exception()andlogger.error(..., exc_info=True) - Celery — task failures via
celery.signals.task_failure(auto-detected)
Bare log errors (opt-in)
By default, only log records carrying an exception are reported. Pass trap_logs=True to also report logger.error() / logger.critical() calls with no exception — they arrive as type LoggedError / LoggedCritical with the log call site as the frame. Off by default deliberately: in a chatty codebase every error-level log line becomes an event, and error trackers that default this on are famous for the resulting floods. The server's 5-minute dedup window still applies either way.
What it sends
Every report includes the exception type, message, traceback, frames (file/line/function/code), and the server's hostname (socket.gethostname()).
Inside Docker that hostname is the container id, which names no machine and changes on every redeploy. Set GHOSTTRAP_SERVER_NAME in the container's environment (e.g. in the box's .env, passed through by compose) and it is sent as server_name instead.
Ad-hoc shell errors (shelved, never streamed)
Errors only stream if the crashing code lives in the repo. Code someone typed — manage.py shell / shell_plus, python -c, piped stdin, a REPL — has no file, no line, no commit, and its author already watched it fail in their own terminal. The SDK detects these processes at init() and tags their reports channel: adhoc; the server stores them on the same quarantined shelf as browser events (capped, deduped, pull-only via ghosttrap shelf) instead of waking the repo's agent. Service processes — web workers, celery, real management commands like migrate — stream normally.
Overrides: GHOSTTRAP_CHANNEL=service or GHOSTTRAP_CHANNEL=adhoc forces the channel; GHOSTTRAP_DISABLE=1 makes init() a no-op for that process.
Django
INSTALLED_APPS = [
...
"ghosttrap.django.GhostTrapApp",
]
MIDDLEWARE = [
"ghosttrap.django.GhostTrapMiddleware",
...
]
Browser / JavaScript errors (quarantined)
Browser errors are captured but never stream. A browser can't hold a secret, so its events are anonymous — and ghosttrap streams carry agent-to-agent messages that agents act on. Rather than let anonymous text into that channel, browser events go to a quarantined shelf: stored server-side, capped and deduped, never fanned out to peek/watch, never touching the cursor. Retrieval is pull-only via ghosttrap shelf (from ghosttrap-cli), which labels them as untrusted telemetry.
Wiring is the same two lines as before:
# urls.py
path("ghosttrap/", include("ghosttrap.django.urls")),
<!-- base template -->
<script src="{% static 'ghosttrap/ghosttrap.js' %}" defer></script>
The script hooks uncaught errors and unhandled promise rejections, drops known junk (opaque cross-origin "Script error.", browser-extension noise), dedupes per page, and caps itself at 10 reports per page load. The relay forwards to the server's /trapjs/ ingest using your init() config — no token in page source, no CORS, no third-party request for ad blockers to eat. Server-side: 5-minute dedup and a 500-event per-repo retention cap.
Manually trap an event
For errors you catch and handle but still want logged, or for non-exception conditions worth flagging:
import ghosttrap
try:
do_thing()
except ValueError as e:
ghosttrap.trap(e) # caught exception — sent with type, message, traceback
fallback()
ghosttrap.trap("payment gateway returned 503") # synthetic event labelled "TrappedEvent"
trap() accepts an exception instance or a string. Strings get the caller's stack attached and a TrappedEvent type so they're distinct from real exceptions in the CLI. Both go through the same 5-minute dedup window as the auto-hook.
User context
Off by default. Pass send_user=True to init() and the Django middleware will attach the authenticated user's id and username to each report. Has no effect outside Django.
ghosttrap.init("t_your_token_here", send_user=True)
Zero dependencies
Pure Python stdlib. No transitive dependencies in your production image.
Metadata
Release files for ghosttrap-sdk 0.4.12
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ghosttrap_sdk-0.4.12.tar.gz | 11.5 kB | Without provenance | Details for ghosttrap_sdk-0.4.12.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ghosttrap_sdk-0.4.12-py3-none-any.whl | Python 3 | none | any | Details for ghosttrap_sdk-0.4.12-py3-none-any.whl |
Total release size: 22.8 kB
Release files / ghosttrap_sdk-0.4.12.tar.gz
| Download URL | ghosttrap_sdk-0.4.12.tar.gz |
|---|---|
| Size | 11.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7a39aa5114149409db1ee63afcb6796cce1bf957573f7ff2bbd27727157f2f8b
|
|
BLAKE2b-256 checksum How to use checksums |
4528458a0346f6ec68f82798196afaf11c9fd09c9c1b79b8197a68151f7876a1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / ghosttrap_sdk-0.4.12-py3-none-any.whl
| Download URL | ghosttrap_sdk-0.4.12-py3-none-any.whl |
|---|---|
| Size | 11.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bff1a5c263b47d09d531d1a68b023b62e9e2b5f63cfac1972147d67963368e87
|
|
BLAKE2b-256 checksum How to use checksums |
bc232fc4cc079893f8938f4c70f4b7b478ef27dc815e3a9dd89aa194b46cb016
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for ghosttrap-sdk 0.4.12
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.