Associating Traces with Users

Tracking which user triggered a particular trace is crucial for:

  • User-specific debugging
  • Analyzing usage patterns
  • Filtering traces in the dashboard
  • Compliance with data regulations

In Laminar, you can associate traces with users by setting a user ID in the trace metadata.

Setting User ID

The recommended approach is to add the user ID as metadata to your traces. This allows you to filter and search for traces by user ID later.

import { Laminar } from '@lmnr-ai/lmnr';

// Option 1: Using withMetadata
Laminar.withMetadata({
    userId: 'user_123'
}, () => {
    // All traces created inside this function will have userId: user_123
});

// Option 2: With the observe wrapper
import { observe } from '@lmnr-ai/lmnr';

await observe({ 
    name: 'processUserRequest',
    userId: 'user_123'
}, async () => {
    // Process user request here
});

Privacy Considerations

When including user IDs in traces, consider the following privacy practices:

  • Use anonymous or pseudonymous IDs rather than personally identifiable information
  • Consider your data retention policies
  • Use Laminar’s tracing level settings to control sensitive data
  • Follow relevant data protection regulations
// Use TracingLevel.META_ONLY for sensitive user operations
import { withTracingLevel, TracingLevel } from "@lmnr-ai/lmnr";

withTracing(TracingLevel.META_ONLY, () => {
    // Sensitive operations here will only record metadata
    // Input/output content won't be saved
});

Combining User ID with Sessions

For a complete picture of user interactions, combine user IDs with session tracking:

// Track both user and session
Laminar.withMetadata({
    userId: 'user_123'
}, () => {
    Laminar.withSession({ sessionId: 'session_456' }, () => {
        // All operations here are tracked with both user ID and session ID
    });
});