Memory¶
Memory primitives for conversation history and long-term recall.
Memory¶
Memory
¶
Memory interface for agent execution.
Provides working memory (conversation history) and long-term memory (persistent facts, preferences, etc.).
Memory is always available on TaskContext. Sessions are created lazily on first write - no data exists until something is added.
Example
async def my_agent(ctx: TaskContext) -> dict: # Get conversation history messages = await ctx.memory.get_messages()
# Search relevant memories
memories = await ctx.memory.search("user preferences")
# Add response (creates session if needed)
await ctx.memory.add_message("assistant", response)
return {"response": response}
Source code in redis_agent_kit/memory.py
with_session
¶
with_session(session_id: str | None = None, user_id: str | None = None) -> Memory
Return a Memory instance bound to a session.
If session_id is None, generates a new one. Session is not created in Redis until first write.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str | None
|
Session ID to use (or None to generate) |
None
|
user_id
|
str | None
|
Optional user ID for memory isolation |
None
|
Returns:
| Type | Description |
|---|---|
Memory
|
New Memory instance bound to the session |
Source code in redis_agent_kit/memory.py
add_message
async
¶
Add a message to working memory.
Creates session on first call if it doesn't exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
role
|
str
|
Message role (user, assistant, system) |
required |
content
|
str
|
Message content |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Session ID, or None if memory disabled |
Source code in redis_agent_kit/memory.py
get_messages
async
¶
get_messages(limit: int | None = None) -> MessageList
Get messages from current session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limit
|
int | None
|
Max messages to return (None for all) |
None
|
Returns:
| Type | Description |
|---|---|
MessageList
|
MessageList with 'role' and 'content' dicts. Supports: |
MessageList
|
|
MessageList
|
|
MessageList
|
|
Source code in redis_agent_kit/memory.py
get_history
async
¶
get_history(limit: int | None = 10, format: str = '{role}: {content}', separator: str = '\n') -> str
Get conversation history formatted as text.
Convenience method that formats messages for LLM context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limit
|
int | None
|
Max messages to include (None for all) |
10
|
format
|
str
|
Format string for each message (supports {role}, {content}) |
'{role}: {content}'
|
separator
|
str
|
String to join messages |
'\n'
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted conversation history string |
Example
Default format¶
history = await ctx.memory.get_history(limit=10)
"user: Hello\nassistant: Hi there!"¶
Custom format¶
history = await ctx.memory.get_history( format="[{role}] {content}", separator="\n---\n" )
Source code in redis_agent_kit/memory.py
get_context
async
¶
Get context string from working memory.
Returns summarized context if available, otherwise None.
Source code in redis_agent_kit/memory.py
set_data
async
¶
Set arbitrary data in session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Data key |
required |
value
|
Any
|
Data value (must be JSON serializable) |
required |
Source code in redis_agent_kit/memory.py
get_data
async
¶
Get arbitrary data from session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Data key |
required |
default
|
Any
|
Default value if key not found |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Value or default |
Source code in redis_agent_kit/memory.py
search
async
¶
search(text: str, limit: int = 10, memory_type: str | None = None, topics: list[str] | None = None) -> MemorySearchResults
Search long-term memories by semantic similarity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Search query |
required |
limit
|
int
|
Max results |
10
|
memory_type
|
str | None
|
Filter by type (semantic, episodic, etc.) |
None
|
topics
|
list[str] | None
|
Filter by topics |
None
|
Returns:
| Type | Description |
|---|---|
MemorySearchResults
|
MemorySearchResults with 'id', 'text', 'memory_type', 'score', etc. Supports: |
MemorySearchResults
|
|
MemorySearchResults
|
|
MemorySearchResults
|
|
Source code in redis_agent_kit/memory.py
search_text
async
¶
search_text(text: str, limit: int = 10, separator: str = '\n', memory_type: str | None = None, topics: list[str] | None = None) -> str
Search long-term memories and return text content only.
Convenience method that returns just the text from search results, formatted for direct use in LLM context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Search query |
required |
limit
|
int
|
Max results |
10
|
separator
|
str
|
String to join memory texts |
'\n'
|
memory_type
|
str | None
|
Filter by type (semantic, episodic, etc.) |
None
|
topics
|
list[str] | None
|
Filter by topics |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Concatenated memory texts, or empty string if no results |
Example
Get relevant context for a query¶
memory_context = await ctx.memory.search_text(ctx.message, limit=3) prompt = f"Context:\n{memory_context}\n\nQuestion: {ctx.message}"
Source code in redis_agent_kit/memory.py
create_memory
async
¶
create_memory(text: str, memory_type: str = 'semantic', topics: list[str] | None = None, entities: list[str] | None = None) -> str | None
Create a long-term memory explicitly.
For passive memory creation, just use working memory - the system auto-promotes based on configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Memory content |
required |
memory_type
|
str
|
Type (semantic, episodic, etc.) |
'semantic'
|
topics
|
list[str] | None
|
Associated topics |
None
|
entities
|
list[str] | None
|
Associated entities |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
Memory ID, or None if disabled |
Source code in redis_agent_kit/memory.py
delete_memories
async
¶
Delete long-term memories by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
memory_ids
|
list[str]
|
List of memory IDs to delete |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of memories deleted |
Source code in redis_agent_kit/memory.py
delete_session
async
¶
Delete current working memory session.
Returns:
| Type | Description |
|---|---|
bool
|
True if deleted, False if not found or disabled |
Source code in redis_agent_kit/memory.py
MessageList¶
MessageList
¶
Bases: list[dict[str, str]]
A list of messages with formatting methods.
Supports .dict(), .json(), and .markdown() for different output formats.
json
¶
markdown
¶
Return messages formatted as markdown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
format
|
str
|
Format string for each message (supports {role}, {content}) |
'**{role}**: {content}'
|
separator
|
str
|
String to join messages |
'\n\n'
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted markdown string |
Source code in redis_agent_kit/memory.py
MemorySearchResults¶
MemorySearchResults
¶
Bases: list[dict[str, Any]]
A list of memory search results with formatting methods.
Supports .dict(), .json(), and .markdown() for different output formats.
json
¶
markdown
¶
Return results formatted as markdown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
format
|
str
|
Format string for each result (supports {text}, {memory_type}, etc.) |
'- {text}'
|
separator
|
str
|
String to join results |
'\n'
|
include_metadata
|
bool
|
Include memory_type and topics in output |
False
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted markdown string |
Source code in redis_agent_kit/memory.py
NoOpMemory¶
NoOpMemory
¶
Bases: Memory
Memory implementation that does nothing (for disabled memory).
Source code in redis_agent_kit/memory.py
with_session
¶
with_session(session_id: str | None = None, user_id: str | None = None) -> Memory
Return a Memory instance bound to a session.
If session_id is None, generates a new one. Session is not created in Redis until first write.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str | None
|
Session ID to use (or None to generate) |
None
|
user_id
|
str | None
|
Optional user ID for memory isolation |
None
|
Returns:
| Type | Description |
|---|---|
Memory
|
New Memory instance bound to the session |
Source code in redis_agent_kit/memory.py
get_history
async
¶
get_history(limit: int | None = 10, format: str = '{role}: {content}', separator: str = '\n') -> str
Get conversation history formatted as text.
Convenience method that formats messages for LLM context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limit
|
int | None
|
Max messages to include (None for all) |
10
|
format
|
str
|
Format string for each message (supports {role}, {content}) |
'{role}: {content}'
|
separator
|
str
|
String to join messages |
'\n'
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted conversation history string |
Example
Default format¶
history = await ctx.memory.get_history(limit=10)
"user: Hello\nassistant: Hi there!"¶
Custom format¶
history = await ctx.memory.get_history( format="[{role}] {content}", separator="\n---\n" )
Source code in redis_agent_kit/memory.py
search_text
async
¶
search_text(text: str, limit: int = 10, separator: str = '\n', memory_type: str | None = None, topics: list[str] | None = None) -> str
Search long-term memories and return text content only.
Convenience method that returns just the text from search results, formatted for direct use in LLM context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Search query |
required |
limit
|
int
|
Max results |
10
|
separator
|
str
|
String to join memory texts |
'\n'
|
memory_type
|
str | None
|
Filter by type (semantic, episodic, etc.) |
None
|
topics
|
list[str] | None
|
Filter by topics |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Concatenated memory texts, or empty string if no results |
Example
Get relevant context for a query¶
memory_context = await ctx.memory.search_text(ctx.message, limit=3) prompt = f"Context:\n{memory_context}\n\nQuestion: {ctx.message}"