Skip to main content
Wrapper methods instrument your existing provider clients without requiring you to emit events manually. Pass a client to the appropriate wrapper, use the returned client in place of the original, and Mortem records every LLM call, tool invocation, and Solana transaction as a typed event inside the active session. All wrappers use structural typing — the SDK does not import or hard-depend on any provider package. Your bundle size is unaffected if a provider is not installed, and wrappers work across provider versions as long as the underlying API shape matches.
Wrappers only emit events when called inside an active session.run() context. If a wrapped client is called outside a session, the original method runs unchanged with no instrumentation overhead.

OpenAI

Wrap your OpenAI client once at startup and use it everywhere in your agent. The wrapper instruments chat.completions.create for both streaming and non-streaming responses.

Anthropic

Wrap your Anthropic client in the same way. The wrapper instruments messages.create and captures streaming content deltas, tool use blocks, and token usage.

Ollama

Wrap the Ollama client to capture ollama.chat calls, including JSON-mode requests and streamed responses. Ollama costs are billed externally through ollama.com, so the dashboard shows usage as tracked externally rather than as a USD estimate.

Vercel AI SDK

Use wrapTools and wrapLanguageModel together to instrument Vercel AI SDK agent loops. wrapTools patches each tool’s execute function to emit tool_call events. wrapLanguageModel patches doGenerate and doStream to emit llm_call events.
Always wrap both tools and model when using the Vercel AI SDK. Wrapping only the model captures LLM calls but misses individual tool invocations, and vice versa.

LangChain

Use mortem.langchainHandler() to get a callback handler and add it to your LangChain chains or agents. The handler captures LLM start/end/error and tool start/end/error events without a hard dependency on @langchain/core.
If you need a handler that subclasses BaseCallbackHandler for compatibility with stricter LangChain type checks, use the async factory instead:
createLangChainHandlerAsync dynamically imports @langchain/core/callbacks/base at runtime and returns a proper subclass when it is available, falling back to the structural handler if LangChain is not installed.

Solana web3.js Connection

Wrap your Solana Connection to record sendRawTransaction and sendTransaction calls as solana_tx events. The wrapper also polls for confirmation in the background and updates the event with the confirmation status.
The wrapper detects the cluster from the RPC URL (mainnet, devnet, or localnet) and records instruction names from sendTransaction calls where the transaction object exposes them.

Wrapper method reference

Each wrapper is idempotent — calling it more than once on the same client returns the already-patched client without double-wrapping.