Session Tracking

Introduction

A session represents a sequence of operations, grouped by a tag, such as chain_id, run_id, or user_id. Sessions can represent agent runs, reasoning chains, users, deployments, and so on. In Graphsignal, any tag can serve as session key to be used for aggregation, scoring and visualization.

Integration

See the Quick Start for instructions on installing and configuring the Graphsignal tracer.

Python

Set a tag at application level:

graphsignal.configure(api_key='my-api-key', deployment='my-app', tags={'run_id', '123'})
graphsignal.set_tag('run_id', '123')

Set a tag on every request, e.g. in a request handler. This will set a Python contextvar and will be picked up by all spans of the current context.

graphsignal.set_context_tag('chain_id', my_chain_id)

graphsignal.set_context_tag('chain_id', my_chain_name, append_uuid=True)

graphsignal.remove_context_tag('chain_id')

You can also set tags directly, when tracing manually:

with graphsignal.trace('generate', tags=dict(chain_id=my_chain_id)):
    ...
OpenAI (Python)

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

graphsignal.set_context_tag('chain_id', my_chain_id)

Alternatively, a more reliable way 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'chain_id={my_chain_id}'
    })
LangChain (Python)

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

graphsignal.set_context_tag('chain_id', my_chain_id)

Alternatively, a more reliable way is passing tags dictionary to callback constructor.

from graphsignal.callbacks.langchain import GraphsignalCallbackHandler, GraphsignalAsyncCallbackHandler

chat = ChatOpenAI(
    callbacks=[GraphsignalCallbackHandler(tags=dict(chain_id=my_chain_id))])
CLI

When adding Graphsignal at command line, you can set tags in an environment variable.

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