Lytix supports sending log events without being on the critical path. We use Traceloop to power this feature.

Prerequisite Please see here to get and set your API Key environment variable.

Install the SDK

To get started first download the relevant SDK for your language of choice.

npm install @lytix/client

Initialize the Logger

Now you can initialize the logger with your API key.

import { LytixAsyncLogger } from "@lytix/client";

const logger = new LytixAsyncLogger({
  lytixAPIKey: $LX_API_KEY,
  providers: {
    openAI: OpenAI,
  },
});

logger.init();

Note: If you are in the EU you have to update the baseURL on the LytixAsyncLogger to https://eu.api.lytix.co/v2/metrics/async.

Start Collecting Logs ๐Ÿš€

Now you can call OpenAI as you normally would, and logs will automatically get sent to the Lytix platform.

const client = new OpenAI({
    apiKey: $OPENAI_API_KEY,
});

await client.chat.completions.create({
    messages: [
        { role: "user", content: "Hello, how are you? Respond in JSON" },
    ],
    model: "gpt-4o-mini",
    temperature: 0.5,
    max_tokens: 1000,
});

Optional Metadata

You can also add metadata to your logs. This can be useful for tracking things like user idโ€™s, session idโ€™s, or other metadata.

To do this, you just need to first set the parameters on the logger:

const response = await logger.withMetadata(
    {
      /**
       * See below for all metadata options
       */
    },
    async () => {
      return await client.chat.completions.create({
        ...
      });
    }
);

Metadata Options

You can set the following metadata fields:

  • userId: The user id of the user making the request
  • sessionId: The session id of the user making the request
  • workflowName: The name of the workflow the request was made in
  • lytix-metadata-*: Any additional metadata you would like to track, e.g. lytix-metadata-env: dev will add the metadata key env with the value dev