> ## Documentation Index
> Fetch the complete documentation index at: https://agno-v2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Optimize User Memories

> Optimize all memories for a given user using the default summarize strategy. This operation combines all memories into a single comprehensive summary, achieving maximum token reduction while preserving all key information. To use a custom model, specify the model parameter in 'provider:model_id' format (e.g., 'openai:gpt-4o-mini', 'anthropic:claude-3-5-sonnet-20241022'). If not specified, uses MemoryManager's default model (gpt-4o). Set apply=false to preview optimization results without saving to database.



## OpenAPI

````yaml post /optimize-memories
openapi: 3.1.0
info:
  title: Agno API Reference
  description: The all-in-one, private, secure agent platform that runs in your cloud.
  version: 3.0.4
servers: []
security: []
paths:
  /optimize-memories:
    post:
      tags:
        - Memory
      summary: Optimize User Memories
      description: >-
        Optimize all memories for a given user using the default summarize
        strategy. This operation combines all memories into a single
        comprehensive summary, achieving maximum token reduction while
        preserving all key information. To use a custom model, specify the model
        parameter in 'provider:model_id' format (e.g., 'openai:gpt-4o-mini',
        'anthropic:claude-3-5-sonnet-20241022'). If not specified, uses
        MemoryManager's default model (gpt-4o). Set apply=false to preview
        optimization results without saving to database.
      operationId: optimize_memories
      parameters:
        - name: db_id
          in: query
          required: false
          schema:
            anyOf:
              - type: string
              - type: 'null'
            description: Database ID to use for optimization
            title: Db Id
          description: Database ID to use for optimization
        - name: table
          in: query
          required: false
          schema:
            anyOf:
              - type: string
              - type: 'null'
            description: Table to use for optimization
            title: Table
          description: Table to use for optimization
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/OptimizeMemoriesRequest'
      responses:
        '200':
          description: Memories optimized successfully
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/OptimizeMemoriesResponse'
              example:
                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
        '400':
          description: Bad request - User ID is required or invalid model string format
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/BadRequestResponse'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/UnauthenticatedResponse'
        '403':
          description: 'Insufficient permissions. Required scope(s): memories:write'
        '404':
          description: No memories found for user
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/NotFoundResponse'
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ValidationErrorResponse'
        '500':
          description: Failed to optimize memories
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/InternalServerErrorResponse'
      security:
        - {}
        - HTTPBearer: []
components:
  schemas:
    OptimizeMemoriesRequest:
      properties:
        user_id:
          type: string
          title: User Id
          description: User ID to optimize memories for
        model:
          anyOf:
            - type: string
            - type: 'null'
          title: Model
          description: >-
            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:
          type: boolean
          title: Apply
          description: >-
            If True, apply optimization changes to database. If False, return
            preview only without saving.
          default: true
      type: object
      required:
        - user_id
      title: OptimizeMemoriesRequest
      description: Schema for memory optimization request
    OptimizeMemoriesResponse:
      properties:
        memories:
          items:
            $ref: '#/components/schemas/UserMemorySchema'
          type: array
          title: Memories
          description: List of optimized memory objects
        memories_before:
          type: integer
          minimum: 0
          title: Memories Before
          description: Number of memories before optimization
        memories_after:
          type: integer
          minimum: 0
          title: Memories After
          description: Number of memories after optimization
        tokens_before:
          type: integer
          minimum: 0
          title: Tokens Before
          description: Token count before optimization
        tokens_after:
          type: integer
          minimum: 0
          title: Tokens After
          description: Token count after optimization
        tokens_saved:
          type: integer
          minimum: 0
          title: Tokens Saved
          description: Number of tokens saved through optimization
        reduction_percentage:
          type: number
          maximum: 100
          minimum: 0
          title: Reduction Percentage
          description: Percentage of token reduction achieved
      type: object
      required:
        - memories
        - memories_before
        - memories_after
        - tokens_before
        - tokens_after
        - tokens_saved
        - reduction_percentage
      title: OptimizeMemoriesResponse
      description: Schema for memory optimization response
    BadRequestResponse:
      properties:
        detail:
          type: string
          title: Detail
          description: Human-readable error message
        error_id:
          anyOf:
            - type: string
            - type: 'null'
          title: Error Id
          description: >-
            Stable identifier for the specific error, present only when the
            error carries one
        error_type:
          anyOf:
            - type: string
            - type: 'null'
          title: Error Type
          description: Category of the error, present only when the error carries one
      type: object
      required:
        - detail
      title: BadRequestResponse
      example:
        detail: Bad request
    UnauthenticatedResponse:
      properties:
        detail:
          type: string
          title: Detail
          description: Human-readable error message
        error_id:
          anyOf:
            - type: string
            - type: 'null'
          title: Error Id
          description: >-
            Stable identifier for the specific error, present only when the
            error carries one
        error_type:
          anyOf:
            - type: string
            - type: 'null'
          title: Error Type
          description: Category of the error, present only when the error carries one
      type: object
      required:
        - detail
      title: UnauthenticatedResponse
      example:
        detail: Unauthenticated access
    NotFoundResponse:
      properties:
        detail:
          type: string
          title: Detail
          description: Human-readable error message
        error_id:
          anyOf:
            - type: string
            - type: 'null'
          title: Error Id
          description: >-
            Stable identifier for the specific error, present only when the
            error carries one
        error_type:
          anyOf:
            - type: string
            - type: 'null'
          title: Error Type
          description: Category of the error, present only when the error carries one
      type: object
      required:
        - detail
      title: NotFoundResponse
      example:
        detail: Not found
    ValidationErrorResponse:
      properties:
        detail:
          anyOf:
            - type: string
            - items:
                $ref: '#/components/schemas/ValidationErrorDetail'
              type: array
          title: Detail
          description: >-
            A single message for an explicitly raised 422, or a list of
            field-level errors for a request-validation failure
      type: object
      required:
        - detail
      title: ValidationErrorResponse
      description: >-
        422 body. Two runtime shapes share this status code, and the same
        endpoint can

        return either, so ``detail`` is typed as their union:


        - FastAPI's request-validation handler emits a **list** of field-level
        errors (built-in
          coercion errors and custom-validator ``ValueError``s alike).
        - A route that raises ``HTTPException(status_code=422, detail="...")``
        for a semantic
          check (e.g. an invalid cron expression) emits a **string** through the HTTPException
          handler.
      example:
        detail:
          - loc:
              - body
              - endpoint
            msg: Value error, Endpoint must be a path, not a full URL
            type: value_error
    InternalServerErrorResponse:
      properties:
        detail:
          type: string
          title: Detail
          description: Human-readable error message
        error_id:
          anyOf:
            - type: string
            - type: 'null'
          title: Error Id
          description: >-
            Stable identifier for the specific error, present only when the
            error carries one
        error_type:
          anyOf:
            - type: string
            - type: 'null'
          title: Error Type
          description: Category of the error, present only when the error carries one
      type: object
      required:
        - detail
      title: InternalServerErrorResponse
      example:
        detail: Internal server error
    UserMemorySchema:
      properties:
        memory_id:
          type: string
          title: Memory Id
          description: Unique identifier for the memory
        memory:
          type: string
          title: Memory
          description: Memory content text
        topics:
          anyOf:
            - items:
                type: string
              type: array
            - type: 'null'
          title: Topics
          description: Topics or tags associated with the memory
        agent_id:
          anyOf:
            - type: string
            - type: 'null'
          title: Agent Id
          description: Agent ID associated with this memory
        team_id:
          anyOf:
            - type: string
            - type: 'null'
          title: Team Id
          description: Team ID associated with this memory
        user_id:
          anyOf:
            - type: string
            - type: 'null'
          title: User Id
          description: User ID who owns this memory
        updated_at:
          anyOf:
            - type: string
              format: date-time
            - type: 'null'
          title: Updated At
          description: Timestamp when memory was last updated
      type: object
      required:
        - memory_id
        - memory
      title: UserMemorySchema
    ValidationErrorDetail:
      properties:
        loc:
          items:
            anyOf:
              - type: string
              - type: integer
          type: array
          title: Loc
          description: Path to the offending field, e.g. ['body', 'endpoint']
        msg:
          type: string
          title: Msg
          description: Human-readable error message
        type:
          type: string
          title: Type
          description: Error type identifier, e.g. 'value_error' or 'missing'
      type: object
      required:
        - loc
        - msg
        - type
      title: ValidationErrorDetail
      description: >-
        One field-level error inside a 422 body, matching FastAPI's default
        shape.
  securitySchemes:
    HTTPBearer:
      type: http
      scheme: bearer

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.