Skip to main content

How to track threads

A Thread is a sequence of traces representing a single thread. Each response is represented as it's own trace, but these traces are linked together by being part of the same thread.

To associate traces together, you need to pass in a special metadata key where the value is the unique identifier for that thread.

Below is an example of logging conversations:


import openai
from langsmith import traceable
import uuid
client = openai.Client()
session_id = str(uuid.uuid4())
@traceable(
run_type="chain",
name="OpenAI Assistant",
)
def assistant(
messages: list[dict]
):
return client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "system", "content": "You are a helpful assistant."}] + messages,
).choices[0].message
messages = [
{"role": "user", "content": "hi! im bob"}
]
response = assistant(
messages,
langsmith_extra={"metadata": {"session_id": session_id}}
)
messages = messages + [
response,
{"role": "user", "content": "what's my name?"}
]
response = assistant(
messages,
langsmith_extra={"metadata": {"session_id": session_id}}
)

How to view threads

You can view threads by clicking on the Threads tag in a project. You will then see a list of all recent threads.

Thread Tab

You can then click into a particular thread. This will open the history for a particular thread. If your threads are formatted as chat messages, you will a chatbot-like UI where you can see a history of inputs and outputs.

Conversation

What are special metadata keys that associate traces as part of the same thread?

In order to log runs as part of the same thread you need to pass a special metadata key to the run. The key value is the unique identifier for that conversation. The key name should be one of:

  • session_id
  • thread_id
  • conversation_id.

Was this page helpful?


You can leave detailed feedback on GitHub.