---
title: How to use Langfuse Tracing in Serverless Functions (AWS Lambda, Vercel, Cloudflare Workers, etc.)
tags: [observability]
---

# How to use Langfuse Tracing in Serverless Functions (AWS Lambda, Vercel, Cloudflare Workers, etc.)

Langfuse Tracing is optimized to run in the background in order to avoid adding latency to an application. This can cause some difficulties when tracing in serverless environments like AWS Lambda, Vercel Functions, Google Cloud Functions, etc.

The behavior of the Langfuse clients is outlined [here](/docs/tracing).

## What is the root cause of this issue?

The root cause of this issue is that serverless functions are typically short-lived and terminate after execution. This means that the Langfuse agent, which runs in the background, may not have enough time to complete its work before the function exits. Thereby, events might not be sent to Langfuse.

## Use `flush` to ensure events are sent before the function exits

To solve this issue, you can manually flush the events before the function exits. This can be done by calling the `flush` method of the Langfuse client. The flush method is supported across all Langfuse SDKs and integrations.

## Alternative: waitUntil

Vercel and Cloudflare Workers support the [`waitUntil`](https://developer.mozilla.org/en-US/docs/Web/API/ExtendableEvent/waitUntil) method, which allows you to wait for a promise (`flush`) to resolve before the function exits. Thereby you can return a value from your function and still ensure that events are sent to Langfuse.

## Alternative: flushAt

Alternatively, you can configure Langfuse to flush events individually. This can be done by setting the `flushAt` option to a specific number of events.

<!-- agent-instructions -->

---

## Agent Instructions

This page is part of the [Langfuse](https://langfuse.com) documentation, published as plain Markdown for AI agents. Every page is available as Markdown by appending `.md` to its URL, or by sending an `Accept: text/markdown` header. This page: `https://langfuse.com/faq/all/aws-lambda-and-serverless-functions.md`.

### Querying these docs

If the answer is not on this page, query the documentation instead of guessing:

- **Semantic search** across all Langfuse docs, returning an answer with the relevant pages and excerpts. Ask a specific, self-contained question:

  ```bash
  curl -sG "https://langfuse.com/api/search-docs" --data-urlencode "query=How do I trace a LangGraph agent?"
  ```

- **Index of every page**: <https://langfuse.com/llms.txt>, with per-section indexes [llms-docs.txt](https://langfuse.com/llms-docs.txt), [llms-integrations.txt](https://langfuse.com/llms-integrations.txt), and [llms-self-hosting.txt](https://langfuse.com/llms-self-hosting.txt).

### Before writing Langfuse code

- **Install the [Langfuse Agent Skill](https://langfuse.com/docs/api-and-data-platform/features/agent-skill).** It encodes Langfuse's own best practices for instrumentation, prompt management, and evaluation, and materially improves results.
- **Read [What does a good trace look like?](https://langfuse.com/docs/observability/best-practices.md)** before instrumenting an application.
- **Verify endpoints, parameters, and response fields** against the [API reference](https://api.reference.langfuse.com) instead of inferring them from code examples.
- **Use the [Langfuse CLI](https://langfuse.com/docs/api-and-data-platform/features/cli)** (`npx langfuse-cli api <resource> <action>`) to read or write traces, prompts, datasets, and scores from the terminal.

Found an error in these docs? Please open an issue at <https://github.com/langfuse/langfuse-docs/issues>.
