---
title: Trace an LLM Application in AWS Lambda
description: >-
  Instrument an existing AWS Lambda function with Agent Observability by using
  the Datadog Extension and language layers.
breadcrumbs: >-
  Docs > Agent Observability > Agent Observability Guides > Trace an LLM
  Application in AWS Lambda
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# Trace an LLM Application in AWS Lambda

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com, us2.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md). ({% placeholder "user-datadog-site-name" /%}).
{% /alert %}

{% /callout %}

To instrument an existing AWS Lambda function with Agent Observability, you can use the Datadog Extension and respective language layers.

1. Open a Cloudshell in the AWS console.
1. Install the Datadog CLI client

```shell
npm install -g @datadog/datadog-ci
```
Set the Datadog API key and site
```shell
export DD_API_KEY=<YOUR_DATADOG_API_KEY>
export DD_SITE=<YOUR_DATADOG_SITE>
```

If you already have or prefer to use a secret in Secrets Manager, you can set the API key by using the secret ARN:

```shell
export DATADOG_API_KEY_SECRET_ARN=<DATADOG_API_KEY_SECRET_ARN>
```

Instrument your Lambda function with Agent Observability (this requires at least version 77 of the Datadog Extension layer)

{% tab title="Python" %}

```shell
datadog-ci lambda instrument -f <YOUR_LAMBDA_FUNCTION_NAME> -r <AWS_REGION> -v 127 -e 99 --llmobs <YOUR_LLMOBS_ML_APP>
```

{% /tab %}

{% tab title="Node.js" %}

```shell
datadog-ci lambda instrument -f <YOUR_LAMBDA_FUNCTION_NAME> -r <AWS_REGION> -v 143 -e 99 --llmobs <YOUR_LLMOBS_ML_APP>
```

{% /tab %}

{% tab title="Java" %}

```shell
datadog-ci lambda instrument -f <YOUR_LAMBDA_FUNCTION_NAME> -r <AWS_REGION> -v 28 -e 99 --llmobs <YOUR_LLMOBS_ML_APP>
```

{% /tab %}



Invoke your Lambda function and verify that Agent Observability traces are visible in the Datadog UI.

Manually flush Agent Observability traces by using the `flush` method before the Lambda function returns.

{% tab title="Python" %}

```python
from ddtrace.llmobs import LLMObs
def handler():
  # function body
  LLMObs.flush()
```

{% /tab %}

{% tab title="Node.js" %}

```javascript
import tracer from 'dd-trace';
const llmobs = tracer.llmobs;

export const handler = async (event) => {
  // your function body
  llmobs.flush();
};
```

{% /tab %}

## Further Reading{% #further-reading %}

Additional helpful documentation, links, and articles:

- [Agent Observability SDK Reference](https://docs.datadoghq.com/llm_observability/instrument/sdk.md)
