How It Works
EveryAgent instance maintains a dictionary of sessions keyed by session_id. Each session holds the full message list exchanged since the session was created or last cleared.
Session is resolved
When
agent.chat() is called with a session_id, the agent looks up the existing session or creates a new one. If no session_id is provided, a default session is used.User message is appended
The incoming user message is added to
session.messages as a {"role": "user", "content": "..."} dict.LLM receives accumulated history
The full
session.messages list — including the system prompt, all prior turns, and any tool results — is sent to the LLM provider.Session APIs
The agent exposes several methods for working with sessions directly.Starting a conversation
Inspecting session history
Clearing session history
clear_session() resets the message list for a session while retaining the session entry itself.
clear_session() only removes the in-memory message history. If long-term memory (memory=True) is also enabled, facts already persisted to LanceDB are not deleted. Use agent.simplemem.clear_memories() to wipe vector storage.Context Window Management
As conversations grow, the accumulated message history can approach the LLM’s context limit. Logicore handles this withContextMiddleware, which is wired into the agent’s chat loop.
How ContextMiddleware works
ContextMiddleware estimates the token count of the current message list using a character-length heuristic (1 token ≈ 4 characters). When the count exceeds the configured threshold, it compresses the oldest messages into a summary.
"role": "system" message so the LLM treats it as authoritative background context.
Configuring the threshold
Thecontext_compression parameter on the Agent constructor controls when compression activates. Pass an integer token count:
ContextMiddleware always preserves the 10 most recent messages verbatim (preserve_recent_count = 10) to avoid disrupting the immediate conversational flow.
Multi-Session Management with SessionManager
For applications that need to persist sessions across process restarts (CLI tools, long-running services), Logicore provides SessionManager backed by SQLite.
SessionManager Methods
save_session(session_id, messages, metadata)
save_session(session_id, messages, metadata)
Persists the message list and optional metadata to the SQLite
agent_state table. Creates the session record if it does not exist, then updates the last_activity timestamp.load_session(session_id)
load_session(session_id)
Returns the saved message list for a session, or an empty list if the session does not exist.
list_sessions()
list_sessions()
Returns all sessions that have at least one user message, ordered by
last_activity descending. Each entry includes session_id, title, provider, model, message_count, and timestamps.delete_session(session_id)
delete_session(session_id)
Clears the saved message list for the session by setting it to an empty list.
session_exists(session_id)
session_exists(session_id)
Returns
True if the session has a saved state entry, False otherwise.update_session_title(session_id, title)
update_session_title(session_id, title)
Updates the human-readable title stored in session metadata.
Session Isolation
Differentsession_id values are always fully isolated from each other:
- They do not share message history.
- Clearing one session has no effect on others.
- The same agent instance can hold many concurrent sessions simultaneously.