Skip to main content
Sessions group related traces together, making it easier to monitor and debug complex workflows. Track entire user interactions or multi-step processes as a single unit.

What Are Sessions?

A session represents a logical grouping of traces. Common examples:
  • A conversation with a user
  • A batch processing job
  • A multi-step workflow
  • A user’s session on your application

Creating Sessions

There are two ways to create sessions: via the trace() function (recommended) or via telemetry metadata on individual prompt calls.

Using trace() with Session Options

import { trace } from "@agentmark-ai/sdk";
import { client } from "./agentmark.client";
import { generateText } from "ai";

const sessionId = `session-${Date.now()}`;

const { result: greeting } = await trace(
  {
    name: 'handle-greeting',
    sessionId,
    sessionName: 'Customer Support Chat #12345',
    userId: 'user-123'
  },
  async (ctx) => {
    const prompt = await client.loadTextPrompt('chat.prompt.mdx');
    const input = await prompt.format({
      props: { message: 'Hello!' },
      telemetry: { isEnabled: true }
    });
    return await generateText(input);
  }
);

// Later, another trace in the same session
const { result: followUp } = await trace(
  {
    name: 'handle-follow-up',
    sessionId,
    sessionName: 'Customer Support Chat #12345',
    userId: 'user-123'
  },
  async (ctx) => {
    const prompt = await client.loadTextPrompt('chat.prompt.mdx');
    const input = await prompt.format({
      props: { message: 'What can you help me with?' },
      telemetry: { isEnabled: true }
    });
    return await generateText(input);
  }
);

Using Telemetry Metadata

For cases where you don’t need explicit tracing, pass session info through telemetry metadata:
const sessionId = `session-${Date.now()}`;

async function handleUserMessage(message: string) {
  const prompt = await client.loadTextPrompt('chat.prompt.mdx');
  const input = await prompt.format({
    props: { message },
    telemetry: {
      isEnabled: true,
      functionId: 'chat-handler',
      metadata: { userId: 'user-123', sessionId, sessionName: 'Customer Support Chat' }
    }
  });
  return await generateText(input);
}

await handleUserMessage('Hello!');
await handleUserMessage('What can you help me with?');

Viewing Sessions

Sessions page with search, filters, and sortable columns Access sessions under the Sessions tab in the Dashboard or at http://localhost:3000 locally. Search by session ID or name, filter by date range and user, and sort by cost, tokens, duration, or trace count.

Sessions API

You can list and retrieve sessions programmatically using the CLI or REST API. The local dev server and cloud gateway expose the same endpoints, so you can query sessions locally during development.
# List sessions from the local dev server
agentmark api sessions list --limit 10

# Get a specific session with its traces
agentmark api sessions get <sessionId>

# Target the cloud gateway
agentmark api sessions list --remote --limit 10
Both endpoints support pagination with limit and offset parameters. The list endpoint also supports filtering by name and userId. See the Sessions API reference for full request and response details.

Best practices

  • Use consistent session IDs — session-${userId}-${Date.now()} not ${Math.random()}
  • Provide descriptive names — "Customer Support: Billing Issue #4532" not "Session 1"
  • Limit session scope — One ticket, one conversation, one batch job
  • Create new sessions for new interactions — Don’t reuse sessions across unrelated workflows

Tracing Setup

Full trace() API reference

Filtering & Search

Find sessions across dimensions

Have Questions?

We’re here to help! Choose the best way to reach us: