Skip to main content

How sessions work

Every chat() call is tied to a user_id and an optional platform. Together they form a session key:
LangChat maintains one UserSession per key. Within a session:
  • The last N exchanges are kept in memory (default: 20, configured via max_chat_history)
  • Full history is loaded from Supabase when the session is first accessed
  • Each new exchange is saved to Supabase in the background (non-blocking)

user_id

Identifies the person chatting. Use any stable, unique identifier — a database UUID, an email address, or a session token.
Each user_id has its own isolated conversation history. User A never sees User B’s messages.

platform

Groups conversations by application or context. Use it when multiple apps or surfaces share the same LangChat backend.
If you don’t pass platform, it defaults to "default".

History window

max_chat_history controls how many past exchanges are included in each prompt. One exchange = one user message + one AI response.
Trade-offs:

Get a session object

Access the underlying UserSession directly if you need to inspect or manipulate it:

How history is loaded

When you first call chat() for a user:
  1. LangChat checks the in-memory session cache — if the session exists, it uses that
  2. If not, it creates a new UserSession which loads recent messages from Supabase
  3. The loaded history is stored in memory for subsequent calls
This means after a server restart, users can continue where they left off.

Database schema

Chat history is stored in the chat_history table: LangChat creates this table automatically on first run.

Multi-tenant isolation

When running LangChat as a shared API, conversations are naturally isolated because:
  • Each user_id + platform combination is a separate session
  • History queries always filter by both user_id and platform
  • In-memory session cache is keyed by "{user_id}_{platform}"
No extra configuration is needed for multi-tenant use.