Skip to main content

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 exceptions
  • threading.excepthook — unhandled exceptions in background threads (these never reach sys.excepthook)
  • Python logging — logger.exception() and logger.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()).

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 jslogs (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.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ghosttrap-sdk 0.4.10
File Size Uploaded Provenance Actions
ghosttrap_sdk-0.4.10.tar.gz 10.0 kB Without provenance Details for ghosttrap_sdk-0.4.10.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ghosttrap-sdk 0.4.10
File Interpreter ABI Platform Actions
ghosttrap_sdk-0.4.10-py3-none-any.whl Python 3 none any Details for ghosttrap_sdk-0.4.10-py3-none-any.whl

Total release size: 20.1 kB

Release files / ghosttrap_sdk-0.4.10.tar.gz

Download URL ghosttrap_sdk-0.4.10.tar.gz
Size 10.0 kB
Tags Source
SHA-256 checksum
How to use checksums
39f5561ad2959d7abcabf84964bde31b9300433a0283a7d3bf85e4192419849f
BLAKE2b-256 checksum
How to use checksums
010a66f61bf66247708f7054f82cfb87d7f09b79620faf896ed8401294285598
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / ghosttrap_sdk-0.4.10-py3-none-any.whl

Download URL ghosttrap_sdk-0.4.10-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
00d425504a949dca08c52595ab6b0b70540f7b186701ee4a2570825fe1860a2c
BLAKE2b-256 checksum
How to use checksums
ce80cf391f91e9f94ff6c427eb30be506f0fb6676036e4acd7c0d4d9f517414f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Security for ghosttrap-sdk 0.4.10

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page