Query Memory

Search and retrieve memories using semantic search with multi-hop navigation

Query Memory

Search and retrieve memories using semantic search with multi-hop navigation.

Endpoint

POST /api/query

Request Body

interface QueryRequest {
  query: string;
  limit?: number;
  max_hops?: number;
  min_strength?: number;
  sector_id?: string;
  metadata_filter?: Record<string, any>;
  include_paths?: boolean;
}

Parameters

ParameterTypeRequiredDefaultDescription
querystringYes-Search query text
limitnumberNo10Max results to return
max_hopsnumberNo3Multi-hop depth
min_strengthnumberNo0.3Minimum memory strength
sector_idstringNonullSearch within sector
metadata_filterobjectNo{}Filter by metadata
include_pathsbooleanNotrueInclude traversal paths

Response

interface QueryResponse {
  results: MemoryResult[];
  query_time_ms: number;
  total_memories_searched: number;
}

interface MemoryResult {
  id: string;
  content: string;
  score: number;
  strength: number;
  hops: number;
  path?: string[];
  metadata: Record<string, any>;
}

Examples

Basic Query

from openmemory import OpenMemory

om = OpenMemory(api_key="your_api_key")

# Simple search
results = om.query(
    query="How does Python handle memory management?",
    limit=5
)

for result in results:
    print(f"Score: {result.score:.3f}")
    print(f"Content: {result.content}")
    print("---")

Multi-hop Query

# Navigate through related memories
results = om.query(
    query="machine learning optimization",
    max_hops=4,
    include_paths=True
)

for result in results:
    print(f"Found after {result.hops} hops")
    print(f"Path: {' → '.join(result.path)}")
    print(f"Content: {result.content}")

Filtered Query

# Filter by metadata and sector
results = om.query(
    query="authentication patterns",
    sector_id="work/backend",
    metadata_filter={
        "language": "python",
        "verified": True
    },
    min_strength=0.5
)

TypeScript

const results = await om.query({
  query: 'React hooks best practices',
  limit: 10,
  maxHops: 2,
  metadataFilter: {
    category: 'react',
  },
});

results.forEach((r) => {
  console.log(`${r.score}: ${r.content}`);
});

See Add Memory for creating memories and Reinforcement for strengthening them.

© 2025 OpenMemory · MIT License