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
47 changes: 47 additions & 0 deletions docs/sessions/memory.md
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,53 @@ For example, you can automate this step with a callback:
--8<-- "examples/kotlin/snippets/sessions/MemoryExample.kt:auto_save_callback"
```

## Extend memory capabilities

Memory services extended from `BaseMemoryService` support adding sessions and
events to agent memory, including custom metadata. Use the
`add_session_to_memory` and `add_events_to_memory` methods of memory services
such as `InMemoryMemoryService` to amend memory data, as shown in the
following code example:

```python
import asyncio
from google.adk.memory import InMemoryMemoryService

# Assume my_memory_service is an instance of InMemoryMemoryService
# and my_latest_events is a list of new adk.Event objects from the latest turn.
my_latest_events = [...]

async def update_incremental_memory(my_memory_service, my_latest_events):
# Example 1: Basic incremental update
await my_memory_service.add_events_to_memory(
app_name="my-app",
user_id="my-user",
events=my_latest_events,
session_id="my-optional-session-id"
)

# Example 2: Incremental update with Custom Metadata
await my_memory_service.add_events_to_memory(
app_name="my-app",
user_id="my-user",
events=my_latest_events,
session_id="my-optional-session-id",
custom_metadata={
"my_custom_key": "my_custom_value"
}
)

async def update_session_memory(my_memory_service, my_completed_session):
# Example 3: Applying custom metadata to a full session
await my_memory_service.add_session_to_memory(
session=my_completed_session,
custom_metadata={
"category": "user_preference"
}
)

```

## Advanced concepts

### How memory works in practice
Expand Down
Loading