Update your Python SDK to version 2.0 for enhanced stability and easier integration. Learn more

Automatically Tracking Users

If your LLM provider supports passing End User IDs, Baserun will automatically collect the user ID and track user activity.

Please note that sending identifying information about your users, such as username or email address, to 3rd party services is not recommended. Instead, you can e.g. use hash function to avoid that, which we reflect in code examples below.

from hashlib import sha256
from openai import OpenAI

def ask_question(question: str) -> str:
    client = OpenAI()
    completion = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": question}],
        user=sha256(b"user@example.com").hexdigest(),
    )
    content = completion.choices[0].message.content
    return content

Sessions

A Session is a collection of LLM calls that are related to a long-term user interaction. A session can be started, resumed, and ended at any time and may contain many traces.

User Sessions are used to group traces together in the Users and Sessions monitoring pages.

For the Use cases of a chat bot, a Session may be the entire conversation. For an agent, a Session may encompass a single task execution. The scope of the session is specific to your application and what is most useful for you.

Adding Sessions

If you’re building a system where user interactions happen in a sequence (e.g. a chat bot) you’ll find it useful to group these interactions into a single session. The (with_session) context manager in Baserun allows you to do just that. By providing a user identifier, you can create a new session. By providing a session ID you can continue an existing session. This allows you to group all related traces under either the User or Session, making it easier to debug or analyze the user’s journey through your application.

Create a new session

from hashlib import sha256

import baserun
   
with baserun.with_session(user_identifier=sha256(b"user@example.com").hexdigest(),) as session:
    # Add business logic including traces in here
    ...
    # Store session ID to be continued later
    session_id = session.id
    run_chatbot()

Continuing an existing session

Using the Session ID that was collected when the session was started you can then resume that session by providing the session ID.

Full example

from hashlib import sha256

import baserun
import openai


def example():
    response = openai.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[
            {
                "role": "user",
                "content": 'Say "this is a test"',
            }
        ],
    )
    return response.choices[0].message


if __name__ == "__main__":
    baserun.api_key = YOUR_BASERUN_API_KEY_HERE
    openai.api_key = YOUR_OPENAI_API_KEY_HERE
    session_id = YOUR_PREVIOUSLY_SAVED_SESSION_ID
    baserun.init()

    with baserun.with_session(user_identifier=sha256(b"user@example.com").hexdigest(), session_identifier=session_id):
        print(example())

Providing a user identifier (the value is up to you) will allow you to group traces in the Users and Sessions monitoring pages.