Skip to main content
Assuming familiarity with the core LangChain libraries, this article presents practical tips and techniques to debug LangChain applications and gain visibility into their runtime behavior. LangChain can feel overwhelming at first—many parts of a chain (prompts, LLMs, output parsers, and agents) execute behind the scenes. When formatting is incorrect or responses are unexpected, you need tools that reveal the execution lifecycle so you can diagnose and fix issues quickly. In this article you’ll learn:
  • 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.
Observability helps you answer targeted questions such as:
  • 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?
Below are concrete techniques and examples you can adopt to make these answers visible.

Summary of techniques

Enable verbose/debug output

Many LangChain classes expose a verbose 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)
JavaScript / TypeScript (chat models and chains)
Tip: enabling verbose will print prompts and responses to stdout. If you have sensitive input (API keys, personal data), avoid printing them or sanitize logs.
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
Below are minimal, robust examples that store events in memory (easy to extend to file, DB, or observability platforms). Python — custom callback handler
Register the handler with a chain or LLM:
JavaScript — callback-like handlers
  • The JS SDK uses a callbacks array to pass handlers. Create a handler object with methods such as onLLMStart, onLLMEnd, onChainStart, onChainEnd, etc., and pass it to the model/chain.
  • Example reference: LangChain.js callbacks docs (link for reference).
Patterns for callbacks
  • 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).
Example: writing prompts + responses to a file

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_action and on_agent_finish.
    • Inspect tool inputs to verify the agent’s reasoning step.
  • Prompt formatting issues:
    • Enable verbose=True or capture the final prompt text in callbacks.
    • Validate templates with example inputs before running them in production.

Best practices

  • Always start with verbose=True in 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


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.

Watch Video