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 or Anthropic models 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. Get Laminar project API key. Get OpenAI API key. Get Anthropic API key.
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.

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. Make sure to enable experimental_telemetry in the generateText function and pass the tracer to it.
app/api/chat/route.ts

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

These files contain the LLM clients, but more importantly patching those functions by Laminar.
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://www.lmnr.ai. The trace will show:
  1. The Next.js API route execution
  2. The OpenAI or Anthropic API calls
  3. Token usage and response details

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

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
  • Ensure all dependencies are installed
  • Review the Next.js logs for any application errors