diff --git a/docs/observability/how_to_guides/index.md b/docs/observability/how_to_guides/index.md index 9c1b3da67..ae2b40bf6 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 JS functions in serverless environments](./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..9059ddbbb --- /dev/null +++ b/docs/observability/how_to_guides/tracing/serverless_environments.mdx @@ -0,0 +1,37 @@ +# 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](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. + - 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: + +```ts +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(); +``` 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.