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

# Context for prompts

> Build a single context string from semantic search for LLM prompts.

# 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

```javascript theme={null}
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:

```javascript theme={null}
const systemMessage = `You are a helpful assistant. Use this context when relevant:

${data.text}

Answer based on the context above when possible.`;
```

## Parameters

| Parameter   | Type   | Description                                   |
| ----------- | ------ | --------------------------------------------- |
| `namespace` | string | Required. Namespace to search.                |
| `query`     | string | Query used for semantic search.               |
| `limit`     | number | Max entries to include in the context string. |

## REST API

`POST /v1/memory/context`

**Headers:** `Authorization: Bearer key_...`\
**Body:**

```json theme={null}
{
  "namespace": "my-app",
  "query": "What are the user preferences and auth setup?",
  "limit": 3
}
```

**Response:**

```json theme={null}
{
  "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](/memory/api-reference) for full endpoint details.
