Skip to main content
AgentDbg’s LangChain integration gives you full observability into your LangChain and LangGraph agents without manually wrapping each call. Add the callback handler once, and every LLM invocation and tool execution is automatically recorded to the active AgentDbg run—ready to inspect in the timeline viewer.

What gets captured

The AgentDbgLangChainCallbackHandler hooks into LangChain’s built-in callback system and records two event types:
  • LLM calls — triggered by on_llm_start / on_chat_model_starton_llm_end. Records model name, prompt, response text, and token usage.
  • Tool calls — triggered by on_tool_starton_tool_end / on_tool_error. Records tool name, input arguments, result, and error status.
LLM errors are recorded as LLM_CALL events with status="error". Tool errors are recorded as TOOL_CALL events with status="error" and include the error message.

Installation

Install AgentDbg with the LangChain extra:
This installs langchain-core alongside AgentDbg. If you import the integration without langchain-core present, you get a clear ImportError with install instructions.

Setting up the handler

1

Install the package

2

Wrap your entrypoint with @trace

The handler requires an active AgentDbg run. Use the @trace decorator on the function that calls your chain:
Alternatively, set AGENTDBG_IMPLICIT_RUN=1 in your environment to start a run automatically without the decorator.
3

Create the handler

Inside your traced function, instantiate the handler:
4

Pass the config to your chain

Pass config to any LangChain chain, LLM, or tool invocation:

Full example

This example uses a fake LLM so it runs without any API key or network calls:
Run it and open the timeline:

Guardrails with LangChain

All AgentDbg guardrails work with the callback handler. When a guardrail fires (for example, stop_on_loop detecting a repeated pattern), the handler immediately stops the run—bypassing LangChain’s except Exception error handling and LangGraph’s graph executor—so no further token-spending calls are made.

Reusing the handler across runs

If you call your traced function multiple times with the same handler instance, call handler.reset() between runs to clear the abort state:

Checking for aborts after invoke

As a defensive fallback, the handler stores any guardrail exception on handler.abort_exception. Use handler.raise_if_aborted() to re-raise it if needed after an invoke() call returns:
The handler requires an active AgentDbg run. Wrap your entrypoint with @trace, use traced_run(...), or set AGENTDBG_IMPLICIT_RUN=1 in your environment.