Skip to main content
Beta — This feature is subject to change.
The Braintrust Lambda extension runs alongside your AWS Lambda function and accepts Braintrust trace batches locally, reducing the amount of time that the Braintrust SDK’s flush() method spends in the request path. Use the extension when your Lambda function already logs traces to Braintrust, calls the SDK’s flush() method before returning, and needs to reduce trace delivery latency.

Compatibility

The extension supports project-log tracing from these Braintrust SDKs: The extension does not support Braintrust’s Ruby, Go, Java, or .NET SDKs.

Setup

Start with a Lambda function that already logs traces to Braintrust with a supported SDK. Add the extension, point the SDK at the local listener, and keep flush() in your handler.
1

Attach the extension layer

Add the published layer ARN for your function’s AWS region and architecture. Replace <version> with the version from the Braintrust Lambda Extension release.For x86_64 in us-west-2:
For arm64 in us-west-2:
The extension layer is published only in us-west-2. For container-image Lambda functions, copy the extension executable into /opt/extensions/braintrust-lambda-extension instead of attaching a layer.
2

Point the SDK at the extension

Set these environment variables on the Lambda function so Braintrust trace data goes to the extension:
The extension uses the API key from inbound SDK requests. You do not need to set BRAINTRUST_API_KEY as a Lambda environment variable if your application already provides the key to the SDK. You can set BRAINTRUST_API_KEY on the Lambda function as a fallback, but passing credentials through the SDK keeps the key out of the function configuration.
3

Use a project ID

When initializing your logger, pass project_id directly when possible (to find your project ID, go to Settings > General). If your logger initialization passes only a project name, also set BRAINTRUST_PROJECT_ID so the extension knows which project to use.
4

Flush at the end of the handler

Keep the explicit flush() call at the end of your handler. With the extension attached, flush() waits for a local handoff instead of waiting for remote Braintrust delivery.
Python
5

Verify traces in Braintrust

Invoke your Lambda function, then go to Logs. If the trace does not appear after a short delay, check the function’s CloudWatch logs for Braintrust Lambda extension errors.
6

Validate latency improvement

Time flush() before and after attaching the extension. If you track Lambda performance, also compare function Duration, which can include extension work after the handler returns.
Use the same layer and environment-variable configuration in your infrastructure code.
CloudFormation
Terraform

Extension configuration

The extension supports these environment variables for local intake, trace delivery, and buffering. To further reduce latency, you can adjust the way the SDK hands traces to the extension:
  • Set BRAINTRUST_SYNC_FLUSH=1 so flush() sends queued traces to the local extension itself instead of coordinating with the SDK’s background publisher.
  • Set BRAINTRUST_NUM_RETRIES=0 to skip SDK retries for the local handoff. After the extension receives a payload, the extension handles retries to Braintrust.
Leave SDK retries enabled if you want the SDK to retry the handoff before the extension has accepted the payload.

Troubleshooting

Use Lambda logs and Braintrust logs together when checking the extension.
  • Verify the function has the extension layer attached for the correct architecture.
  • Verify BRAINTRUST_APP_URL and BRAINTRUST_API_URL are set to http://127.0.0.1:49891.
  • Verify your function still calls flush() before returning.
  • Verify the Lambda execution environment allows outbound HTTPS traffic. If the function is attached to a VPC, confirm the selected subnets have internet access.
  • Pass project_id directly to your logger when possible.
  • If your logger initialization uses a project name, set BRAINTRUST_PROJECT_ID on the Lambda function.
  • Confirm the SDK is pointing at the local extension, not the remote Braintrust data plane.
  • Review the SDK settings in extension configuration.
The SDK can send oversized payloads to a separate upload endpoint, /logs3/overflow. The extension does not support that endpoint.By default, BRAINTRUST_LAMBDA_DISABLE_SDK_OVERFLOW=1 prevents the SDK from using /logs3/overflow, so payloads stay on the upload path the extension can receive and retry. Leave this setting enabled unless Braintrust support recommends changing it.This setting keeps payloads on the extension-supported upload path, but it does not guarantee that every oversized payload can be delivered.

Considerations

When using the extension, keep the following things in mind:
  • A successful flush() confirms that the extension accepted trace batches in the Lambda execution environment, not that Braintrust has durably ingested them.
  • The extension retries delivery while the execution environment is active or reused for a later warm invocation, but undelivered batches can be lost if AWS destroys the execution environment before delivery succeeds.
  • Extension work after the handler returns can still contribute to Lambda Duration and billing metrics.
  • Attaching the extension layer adds cold-start work.
  • The extension only supports trace logging from a project logger. It does not proxy prompt loading, datasets, attachments, experiment logging, or arbitrary Braintrust API calls.
  • Keep the default BRAINTRUST_LAMBDA_DISABLE_SDK_OVERFLOW=1 setting unless Braintrust support recommends changing it. The extension does not support the SDK’s separate upload path for oversized log payloads.

Resources