Integrates withClaudeGPT-4GeminiLlama

Integration Guide

Get started with CLS++ Memory in minutes. Follow this guide to integrate cross-LLM memory into your application.

Quick Start

Step 1

Install the SDK

Add the CLS++ Memory SDK to your project using your preferred package manager.

npm install @cls-memory/sdk
Step 2

Initialize the Client

Create a CLS++ Memory client instance with your API key.

import { CLSMemory } from '@cls-memory/sdk';

const memory = new CLSMemory({
  apiKey: process.env.CLS_API_KEY,
  // Optional: specify default provider
  defaultProvider: 'claude',
});
Step 3

Store & Retrieve Context

Use the client to store context after LLM calls and retrieve it when switching providers.

// After a conversation with Claude
await memory.store({
  sessionId: userId,
  provider: 'claude',
  messages: claudeMessages,
});

// When switching to GPT
const context = await memory.retrieve({
  sessionId: userId,
  targetProvider: 'gpt-4',
});

// Use with OpenAI
const response = await openai.chat.completions.create({
  model: 'gpt-4',
  messages: context.messages,
});

Framework Guides

Next.js API Route Example

Complete example of integrating CLS++ Memory with Claude in a Next.js API route.

// app/api/chat/route.ts
import { CLSMemory } from '@cls-memory/sdk';
import Anthropic from '@anthropic-ai/sdk';

const memory = new CLSMemory({ apiKey: process.env.CLS_API_KEY! });
const anthropic = new Anthropic();

export async function POST(request: Request) {
  const { sessionId, message, provider } = await request.json();
  
  // Retrieve existing context
  const context = await memory.retrieve({
    sessionId,
    targetProvider: provider,
  });
  
  // Build messages array
  const messages = [
    ...context.messages,
    { role: 'user', content: message }
  ];
  
  // Call Claude
  const response = await anthropic.messages.create({
    model: 'claude-3-opus-20240229',
    messages,
    system: context.systemPrompt,
  });
  
  // Store updated context
  await memory.store({
    sessionId,
    provider: 'claude',
    messages: [...messages, { 
      role: 'assistant', 
      content: response.content[0].text 
    }],
  });
  
  return Response.json({ 
    message: response.content[0].text 
  });
}

Best Practices

  • Use consistent session IDs

    Use a unique, consistent identifier for each user or conversation thread (e.g., user ID + conversation ID).

  • Store after each exchange

    Call memory.store() after each LLM response to ensure context is always up-to-date.

  • Handle errors gracefully

    Always wrap memory operations in try-catch. If retrieval fails, continue with an empty context rather than failing the request.

  • Use metadata for rich context

    Store user preferences, conversation summaries, and key decisions in metadata for better context retrieval.