This section explains what happens internally when you call the debugger, in the exact order the code executes.
from llm_failure_debugger import Debugger
debugger = Debugger.from_openai(api_key="sk-...")
result = debugger("Who won the Nobel Prize in Physics in 2026?")➡️ This triggers the Debugger.__call__() method, which internally calls:
Debugger.debug(prompt, auto_repair=True)File: core/precheck.py
precheck_risks = self.precheck.predict(prompt)What happens:
-
Detects risky prompts before model generation
-
Example risks:
- Future questions → hallucination risk
- Very short prompts → consistency risk
- Logic-heavy prompts → reasoning risk
Output example:
{
FailureType.HALLUCINATION: 0.7,
FailureType.CONSISTENCY_ERROR: 0.4
}➡️ These risks are stored in internals.metadata for later explanation.
File: adapters/base.py
output, internals = self.adapter.analyze(prompt)Internally:
output = adapter.generate(prompt)
internals = adapter.extract_internals(prompt, output)Depending on the adapter:
- OpenAI → token logprobs, tool traces
- Ollama / HF → text only
- Custom → user-defined internals
Result:
output = "The Nobel Prize in Physics 2026 was won by John Smith."
internals = ModelInternals(...)File: analysis/signals.py
signals = []
for extractor in EXTRACTORS:
signals.extend(
extractor.extract(input_text, output_text, internals, capabilities)
)Each extractor detects one specific failure signal:
Examples:
TemporalHallucinationExtractorUngroundedClaimExtractorEntropySignalExtractorKnowledgeContradictionExtractor
Example extracted signals:
[
FailureSignal(name="temporal_hallucination", value=1.0),
FailureSignal(name="rag_ungrounded", value=0.6)
]➡️ Signals are low-level evidence, not failures yet.
File: core/inference.py
failure_hypotheses = self.inference_engine.infer(signals)Internally:
score += signal.value * weight * signal.confidence
confidence = 1 - exp(-score)Output:
[
(FailureType.HALLUCINATION, 0.88, ["temporal_hallucination", "rag_ungrounded"])
]➡️ Now the system knows WHAT failed, with confidence.
File: analysis/root_cause.py
root_causes = self.root_cause_analyzer.analyze(relevant_signals)Signal → Root Cause mapping:
"temporal_hallucination" → KNOWLEDGE_CONTRADICTION
"rag_ungrounded" → UNGROUNDED_GENERATIONOutput:
[
RootCause.KNOWLEDGE_CONTRADICTION,
RootCause.UNGROUNDED_GENERATION
]➡️ This answers WHY the failure occurred.
File: analysis/attention_localization.py
failure.localization = FailureLocalization(
level="knowledge_retrieval_stage",
evidence=["rag_ungrounded"],
confidence=0.88
)Examples of stages:
- knowledge retrieval
- reasoning
- decoding
- tool planning
➡️ This answers WHERE in the pipeline it failed.
File: analysis/causal_graph.py
self._causal_failures.append(failure)
samples = self._causal_builder.build(self._causal_failures)
self._causal_graph = self._causal_learner.learn(samples)Learned structure:
rag_ungrounded → ungrounded_generation → hallucination
temporal_hallucination → knowledge_contradiction → hallucination
➡️ The system learns failure patterns over time.
File: analysis/recommendations.py
recommendations = self.recommendation_engine.recommend(
root_causes,
causal_graph=self._causal_graph
)Example output:
[
"Add retrieval-augmented generation (RAG)",
"Force model to say 'I don't know' if unsure",
"Add temporal grounding instructions"
]File: utils/repair.py
repaired_prompt, status = self.repair_engine.repair(
original_prompt,
[best_root_cause]
)Repaired prompt:
Who won the Nobel Prize in Physics in 2026?
Instruction:
- Cite sources and say 'I don't know' if unsure.
- Do not assume the premise is correct.
➡️ Only triggered when:
- Severity ≥ MEDIUM
- Confidence ≥ 0.6
repair_safe = RepairEvaluator.is_safe(repaired_output)Checks for:
- Safe abstention
- Premise rejection
- No hallucinated facts
failures_log_YYYY-MM-DD_HH-MM-SS.jsonlFile: tracking/tracker.py
self.tracker.log(model_name, model_version, failures)File: tracking/active_learning.py
selected = ActiveLearningSelector().select(failures, k=10)➡️ Selects failures for:
- Human review
- Dataset augmentation
- Retraining
DebugResult(
failures=[FailureInstance(...)],
signals=[FailureSignal(...)],
metadata={
"failure_log_path": "...",
"repaired_prompt": "...",
"repair_safe": True
}
)User-friendly access:
result.has_failures
result.recommendations
result.repaired_promptPrompt → Precheck → LLM → Signals → Failure → Root Cause
→ Causal Learning → Repair → Evaluation → Logging