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

# POST /context — get a prompt-ready memory block

> Request body, response schema, and LLM integration examples for the OMP context endpoint that returns ranked, citation-tagged memory text.

`POST /context` returns a single ranked, citation-tagged text block sized to your token budget. It is designed to be prepended directly to an LLM system prompt without any further processing. The provider selects and ranks the most relevant memories for the given query, formats them as prose, and reports which memory IDs it drew from.

## Request body

<ParamField body="query" type="string" required>
  The user's question or task description. The provider uses this to rank and select which memories to include in the context block.
</ParamField>

<ParamField body="user_id" type="string" required>
  The user whose memories to draw from. Context is always scoped to a single user.
</ParamField>

<ParamField body="scope" type="string">
  Restrict the context to memories whose scope starts with this prefix (e.g. `coding` to pull only coding-related memories).
</ParamField>

<ParamField body="token_budget" type="integer" default="500">
  Maximum number of tokens the provider should use when constructing the returned `text`. The provider will truncate or omit lower-ranked memories to stay within this budget.
</ParamField>

## Response — 200 OK

<ResponseField name="text" type="string" required>
  The ranked, citation-tagged text block ready for LLM prompt injection. Citations are embedded inline (e.g. `[mem_abc123]`) so the model can reference them.
</ResponseField>

<ResponseField name="citations" type="object[]" required>
  List of memory sources used to build the context block.

  <Expandable title="citation properties">
    <ResponseField name="memory_id" type="string">
      The ID of the memory referenced in `text`.
    </ResponseField>

    <ResponseField name="score" type="number">
      Relevance score for this memory relative to the query.
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="token_count" type="integer">
  The actual token count of the returned `text`. May be `null` if the provider does not track token counts.
</ResponseField>

## Example

```bash theme={null}
curl -s -X POST http://localhost:8080/context \
  -H "Content-Type: application/json" \
  -d '{
    "query": "set up a new node project",
    "user_id": "u1",
    "scope": "coding",
    "token_budget": 300
  }'
```

```json theme={null}
{
  "text": "The user prefers pnpm over npm for package management [mem_abc123]. They use dark mode in their editor [mem_def456].",
  "citations": [
    { "memory_id": "mem_abc123", "score": 0.94 },
    { "memory_id": "mem_def456", "score": 0.61 }
  ],
  "token_count": 38
}
```

## Python SDK equivalent

```python theme={null}
ctx = mem.context(
    query="set up a new node project",
    user_id="u1",
    scope="coding",
    token_budget=300,
)
print(ctx.text)
print(ctx.citations)
print(ctx.token_count)
```

## LLM integration

Prepend `ctx.text` to your system prompt to ground the model in the user's memory:

```python theme={null}
import openai

ctx = mem.context(
    query=user_message,
    user_id=uid,
    scope="coding",
    token_budget=400,
)

response = openai.chat.completions.create(
    model="gpt-4o",
    messages=[
        {
            "role": "system",
            "content": f"Relevant user memory:\n{ctx.text}\n\nCitations: {ctx.citations}",
        },
        {"role": "user", "content": user_message},
    ],
)
```

<Tip>
  Keep `token_budget` well below your model's context window limit so there is room for the system prompt, conversation history, and the model's response. A value between 300 and 600 tokens is a good starting point for most use cases.
</Tip>
