 In this video, I want to talk about threads and tracking threads in LangSmith. So LangSmith captures traces, and each trace can have a subset of runs. But these traces are oftentimes different calls and invocations. So one way to think about this is when you're having a conversation with a chatbot, each time the chatbot responds, that's one trace. There might be many different things happening inside it. It might be doing a retrieval step and then responding, but that's one trace. But when you have a conversation, you send a message as the chatbot sends a message, you send a message as the chatbot sends a message, and each of those messages are separate traces. But when debugging and trying to understand what's going on, it can be useful to tie those traces together so you have a full view of the conversation that's happening. And so this is where threads come in. And basically what you can do is you can attach a thread ID to traces and then you can view and group traces by those thread IDs. So I'm going to walk through a quick example of doing exactly that. So we have an example here where we're not using link chain at all. This is just raw open AI and where we're tracking this thread ID. And so there's three different ways that you can specify a thread ID. It can either be called session ID, thread ID, or conversation ID. And so here you'll see that we're putting session ID as an extra metadata argument. So if I copy this code snippet, I'm going to bring it over to my Jupyter notebook I have. I'm going to run this. And let's take a closer look at what exactly is going on. So I'm using OpenAI. I'm importing this traceable decorator. This has to do with tracing. This is a really easy way to basically trace your runs. And then I'm defining the session ID. And so this is a unique identifier that I'm going to be passing through in the metadata. I have this function here, which is a nice little thing that basically just calls OpenAI. And then I have my conversation that I'm simulating. So I have the messages that I send in first. Hi, I'm Bob. I send this into the assistant. And I pass in this Langsmith extra, this metadata key. And this is important because this metadata key isn't known ahead of time. And so if it was known ahead of time, we could have specified it as part of the traceable because it's not known ahead of time because it's this unique session ID and it's going to be different for each session, I'm going to pass it in with this LangSmith extra. Then I'm going to get back the response. I'm adding it into my list of messages, and then I'm asking another one, what's my name? And then I'm calling the assistant again. And so if we go to LangSmith, these are getting logged to my default project. And so if we go to Langsmith, these are getting logged to my default project. And so if I go to threads, I can see that I have a new thread here. I can click into it. And here I have basically this this very chatbot conversation like view where I can see the the conversation as it kind of like unfolds. From here, I can also open the individual traces. So if I want to see the kind of like full trace here I can open it up and it's not that interesting because I just have a single call here. But the point is that you can view the conversations together but then you can easily drill into individual traces if you want to and so it provides kind of like the best of both worlds. I'm going to show this again with langchain this time. So here let me copy this code snippet, I'm going to go into the notebook, and we can see here that I am basically I'm creating a really simple chain that's just a prompt plus a model. I'm creating my list of messages and then I'm creating this run config and so this is just a dictionary with this metadata key. I'm specifying conversation ID this time, so you can see how I'm alternating the different keys that I could specify. And then when I call the chain, when I use.invoke, I'm passing in this. This is just my input. But then I'm passing in config equals config. And remember, I defined my config up here, and it's just this metadata with the conversation ID. I then add to my messages and then I call it again. And so let me run this. Now I can go back here. I can go back here. I can see that I have a new conversation thread here. And when I click on this, this is what happened when I called it with link chain. So hopefully this is a good example of how you can easily attach this thread ID to any traces that you have, and then you can view them in a very user-friendly way, again, whether you're using link chain or not. That's it for this video.
Threads: Unified chat views for conversation debugging
In chat applications, there is a back and forth between human messages and AI responses. Each message turn is a trace, but you may want to see the whole thread history in one place if you’re trying to understand or debug a conversation. With Threads, we've now introduced a way to view the full back-and-forth of a conversation in a single view.

Documentation: https://docs.smith.langchain.com/monitoring/faq/threads
Blog post: https://blog.langchain.dev/langsmith-production-logging-automations/