16 of 174 subtitles documented + 1 independent supplementary unit(s) · Writing progress, not your learning progress · Videos not visually reviewed
Debugging: inspect messages, metadata, and failure locations
Objective and entry check
Distinguish exception-free execution, a correct answer, and the information available to diagnose failures. From the previous lesson, you should know that a template produces a PromptValue and a chat model returns an AIMessage.
Before continuing: if a summary is incorrect but no exception occurs, was execution successful? Does a wrong input key necessarily mean the model ran?
Entry-check answer
Execution may have completed technically while answer quality failed. A wrong key can fail at the template before the model runs.
Why is printing the answer insufficient?
Printing a result sometimes suffices for a small function. A chain has multiple stages, and its final output does not reveal where an input changed or which stage failed. Debugging lets us pause execution and inspect state; tracing records events across execution stages. These are general software-engineering ideas, not inventions introduced by LangChain.
In lecture 011, the instructor sets an IDE breakpoint and expands the response object. Inspecting its actual type is more useful than guessing from a variable named response.
What does a message contain besides text?
For the earlier offline example:
prompt, chain = make_chain()
result = chain.invoke({"information": INFORMATION})
print(type(result).__name__)
print(result.content)
print(result.response_metadata)
print(result.usage_metadata)
This fragment uses make_chain and INFORMATION imported from the preceding example. The result type is AIMessage. Its content holds response content; response_metadata contains integration/provider-specific information; usage_metadata may supply usage statistics when available. Do not assume every field exists identically for every model.
The fake-model experiment returned None for usage_metadata. That means usage information was not supplied, not that real inference consumed zero tokens. Monetary cost is not necessarily a message field either: calculating it requires usage data and valid pricing. This qualifies the subtitles' broad discussion of tokens and cost.
Where should breakpoints go?
- Before formatting: are the keys and data correct?
- After formatting: what instruction does
prompt.invoke(inputs).to_string()actually contain? - After model execution: what are the result type, content, and available metadata?
Pausing before invoke means the new response has not been produced yet. Pausing afterward does not undo the call or its cost. Our diagnostic experiment uses a fake model.
A local execution trace
debug_chain.py uses LangChain callbacks to collect start, end, and error events in memory. A callback is a function/method the framework calls when an event occurs; it is not a language model.
python debug_chain.py
Run from examples using an environment with the example dependencies. The successful path produced:
chain-start → chain-start → chain-end
→ model-start → model-end → chain-end
The first start belongs to the full sequence, the second to the template. Run and parent IDs establish nesting; ordering event strings alone does not establish parentage. The wrong-key path produced error events without any model-start. The code checks both properties.
This example does not write input payloads into public files. It uses tracing_context(enabled=False) to disable cloud tracing for the experiment. This small callback is an introduction to tracing, not a replacement for every LangSmith feature.
Separate three failure categories
| Symptom | Possible cause to investigate | First inspection |
|---|---|---|
| Template raises an error | Input contract mismatch | Template variables versus dictionary keys |
| Model runs but answer is incomplete | Instructions, limits, or model capability | Final input and response |
| Usage information is absent | Integration did not supply it | Field presence, rather than assuming zero |
These suggest diagnostic paths, not definitive explanations for every failure.
Independent practice
- Why must missing
usage_metadatanot be reported as zero cost? - Which two observations indicate that the model did not run for the invalid input?
- The final prompt is correct, but the summary adds an unsupported fact. Which stage do you inspect?
Answer 1
Missing information and a zero value differ. Model identity, valid pricing, and usage data are separate requirements. A fake model does not measure real inference usage.
Answer 2
The template reports an error and the same recorded execution has no model-start event. In this experiment callbacks cover execution from its start; absence in an incompletely instrumented system would not alone prove non-execution.
Answer 3
Compare model-stage input with output and evaluate evidence fidelity. Technical completion and summary correctness are different criteria.
Recall and continuation
Without the text, distinguish a breakpoint, callback, and metadata. If you can locate template failures separately from quality failures, continue to local models. Otherwise, revisit the successful and failed paths using fresh input.
Course basis: the complete lecture 011 subtitle file; the video was not directly reviewed. The callback example, table, and exercises supplement the course and were executed and checked with langchain-core==1.5.2. Prepared on 2026-10-16.