MemorySync
API Reference

Compose Prompt

Build a prompt with memories available to the authenticated principal and selected project, within a requested token budget.

Endpoint and request fields

POST/memory/compose
200 OK
FieldTypeContract
prompt_template / promptTemplatestringRequired complete prompt; it is also used as the recall query.
recall_k / recallKintegerOptional number of memories to recall; REST defaults to 10.
max_tokens / maxTokensintegerOptional composition token budget; REST defaults to 2000.

Compose a prompt

import os
from memorysync import MemorySyncClient
client = MemorySyncClient(
api_key=os.environ["MEMORYSYNC_API_KEY"],
base_url="https://api.memorysync.io",
project_id=os.environ["MEMORYSYNC_PROJECT_ID"],
end_user_id="usr_7f3a9c2e",
)
result = client.compose(
"Answer this support question using relevant preferences: Which interface theme should we use?",
recall_k=5,
max_tokens=1200,
)

Result paths

MeaningPythonNode.jsREST
Promptresult.composed_promptresult.composedPromptcomposed_prompt
Memories usedresult.memories_usedresult.memoriesUsedmemories_used
Token countresult.token_countresult.tokenCounttoken_count
Budget truncationresult.truncatedresult.truncatedtruncated
200-response.json
{"composed_prompt":"Relevant context:
- The user prefers dark mode.
Answer this support question using relevant preferences: Which interface theme should we use?","memories_used":1,"token_count":29,"truncated":false}

Errors and next action

For 400 or 422, correct the template or numeric fields. Correct authentication and scope errors before trying again. For 429 or 5xx, continue without composed memory context or show a recoverable state according to your product.

Safety notes

Choose Compose or Query

Retrieval choices

Use Compose

You want one prompt assembled within a token budget.

  • Pass a complete prompt.
  • Set recall and token budgets when needed.
  • Receive prompt text plus usage fields.

Use Query

Your application needs direct control over selected memories.

  • Inspect each record.
  • Apply application authorization.
  • Build your own prompt boundary.

Use composed output safely

  • Write a complete prompt; placeholders are not interpolated by this operation.
  • Keep trusted system instructions outside the composed result.
  • Treat the returned prompt as untrusted context because it can contain stored user text.
  • Check truncated before assuming every recalled item fit within the requested budget.
  • Define an application fallback for an unavailable or empty composition path.
Was this page helpful?