Mortem integrates with LangChain through a callback handler. You attach the handler to your chain or agent, and Mortem automatically records each LLM call and tool invocation as a trace event. No changes to your prompt templates, chains, or tool definitions are required.Documentation Index
Fetch the complete documentation index at: https://docs.mortemlabs.com/llms.txt
Use this file to discover all available pages before exploring further.
What gets captured
TheMortemCallbackHandler intercepts LangChain lifecycle events and records:
- LLM calls — model name, prompts passed to the LLM, and the full output
- Tool calls — tool name, input string, and output string
- Errors — failures on any LLM or tool step are recorded as failed events on the trace
Prerequisites
Install the SDK and create an agent in the dashboard before continuing. You needMORTEM_API_KEY and MORTEM_AGENT_ID set in your environment. LangChain (langchain and @langchain/core) must be installed separately — Mortem does not depend on them.
Integration
Initialize the Mortem client
Create a
Mortem instance at module scope.verifyToken is only needed during your first deployment. Once the dashboard shows the agent as verified, remove MORTEM_VERIFY_TOKEN from your environment and code.Create the callback handler
Call Alternatively, import
mortem.langchainHandler() to get a MortemCallbackHandler instance. This is a synchronous call that returns the handler immediately.createLangChainHandlerAsync directly from the SDK if you want a handler that dynamically subclasses BaseCallbackHandler from @langchain/core. This is useful when you need the handler to satisfy strict instanceof checks in framework code:Attach the handler to your chain or agent
Pass the handler in the
callbacks array when invoking your chain. You can add it at the chain level, the model level, or at the point of invocation — LangChain propagates callbacks through the call tree.Complete example
Handler variants
The SDK exports three ways to create a LangChain callback handler:| Method | Description |
|---|---|
mortem.langchainHandler() | Synchronous. Returns a MortemCallbackHandler instance immediately. Works via duck-typing for most LangChain APIs. |
createLangChainHandler() | Same as above, exported directly from @mortemlabs/sdk. Use this if you import the function directly rather than through the Mortem client. |
createLangChainHandlerAsync() | Async. Dynamically imports @langchain/core/callbacks/base and subclasses BaseCallbackHandler. Use when strict instanceof checks are required. Falls back to the synchronous handler if @langchain/core is not installed. |
You can import
createLangChainHandler and createLangChainHandlerAsync directly from @mortemlabs/sdk without a Mortem client instance. This is useful if you manage the handler lifecycle separately from the client.Agent callbacks
For LangChain agents, pass the handler in thecallbacks array at the agent executor level. LangChain propagates the handler to all nested chains and tools automatically: