Ragex.Agent.Memory
(Ragex v0.11.0)
View Source
ETS-based conversation memory for agent sessions.
Manages multi-turn conversations with:
- Session lifecycle management
- Message history with role tracking
- Context window management (truncation)
- Tool result storage
- Automatic session expiration
Usage
# Create a new session
{:ok, session} = Memory.new_session(%{project_path: "/path/to/project"})
# Add messages
:ok = Memory.add_message(session.id, :user, "Analyze this project")
:ok = Memory.add_message(session.id, :assistant, "I'll analyze...")
# Add tool result
:ok = Memory.add_tool_result(session.id, "call_123", %{status: "success"})
# Get conversation context for LLM
{:ok, messages} = Memory.get_context(session.id, max_tokens: 4000)
# End session
:ok = Memory.clear_session(session.id)
Summary
Functions
Add a message to a session.
Add a tool result to the session.
Returns a specification to start this module under a supervisor.
Clear/delete a session.
Get conversation context suitable for LLM consumption.
Get all messages from a session.
Get a session by ID.
List all active sessions.
Create a new conversation session.
Check if a session exists and is active.
Get session statistics.
Update session metadata.
Functions
Add a message to a session.
Parameters
session_id- Session IDrole- Message role: :system, :user, :assistant, or :toolcontent- Message contentopts- Optional::name- Function name for tool messages:tool_call_id- Tool call ID for tool response messages:tool_calls- Tool calls made by assistant
Returns
:ok- Message added{:error, :not_found}- Session not found
Add a tool result to the session.
Tool results are stored separately and can be referenced by tool_call_id.
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec clear_session(String.t()) :: :ok
Clear/delete a session.
Get conversation context suitable for LLM consumption.
Applies truncation to fit within context window limits.
Options
:max_chars- Maximum total characters (default: 32,000):include_system- Include system messages (default: true):format- Output format: :openai (default) or :anthropic
Get all messages from a session.
Options
:limit- Maximum number of messages to return (most recent)
@spec get_session(String.t()) :: {:ok, Ragex.Agent.Memory.Session.t()} | {:error, :not_found}
Get a session by ID.
Returns
{:ok, session}- Session found{:error, :not_found}- Session doesn't exist or expired
@spec list_sessions(keyword()) :: [Ragex.Agent.Memory.Session.t()]
List all active sessions.
Options
:limit- Maximum number of sessions to return
@spec new_session(map()) :: {:ok, Ragex.Agent.Memory.Session.t()}
Create a new conversation session.
Parameters
metadata- Optional metadata map (e.g., project_path, issues)
Returns
{:ok, session}- New session struct
Check if a session exists and is active.
@spec stats() :: map()
Get session statistics.
Update session metadata.