> ## Documentation Index
> Fetch the complete documentation index at: https://docs.baserun.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Users and sessions

> Tracing additional user-specific data and end-to-end user journeys

<Tip>
  Update your Python SDK to version 2.0 for enhanced stability and easier
  integration. [Learn more](../SDK_2.0/python/sessions)
</Tip>

## Automatically Tracking Users

If your LLM provider supports passing [End User IDs](https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids), Baserun will automatically collect the user ID and track user activity.

<Note>
  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.
</Note>

<CodeGroup>
  ```python python theme={null}
  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
  ```

  ```typescript typescript theme={null}
  import OpenAI from "openai"
  import { createHash } from "crypto"

  const openai = new OpenAI()

  async function ask_question(question: string) {
    return openai.chat.completions.create({
      messages: [{ role: "user", content: question }],
      user: createHash("sha256").update("user@example.com").digest("hex"),
      model: "gpt-3.5-turbo",
    })
  }
  ```
</CodeGroup>

## 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](https://app.baserun.ai/monitoring/users) and [Sessions](https://app.baserun.ai/monitoring/user-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

<CodeGroup>
  ```python python theme={null}
  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()
  ```

  ```typescript typescript theme={null}
  import { baserun } from "baserun";
  import { createHash } from "crypto";

  async function main() {
    await baserun.init();
    const { data, sessionId } = await baserun.session(
      async function () {
        // Add business logic including traces in here
      },
      {
        user: createHash("sha256").update("user@example.com").digest("hex"),
      },
    );

    // Store session ID to be continued later
    console.log({ data, sessionId });
  }

  main();

  ```
</CodeGroup>

### 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

<CodeGroup>
  ```python python theme={null}
  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())
  ```

  ```typescript typescript theme={null}
  import { baserun } from "baserun";
  import { createHash } from "crypto";
  import OpenAI from "openai";

  const openai = new OpenAI();

  async function main() {
    await baserun.init();
    const { data, sessionId } = await baserun.session(
      async function () {
        return openai.chat.completions.create({
          messages: [
            {
              role: "user",
              content: `Say "this is a test"`,
            },
          ],
          model: "gpt-3.5-turbo",
        });
      },
      {
        sessionId: YOUR_PREVIOUSLY_SAVED_SESSION_ID,
        user: createHash("sha256").update("user@example.com").digest("hex"),
      },
    );

    console.log({ data, sessionId });
  }

  main();
  ```
</CodeGroup>

Providing a user identifier (the value is up to you) will allow you to group traces in the [Users](https://app.baserun.ai/monitoring/users) and [Sessions](https://appbaserun.ai/monitoring/user-sessions) monitoring pages.
