Overview
Klaus uses SQLite to persist conversation history, session metadata, and user knowledge profiles. The database is stored at~/.klaus/klaus.db.
Memory Class
TheMemory class provides persistent storage for sessions, exchanges, and knowledge profiles.
Constructor
Path to the SQLite database file. Defaults to
~/.klaus/klaus.db.Data Classes
Session
ExchangeRecord
Session Methods
create_session(title: str) -> Session
Create a new session with a title.
Human-readable session title
Session object with generated UUID.
Example:
list_sessions() -> list[Session]
List all sessions, ordered by updated_at (most recent first).
Returns: List of Session objects.
Example:
update_session_title(session_id: str, title: str) -> None
Update a session’s title and updated_at timestamp.
Session UUID
New title
delete_session(session_id: str) -> None
Delete a session and all its exchanges.
Session UUID
get_session_notes_file(session_id: str) -> str | None
Return the notes file path for a session, or None if not set.
Session UUID
None.
set_session_notes_file(session_id: str, notes_file: str | None) -> None
Persist the notes file path for a session.
Session UUID
Relative notes file path or
None to clearExchange Methods
save_exchange(session_id: str, user_text: str, assistant_text: str, image_base64: str | None = None, searches: list[dict] | None = None) -> ExchangeRecord
Save a user-assistant exchange to the database.
Session UUID
User’s question or input
Assistant’s response
Base64-encoded image data (if applicable)
List of search result dictionaries
ExchangeRecord with generated UUID.
Example:
get_exchanges(session_id: str) -> list[ExchangeRecord]
Get all exchanges for a session, ordered by creation time (oldest first).
Session UUID
ExchangeRecord objects.
count_exchanges(session_id: str | None = None) -> int
Count exchanges in a session, or all exchanges if session_id is None.
Session UUID, or
None to count all exchangesKnowledge Profile Methods
update_knowledge(topic: str, summary: str, comfort_level: str = "learning") -> None
Update or insert a knowledge profile entry for a topic.
Topic name (e.g., “neural scaling laws”)
Brief summary of user’s knowledge on this topic
User’s comfort level:
"beginner", "learning", or "expert"knowledge_profile table remains empty.
get_knowledge_summary(limit: int = 20) -> str
Return a text summary of the user’s knowledge profile for inclusion in prompts.
Maximum number of topics to include
get_recent_exchanges_summary(session_id: str, limit: int = 5) -> str
Return a brief summary of recent exchanges for context.
Session UUID
Maximum number of recent exchanges to include
Database Schema
sessions table
exchanges table
knowledge_profile table
Implementation Details
- Thread-safe: Uses
check_same_thread=Falsefor multi-threaded access. - Row factory: Uses
sqlite3.Rowfor dictionary-like row access. - Automatic migration: The
notes_filecolumn is added to existing databases via_migrate_sessions_notes_file(). - Image hashing: Images are hashed using SHA256 (truncated to 16 chars) to detect duplicate page views.
- Search tracking: Web search queries and result counts are stored as JSON in the
searches_jsoncolumn.
Source Reference
Seeklaus/memory.py for the full implementation.