Python API¶
The four objects re-exported from the top-level ctxrot module.
from ctxrot import (
CtxRotCallback, # attach to dspy.configure(callbacks=[...])
CtxRotStore, # read/write the SQLite database directly
analyze_session, # compute local rot metrics
run_deep_analysis, # RLM-powered semantic analysis
)
CtxRotCallback¶
The DSPy BaseCallback you attach to your dspy.configure(...) call. Pass store_content=True if you want the full prompt/completion text captured — it's required for repetition analysis.
Bases: BaseCallback
Captures LM and tool call data into SQLite.
Auto-creates a new session each time a top-level DSPy module run starts.
Concurrency-safe: each asyncify/streamify worker thread gets its own
_SessionState via a ContextVar, so multiple concurrent agent calls
do not interfere with each other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_path
|
str
|
Path to SQLite database file. |
'ctxrot.db'
|
store_content
|
bool
|
If True, also store full prompt messages and completion text for context rot analysis. |
False
|
Source code in ctxrot/callback.py
CtxRotStore¶
A thin SQLite wrapper. CtxRotCallback uses it internally, but you can also instantiate it yourself (with read_only=True for safety) when you want to query sessions programmatically without going through the CLI.
Persists LM call, tool call, and session data in SQLite.
Source code in ctxrot/storage.py
get_latest_session_id
¶
get_session_ids
¶
get_session_ids(
since: str | None = None,
until: str | None = None,
terminal_state: str | None = None,
) -> list[str]
Return session IDs matching the filters, ordered by started_at ASC.
since and until are compared against started_at as ISO-8601 strings
(lexicographic order matches chronological order). terminal_state is an
exact match.
Source code in ctxrot/storage.py
get_session
¶
Source code in ctxrot/storage.py
get_session_summary
¶
Source code in ctxrot/storage.py
get_growth_data
¶
Source code in ctxrot/storage.py
get_tool_impact
¶
Source code in ctxrot/storage.py
get_lm_call_content
¶
Return stored LM call content for a session, ordered by seq.
Source code in ctxrot/storage.py
get_tool_call_content
¶
Return stored tool call content for a session.
Source code in ctxrot/storage.py
truncate_all
¶
close
¶
Source code in ctxrot/storage.py
analyze_session¶
Compute local repetition + efficiency metrics for one session. Returns a plain dict that's safe to json.dumps.
Analyze a session for context rot signals.
Returns a dict with
- "has_content": whether store_content data was available
- "repetition": per-iteration repetition scores (or None)
- "efficiency": per-iteration efficiency ratios
- "summary": human-readable summary dict
- "session": session metadata (id, started_at, ended_at, model, mode, terminal_state)
Source code in ctxrot/analysis.py
run_deep_analysis¶
Kick off an RLM-powered deep analysis. Requires Deno and an API key. See Deep analysis for the full workflow and the CLI wrapper.
Run RLM-powered deep analysis on a session.
API credentials are resolved in order
- Explicit api_key / api_base parameters
- Environment variables (OPENAI_API_KEY, OPENAI_API_BASE)
- Variables loaded from env_file (default: .env)
Returns a dict with
- "report": markdown analysis report
- "trajectory": RLM's REPL interaction history
- "session_id": the analyzed session ID
- "missing_sections": list of required report sections not found
Source code in ctxrot/deep_analysis.py
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | |