> For the complete documentation index, see [llms.txt](https://dnotifier.gitbook.io/product-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dnotifier.gitbook.io/product-docs/ai/sessions.md).

# AI sessions

An **AI session** groups multiple `sendAI` turns under one id so the platform can maintain context across follow-up questions. Pass **`sessionId`** on subsequent calls to continue the same conversation.

## How sessions work

```
  Turn 1                          Turn 2
  ──────                          ──────

  sendAI({ message })             sendAI({
    (no sessionId)                  sessionId: "<from turn 1>",
  })                                message: { text: "follow-up" }
       │                                 │
       ▼                                 ▼
  new session id created          same session continued
```

On the **first** call, omit `sessionId` (or pass `null`). The SDK generates a session id and returns it in the response metadata.

On **follow-up** calls, pass that id as `sessionId`.

{% hint style="info" %}
Sessions are tied to **`senderId`**. Use a stable user id per end user or support ticket.
{% endhint %}

## Extracting `sessionId` (first turn)

```js
const first = await notifier.sendAI({
  senderId: "user-123",
  message: { text: "Start a new support session." },
});

const sessionId =
  first?.metadata?.packet?.id ??
  first?.metadata?.id;
```

Store `sessionId` in your database keyed by user, ticket, or chat thread.

## Continuing a session

```js
await notifier.sendAI({
  senderId: "user-123",
  sessionId,
  message: { text: "Can you elaborate on the second point?" },
});
```

## Sessions vs `messages[]` history

| Approach                        | When to use                                                                              |
| ------------------------------- | ---------------------------------------------------------------------------------------- |
| **`sessionId`**                 | Server-managed continuity; simpler client code                                           |
| **Full `messages[]` each call** | You control exact context window; stateless servers                                      |
| **`fetchAIHistory`**            | Audit or rebuild UI from stored turns — see [AI history](/product-docs/ai/ai-history.md) |

## Transport

Use **`transport: "http"`** for session continuation.

## Language guides

| Language                | Guide                                                                          |
| ----------------------- | ------------------------------------------------------------------------------ |
| JavaScript / TypeScript | [javascript-typescript.md](/product-docs/ai/sessions/javascript-typescript.md) |
| Dart / Flutter          | [dart-flutter.md](/product-docs/ai/sessions/dart-flutter.md)                   |

## Next steps

* [**AI history**](/product-docs/ai/ai-history.md) — List and delete stored turns
* [**Session logging**](/product-docs/ai/session-logging.md) — Dashboard telemetry with `logs: true`
