Skip to main content
POST
/
optimize-memories
Optimize User Memories
curl --request POST \
  --url https://api.example.com/optimize-memories \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '
{
  "user_id": "<string>",
  "model": "<string>",
  "apply": true
}
'
import requests

url = "https://api.example.com/optimize-memories"

payload = {
"user_id": "<string>",
"model": "<string>",
"apply": True
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.text)
const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({user_id: '<string>', model: '<string>', apply: true})
};

fetch('https://api.example.com/optimize-memories', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));
<?php

$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_URL => "https://api.example.com/optimize-memories",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'user_id' => '<string>',
'model' => '<string>',
'apply' => true
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
package main

import (
"fmt"
"strings"
"net/http"
"io"
)

func main() {

url := "https://api.example.com/optimize-memories"

payload := strings.NewReader("{\n \"user_id\": \"<string>\",\n \"model\": \"<string>\",\n \"apply\": true\n}")

req, _ := http.NewRequest("POST", url, payload)

req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")

res, _ := http.DefaultClient.Do(req)

defer res.Body.Close()
body, _ := io.ReadAll(res.Body)

fmt.Println(string(body))

}
HttpResponse<String> response = Unirest.post("https://api.example.com/optimize-memories")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"user_id\": \"<string>\",\n \"model\": \"<string>\",\n \"apply\": true\n}")
.asString();
require 'uri'
require 'net/http'

url = URI("https://api.example.com/optimize-memories")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"user_id\": \"<string>\",\n \"model\": \"<string>\",\n \"apply\": true\n}"

response = http.request(request)
puts response.read_body
{
  "memories": [
    {
      "memory_id": "f9361a69-2997-40c7-ae4e-a5861d434047",
      "memory": "User has a 3-year-old golden retriever named Max who loves fetch and walks. Lives in San Francisco's Mission district, works as a product manager in tech. Enjoys hiking Bay Area trails, trying new restaurants (especially Japanese, Thai, Mexican), and learning piano for 1.5 years.",
      "topics": [
        "pets",
        "location",
        "work",
        "hobbies",
        "food_preferences"
      ],
      "user_id": "user2",
      "updated_at": "2025-11-18T10:30:00Z"
    }
  ],
  "memories_before": 4,
  "memories_after": 1,
  "tokens_before": 450,
  "tokens_after": 180,
  "tokens_saved": 270,
  "reduction_percentage": 60
}
{
"detail": "Bad request",
"error_code": "BAD_REQUEST"
}
{
"detail": "Unauthenticated access",
"error_code": "UNAUTHENTICATED"
}
{
"detail": "Not found",
"error_code": "NOT_FOUND"
}
{
"detail": "Validation error",
"error_code": "VALIDATION_ERROR"
}
{
"detail": "Internal server error",
"error_code": "INTERNAL_SERVER_ERROR"
}

Authorizations

Authorization
string
header
required

Bearer authentication header of the form Bearer <token>, where <token> is your auth token.

Query Parameters

db_id
string | null

Database ID to use for optimization

table
string | null

Table to use for optimization

Body

application/json

Schema for memory optimization request

user_id
string
required

User ID to optimize memories for

model
string | null

Model to use for optimization in format 'provider:model_id' (e.g., 'openai:gpt-4o-mini', 'anthropic:claude-3-5-sonnet-20241022', 'google:gemini-2.0-flash-exp'). If not specified, uses MemoryManager's default model (gpt-4o).

apply
boolean
default:true

If True, apply optimization changes to database. If False, return preview only without saving.

Response

Memories optimized successfully

Schema for memory optimization response

memories
UserMemorySchema · object[]
required

List of optimized memory objects

memories_before
integer
required

Number of memories before optimization

Required range: x >= 0
memories_after
integer
required

Number of memories after optimization

Required range: x >= 0
tokens_before
integer
required

Token count before optimization

Required range: x >= 0
tokens_after
integer
required

Token count after optimization

Required range: x >= 0
tokens_saved
integer
required

Number of tokens saved through optimization

Required range: x >= 0
reduction_percentage
number
required

Percentage of token reduction achieved

Required range: 0 <= x <= 100