Overview

The init function initializes the Lucidic SDK with your credentials, configures providers, and optionally starts a new session. This must be called before any other SDK operations.

Syntax

await lai.init(params?: InitParams): Promise<string>

Parameters

params
InitParams
Initialization object with the following properties:

Returns

Returns a Promise that resolves to the session ID (string).

Examples

Basic Initialization

import * as lai from 'lucidicai';

// Initialize with environment variables
await lai.init();

With Session Creation

import OpenAI from 'openai';
await lai.init({
  sessionName: "Customer Support Bot",
  instrumentModules: { OpenAI },
  task: "Answer customer questions"
});
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

With Custom Configuration

await lai.init({
  apiKey: "your-api-key",
  agentId: "your-agent-id",
  sessionName: "Production Agent",
  providers: ["openai", "anthropic"],
  maskingFunction: (text) => text.replace(/\b\d{3}-\d{2}-\d{4}\b/g, 'XXX-XX-XXXX'),
  debug: true
});

Continue Existing Session

import { Anthropic } from '@anthropic-ai/sdk';
await lai.init({
  sessionId: "existing-session-uuid",
  instrumentModules: { anthropic: Anthropic }
});

Important Notes

Provider Import Order

Preferred pattern: import providers and pass them via instrumentModules:
// ✅ Preferred (ESM)
import OpenAI from 'openai';
await lai.init({ instrumentModules: { OpenAI } });

// ❌ WRONG - do not rely on providers array for ESM imports
await lai.init({ providers: ["openai"] }); // discouraged
import OpenAI from 'openai'; // importing after init may miss instrumentation

Auto-End Behavior

When autoEnd is true (default), the SDK:
  • Registers exit handlers for graceful shutdown
  • Automatically ends active sessions on process exit
  • Cleans up telemetry instrumentation

Environment Variables

The SDK checks these environment variables if not provided in config:
  • LUCIDIC_API_KEY - API key for authentication
  • LUCIDIC_AGENT_ID - Agent identifier

Error Handling

try {
  await lai.init({
    sessionName: "My Session",
    providers: ["openai"]
  });
} catch (error) {
  if (error.message.includes('API key')) {
    console.error('Invalid API credentials');
  } else {
    console.error('Initialization failed:', error);
  }
}

See Also