Skip to main content

tokentrap (Python backend)

Level-2 TokenTrap backend: full FastAPI service with persistent sessions, structured logging, canary tokens, an OpenAI-compatible bait endpoint, and a one-command CLI.

Defensive only. Traps fire on connections the attacker initiates — no exploits, no outbound traffic, no human deception. Operators are solely responsible for lawful use in their jurisdiction. See https://github.com/kineticquant/TokenTrap/blob/main/docs/ethics-and-legal.md.

Install

pip install "tokentrap"            # published package
# or from this monorepo:
pip install -e "packages/backend-python[test]"

60-second start

tokentrap serve --port 8787 --strength aggressive --canary-tokens audit-777
# then point the widget at it:
#   TokenTrap.init({ apiEndpoint: "http://127.0.0.1:8787", ... })

Environment variables (all optional): TOKENTRAP_PERSONA, TOKENTRAP_STRENGTH, TOKENTRAP_CANARY_TOKENS, TOKENTRAP_EXTRA_KEYWORDS, TOKENTRAP_RATE_LIMIT, TOKENTRAP_LOG_WEBHOOK, TOKENTRAP_LLM_MODEL.

Endpoints

Route Purpose
POST /api/chat Widget protocol ({sessionId?, message} -> {reply, turn, meta})
POST /v1/chat/completions OpenAI-compatible bait for agents scanning for exposed API endpoints
GET /api/healthz Liveness

/v1/chat/completions is fully stateless: the turn index is derived from the replayed message list, so agents that grow their transcript each iteration walk straight up the escalation ladder.

Embed in an existing FastAPI app

from fastapi import FastAPI
from token_trap import create_app, TrapConfig

app = FastAPI()
app.mount("/trap", create_app(TrapConfig(strength="maximum")))

Optional real-LLM dressing

With pip install "tokentrap[llm]", set llm_model / --llm-model and turn-0 messages get answered by a real model before the trap engages. Failures fall back to the static engagement payload.

Tests

pytest

Metadata

Release files for tokentrap 1.0.3

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

Source distribution (sdist)

Source distribution for tokentrap 1.0.3
File Size Uploaded
tokentrap-1.0.3.tar.gz 19.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tokentrap 1.0.3
File Interpreter ABI Platform
tokentrap-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 36.8 kB

Release files / tokentrap-1.0.3.tar.gz

Download URL tokentrap-1.0.3.tar.gz
Size 19.4 kB
Tags Source
SHA-256 checksum
How to use checksums
0a971b8ad323422619de2b9d69e10ec17c3e4b6a03abbdcf79ee46b057e82545
BLAKE2b-256 checksum
How to use checksums
b7e0e28175f004debad0a25c605194a9df1720c9633060859bef7a14c8fa1461
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 24, 2026.

Transparency log

Release files / tokentrap-1.0.3-py3-none-any.whl

Download URL tokentrap-1.0.3-py3-none-any.whl
Size 17.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1b3e6d853b0d112bf1ecff05c5f30e793e483c97abd1ec4c21f9628a2d638708
BLAKE2b-256 checksum
How to use checksums
3565cc0ae0c9165e653cd8810a115aa6cf58db5bb535f61b6633b8ce5786584c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 24, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

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