- How to enable built-in debug and verbose flags to surface internal information during execution.
- How to use callbacks to intercept and inspect events across prompts, LLM calls, chains, and agents.
- Practical patterns to capture intermediate inputs and outputs so you can trace where formatting or logic errors originate.
This article focuses on techniques for observability and debugging. A demo in this article shows how to enable debug/verbose output and how to implement callbacks to capture detailed runtime information.
Why observability matters for LangChain
When a chain produces unexpected results, the root cause can be in any layer:- prompt formatting or template errors,
- LLM call parameters (temperature, max tokens),
- output parser logic (parsing failures),
- agent/tool execution and tool return values.
- What prompt was actually sent to the LLM?
- What intermediate outputs did a chain produce before the final result?
- Which tools did an agent call and with what arguments?
- Did an output parser throw an error or silently return None?
Summary of techniques
Enable verbose/debug output
Many LangChain classes expose averbose flag that prints helpful information to stdout during execution. You can enable verbose on the LLM, chains, and some high-level components.
Python (Chat models and chains)
Be careful when enabling verbose debug logging in production. Verbose logs may include user data, prompts, or model outputs that are sensitive. Always sanitize or disable verbose output for production environments.
Callbacks — capture lifecycle events
Callbacks are the most flexible way to intercept runtime events. With a custom callback handler you can capture:- prompt text before sending to the LLM
- LLM outputs and timings
- chain inputs/outputs at each step
- agent decisions and tool calls
- parser errors and exceptions
- The JS SDK uses a
callbacksarray to pass handlers. Create a handler object with methods such asonLLMStart,onLLMEnd,onChainStart,onChainEnd, etc., and pass it to the model/chain. - Example reference: LangChain.js callbacks docs (link for reference).
- Persist events to a JSON file or database for later analysis.
- Enrich events with timestamps, latencies, and environment metadata.
- Correlate events with unique run IDs to stitch together multi-step flows.
Capture intermediate inputs and outputs
When parsing fails or outputs are malformed, locating the exact intermediate value that caused the failure is crucial. Common capture patterns:- In-memory capture: store intermediate values in a list or dict on a callback handler for quick debugging.
- File-based capture: dump prompts and responses to a file (rotating logs) for persistent review.
- Structured logging & tracing: send events to an observability backend (e.g., Datadog, Elastic, or OpenTelemetry).
Debugging common problems
-
Unexpected output format from parser:
- Capture raw LLM text before parsing to see if the model deviated from the template.
- Use output parser tests with unit tests (feed sample LLM outputs to parser and assert parse results).
-
Agent selects wrong tool:
- Enable agent callbacks to capture
on_agent_actionandon_agent_finish. - Inspect tool inputs to verify the agent’s reasoning step.
- Enable agent callbacks to capture
-
Prompt formatting issues:
- Enable
verbose=Trueor capture the final prompt text in callbacks. - Validate templates with example inputs before running them in production.
- Enable
Best practices
- Always start with
verbose=Truein development to surface immediate issues. - Use callback handlers to store traces, then switch verbose off in production.
- Sanitize logs before persisting or sending to external observability systems.
- Add unit tests for prompts and parsers to catch format regressions early.
Quick reference table
Links and references
- LangChain documentation — https://langchain.com/docs
- LangChain callbacks (Python) — https://python.langchain.com/en/latest/modules/callbacks/index.html
- LangChain.js callbacks — https://js.langchain.com/docs/modules/callbacks/overview
By combining verbose runtime flags with custom callback handlers and a standardized persistence pattern for traces, you can quickly locate the source of errors, validate prompt formats, and build a robust observability workflow around LangChain applications.