> ## Documentation Index
> Fetch the complete documentation index at: https://docs.memobase.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Filtering Profiles at Retrieval

Memobase tracks and models a comprehensive profile for each user. You can use this profile in your [AI prompts](/features/context) to provide a global understanding of the user.

While user profiles are generally concise, it is good practice to control the final size of the context you inject into your prompts. Memobase provides several parameters to help you filter profiles at retrieval time.

## Rule-Based Filtering

You can pass rules to the Memobase API to filter profiles based on specific criteria:

* `max_token_size`: Sets the maximum token size for the entire profile context.
* `prefer_topics`: Ranks specified topics higher, making them more likely to be included in the final output.
* `only_topics`: Includes *only* the specified topics, filtering out all others.

Detailed parameter descriptions can be found in the [API documentation](/api-reference/profiles/profile).

## Context-Aware Filtering

Memobase also offers a powerful semantic filtering capability. By passing the latest chat messages to the API, you can retrieve only the most "contextual" or relevant profiles for the current conversation.

This is more advanced than a simple embedding-based search. Memobase uses the LLM to reason about which profile attributes would be most helpful for generating the next response.

For example, if a user says, "Find some restaurants for me," Memobase will intelligently rank profiles like `contact_info::city`, `interests::food`, and `health::allergies` higher in the results.

<CodeGroup>
  ```python Python theme={null}
  from memobase import MemoBaseClient

  client = MemoBaseClient(project_url='YOUR_PROJECT_URL', api_key='YOUR_API_KEY')
  user = client.get_user('some_user_id')

  # Retrieve profile context relevant to the last message
  contextual_profile = user.profile(
      chats=[{"role": "user", "content": "Find some restaurants for me"}],
      need_json=True
  )

  print(contextual_profile)
  ```

  ```json Output theme={null}
  {
    "contact_info": {
      "city": "San Francisco"
    },
    "interests": {
      "food": "Loves Italian and Japanese cuisine"
    },
    "health": {
      "allergies": "None"
    }
  }
  ```
</CodeGroup>

For more details on contextual search, see the [Profile Search documentation](/features/profile/profile_search).
