Trace JS functions in serverless environments
note
This section is relevant for those using the LangSmith JS SDK version 0.2.0 and higher. If you are tracing using LangChain.js or LangGraph.js in serverless environments, see this guide.
When tracing JavaScript functions, LangSmith will trace runs in the background by default to avoid adding latency. In serverless environments where the execution context may be terminated abruptly, it's important to ensure that all tracing data is properly flushed before the function completes.
To make sure this occurs, you can either:
- Set an environment variable named
LANGSMITH_TRACING_BACKGROUND
to"false"
. This will cause your traced functions to wait for tracing to complete before returning.- Note that this is named differently from the environment variable in LangChain.js because LangSmith can be used without LangChain.
- Pass a custom client into your traced runs and
await
theclient.awaitPendingTraceBatches();
method.
Here's an example of using awaitPendingTraceBatches
alongside the traceable
method:
import { Client } from "langsmith";
import { traceable } from "langsmith/traceable";
const langsmithClient = new Client({});
const tracedFn = traceable(
async () => {
return "Some return value";
},
{
client: langsmithClient,
}
);
const res = await tracedFn();
await langsmithClient.awaitPendingTraceBatches();