Skip to main content
Sessions are critical for multi-turn commerce conversations. They allow the LLM to remember what’s in the user’s cart and maintain context across different platforms.

Overview

Every interaction with the LLM Gateway is associated with a sessionId. This ID is used to retrieve:
  • Cart ID: The current shopping cart for the user.
  • Context: Recent interactions, preferences, or user IDs.
  • Merchant ID: In multi-vendor environments, the current merchant being interacted with.

Session Storage

By default, the gateway uses an in-memory session manager, which is perfect for development but not suitable for production. For production, use a Redis-based session manager to ensure high availability and shared state across multiple gateway instances.
const gateway = new LLMGateway({
  session: {
    redis: {
      url: process.env.REDIS_URL,
      token: process.env.REDIS_TOKEN, // Optional for serverless Redis like Upstash
    },
    ttl: 3600 * 24, // 24 hour expiration
  }
});

Creating & Retrieving Sessions

When using the HTTP Adapter, you typically pass the sessionId in the header or as part of the tool execution request.
curl -X POST https://api.betterdata.co/v1/execute \
  -H "X-Session-Id: session_123" \
  ...
The gateway will automatically look up the session and provide the associated cart to your backend handlers.

Cross-Platform Transfer

One of the unique features of the LLM Gateway is the ability to transfer sessions across different AI assistants.
  1. User starts a conversation in Claude and adds items to a cart.
  2. The user wants to “Checkout on Mobile”.
  3. The gateway generates a secure transfer link.
  4. The user opens the link on their phone, and the session (including the cart) is restored.

Best Practices

  • Use UUIDs: Always use long, unpredictable strings for session IDs.
  • Set TTLs: Configure appropriate time-to-live values based on your user behavior (e.g., 24 hours for carts).
  • Secure Handling: Treat session IDs like sensitive data, as they provide access to a user’s cart.