chat.completions.create at runtime. It does not import the openai package itself, so the wrapper works with any version of the SDK and adds no extra dependency to your agent.
What gets captured
For each call tochat.completions.create, Mortem records an llm_call event containing:
- Prompts — the full message array passed to the model
- Completion — the assistant message content returned
- Model — the model string from the request parameters
- Token usage —
prompt_tokens,completion_tokens, andtotal_tokensfrom the response - Cost estimate — calculated from token counts and recorded on the event
- Finish reason —
stop,length,tool_calls, or any other finish reason the model returns - Tool calls — structured tool call arguments when the model uses function calling
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. This is typically done 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 OpenAI client
Pass your OpenAI client instance to You can replace the original variable if you want tracing for every call in the module:
mortem.wrapOpenAI. The wrapper patches chat.completions.create in place and returns the same client reference. All subsequent calls on the wrapped client are automatically traced.3
Start a session and run the agent
Create a session with
mortem.startSession, then run your agent logic inside session.run. All chat.completions.create calls made through the wrapped client inside the callback are automatically associated with this trace.Complete example
Streaming
When you passstream: true to chat.completions.create, the wrapper detects the async-iterable response and taps it with a generator that accumulates content deltas. The llm_call event is completed with the assembled text once the stream is exhausted.
Function calling
When the model returns tool calls, Mortem captures each tool call block under theoutput.toolCalls field of the llm_call event. Each entry includes the tool call ID, function name, and raw arguments string as returned by the API.