From ac9b8ef371d9418e5ded4bbe044d67f1f487de78 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Sat, 26 Oct 2024 17:48:55 -0700 Subject: [PATCH 1/6] Adds docs on tracing in serverless environments in JS --- docs/observability/how_to_guides/index.md | 1 + .../tracing/serverless_environments.mdx | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 docs/observability/how_to_guides/tracing/serverless_environments.mdx diff --git a/docs/observability/how_to_guides/index.md b/docs/observability/how_to_guides/index.md index 9c1b3da67..829e48c7d 100644 --- a/docs/observability/how_to_guides/index.md +++ b/docs/observability/how_to_guides/index.md @@ -34,6 +34,7 @@ Set up LangSmith tracing to get visibility into your production applications. - [Prevent logging of sensitive data in traces](./how_to_guides/tracing/mask_inputs_outputs) - [Trace generator functions](./how_to_guides/tracing/trace_generator_functions) - [Calculate token-based costs for traces](./how_to_guides/tracing/calculate_token_based_costs) +- [Trace in serverless environments (JS only)](./how_to_guides/tracing/serverless_environments) ## Tracing projects UI & API diff --git a/docs/observability/how_to_guides/tracing/serverless_environments.mdx b/docs/observability/how_to_guides/tracing/serverless_environments.mdx new file mode 100644 index 000000000..5c11ecddd --- /dev/null +++ b/docs/observability/how_to_guides/tracing/serverless_environments.mdx @@ -0,0 +1,34 @@ +# Trace in serverless environments (JS only) + +:::note +This section is relevant for those using the LangSmith SDK version 0.2.0 and higher. +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). +::: + +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. +- Pass a custom client into your traced runs and `await` the `client.awaitPendingTraceBatches();` method. + +Here's an example of using `awaitPendingTraceBatches` alongside the [`traceable`](/observability/how_to_guides/tracing/annotate_code) method: + +```ts +import { Client } from "langsmith"; +import { traceable } from "langsmith/"; + +const langsmithClient = new Client({}); + +const tracedFn = traceable( + async () => { + return "Some return value"; + }, + { + client: langsmithClient, + } +); + +await langsmithClient.awaitPendingTraceBatches(); +``` From 9df93a0ee9f7a563cc05a26fcdf49c10111ec20e Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Sat, 26 Oct 2024 17:51:36 -0700 Subject: [PATCH 2/6] Fix typo --- .../how_to_guides/tracing/serverless_environments.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/observability/how_to_guides/tracing/serverless_environments.mdx b/docs/observability/how_to_guides/tracing/serverless_environments.mdx index 5c11ecddd..4c772a558 100644 --- a/docs/observability/how_to_guides/tracing/serverless_environments.mdx +++ b/docs/observability/how_to_guides/tracing/serverless_environments.mdx @@ -17,7 +17,7 @@ Here's an example of using `awaitPendingTraceBatches` alongside the [`traceable` ```ts import { Client } from "langsmith"; -import { traceable } from "langsmith/"; +import { traceable } from "langsmith/traceable"; const langsmithClient = new Client({}); From 0aa9c346afbb5f23c143f5fbfe4d56f9fd2580e1 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Sat, 26 Oct 2024 18:00:00 -0700 Subject: [PATCH 3/6] Fix typo --- .../how_to_guides/tracing/serverless_environments.mdx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/observability/how_to_guides/tracing/serverless_environments.mdx b/docs/observability/how_to_guides/tracing/serverless_environments.mdx index 4c772a558..a6928dc13 100644 --- a/docs/observability/how_to_guides/tracing/serverless_environments.mdx +++ b/docs/observability/how_to_guides/tracing/serverless_environments.mdx @@ -30,5 +30,7 @@ const tracedFn = traceable( } ); +const res = await tracedFn(); + await langsmithClient.awaitPendingTraceBatches(); ``` From f823ceda8264b0f57183ca0f01ebe23827203735 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 28 Oct 2024 09:12:51 -0700 Subject: [PATCH 4/6] Update copy --- .../how_to_guides/tracing/serverless_environments.mdx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/observability/how_to_guides/tracing/serverless_environments.mdx b/docs/observability/how_to_guides/tracing/serverless_environments.mdx index a6928dc13..dd36d194c 100644 --- a/docs/observability/how_to_guides/tracing/serverless_environments.mdx +++ b/docs/observability/how_to_guides/tracing/serverless_environments.mdx @@ -1,7 +1,7 @@ # Trace in serverless environments (JS only) :::note -This section is relevant for those using the LangSmith SDK version 0.2.0 and higher. +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](https://js.langchain.com/docs/how_to/callbacks_serverless). ::: @@ -11,6 +11,7 @@ In serverless environments where the execution context may be terminated abruptl 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](https://js.langchain.com/docs/how_to/callbacks_serverless) in LangChain.js because LangSmith can be used without LangChain. - Pass a custom client into your traced runs and `await` the `client.awaitPendingTraceBatches();` method. Here's an example of using `awaitPendingTraceBatches` alongside the [`traceable`](/observability/how_to_guides/tracing/annotate_code) method: From 6590421b54f98196969cedd7aa3d462bc4cd2b62 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 28 Oct 2024 09:29:28 -0700 Subject: [PATCH 5/6] More copy updates --- docs/observability/how_to_guides/index.md | 2 +- .../how_to_guides/tracing/trace_with_langchain.mdx | 2 +- .../version-old/tracing/faq/langchain_specific_guides.mdx | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/observability/how_to_guides/index.md b/docs/observability/how_to_guides/index.md index 829e48c7d..ae2b40bf6 100644 --- a/docs/observability/how_to_guides/index.md +++ b/docs/observability/how_to_guides/index.md @@ -34,7 +34,7 @@ Set up LangSmith tracing to get visibility into your production applications. - [Prevent logging of sensitive data in traces](./how_to_guides/tracing/mask_inputs_outputs) - [Trace generator functions](./how_to_guides/tracing/trace_generator_functions) - [Calculate token-based costs for traces](./how_to_guides/tracing/calculate_token_based_costs) -- [Trace in serverless environments (JS only)](./how_to_guides/tracing/serverless_environments) +- [Trace JS functions in serverless environments](./how_to_guides/tracing/serverless_environments) ## Tracing projects UI & API diff --git a/docs/observability/how_to_guides/tracing/trace_with_langchain.mdx b/docs/observability/how_to_guides/tracing/trace_with_langchain.mdx index 273fb32d4..8e6bd28e8 100644 --- a/docs/observability/how_to_guides/tracing/trace_with_langchain.mdx +++ b/docs/observability/how_to_guides/tracing/trace_with_langchain.mdx @@ -281,7 +281,7 @@ console.log(runId);`), 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. -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"`. +You can make callbacks synchronous by setting the `LANGCHAIN_CALLBACKS_BACKGROUND` environment variable to `"false"`. For both languages, LangChain exposes methods to wait for traces to be submitted before exiting your application. Below is an example: diff --git a/versioned_docs/version-old/tracing/faq/langchain_specific_guides.mdx b/versioned_docs/version-old/tracing/faq/langchain_specific_guides.mdx index 0fa24f32a..5c8d27efc 100644 --- a/versioned_docs/version-old/tracing/faq/langchain_specific_guides.mdx +++ b/versioned_docs/version-old/tracing/faq/langchain_specific_guides.mdx @@ -187,7 +187,7 @@ This tactic is also useful for when you have multiple chains running in a shared 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. -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. +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. 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. For both languages, LangChain exposes methods to wait for traces to be submitted before exiting your application. From 1eac3e86d9df20dedea388da29a326f75fc1cce3 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 28 Oct 2024 09:31:15 -0700 Subject: [PATCH 6/6] Fix title --- .../how_to_guides/tracing/serverless_environments.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/observability/how_to_guides/tracing/serverless_environments.mdx b/docs/observability/how_to_guides/tracing/serverless_environments.mdx index dd36d194c..9059ddbbb 100644 --- a/docs/observability/how_to_guides/tracing/serverless_environments.mdx +++ b/docs/observability/how_to_guides/tracing/serverless_environments.mdx @@ -1,4 +1,4 @@ -# Trace in serverless environments (JS only) +# Trace JS functions in serverless environments :::note This section is relevant for those using the LangSmith JS SDK version 0.2.0 and higher.