Skip to main content
When inspecting a simple Jupyter notebook that uses LangChain, the typical flow is:
  1. Define a prompt.
  2. Create an LLM instance.
  3. Wrap both in a chain.
  4. Invoke the chain with inputs.
This guide provides a concise, corrected example showing the pattern, how to enable global debug logging in LangChain, and how to interpret the runtime logs to diagnose issues, measure latency, and inspect intermediate prompts.

Quick example: a minimal Chat LLM chain

The example below demonstrates a minimal ChatOpenAI + ChatPromptTemplate + LLMChain setup. Note the updated import paths that match common LangChain usage.

Typical chain output

A typical returned result (dictionary) contains the inputs and the generated text. Example:

Why enable LangChain debug logging?

As chains grow in complexity—multiple components, custom transforms, callbacks, or several LLM calls—it’s easy to lose visibility into what happened during execution. LangChain provides a global debug flag that instruments runs and prints structured, per-component logs showing inputs, prompts, outputs, and timing. This visibility helps you:
  • Diagnose unexpected outputs or logic errors.
  • Inspect intermediate prompts and transformed inputs.
  • Measure token usage and per-component latency.
  • Identify which component produced an error or unexpected text.

Enabling global debug logging

Turn on global debug tracing with:
Below is a full example that enables debug and runs the same chain:

Sample debug output (abridged)

When debug is enabled, LangChain prints a structured trace. The example below is normalized for clarity:

Interpreting the debug tags

Use the following as a quick reference to the most common log tags:

Example LLM output metadata

The LLM output and the chain run often include metadata such as token usage and model identification. Example:

When to enable debug logs

  • During development and troubleshooting.
  • When iterating on prompt engineering and wanting to inspect the exact prompt(s) sent.
  • To measure token usage and latency for cost/optimization decisions.
  • While building or validating multi-component chains to see intermediate inputs/outputs.
Enable set_debug(True) while developing or troubleshooting chains to obtain a detailed execution trace of each component. Debug logs may contain sensitive data—do not enable them in production or when handling private data.
If you need help interpreting a specific debug trace or want assistance instrumenting more complex multi-component chains (callbacks, transforms, or retrievers), share the trace and chain configuration and we can walk through it step by step.

Watch Video