Lesson 13 / 31

Callbacks, Tracing and Debugging

Observe every step of a chain.

See inside the chain

When an answer is wrong you need to see each step's input and output. Callbacks are handlers that receive events (chain start/end, model start/end, tool calls, errors); you pass them in the config of any call. LangSmith (the hosted tracing and evaluation service from the LangChain team) and open-source or other observability tools collect these events into traces so you can inspect prompts, latency, token counts and failures. Also useful: set_debug(True) for verbose local logs, and printing the rendered prompt. In production, avoid logging sensitive data, or redact it.

A custom callback, run

I ran this offline in a Python virtual environment with langchain-core 1.6.6, langchain-text-splitters 1.1.2 and llama-index-core 0.14.25. No API key or network call is needed because a fake model or a toy embedding stands in for the real one. The handler records events while a two-step chain runs on 5. The result is 11 (5*2 then +1) and the events show the outer sequence starting, each inner step starting and ending with its output, then the sequence ending with 11.

from langchain_core.callbacks import BaseCallbackHandler
from langchain_core.runnables import RunnableLambda

class Log(BaseCallbackHandler):
    def __init__(self): self.events = []
    def on_chain_start(self, serialized, inputs, **kw): self.events.append("start")
    def on_chain_end(self, outputs, **kw): self.events.append(f"end:{outputs}")

log = Log()
chain = RunnableLambda(lambda x: x * 2) | RunnableLambda(lambda x: x + 1)
print(chain.invoke(5, config={"callbacks": [log]}))
print(log.events)

Output:

11
['start', 'start', 'end:10', 'start', 'end:11', 'end:11']

Quick check: What do callbacks receive?

  • The model weights
  • Events such as chain start/end, model calls and errors
  • The user's password
  • The GPU temperature only
Answer

Events such as chain start/end, model calls and errors — Callbacks observe the lifecycle of runs for logging and tracing.