|
| 1 | +--- |
| 2 | +alwaysopen: false |
| 3 | +categories: |
| 4 | +- docs |
| 5 | +- develop |
| 6 | +- ai |
| 7 | +description: Learn to use the Redis Agent Memory API for agent memory and semantic memory search. |
| 8 | +hideListLinks: true |
| 9 | +linktitle: API and SDK examples |
| 10 | +title: Use the Redis Agent Memory API and SDK |
| 11 | +weight: 10 |
| 12 | +--- |
| 13 | + |
| 14 | +Use the [Agent Memory API]({{< relref "/develop/ai/context-engine/agent-memory/api-reference" >}}) from your client app to store and retrieve agent memory information. |
| 15 | + |
| 16 | +You can use any standard REST client or library to access the API. If your app is written in Python, you can also use the [Agent Memory Software Development Kit](https://pypi.org/project/redis-agent-memory/) (SDK) to access the API. |
| 17 | + |
| 18 | +## Authentication |
| 19 | + |
| 20 | +To access the Agent Memory API, you need: |
| 21 | + |
| 22 | +- Agent Memory API endpoint |
| 23 | +- an Agent Memory API user key |
| 24 | +- a Store ID |
| 25 | + |
| 26 | +When you call the API, you need to pass the Agent Memory API key in the `Authorization` header as a Bearer token and the store ID as the `storeId` path parameter. |
| 27 | + |
| 28 | +For example: |
| 29 | + |
| 30 | +```sh |
| 31 | +curl -s -X GET "https://$HOST/v1/stores/$STORE_ID/session-memory" \ |
| 32 | + -H "accept: application/json" \ |
| 33 | + -H "Authorization: Bearer $API_KEY" |
| 34 | +``` |
| 35 | + |
| 36 | +This example expects several variables to be set in the shell: |
| 37 | + |
| 38 | +- **$HOST** - the Agent Memory API endpoint |
| 39 | +- **$STORE_ID** - the Store ID of your Agent Memory service |
| 40 | +- **$API_KEY** - The Agent Memory API token |
| 41 | + |
| 42 | +## Examples |
| 43 | + |
| 44 | +### Add session event |
| 45 | + |
| 46 | +Use [`POST /v1/stores/{storeId}/session-memory/events`]({{< relref "/develop/ai/context-engine/agent-memory/api-reference#tag/session-memory/operation/AddSessionEvent" >}}) to add an event to a session in short-term memory. If a session doesn't exist yet, it will be created. |
| 47 | + |
| 48 | +```json |
| 49 | +POST /v1/stores/{storeId}/session-memory/events |
| 50 | +{ |
| 51 | + "sessionId": "abcd-efgh", |
| 52 | + "actorId": "user-name", |
| 53 | + "role": "USER", |
| 54 | + "content": [ |
| 55 | + { |
| 56 | + "text": "I'm planning a trip to Japan next month." |
| 57 | + } |
| 58 | + ], |
| 59 | + "createdAt": "2026-05-02T18:15:06Z", |
| 60 | + "metadata": { |
| 61 | + "browser": "Chrome", |
| 62 | + "source": "web-chat" |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +Use this endpoint to store conversations between your users and your AI agent. You can use the `metadata` object to store additional metadata for your application. |
| 68 | + |
| 69 | +The Agent Memory model will automatically promote relevant short-term memories to long-term memory. |
| 70 | + |
| 71 | +### Add Long-term memories |
| 72 | + |
| 73 | +You may want to add one or more long-term memories to add specific preference information. |
| 74 | + |
| 75 | +Use [`POST /v1/stores/{storeId}/long-term-memory/`]({{< relref "/develop/ai/context-engine/agent-memory/api-reference#tag/long-term-memory/operation/BulkCreateLongTermMemories" >}}) to add one or more long-term memories to long-term memory storage. |
| 76 | + |
| 77 | +```json |
| 78 | +POST /v1/stores/{storeId}/long-term-memory |
| 79 | +{ |
| 80 | + "memories": [ |
| 81 | + { |
| 82 | + "id": "cofIXpuMmg", |
| 83 | + "text": "The user prefers vegetarian food.", |
| 84 | + "memoryType": "episodic", |
| 85 | + "sessionId": "abcd-efgh", |
| 86 | + "ownerId": "user-name", |
| 87 | + } |
| 88 | + ] |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +### Search Long-term memories |
| 93 | + |
| 94 | +Use [`POST /v1/stores/{storeId}/long-term-memory/search`]({{< relref "/develop/ai/context-engine/agent-memory/api-reference#tag/long-term-memory/operation/SearchLongTermMemory" >}}) to search for long-term memories. |
| 95 | + |
| 96 | +```json |
| 97 | +POST /v1/stores/{storeId}/long-term-memory/search |
| 98 | +{ |
| 99 | + "text": "user preferences", |
| 100 | + "similarityThreshold": 0.48725898820184166, |
| 101 | + "filter": { |
| 102 | + "sessionId": { |
| 103 | + "eq": "abcd-efgh" |
| 104 | + }, |
| 105 | + "ownerId": { |
| 106 | + "in": [ |
| 107 | + "user1", |
| 108 | + "user2" |
| 109 | + ] |
| 110 | + } |
| 111 | + }, |
| 112 | + "filterOp": "any" |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +In the `filter` object of the request body, you can filter the search by any of the following values: |
| 117 | + |
| 118 | +| Filter | Data type | Definition | Supported operators | |
| 119 | +|--------|----------|------------|---------------------| |
| 120 | +| `sessionId` | string | The session ID the memory comes from. | `eq`, `ne`, `in`, `all` | |
| 121 | +| `ownerId` | string | The owner ID of the memory. | `eq`, `ne`, `in`, `all` | |
| 122 | +| `namespace` | string | The namespace of the memory. | `eq`, `ne`, `in`, `all` | |
| 123 | +| `topics` | string | The topics of the memory. | `eq`, `ne`, `in`, `all` | |
| 124 | +| `memoryType` | string | The type of memory (`semantic`, `episodic`, `message`). | `eq`, `ne`, `in`, `all` | |
| 125 | +| `createdAt` | string (ISO 8601) | The timestamp when the memory was created. | `eq`, `gt`, `lt`, `gte`, `lte` | |
| 126 | + |
| 127 | +For all values, you must set only one of these operators: |
| 128 | + |
| 129 | +| Operator | Definition | |
| 130 | +|----------|---------------------| |
| 131 | +| `eq` | Returns memories with the value equal to the provided value. | |
| 132 | +| `ne` | Returns memories where the value is not the provided value. | |
| 133 | +| `in` | Returns memories where the value is one of a list of provided values. | |
| 134 | +| `all` | Returns memories where the value matches all of the provided values. | |
| 135 | +| `gt` | Returns memories where the value is greater than the provided value. | |
| 136 | +| `lt` | Returns memories where the value is less than the provided value. | |
| 137 | +| `gte` | Returns memories where the value is greater than or equal to the provided value. | |
| 138 | +| `lte` | Returns memories where the value is less than or equal to the provided value. | |
0 commit comments