Overview

Effective trace structuring enhances the value of your tracing data, making it easier to debug, analyze, and optimize your LLM applications. This section covers the essential components of trace structuring in Laminar:

ComponentDescription
ObserveCreate structured traces with the observe decorator/wrapper
SessionsGroup related traces together for better organization
User IDAssociate traces with specific users for targeted analysis
MetadataAdd contextual information to your traces for filtering and grouping

Why Structure Matters

Without structure, each LLM call creates an isolated trace, making it difficult to:

  • Understand the relationships between different LLM calls
  • Track user journeys through your application
  • Debug complex multi-step workflows
  • Find relevant traces quickly

Properly structured traces provide a clear picture of your application’s flow, making it easier to identify bottlenecks, debug issues, and optimize performance.

Getting Started

Start by implementing the observe wrapper or decorator to create parent spans that group your LLM calls into meaningful traces. Then, enhance these traces with sessions, user IDs, and metadata to create a comprehensive tracing structure.

// Example: A well-structured trace implementation
import { Laminar, observe } from '@lmnr-ai/lmnr';

// Initialize with your project key
Laminar.initialize({
  projectApiKey: process.env.LMNR_PROJECT_API_KEY,
});

// Set up session and metadata for all traces in this request
Laminar.withSession({ sessionId: `session-${requestId}` }, () => {
  Laminar.withMetadata({
    userId: user.id,
    environment: process.env.NODE_ENV
  }, async () => {
    // Create a parent span for the entire request
    await observe({ name: 'processUserRequest' }, async () => {
      // Your LLM calls and other operations will be grouped 
      // under this parent span and inherit the session and metadata
    });
  });
});

Explore each component in detail through the links above to implement a comprehensive tracing structure for your LLM applications.