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

# Assemble Context

> POST /v1/context — Assemble a context block from memories for LLM prompts.

<Snippet file="snippets/x402-callout.mdx" />

**Price:** \$0.01 USDC

Automatically assemble relevant memories into a context block ready for LLM prompts. This is useful for building AI assistants that need contextual memory.

## Request Body

<ParamField body="query" type="string">
  Natural language query describing what context is needed. Used to find relevant memories.
</ParamField>

<ParamField body="namespace" type="string">
  Filter by namespace.
</ParamField>

<ParamField body="session_id" type="string">
  Filter by session ID.
</ParamField>

<ParamField body="agent_id" type="string">
  Filter by agent ID.
</ParamField>

<ParamField body="max_memories" type="number">
  Maximum number of memories to include. Default: `10`. Max: `100`.
</ParamField>

<ParamField body="max_tokens" type="number">
  Target maximum tokens for the context. Default: `4000`. Range: `100-16000`.
</ParamField>

<ParamField body="format" type="string">
  Output format: `text` (plain text) or `structured` (JSON with metadata). Default: `text`.
</ParamField>

<ParamField body="include_metadata" type="boolean">
  Include memory metadata (tags, importance, type) in the output. Default: `false`.
</ParamField>

<ParamField body="summarize" type="boolean">
  Use LLM to summarize multiple similar memories into fewer entries. Default: `false`.
</ParamField>

## Response (200 OK)

<ResponseField name="context" type="string">
  The assembled context text or JSON.
</ResponseField>

<ResponseField name="memories_used" type="number">
  Number of memories included in the context.
</ResponseField>

<ResponseField name="tokens" type="number">
  Approximate token count of the context.
</ResponseField>

## Example

<CodeGroup>
  ```bash curl theme={null}
  curl -X POST https://api.memoclaw.com/v1/context \
    -H "Content-Type: application/json" \
    -d '{
      "query": "user preferences and project context",
      "max_memories": 5,
      "max_tokens": 2000,
      "format": "text"
    }'
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch("https://api.memoclaw.com/v1/context", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      query: "user preferences and project context",
      max_memories: 5,
      max_tokens: 2000,
      format: "text",
    }),
  });
  const data = await response.json();
  ```

  ```python Python theme={null}
  from memoclaw import MemoClaw

  client = MemoClaw()

  result = client.assemble_context(
      query="user preferences and project context",
      max_memories=5,
      max_tokens=2000,
      format="text",
  )
  print(result.context)
  ```

  ```typescript TypeScript theme={null}

  import { MemoClawClient } from "memoclaw";

  const client = new MemoClawClient();

  const result = await client.assembleContext({
    query: "user preferences and project context",
    maxMemories: 5,
    maxTokens: 2000,
    format: "text",
  });
  console.log(result.context);
  ```
</CodeGroup>

```json Response theme={null}
{
  "context": "User preferences:\n- Prefers dark mode\n- Uses vim keybindings\n- Timezone: PST\n\nProject context:\n- Using PostgreSQL 15 with pgvector\n- Deploy to staging before production",
  "memories_used": 5,
  "tokens": 180
}
```

```json Response (structured format) theme={null}
{
  "context": {
    "memories": [
      {
        "content": "User prefers dark mode",
        "importance": 0.8,
        "memory_type": "preference",
        "tags": ["ui"],
        "source": "recall"
      },
      {
        "content": "Uses vim keybindings",
        "importance": 0.7,
        "memory_type": "preference",
        "tags": ["editor"],
        "source": "recall"
      }
    ]
  },
  "memories_used": 2,
  "tokens": 85
}
```
