Skip to content

Commit e369e12

Browse files
authored
feat(js): Adds docs on tracing in serverless environments in JS (#492)
1 parent 620f79d commit e369e12

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

docs/observability/how_to_guides/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Set up LangSmith tracing to get visibility into your production applications.
3434
- [Prevent logging of sensitive data in traces](./how_to_guides/tracing/mask_inputs_outputs)
3535
- [Trace generator functions](./how_to_guides/tracing/trace_generator_functions)
3636
- [Calculate token-based costs for traces](./how_to_guides/tracing/calculate_token_based_costs)
37+
- [Trace JS functions in serverless environments](./how_to_guides/tracing/serverless_environments)
3738

3839
## Tracing projects UI & API
3940

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Trace JS functions in serverless environments
2+
3+
:::note
4+
This section is relevant for those using the LangSmith JS SDK version 0.2.0 and higher.
5+
If you are tracing using LangChain.js or LangGraph.js in serverless environments, see [this guide](https://js.langchain.com/docs/how_to/callbacks_serverless).
6+
:::
7+
8+
When tracing JavaScript functions, LangSmith will trace runs in the background by default to avoid adding latency.
9+
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.
10+
11+
To make sure this occurs, you can either:
12+
13+
- Set an environment variable named `LANGSMITH_TRACING_BACKGROUND` to `"false"`. This will cause your traced functions to wait for tracing to complete before returning.
14+
- Note that this is named differently from the [environment variable](https://js.langchain.com/docs/how_to/callbacks_serverless) in LangChain.js because LangSmith can be used without LangChain.
15+
- Pass a custom client into your traced runs and `await` the `client.awaitPendingTraceBatches();` method.
16+
17+
Here's an example of using `awaitPendingTraceBatches` alongside the [`traceable`](/observability/how_to_guides/tracing/annotate_code) method:
18+
19+
```ts
20+
import { Client } from "langsmith";
21+
import { traceable } from "langsmith/traceable";
22+
23+
const langsmithClient = new Client({});
24+
25+
const tracedFn = traceable(
26+
async () => {
27+
return "Some return value";
28+
},
29+
{
30+
client: langsmithClient,
31+
}
32+
);
33+
34+
const res = await tracedFn();
35+
36+
await langsmithClient.awaitPendingTraceBatches();
37+
```

docs/observability/how_to_guides/tracing/trace_with_langchain.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ console.log(runId);`),
281281

282282
In LangChain Python, LangSmith's tracing is done in a background thread to avoid obstructing your production application. This means that your process may end before all traces are successfully posted to LangSmith. This is especially prevalent in a serverless environment, where your VM may be terminated immediately once your chain or agent completes.
283283

284-
In LangChain JS/TS, the default is to block for a short period of time for the trace to finish due to the greater popularity of serverless environments. You can make callbacks asynchronous by setting the `LANGCHAIN_CALLBACKS_BACKGROUND` environment variable to `"true"`.
284+
You can make callbacks synchronous by setting the `LANGCHAIN_CALLBACKS_BACKGROUND` environment variable to `"false"`.
285285

286286
For both languages, LangChain exposes methods to wait for traces to be submitted before exiting your application.
287287
Below is an example:

versioned_docs/version-old/tracing/faq/langchain_specific_guides.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ This tactic is also useful for when you have multiple chains running in a shared
187187

188188
In LangChain Python, LangSmith's tracing is done in a background thread to avoid obstructing your production application. This means that your process may end before all traces are successfully posted to LangSmith. This is especially prevalent in a serverless environment, where your VM may be terminated immediately once your chain or agent completes.
189189

190-
In LangChain JS, prior to `@langchain/core` version `0.3.0`, the default was to block for a short period of time for the trace to finish due to the greater popularity of serverless environments. Versions `>=0.3.0` will have the same default as Python.
190+
In LangChain JS, prior to `@langchain/core` version `0.3.0`, the default was to block for a short period of time for the trace to finish due to the greater popularity of serverless environments. Versions `>=0.3.0` have the same default as Python.
191191
You can explicitly make callbacks synchronous by setting the `LANGCHAIN_CALLBACKS_BACKGROUND` environment variable to `"false"` or asynchronous by setting it to `"true"`. You can also check out [this guide](https://js.langchain.com/docs/how_to/callbacks_serverless) for more options for awaiting backgrounded callbacks in serverless environments.
192192

193193
For both languages, LangChain exposes methods to wait for traces to be submitted before exiting your application.

0 commit comments

Comments
 (0)