A session represents one complete agent run — one decision cycle, one trade attempt, one strategy evaluation. Every session becomes a trace in the Mortem dashboard with a full chronological replay of everything the agent did. Start a session at the beginning of a run, and close it when the run ends.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.
Start a session
Callmortem.startSession(options) to open a new trace. The method always returns a Session — if session creation fails internally, the SDK returns a no-op session that silently absorbs further calls rather than throwing into your agent code.
SessionOptions
A plain-text description of what the agent is trying to do in this run. This is the first thing visible in the trace list and detail view.
Arbitrary labels attached to the trace. Use these to filter runs by strategy, market, cluster, or any other dimension that matters to your team.
Override the auto-generated trace ID. Must be a valid ULID or a string that is unique across your agent’s traces. Useful when you generate correlation IDs upstream and want them to match inside Mortem.
Override the client-level
agentId for this specific session. Useful when a single process manages multiple logical agents.Backfill the session start time. Defaults to
new Date() at the moment startSession is called.Wrap agent work with session.run()
Pass your agent’s main logic to session.run(callback). This is the recommended way to structure every run.
session.run() does three things:
- Activates async trace context so that child events from wrapped clients (OpenAI, Solana, etc.) are automatically linked to this session — even across
awaitboundaries. - Calls
session.complete()automatically when the callback resolves. - Calls
session.fail(error)automatically when the callback throws, then re-throws the error so your own error handling is unaffected.
Close a session manually
If you are not usingsession.run(), close the session explicitly after the agent’s work is done.
Use session.complete() when the run succeeds:
session.fail() when the run errors:
session.end() when you want to set the status explicitly:
Completion methods
Marks the trace as
completed and flushes buffered data. Accepts an optional plain-text description of the outcome.Marks the trace as
errored, records the error message, and flushes. Accepts any thrown value — Error, string, or unknown.Generic completion with explicit control over
status, outputSummary, and errorMessage. Status defaults to "completed" if not provided.Access the trace ID
Every session exposes its trace ID throughsession.traceId (an alias for session.id):
traceId override to a downstream session.
Recommended session pattern
This is the full pattern from the Mortem README. Use it as your starting point:agent.ts
Session methods —
complete, fail, end, and run — are best-effort. Internal failures are swallowed and reported through the optional logger rather than propagating into your agent code. Your bot will not crash because of an SDK error.