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(config?: LucidicConfig): Promise<string>

Parameters

config
LucidicConfig
Configuration object with the following properties:

Returns

Returns a Promise that resolves to the session ID (string) if a session was created, or an empty string if only SDK was initialized.

Examples

Basic Initialization

import * as lai from 'lucidicai';

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

With Session Creation

await lai.init({
  sessionName: "Customer Support Bot",
  providers: ["openai"],
  task: "Answer customer questions"
});

// Now import OpenAI AFTER init
const OpenAI = (await import('openai')).default;
const openai = new OpenAI();

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

await lai.init({
  sessionId: "existing-session-uuid",
  providers: ["anthropic"]
});

Important Notes

Provider Import Order

Providers MUST be imported AFTER init() for instrumentation to work:
// ✅ CORRECT
await lai.init({ providers: ["openai"] });
const OpenAI = (await import('openai')).default;

// ❌ WRONG - OpenAI won't be instrumented
import OpenAI from 'openai';
await lai.init({ providers: ["openai"] });

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