Skip to main content

Context for prompts

The context API runs a semantic search and returns a single formatted string you can drop into a system or user message. No need to manually concatenate search results.

SDK

import { Foundry } from '@withfoundry/sdk';

const foundry = new Foundry({ apiKey: process.env.FOUNDRY_API_KEY });

const { data } = await foundry.memory.getContext({
  namespace: 'my-app',
  query: 'What are the user preferences and auth setup?',
  limit: 3,
});

// data.text = pre-formatted string of the top entries
console.log(data.text);
Use data.text in your prompt:
const systemMessage = `You are a helpful assistant. Use this context when relevant:

${data.text}

Answer based on the context above when possible.`;

Parameters

ParameterTypeDescription
namespacestringRequired. Namespace to search.
querystringQuery used for semantic search.
limitnumberMax entries to include in the context string.

REST API

POST /v1/memory/context Headers: Authorization: Bearer key_...
Body:
{
  "namespace": "my-app",
  "query": "What are the user preferences and auth setup?",
  "limit": 3
}
Response:
{
  "success": true,
  "data": {
    "text": "Entry 1 content here.\n\nEntry 2 content here.\n\nEntry 3 content here.",
    "entryIds": ["mem_abc", "mem_def", "mem_ghi"]
  },
  "meta": { "requestId": "req_xyz" }
}

Format

  • Entries are concatenated into one string, typically separated by newlines or blank lines.
  • Order follows search relevance (highest first). Use limit to control token usage.

When to use

  • RAG / Q&A: Query with the user question, inject data.text into the system or first user message.
  • Chat with memory: Before each turn, get context for the last message or conversation summary and append to the prompt.
Next: Memory API reference for full endpoint details.