Skip to content

Commit 2cca4af

Browse files
committed
Restructure memory documentation and add missing eager creation tool
- Restructure working-memory.md to focus on messages and data first, then structured memories - Combine data examples and improve conversation context examples - Add comprehensive section on producing long-term memories from working memory - Cover both server-side and client-side extraction approaches - Update long-term-memory.md to refer to working memory for automatic promotion - Add manual creation examples with both API and LLM tool usage - Add missing create_long_term_memory tool schema to Python client - Update Python SDK docs with correct tool names (not the non-existent ones) - Add tool call handler and resolver for eager memory creation - All client tests passing
1 parent 215bbe8 commit 2cca4af

File tree

4 files changed

+260
-96
lines changed

4 files changed

+260
-96
lines changed

agent-memory-client/agent_memory_client/client.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,67 @@ def edit_long_term_memory_tool_schema(cls) -> dict[str, Any]:
16121612
},
16131613
}
16141614

1615+
@classmethod
1616+
def create_long_term_memory_tool_schema(cls) -> dict[str, Any]:
1617+
"""
1618+
Get OpenAI-compatible tool schema for creating long-term memories directly.
1619+
1620+
Returns:
1621+
Tool schema dictionary compatible with OpenAI tool calling format
1622+
"""
1623+
return {
1624+
"type": "function",
1625+
"function": {
1626+
"name": "create_long_term_memory",
1627+
"description": (
1628+
"Create long-term memories directly for immediate storage and retrieval. "
1629+
"Use this for important information that should be permanently stored without going through working memory. "
1630+
"This is the 'eager' approach - memories are created immediately in long-term storage. "
1631+
"Examples: User preferences, important facts, key events that need to be searchable right away. "
1632+
"For episodic memories, include event_date in ISO format."
1633+
),
1634+
"parameters": {
1635+
"type": "object",
1636+
"properties": {
1637+
"memories": {
1638+
"type": "array",
1639+
"items": {
1640+
"type": "object",
1641+
"properties": {
1642+
"text": {
1643+
"type": "string",
1644+
"description": "The memory content to store",
1645+
},
1646+
"memory_type": {
1647+
"type": "string",
1648+
"enum": ["episodic", "semantic", "message"],
1649+
"description": "Type of memory: 'episodic' (events/experiences), 'semantic' (facts/preferences), 'message' (conversation snippets)",
1650+
},
1651+
"topics": {
1652+
"type": "array",
1653+
"items": {"type": "string"},
1654+
"description": "Optional topics for categorization",
1655+
},
1656+
"entities": {
1657+
"type": "array",
1658+
"items": {"type": "string"},
1659+
"description": "Optional entities mentioned in the memory",
1660+
},
1661+
"event_date": {
1662+
"type": "string",
1663+
"description": "Optional event date for episodic memories (ISO 8601 format: '2024-01-15T14:30:00Z')",
1664+
},
1665+
},
1666+
"required": ["text", "memory_type"],
1667+
},
1668+
"description": "List of memories to create",
1669+
},
1670+
},
1671+
"required": ["memories"],
1672+
},
1673+
},
1674+
}
1675+
16151676
@classmethod
16161677
def delete_long_term_memories_tool_schema(cls) -> dict[str, Any]:
16171678
"""
@@ -1666,6 +1727,7 @@ def get_all_memory_tool_schemas(cls) -> Sequence[dict[str, Any]]:
16661727
cls.get_add_memory_tool_schema(),
16671728
cls.get_update_memory_data_tool_schema(),
16681729
cls.get_long_term_memory_tool_schema(),
1730+
cls.create_long_term_memory_tool_schema(),
16691731
cls.edit_long_term_memory_tool_schema(),
16701732
cls.delete_long_term_memories_tool_schema(),
16711733
cls.get_current_datetime_tool_schema(),
@@ -1698,6 +1760,7 @@ def get_all_memory_tool_schemas_anthropic(cls) -> Sequence[dict[str, Any]]:
16981760
cls.get_add_memory_tool_schema_anthropic(),
16991761
cls.get_update_memory_data_tool_schema_anthropic(),
17001762
cls.get_long_term_memory_tool_schema_anthropic(),
1763+
cls.create_long_term_memory_tool_schema_anthropic(),
17011764
cls.edit_long_term_memory_tool_schema_anthropic(),
17021765
cls.delete_long_term_memories_tool_schema_anthropic(),
17031766
cls.get_current_datetime_tool_schema_anthropic(),
@@ -1756,6 +1819,12 @@ def get_long_term_memory_tool_schema_anthropic(cls) -> dict[str, Any]:
17561819
openai_schema = cls.get_long_term_memory_tool_schema()
17571820
return cls._convert_openai_to_anthropic_schema(openai_schema)
17581821

1822+
@classmethod
1823+
def create_long_term_memory_tool_schema_anthropic(cls) -> dict[str, Any]:
1824+
"""Get create long-term memory tool schema in Anthropic format."""
1825+
openai_schema = cls.create_long_term_memory_tool_schema()
1826+
return cls._convert_openai_to_anthropic_schema(openai_schema)
1827+
17591828
@classmethod
17601829
def edit_long_term_memory_tool_schema_anthropic(cls) -> dict[str, Any]:
17611830
"""Get edit long-term memory tool schema in Anthropic format."""
@@ -2135,6 +2204,11 @@ async def resolve_function_call(
21352204
elif function_name == "get_long_term_memory":
21362205
result = await self._resolve_get_long_term_memory(args)
21372206

2207+
elif function_name == "create_long_term_memory":
2208+
result = await self._resolve_create_long_term_memory(
2209+
args, effective_namespace, user_id
2210+
)
2211+
21382212
elif function_name == "edit_long_term_memory":
21392213
result = await self._resolve_edit_long_term_memory(args)
21402214

@@ -2279,6 +2353,40 @@ async def _resolve_get_long_term_memory(
22792353
result = await self.get_long_term_memory(memory_id=memory_id)
22802354
return {"memory": result}
22812355

2356+
async def _resolve_create_long_term_memory(
2357+
self, args: dict[str, Any], namespace: str | None, user_id: str | None = None
2358+
) -> dict[str, Any]:
2359+
"""Resolve create_long_term_memory function call."""
2360+
memories_data = args.get("memories")
2361+
if not memories_data:
2362+
raise ValueError(
2363+
"memories parameter is required for create_long_term_memory"
2364+
)
2365+
2366+
# Convert dict memories to ClientMemoryRecord objects
2367+
from .models import ClientMemoryRecord, MemoryTypeEnum
2368+
2369+
memories = []
2370+
for memory_data in memories_data:
2371+
# Apply defaults
2372+
if namespace and "namespace" not in memory_data:
2373+
memory_data["namespace"] = namespace
2374+
if user_id and "user_id" not in memory_data:
2375+
memory_data["user_id"] = user_id
2376+
2377+
# Convert memory_type string to enum if needed
2378+
if "memory_type" in memory_data:
2379+
memory_data["memory_type"] = MemoryTypeEnum(memory_data["memory_type"])
2380+
2381+
memory = ClientMemoryRecord(**memory_data)
2382+
memories.append(memory)
2383+
2384+
result = await self.create_long_term_memory(memories)
2385+
return {
2386+
"status": result.status,
2387+
"message": f"Created {len(memories)} memories successfully",
2388+
}
2389+
22822390
async def _resolve_edit_long_term_memory(
22832391
self, args: dict[str, Any]
22842392
) -> dict[str, Any]:

docs/long-term-memory.md

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -198,21 +198,70 @@ response = await memory_prompt({
198198
# - User's query as final message
199199
```
200200

201-
## Memory Extraction
201+
## Creating Long-Term Memories
202202

203-
By default, the system automatically extracts structured memories from
204-
conversations so they flow from working memory to long-term storage. This
205-
happens in a background process after clients update working memory. This
206-
extraction process can be customized using different **memory strategies**.
203+
There are two main ways to create long-term memories:
207204

208-
The extraction strategy is set in the working memory session and controls what
209-
the server extracts into long-term memory. When you give an LLM the ability to
210-
store long-term memories as a tool, the tool description includes information
211-
about the configured extraction strategy, helping the LLM understand what types
212-
of memories to create.
205+
### 1. Automatic Promotion from Working Memory
213206

214-
!!! info "Memory Strategies"
215-
The system supports multiple extraction strategies (discrete facts, summaries, preferences, custom prompts) that determine how conversations are processed into memories. The extraction strategy set in working memory is visible to LLMs through strategy-aware tool descriptions. See [Memory Extraction Strategies](memory-extraction-strategies.md) for complete documentation and examples.
207+
The most common approach is to let the system automatically promote memories from working memory to long-term storage. This handles extraction strategies, background processing, and batch optimization.
208+
209+
!!! info "Working Memory Integration"
210+
For automatic memory promotion from conversations, see the [Working Memory documentation](working-memory.md). This covers extraction strategies, background processing, and how to configure the memory server to automatically create long-term memories from conversation content.
211+
212+
### 2. Manual Creation via API
213+
214+
For immediate storage of important facts, you can create long-term memories directly using the API or LLM tools.
215+
216+
#### Direct API Calls
217+
218+
```python
219+
# Create memories directly via Python client
220+
await client.create_long_term_memories([
221+
{
222+
"text": "User prefers dark mode interfaces",
223+
"memory_type": "semantic",
224+
"topics": ["preferences", "ui"],
225+
"entities": ["dark mode"],
226+
"user_id": "user_123"
227+
},
228+
{
229+
"text": "User completed Python certification on January 15, 2024",
230+
"memory_type": "episodic",
231+
"event_date": "2024-01-15T10:00:00Z",
232+
"topics": ["education", "certification"],
233+
"entities": ["Python certification"],
234+
"user_id": "user_123"
235+
}
236+
])
237+
```
238+
239+
#### LLM Tool Usage (Eager Creation)
240+
241+
Your LLM can use the `create_long_term_memory` tool for immediate storage:
242+
243+
```python
244+
# LLM tool call for eager memory creation
245+
tools = [client.create_long_term_memory_tool_schema()]
246+
247+
# LLM can call:
248+
# create_long_term_memory(
249+
# memories=[
250+
# {
251+
# "text": "User works as a software engineer at TechCorp",
252+
# "memory_type": "semantic",
253+
# "topics": ["career", "work"],
254+
# "entities": ["software engineer", "TechCorp"]
255+
# }
256+
# ]
257+
# )
258+
```
259+
260+
This approach is ideal when:
261+
- You need memories to be immediately searchable
262+
- You're processing batch data or imports
263+
- You want to bypass working memory entirely
264+
- You have structured data that doesn't need extraction
216265

217266
## Configuration
218267

docs/python-sdk.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,14 @@ async def chat_with_memory(message: str, session_id: str):
214214

215215
The SDK provides these tools for LLM integration:
216216

217-
1. **`eagerly_create_long_term_memory`** - Eagerly create a long-term memory by making an API request
218-
2. **`lazily_create_long_term_memory`** - Lazily create a long-term memory by adding it to working memory (does not require an immediate network request; does require saving working memory afterward)
219-
3. **`search_long_term_memory`** - Search with semantic similarity
220-
4. **`edit_memory`** - Update existing memories
221-
5. **`delete_memory`** - Remove memories
222-
6. **`set_working_memory`** - Update or create a working memory session
223-
7. **`get_or_create_working_memory`** - Retrieve or create a working memory session
217+
1. **`create_long_term_memory`** - Eagerly create long-term memories by making an API request
218+
2. **`add_memory_to_working_memory`** - Lazily create memories by adding them to working memory (promoted to long-term storage later)
219+
3. **`search_memory`** - Search with semantic similarity across long-term memories
220+
4. **`edit_long_term_memory`** - Update existing long-term memories
221+
5. **`delete_long_term_memories`** - Remove long-term memories
222+
6. **`get_or_create_working_memory`** - Retrieve or create a working memory session
223+
7. **`update_working_memory_data`** - Update session-specific data in working memory
224+
8. **`get_current_datetime`** - Get current UTC datetime for grounding relative time expressions
224225

225226
**Note:** The following tool names have been deprecated for clarity:
226227
- `create_long_term_memories` (deprecated) → use `eagerly_create_long_term_memory`

0 commit comments

Comments
 (0)