Skip to main content

Overview

We’ll explore a simple emotional support chat application built with Next.js that:
  1. Provides a chat interface for users seeking emotional support
  2. Uses OpenAI’s gpt-5.6-luna or Anthropic’s claude-haiku-4-5-20251001 to generate empathetic responses
  3. Traces the entire process with Laminar

Setup

You can use the example app from our GitHub repo. Alternatively, for a clean install, follow these steps:
1

Initialize a Next.js app

Learn more in the Next.js docs.
2

Install Dependencies

Let’s now install the required packages:
3

Environment Setup

And then fill in the .env.local file:
.env.local
To get the project API key, go to the Laminar dashboard, click the project settings, and generate a project API key. This is available both in the cloud and in the self-hosted version of Laminar.Specify the key at Laminar initialization. If not specified, Laminar will look for the key in the LMNR_PROJECT_API_KEY environment variable.Get your OpenAI API key and your Anthropic API key from the respective dashboards.
4

Update next.config.ts

Add the following to your next.config.ts file:
next.config.ts
This is because Laminar depends on OpenTelemetry, which uses some Node.js-specific functionality, and we need to inform Next.js about it. Learn more in the Next.js docs.
5

Project Structure

The project has the following structure:

Implementation

Let’s look at the key components of our Next.js application with Laminar tracing:

1. instrumentation.ts

This file is crucial as it initializes Laminar for tracing. Next.js automatically loads this file during initialization.
instrumentation.ts
Laminar only works in the ‘nodejs’ runtime of Next.js. Learn more about the instrumentation.ts file in the Next.js docs.
Laminar must be initialized at the entry point of the application, but after other tracing libraries are initialized. For Next.js, the instrumentation.ts file is ideal for this purpose as it’s loaded early in the application lifecycle.

Passing the modules to instrument explicitly

By default Laminar patches the LLM SDKs it finds at initialization time. Next.js bundles route code, so that automatic lookup can miss a provider you actually use. Pass the module to instrumentModules and Laminar patches that exact module object:
instrumentation.ts
Note the two different shapes: OpenAI is the exported class, while anthropic is the whole module namespace. Other supported keys are bedrock, cohere, langchain, chromadb, qdrant, and pinecone.
instrumentModules only takes effect if the provider packages are listed in serverExternalPackages too. Otherwise Next.js bundles a separate copy of the provider into your route, Laminar patches the unbundled one, and no spans are recorded:
next.config.ts
This is an alternative to the Laminar.patch() calls in step 4 below. Use one or the other, not both: instrumentModules keeps all tracing setup in instrumentation.ts, while Laminar.patch() keeps it next to the client you construct.

2. app/page.tsx

This file contains the main page layout for our chat application:
app/page.tsx

3. app/api/chat/route.ts

This file contains the API route handler that processes chat messages and communicates with the OpenAI API. There is no Laminar-specific code here. The provider clients are already patched, so the calls below are traced as-is.
app/api/chat/route.ts

4. lib/openai.ts and lib/anthropic.ts

These files contain the LLM clients, and patch them with Laminar at the point where they’re constructed. Skip this step if you already passed the modules through instrumentModules above.
lib/openai.ts
lib/anthropic.ts

5. components/chat-ui.tsx

This component handles the chat interface and manages the chat state. Feel free to modify the UI as you see fit, this is just an example.
components/chat-ui.tsx

Running the Application

Start the Next.js development server:

Testing the Application

  1. Navigate to http://localhost:3000 in your browser
  2. Interact with the chat interface by typing messages

Viewing Traces

After interacting with the chat, you can view the traces in your Laminar dashboard at https://laminar.sh. The trace will show:
  1. The Next.js API route execution
  2. The OpenAI or Anthropic API calls
  3. Token usage and response details
Laminar renders this as a transcript, so you read the conversation rather than a tree of span names.

Key Features Demonstrated

  1. Next.js Instrumentation: Using Next.js’s instrumentation API to initialize Laminar
  2. OpenAI or Anthropic Tracing: Automatic tracing of OpenAI or Anthropic API calls
  3. Token Usage: Automatic calculation of tokens used for each OpenAI or Anthropic call
  4. Cost Estimation: Automatic estimation of the cost of each OpenAI or Anthropic call

Example Traces

Next.js Example Traces

A screenshot of a trace from the example app.

Troubleshooting

If you encounter issues:
  • Check that your API keys are correctly set in the .env.local file
  • Verify that Laminar is properly initialized in the instrumentation.ts file
  • Verify that the next.config.ts file is correctly configured
  • Verify that Laminar.patch is called after LLM SDKs are imported
  • If you use instrumentModules, check that every provider package is also in serverExternalPackages
  • Ensure all dependencies are installed
  • Review the Next.js logs for any application errors