messages.create at runtime without importing the @anthropic-ai/sdk package. The wrapper is compatible with any version of the Anthropic SDK and adds no extra dependency to your agent.
What gets captured
For each call tomessages.create, Mortem records an llm_call event containing:
- System prompt — the
systemfield if present - Messages — the full message array including role and content
- Completion — text extracted from all
textcontent blocks in the response - Tool use — structured
tool_useblocks with name, ID, and input arguments - Model — the model string from the request parameters
- Token usage —
input_tokensandoutput_tokensfrom the response - Stop reason —
end_turn,max_tokens,tool_use, or any otherstop_reasonvalue
content_block_delta text chunks and records usage from the message_delta event.
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.
Integration
1
Initialize the Mortem client
Create a
Mortem instance at module scope, typically once at the start of your agent process.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.2
Wrap the Anthropic client
Pass your Anthropic client instance to You can wrap the client at the point of construction to ensure every call in the module is traced:
mortem.wrapAnthropic. The wrapper patches messages.create in place and returns the same client reference.3
Start a session and run the agent
Create a session with
mortem.startSession, then run your agent logic inside session.run. Any messages.create call made through the wrapped client inside the callback is automatically associated with this trace.Complete example
Streaming
When you setstream: true, the wrapper detects the async-iterable response and taps it using a generator. It reads content_block_delta events for text accumulation and message_delta events for the final stop_reason and usage. The llm_call event is completed when the stream ends.
Tool use
When the response containstool_use content blocks, Mortem extracts them and records each tool call under the output.toolCalls field of the llm_call event. Each entry includes the tool call ID, tool name, and the full input object.