Session Tracking

Introduction

Session groups multiple traces together to represent a sequence of operations, e.g. an AI agent workflow execution. Session tracking allows session-level visualization, analytics and automatic issue detection.

Integration

See Quick Start guide on how to install and configure Graphsignal tracer.

Python

Set a session identifier as session_id tag for every request, e.g. in a request handler. This will set a Python contextvar and will be picked up by all traces of the current context.

graphsignal.set_context_tag('session_id', my_session_id)

You can also set tags directly, when tracing manually:

with graphsignal.trace('generate', tags=dict(session_id=my_session_id)):
    ...
OpenAI (Python)

Set a session_id tag in request, call or run context.

graphsignal.set_context_tag('session_id', my_session_id)

Alternatively, a more reliable way to set session_id and other tags is passing tags in extra_headers argument as comma-separated key-value pairs.

client = OpenAI()
response = client.chat.completions.create(
    model="gpt-3.5-turbo", 
    messages=[{'role': 'user', 'content': 'Tell me a joke'}],
    extra_headers: {
        'Graphsignal-Tags': f'session_id={my_session_id}'
    })
LangChain (Python)

In case of automatic integration, set a session_id in request, call or run context.

graphsignal.set_context_tag('session_id', my_session_id)

Alternatively, a more reliable way to set session_id tag and other tags is passing tags dictionary to callback constructor.

from graphsignal.callbacks.langchain import GraphsignalCallbackHandler, GraphsignalAsyncCallbackHandler

chat = ChatOpenAI(
    callbacks=[GraphsignalCallbackHandler(tags=dict(session_id=my_session_id))])
CLI

If you are running a single process per session and added Graphsignal at command line, you can set the session_id tag in an environment variable.

env GRAPHSIGNAL_TAGS="session_id=123" python -m graphsignal <script>