Module 13 Lesson 1: Debugging Chains
Finding the Bug. Techniques and tools for identifying where a multi-step chain is failing or hallucinating.
Debugging Chains: The Detective Work
In Module 4 (Chains), we learned how to pipe many steps together. But as your pipes grow longer, tiny errors propagate.
- Step 1 creates a slight formatting error.
- Step 3 misinterprets that error as a "Math" command.
- Step 5 crashes because it received a string instead of a number.
Debugging AI is harder than debugging code because the "Input" is unpredictable text.
1. The 3 Levels of Debugging
- Print Debugging: Manually printing the output of every step. (Slow, messy).
set_debug(True): A global LangChain flag that prints everything to the console.- Observability Platforms: Professional dashboards like LangSmith.
2. Using set_debug
If your script isn't working and you want to see the "Raw" data passing through the pipes, run this at the very top:
from langchain.globals import set_debug
set_debug(True)
# Now every invoke() will show the full JSON prompt and response
chain.invoke({"input": "..."})
3. Visualizing Propagation Error
graph TD
S1[Step 1: Write JSON] -->|Missing Comma| S2[Step 2: Parse JSON]
S2 -->|Fail| S3[Step 3: Correct Error]
S3 -->|Hallucination| S4[Step 4: Execute Incorrect Tool]
S4 --> Crash[System failure]
4. Common Chain Failure Modes
- Context Stuffing: You retrieved too many documents and hit the token limit.
- Variable Mismatch: You asked for
{question}in your prompt, but you sent{"input": "..."}in your dictionary. - Prompt Drift: The model's tone changes significantly in step 4 compared to step 1.
5. Engineering Tip: Atomic Testing
Don't debug the whole chain at once. Test each Runnable individually.
# Test only the prompt
print(prompt.invoke({"input": "test"}))
# Test only the model
print(model.invoke("test"))
Key Takeaways
- AI Debugging requires seeing the intermediate data between steps.
set_debug(True)is the fastest way to see the raw "Guts" of your chain.- Variable Names must match exactly between the Prompt and the Invoke Dict.
- Testing in isolation is the best defense against complex chain failure.