Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions src/anthropic/lib/tools/_beta_builtin_memory_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,34 +157,43 @@ def clear_all_memory(self) -> BetaFunctionToolResultType:


class BetaAsyncAbstractMemoryTool(BetaAsyncBuiltinFunctionTool):
"""Abstract base class for memory tool implementations.
"""Abstract base class for async memory tool implementations.

This class provides the interface for implementing a custom memory backend for Claude.
This class provides the interface for implementing a custom async memory backend for Claude.

Subclass this to create your own memory storage solution (e.g., database, cloud storage, encrypted files, etc.).

Example usage:

```py
class MyMemoryTool(BetaAbstractMemoryTool):
def view(self, command: BetaMemoryTool20250818ViewCommand) -> BetaFunctionToolResultType:
import asyncio

from anthropic import AsyncAnthropic


class MyMemoryTool(BetaAsyncAbstractMemoryTool):
async def view(self, command: BetaMemoryTool20250818ViewCommand) -> BetaFunctionToolResultType:
...
return "view result"

def create(self, command: BetaMemoryTool20250818CreateCommand) -> BetaFunctionToolResultType:
async def create(self, command: BetaMemoryTool20250818CreateCommand) -> BetaFunctionToolResultType:
...
return "created successfully"

# ... implement other abstract methods


client = Anthropic()
memory_tool = MyMemoryTool()
message = client.beta.messages.run_tools(
model="claude-sonnet-4-5",
messages=[{"role": "user", "content": "Remember that I like coffee"}],
tools=[memory_tool],
).until_done()
async def main() -> None:
client = AsyncAnthropic()
memory_tool = MyMemoryTool()
message = await client.beta.messages.run_tools(
model="claude-sonnet-4-5",
messages=[{"role": "user", "content": "Remember that I like coffee"}],
tools=[memory_tool],
).until_done()


asyncio.run(main())
```
Comment on lines +186 to 197
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we generally assume the code snippet is already being ran in an async context, so you can remove the asyncio usage and the main() function wrapper.

"""

Expand Down