Skip to main content

Local-first SDK and CLI for RAG and agent reliability tracing, citation checks, and failure diagnosis.

Project description

ContextTrace

Local-first evidence-chain debugging for RAG and AI agents.

ContextTrace shows where an answer stopped being grounded:

query -> retrieved context -> answer claims -> citations -> verdicts -> root cause

It is a Python SDK and CLI, not a hosted dashboard. Traces, reports, judge cache, and SQLite state stay local by default.

Install

pip install contexttrace
contexttrace init

Quickstart

contexttrace verify-demo unsupported_claim --report
contexttrace demo --dataset refund_policy
contexttrace report --last --open

Default local storage:

.contexttrace/contexttrace.db

Verify A RAG Trace

Create a portable trace with a query, answer, retrieved contexts, and optional citations:

{
  "query": "How long does refund processing take?",
  "answer": "Refunds are processed within 5 business days.",
  "contexts": [
    {
      "id": "policy",
      "text": "Customers may request refunds within 30 days of purchase."
    }
  ]
}

Run local evidence checks:

contexttrace inspect trace.json
contexttrace verify trace.json --report
contexttrace qa trace.json --corpus docs/ --report

ContextTrace classifies each claim as supported, partially_supported, unsupported, unverifiable, or contradicted, then explains the likely fix.

Local Verification Modes

Mode Use When
lexical Fast default checks with no optional dependencies.
semantic Local paraphrase and role-aware contradiction checks.
local_ml Offline hash-embedding similarity, optionally backed by a local SentenceTransformers model.
judge Higher-accuracy local LLM judging through Ollama, LM Studio, vLLM, or a local OpenAI-compatible server.

Run the stronger local non-LLM verifier:

contexttrace verify trace.json --mode local_ml --report
contexttrace verify-benchmark --mode local_ml --case-set all

Optional neural local-ML support never downloads models automatically:

pip install "contexttrace[local-ml]"
set CONTEXTTRACE_LOCAL_ML_MODEL_PATH=C:\models\bge-small-en-v1.5

Run a local judge with Ollama:

set CONTEXTTRACE_JUDGE_PROVIDER=ollama
set CONTEXTTRACE_JUDGE_MODEL=llama3.1

contexttrace verify trace.json --mode judge --report
contexttrace judge-calibrate --case-set all --report

Remote judges are blocked while local_only: true is active. To use a remote judge, explicitly disable local-only mode and configure the provider/API key.

Diagnose And Regression-Test

# Find whether support existed elsewhere in the corpus.
contexttrace audit trace.json --corpus docs/ --report

# Compare a baseline and current answer after a prompt, model, or retriever change.
contexttrace compare baseline.json current.json --report

# Turn saved failures into replayable endpoint tests.
contexttrace suite create traces/failure.json --out contexttrace-suite.json
contexttrace suite run contexttrace-suite.json --endpoint http://localhost:8000/query --report

Common root causes include retrieval_miss, reranking_failure, chunking_issue, corpus_gap, answer_overreach, stale_source, citation_mismatch, and should_have_abstained.

Capture Existing Systems

Capture one live endpoint response:

contexttrace capture endpoint \
  --endpoint http://localhost:8000/query \
  --query "What is the refund policy?" \
  --answer-path $.answer \
  --contexts-path $.contexts \
  --citations-path $.citations \
  --out traces/refund_trace.json \
  --verify \
  --report

Or capture artifacts from Python:

from contexttrace import capture_rag_trace, write_rag_trace

trace = capture_rag_trace(
    query=question,
    answer=answer,
    contexts=retrieved_docs,
    metadata={"system": "support-rag"},
)
write_rag_trace(trace, "trace.json")

SDK Example

from contexttrace import ContextTrace

ct = ContextTrace(project="support-rag")

with ct.trace(query="What is the refund policy?") as trace:
    chunks = retriever.search("What is the refund policy?")
    trace.log_retrieval(chunks)
    trace.log_context(chunks[:5])

    answer = llm.generate("What is the refund policy?", chunks[:5])
    trace.log_answer(answer, usage={"total_tokens": 1200})
    trace.log_citations([
        {"claim": "Refunds are available within 30 days.", "source_chunk_id": "chunk_12"}
    ])

    result = trace.evaluate()
    print(result["failure"]["failure_type"])

Integrations

pip install "contexttrace[langchain]"
pip install "contexttrace[llamaindex]"
pip install "contexttrace[fastapi]"
pip install "contexttrace[langgraph]"
pip install "contexttrace[otel]"
pip install "contexttrace[all]"

Includes LangChain, LlamaIndex, FastAPI, LangGraph, and OpenTelemetry hooks.

Privacy

ContextTrace makes no network calls unless you point it at an endpoint or configure a judge provider. Local controls include:

  • local_only: true
  • log_chunk_text: false
  • log_answer_text: false
  • storage_path
  • judge_cache_enabled: true
  • judge_cache_path: .contexttrace/judge_cache.json

Limits

ContextTrace is a diagnostic tool, not a correctness proof. Claim extraction is rule-based, contradiction detection is conservative, and high-stakes outputs still need human review.

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

contexttrace-0.8.0.tar.gz (147.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

contexttrace-0.8.0-py3-none-any.whl (174.7 kB view details)

Uploaded Python 3

File details

Details for the file contexttrace-0.8.0.tar.gz.

File metadata

  • Download URL: contexttrace-0.8.0.tar.gz
  • Upload date:
  • Size: 147.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for contexttrace-0.8.0.tar.gz
Algorithm Hash digest
SHA256 81ce299cb93b9f313e742e2412359adc542b2129f103d2494caeeae50968df97
MD5 0ae60306e94a5155cfc3056b2587c864
BLAKE2b-256 b51688c5e0c0c4ecf5baa2a0e01c69d16b4d1cc17c522605651bdaaf388e873c

See more details on using hashes here.

File details

Details for the file contexttrace-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: contexttrace-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 174.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for contexttrace-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 891b29cdf90e6e922f36fb02cd208424d896d0a3c7f5c5331252a69abb9c5954
MD5 4eae8e44e6ea3726371c80157af18471
BLAKE2b-256 3c8b901664e147a80df97c5bb60986f3aac16fe5b9110430e63a7596b01e418d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page