# How to use Langsmith's Unique Custom Traces! #langchain #ai ## Metadata - **Published:** 4/4/2024 - **Duration:** 12 minutes - **YouTube URL:** https://youtube.com/watch?v=NGVLL6-w2K4 - **Channel:** nerding.io ## Description In this video, we'll cover how to easily customize various attributes of the traces you log to LangSmith, LangChain's open-source tracing and debugging tool. You'll learn how to: - Set custom trace names - Add extra metadata to traces - Tag traces for easier filtering - Adjust the level of detail captured Customizing traces allows you to track and debug your LangChain applications more effectively. With just a few lines of code, you can enrich your traces with the exact information you need. Whether you're a LangChain beginner or power user, this quick guide will level up your tracing and debugging workflow with LangSmith. Course: https://forms.gle/PN3RpKD8eHsicL9Z7 ๐Ÿ“ฐ News & Resources: https://sendfox.com/nerdingio ๐Ÿ“ž Book a Call: https://calendar.app.google/M1iU6X2x18metzDeA ๐ŸŽฅ Chapters 00:00 Introduction 00:36 Understanding Traces 01:27 Custom Attributes 02:20 NextJS Example 05:45 Trace Attributes 07:28 Masking inputs and outputs ๐Ÿ”— Links https://github.com/nerding-io/langchain-nextjs-example https://docs.smith.langchain.com/tracing/faq/customizing_trace_attributes https://smith.langchain.com/ โคต๏ธ Let's Connect https://everefficient.ai https://nerding.io https://twitter.com/nerding_io https://www.linkedin.com/in/jdfiscus/ https://www.linkedin.com/company/ever-efficient-ai/ ## Key Highlights ### 1. Customizing Langsmith Trace Attributes Langsmith allows customizing trace attributes like project name, run name, metadata, and tags for enhanced debugging and organization of logged data. ### 2. Dynamic Metadata & Tag Configuration Metadata and tags can be added and modified at runtime during chain invocations, enabling contextual logging and easier identification of specific log entries. ### 3. Masking Sensitive Input/Output Data Langsmith enables selective masking of input or output data in traces, allowing control over which information is logged for privacy or security reasons. Not the same as pii masking but removes info. ### 4. Switching Between Filtered and Unfiltered Tracers It's possible to switch between filtered and unfiltered tracers within the same Langsmith project, providing flexibility in logging different levels of detail. ## Summary ## Langsmith Custom Trace Attributes Video Summary **1. Executive Summary:** This video tutorial explains how to customize Langsmith traces, LangChain's open-source tracing and debugging tool, for enhanced application monitoring and debugging. It covers customizing trace names, adding metadata and tags, and masking sensitive input/output data, enabling developers to better organize and filter their trace data. **2. Main Topics Covered:** * **Understanding Langsmith Traces:** Overview of how Langsmith logs and traces function within LangChain, particularly in the context of JavaScript and Next.js applications. * **Custom Trace Attributes:** Modification of trace attributes like project name, run name, metadata, and tags directly within the code. Defining tracers, metadata and tags when invoking chains. * **Next.js Example:** Demonstrates implementation of custom trace attributes in a Next.js application, including routes for traces, masking, and project management. * **Masking Input/Output Data:** Explanation of how to selectively hide input or output data within Langsmith traces for privacy or security reasons, differing from traditional PII masking. * **Switching Between Filtered and Unfiltered Tracers:** Demonstrates the ability to alternate between filtered and unfiltered tracers within the same Langsmith project for different levels of detail. **3. Key Takeaways:** * Langsmith allows dynamic customization of trace attributes at runtime for enhanced debugging and organization. * Metadata and tags can be added and modified at runtime during chain invocations, enabling contextual logging. * Selective masking of input/output data is possible, providing control over logged information for security or privacy. * Developers can switch between filtered and unfiltered tracers within the same project for flexible logging detail. * Customizing run names provides clarity and easy identification within the Langsmith dashboard. **4. Notable Quotes or Examples:** * "So what that means is we can change things like project name run name even things like metadata or tags but most importantly we can also actually mask different type of input and output information." * Example of defining the Langchain Tracer project name in code: `tracer.project_name = "LangSmith code for my project";` * Example of hiding inputs and output using the Langsmith client: `hide_inputs=True`, `hide_outputs=True` * Showed example of adding Metadata in the llm invoke. **5. Target Audience:** * LangChain developers and engineers * AI application developers using Langsmith for debugging and tracing * Individuals seeking to enhance their LangChain debugging workflow * Developers interested in customizing trace attributes for better monitoring ## Full Transcript hey everyone welcome to nerding IO I'm JD and today we're going to be looking at Lang Smith and the ability to change custom attributes of the traces so what that means is we can change things like project name run name even things like metadata or tags but most importantly we can also actually mask different type of input and output information this is very similar to a Technique we looked at with pii masking so with that let's go ahead and get started all right so the first thing that we're going to do is we're actually going to look at the Lang Smith Lang Smith documentation and understand a little bit about logs and traces or how to log in Trace and so if you've been on this YouTube before um we've gone through a few different examples previously we've looked at the way to use lsmith with python in JavaScript and then there's also sdks for uh the python and then typescript we've even looked at the uh way to post this so you can actually use lsmith through a uh an API what we're going to focus though on today is mostly JavaScript we're actually going to look at it in a nextjs application so you can actually view all of your uh traces here in Lang Smith and we're going to learn how to customize that information so if you look down here there's this how to customize attributes of the traces and the the first thing it starts talking about is typically you actually have this export in your environment variables of what your project name is so uh this is usually linked directly like you can see over here in the tab that it's um something that Lang chain makes up but you can also Define it so you can specifically Define it in your environment variable now you can actually create a tracer and actually Define it in your product and then there's other things like adding meta metadata tags and even uh customizing run name so what we're going to do is we're actually going to look at this in nextjs and we'll pull up the previous uh nextjs repo that we had before so if we go ahead and look at this the first thing that you're going to want to notice is you have these variables nothing's really changed here we had a Lan change uh Lane chain project variable before if we want to mask our uh Lane chain inputs we can turn it on here or we can actually change it in the code so what I've done is I've created two different routes over here for traces tracers and for the masking route as well as the project route project route is kind of like a kitchen sink so that's where we're going to start real quick everyone if you haven't already please remember to like And subscribe it helps more than you know we also have a link below of an upcoming course on privatized gpts that are white labeled for small business and with that let's get back to it so if we take a a look at this and then we go match it with our URL here we're going to pull up this so this is this comes from the pii app so we're using that same repo but if we go ahead and log this we can see what's going to happen it's just going to show us the so if we go ahead and log uh run this we can actually see what our log will be and we're just going to look at our code for now so right now we can see inside of our code right now we're in tracers project rout we're just doing a get we're looking at what our project name is and then we're going to be putting in our input which is coming down later here in our variable for our template we're actually going to take our Lang chain Tracer we're going to change our project name right in the code so this way we could make it uh a variable so we're going to look in our Lang Smith code for my project then we're going to go through our prompt in our chain we have our uh parser output and then this is where we start to change our metadata tags so you can see right here we have top level tags that were Act changing so inside this with config function we can actually Define the tags and our exclusive metadata for this uh runnable config the next thing that's interesting about this is we can actually change it at our runtime as well so once we're actually invoking the chain we can actually look and see what our tags are going to be in our metadata and then lastly we're going to actually do a custom config and see uh how we can actually have this run name of my custom chain and again we're going to have another invoke so these are two separate invokes we're actually doing the meta tags and pipeline uh the top level tags here and then shared tags here and then lastly we're going to be doing a custom runtime name here all right so if we look at our link Smith now we can see we have two uh runs in here and we're inside our project and we're going to go ahead and look at our first one this runnable sequence so when we click here we can actually see our metadata is being both at the top level and the shared level we can also drill down into not just the runnable sequence but the template itself we can see it here we can see right here we also have our metadata in our when we're making the chat open Ai call and then lastly we have it in our string uh string output parser so we know that we're associating this metadata on the fly when we're actually calling this particular function and this makes it easier to find these kinds of logs if we were looking for this particular metadata the other run that we had in here remember we had our first invoke here this is where we had our top level and our uh runtime logs or um metadata and tags and in here we actually are configuring the Run name to something customed again so this is uh a different invocation so if we go back and we look we can already see right here here's our my custom chain we don't have all of the same uh metadata we have our top level metadata but we don't necessarily have the shared metadata that was on the other invocation and so that's a really cool way of again how we can like customize the attributes so that we can understand where we're actually at in our sequence again making it easier to debug all the different information that's coming into uh link Smith the next next thing we're going to do is we're actually going to look at the masking so if we go back to our code and we go over to this endpoint and we look at our masking route it's a little bit different so what we're going to do and this is left over from before uh but where I was trying to put the client in the rout and also test out the typescript but what we're going to do is we're going to load in our Tracer and then we have to Define a lang Smith client now one of the things that was interesting for me is when I tried to put in the inputs from the documentation it said that the value needed to be booing so if you come back here when I was looking at the the masking input so you can do it in the environment variables like we talked about or you can call it here when I used this it kept telling me that it needed to be uh or it expected a boost so I'll just show the eror and so by uh by switching this from an any to the uh a Boolean I was actually able to get it work to work so it was a little deviation from the um the documentation but now what we're going to do is we're actually going to use this Ling uh chain or the client for Lang Smith and then we're going to have our Tracer like we did before we're passing at the client and then that client is where we're going to essentially Define that we want to hide the inputs and outputs and it might not it's not the same as like masking like we did previously with pii where you could kind of hide it and show it and it was storing in um in context but like it's still a way to remove the information from the log so you can still run your Tracer except it won't log the information so we'll see an example of of what we mean here so if we go back to our uh website and in this time we're just going to be using the masking URI go ahead and go refresh and we'll look at the at Lang Smith and see what we get so now we have two different invocations right and so the first one we're not expecting any custom meta metadata but in our run we don't see any data coming back for the input or the output and that's specifically because in our code base we've defined the hide inputs and hide outputs right here is where we're actually doing that uh llm invoke so what could be interesting is we could try and see if well maybe we just want to hide inputs but we don't NE necessarily want to hide outputs and we'll go ahead and do another run and see what happens so now when we come here we can see again we're not seeing the input data that's coming out or coming in but we're seeing the output data and so that could technically be beneficial for if we only want to if if nothing in here is uh you know like pii or anything like that but the input itself and in order to ingest that data could be and then just to to show the other uh llm invoke we're just showing that in that same call we were able to then do a different llm invoke and we'd still be able to see the input in output so this allows us to kind of switch back and forth between a uh filter Tracer and an unfiltered Tracer so you can have different versions but it's still logged into the lsmith project uh every single time all right everyone that's it for today again if you haven't liked and subscribed please remember to do so we also have that upcoming course but today what we went over was Ling Smith and how to customize the tracers so being able to focus on a specific project but also looking at different variables that we can change and the most important thing being able to actually mask information that is going in and out of your logs so with that that happy nerding --- *Generated for LLM consumption from nerding.io video library*