Local-first semantic drift detection for LLM outputs.
Project description
DriftScope
DriftScope is a local-first Python library for detecting semantic drift in LLM outputs. It wraps your model calls, stores prompts and responses in SQLite, embeds outputs locally, calibrates a baseline from your own traffic, and reports when later outputs move outside the learned normal range.
No hosted dashboard. No external data pipeline. No cloud account required.
Install
pip install llm-driftscope
For sentence-transformer embeddings:
pip install "llm-driftscope[embeddings]"
Quick Start
from driftscope import monitor
@monitor(session="support-bot")
def call_llm(prompt: str) -> str:
return my_llm_client.chat(prompt)
answer = call_llm("How do I reset my password?")
By default, events are stored at .driftscope/driftscope.sqlite3.
Generate a local report:
driftscope report --session support-bot
Try the included drift simulation:
python examples/simulate_drift.py
Sample output:
DriftScope report
Session: demo
Samples: 9
Status: drift
Latest score: 1.0015
Latest distance: 1.0202
Threshold: 0.2229
Topic: warming up; topic drift needs two windows
Changepoint: warming up; changepoint detection needs more samples
Drift events: 3
Why DriftScope Exists
Infrastructure metrics can tell you that a service is healthy. They cannot tell you that an LLM has quietly started answering in a different semantic region than it used to. Hosted observability platforms can help, but many developers need a small embeddable library that works inside an existing codebase and keeps data local.
DriftScope focuses on that gap.
Methodology
Fixed distance thresholds are brittle. A customer support bot should usually be consistent. A creative writing assistant naturally has wider semantic variation. DriftScope avoids hardcoded assumptions by calibrating against each application's own warmup data.
During warmup, DriftScope stores output embeddings and estimates the empirical distribution of distances from the baseline centroid. Once enough samples exist, it sets the alert threshold from a high percentile of that null distribution. New outputs are compared against the centroid, smoothed with EWMA, and flagged when the smoothed drift score crosses the calibrated threshold.
The v1 detector reports:
- raw semantic distance from the baseline
- EWMA-smoothed drift score
- calibrated threshold
- drift status
- warmup progress
- topic distribution drift
- changepoint detection over semantic distance
API
from driftscope import DriftConfig, DriftScope
config = DriftConfig(
warmup_samples=50,
threshold_percentile=99,
ewma_alpha=0.3,
)
scope = DriftScope(session="my-app", config=config)
result = scope.record(prompt="hello", output="world")
if result.drifted:
print(result.score, result.threshold)
Decorator form:
from driftscope import monitor
@monitor(session="my-app")
def generate(prompt: str) -> str:
return "model output"
CLI
driftscope report
driftscope report --session my-app
driftscope report --db .driftscope/driftscope.sqlite3
driftscope report --json
driftscope sessions
driftscope export --format csv --out events.csv
Optional sidecar API:
pip install "llm-driftscope[server]"
driftscope serve
The sidecar exposes GET /health, POST /events, and GET /report.
Optional Extras
pip install "llm-driftscope[topics]"
pip install "llm-driftscope[changepoints]"
pip install "llm-driftscope[server]"
pip install "llm-driftscope[full]"
Development
pip install -e ".[dev]"
python -m pytest
python -m build
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llm_driftscope-0.1.0.tar.gz.
File metadata
- Download URL: llm_driftscope-0.1.0.tar.gz
- Upload date:
- Size: 15.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5a1395e885a7c6c370ab784a5f11c38d63ced6c7c2106b3b780c65c5111bba8
|
|
| MD5 |
07a13591b1805babe6ddcb1de22169a2
|
|
| BLAKE2b-256 |
09d27231e7a373cdfea4f84ddb1a279c3c9769fc0fd2acde46d9affd6441c260
|
File details
Details for the file llm_driftscope-0.1.0-py3-none-any.whl.
File metadata
- Download URL: llm_driftscope-0.1.0-py3-none-any.whl
- Upload date:
- Size: 17.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bd7190a298175670798589137db9d512eabdc0483a981ac9a79c9458ea45b6f8
|
|
| MD5 |
d43a6096a2a0a8122a5d0ee38f8b48d2
|
|
| BLAKE2b-256 |
74e054f7f71a4ae3b93b2ec9eb85c5577610c8cc93537ae1a5a1cb8da61bf3b1
|