Installation
Simple Usage
Context
The SDK provides a context system usingAsyncLocalStorage
that allows you to access the session throughout your application code. This is particularly useful for tracking LLM interactions and tool calls.
Serverless
On serverless platforms like Vercel or Cloudflare, you can use thesession.flush()
method to ensure all events are sent to the Mindlytics platform before the function execution ends.
Receiving Events and Errors from Mindlytics
While your application code is decoupled from the Mindlytics service in terms of sending events, it is possible to receive the events you send as well as the analytics events that Mindlytics generates over a web socket connection. You can do this by registering callback handlers when you create a new session.yourData
that you supply will be sent to your handlers as a second argument. You can use this to gain access to contextual information if needed.
Concepts
Except for client-level user identify and alias, all other communication with Mindlytics is contained within a “session”. In a session you may send your own “user defined” events; that is, events that are not specific to Mindlytics but are meaningful to you. In a session you may also send special Mindlytics events related to conversations. Events happen at a point in time, but sessions and conversations they belong to have a start and an end and thus a specific duration. While you can explicitly start and end sessions and conversations, some of this work can be done implicitly. This is particularly useful if you are using the SDK in a stateless, serverless context and handling an ongoing conversation.User ID and Device ID
User IDs are something you can decide to use or not use. If you decide to use them, user ids should be unique for a organization/project pair. You can use anything you like as long as it is a string, and is unique for each user in a project. Device IDs should represent unique devices, like a browser instance or a mobile device uuid. Device IDs are considered globally unique. If you do not use user ids, then you must use device ids. You can use both. For example, when a session begins you may not know the id of the user who starts it. If this is the case, you must supply a “device_id” that is globally unique and represents the device the user is communicating on. This might be a mobile device uuid. This is harder on a browser, but you might use a uuid stored in a local cookie. Sometime during the session/conversation you might discover who the user is and then you can issue a “session_user_identify” event with the user id, who will then be associated with the device_id and the session.Client API
MLSDK_API_KEY
and MLSDK_PROJECT_ID
are checked. You can obtain these values from your account on the Mindlytics SaaS portal. The full set of options for the Client
constructor are:
baseUrl
points to the Mindlytics production server address. This can be changed by specifying baseUrl
or by setting the environment variable MLSDK_SERVER_BASE
.
Returns:
An instance of the Mindlytics client object. This is used primarily to create sessions, but has two other methods for identifying users and managing aliasing outside of normal sessions.
deviceId
if you know it. If the device id has been used to create sessions (see below) then all sessions and associated events with this device id now belong to this user.
Returns:
A Mindlytics user object.
id
and all previous ids become aliases for this user and can be used to refer to this user in the future.
Returns:
A Mindlytics user object.
Session
object. The sessionId
is required and can be a new session id or an existing session id. Session ids should be globally unique with respect to your api key and project id. You need a session to send events. To create a session you must supply either a userId
or a deviceId
. If you do not know the user id at the time a session is started, then you must at least supply a device id which should represent the unique device (eg. mobile phone, browser, etc) the user is coming from. You may also supply a conversationId
which will be attached to events sent using this session instance. This is optional, since you can attach it to individual events.
Session API
attributes
is optional and if specified, the user-defined attributes are associated with the session.
properties
are optional. Supported property types are string, number and boolean. Complex data types are not supported.
attributes
is optional and if specified, the user-defined attributes are associated with the conversation.
session.end()
will automatically close any open conversations, but you can call it if you like. The attributes
is optional and if specified, the user-defined attributes are merged with any existing conversation attributes.
A note on conversation ids. When you callclient.createSession()
you can pass an optionalconversationId
and if you do, this value will be automatically applied as the “conversation_id” to all methods that accept a “conversation_id”. If you do not supply aconversationId
toclient.createSession()
then you must supply it to the events that are related to conversations. Supplying to individual events could allow you to support multiple conversations within a single session.
assistant_id
might be useful in a multi-agent system to keep track agent participation. model
/prompt_tokens
/completion_tokens
if specified will cause the Mindlytics service to calculate the cost of this turn, and accumulate the total cost of a conversation. It uses an online database of popular models to retrieve the most current cost data. Alternatively you can specify cost
directly if you know it or the model you are using is not a common LLM. And you can add your own custom properties.