Skip to content

Python

Here we'll cover how to collect events in python. Our SDK calls are non-blocking and fast, meaning they won't get in the way of your python app's performance.

Install (requires python>3.10)

Firstly, install Trubrics in your venv with:

pip install trubrics

Initialise Trubrics

Then, initialise the SDK

from trubrics import Trubrics

trubrics = Trubrics(
    api_key="your-api-key",
    flush_interval=20,
    flush_batch_size=10,
)

Project API key

Your trubrics API key is unique to your project. Login to Trubrics, and copy your project API key from the settings page. This is a write only API key that allows you to push events.

Parameter Type Description Required
api_key str Your project API key. yes
flush_interval int Time in seconds between automatic flushes (default: 10) no
flush_batch_size int Number of events that trigger a flush (default: 20) no
logger logging.Logger A custom logger no

The client uses python's logging library to log messages, by default at the ERROR level.

To adjust the verbosity of the default logs, specify the log level:

trubrics.logger.setLevel(logging.DEBUG)

Or specify your own logger completely:

trubrics = Trubrics(
    api_key="your-api-key", 
    logger=your_cool_logger
)

Track user events

Finally, track any user action events with:

trubrics.track(
    event="Sign Up",
    user_id="user_id",
    properties={
        "country": "USA",
        "company": "Acme",  # Add more properties as needed
    },
)
Parameter Type Description Required
user_id str The distinct ID of the user that is signed in to your app. yes
event str The name of the event you are tracking. This can range from "User prompt", "Sign in", "Generation", etc. yes
properties dict[str,any] A list of properties of the event. These can be your user properties (e.g. company name) or additional properties of the event (e.g. request latency). Setting properties will allow you to filter and slice analysis in different ways. no
timestamp datetime The timestamp of the event. This defaults to the current timestamp no

Track LLM events

In addition to regular events, you may track LLM events (prompts and generations) with:

trubrics.track_llm(
    user_id="user_id",
    prompt="What is Trubrics?",
    assistant_id="gpt4o",
    generation="Trubrics is the leading product analytics platform for AI applications.",
    properties={
        "$thread_id": "your-thread-id",  # special trubrics property to group conversation
        "model": "gpt-4",
    },
    latency=2,
)
Parameter Type Description Required
user_id string The distinct ID of the user that is signed in to your app. yes
prompt string The user's message. yes
assistant_id string The AI assistant's ID, typically the model name. yes
generation string The assistant's response. yes
properties dict[str,any] A list of properties for both prompts and generations. A single reserved property is $thread_id which groups events by conversation thread. Otherwise these can be your custom properties (e.g. cost, number of tokens). Setting properties will allow you to filter and slice analysis in different ways. no
timestamp datetime The timestamp of the generation. This defaults to the current timestamp no
latency float The time in seconds between the prompt and generation no

Closing the client

To ensure all events are flushed before terminating your app, you may call the close function:

trubrics.close()

User events grouped by session

Based on every user event, Trubrics generates a session_id. This is a unique identifier for the session that the user is interacting with.

This is different to the $thread_id property, which groups events by conversation thread. A user may have multiple conversations with your AI within a single session, and each conversation will have a unique thread ID.

The session ID is generated based on the events that are tracked for a user. If a user has not interacted with your app for more than 30 minutes, the session will be timed out and a new session ID will be generated.