> ## 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.

# Recipes

> Common patterns and recipes for MemoClaw.

## Store User Preference

<CodeGroup>
  ```bash CLI theme={null}
  memoclaw store "User prefers dark mode" \
    --importance 0.8 \
    --tags preferences,ui
  ```

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

  client = MemoClaw()
  client.store(
      "User prefers dark mode",
      importance=0.8,
      tags=["preferences", "ui"],
  )
  ```

  ```bash curl theme={null}
  curl -X POST https://api.memoclaw.com/v1/store \
    -H "Content-Type: application/json" \
    -H "x-wallet-auth: $WALLET:$TIMESTAMP:$SIGNATURE" \
    -d '{
      "content": "User prefers dark mode",
      "importance": 0.8,
      "metadata": { "tags": ["preferences", "ui"] }
    }'
  ```
</CodeGroup>

## Store After Correction

<CodeGroup>
  ```bash CLI theme={null}
  memoclaw store "Correction: The database connection uses port 5433, not 5432" \
    --importance 0.95 \
    --tags corrections
  ```

  ```python Python theme={null}
  client.store(
      "Correction: The database connection uses port 5433, not 5432",
      importance=0.95,
      tags=["corrections"],
  )
  ```

  ```bash curl theme={null}
  curl -X POST https://api.memoclaw.com/v1/store \
    -H "Content-Type: application/json" \
    -H "x-wallet-auth: $WALLET:$TIMESTAMP:$SIGNATURE" \
    -d '{
      "content": "Correction: The database connection uses port 5433, not 5432",
      "importance": 0.95,
      "metadata": { "tags": ["corrections"] }
    }'
  ```
</CodeGroup>

## Recall Before Responding

<CodeGroup>
  ```bash CLI theme={null}
  memoclaw recall "user's theme preference"
  ```

  ```python Python theme={null}
  memories = client.recall("user's theme preference")
  if memories.memories:
      theme = memories.memories[0].content
      print(f"User prefers: {theme}")
  ```

  ```bash curl theme={null}
  curl -X POST https://api.memoclaw.com/v1/recall \
    -H "Content-Type: application/json" \
    -H "x-wallet-auth: $WALLET:$TIMESTAMP:$SIGNATURE" \
    -d '{ "query": "user'\''s theme preference" }'
  ```
</CodeGroup>

## Filter by Namespace

<CodeGroup>
  ```bash CLI theme={null}
  memoclaw recall "database configuration" \
    --namespace project-api \
    --limit 10
  ```

  ```python Python theme={null}
  memories = client.recall(
      "database configuration",
      namespace="project-api",
      limit=10,
  )
  ```

  ```bash curl theme={null}
  curl -X POST https://api.memoclaw.com/v1/recall \
    -H "Content-Type: application/json" \
    -H "x-wallet-auth: $WALLET:$TIMESTAMP:$SIGNATURE" \
    -d '{
      "query": "database configuration",
      "namespace": "project-api",
      "limit": 10
    }'
  ```
</CodeGroup>

## Batch Import

<CodeGroup>
  ```bash CLI theme={null}
  memoclaw store-batch \
    '{"content": "Uses PostgreSQL 15", "importance": 0.9}' \
    '{"content": "Deploys to Railway", "importance": 0.8}' \
    '{"content": "Team of 3 developers", "importance": 0.7}'
  ```

  ```python Python theme={null}
  memories = [
      {"content": "Uses PostgreSQL 15", "importance": 0.9},
      {"content": "Deploys to Railway", "importance": 0.8},
      {"content": "Team of 3 developers", "importance": 0.7},
  ]

  result = client.store_batch(memories)
  print(f"Stored {result.count} memories")
  ```

  ```bash curl theme={null}
  curl -X POST https://api.memoclaw.com/v1/store/batch \
    -H "Content-Type: application/json" \
    -H "x-wallet-auth: $WALLET:$TIMESTAMP:$SIGNATURE" \
    -d '{
      "memories": [
        { "content": "Uses PostgreSQL 15", "importance": 0.9 },
        { "content": "Deploys to Railway", "importance": 0.8 },
        { "content": "Team of 3 developers", "importance": 0.7 }
      ]
    }'
  ```
</CodeGroup>

## Extract Facts from Conversation

<CodeGroup>
  ```bash CLI theme={null}
  memoclaw ingest \
    --messages '[{"role":"user","content":"I prefer dark mode and use vim."},{"role":"assistant","content":"Got it!"},{"role":"user","content":"My timezone is PST."}]' \
    --auto-relate
  ```

  ```python Python theme={null}
  result = client.ingest(
      messages=[
          {"role": "user", "content": "I prefer dark mode and use vim."},
          {"role": "assistant", "content": "Got it!"},
          {"role": "user", "content": "My timezone is PST."},
      ],
      auto_relate=True,
  )
  print(f"Extracted {result.facts_extracted} facts")
  print(f"Created {result.relations_created} relations")
  ```

  ```bash curl theme={null}
  curl -X POST https://api.memoclaw.com/v1/ingest \
    -H "Content-Type: application/json" \
    -H "x-wallet-auth: $WALLET:$TIMESTAMP:$SIGNATURE" \
    -d '{
      "messages": [
        { "role": "user", "content": "I prefer dark mode and use vim." },
        { "role": "assistant", "content": "Got it!" },
        { "role": "user", "content": "My timezone is PST." }
      ],
      "auto_relate": true
    }'
  ```
</CodeGroup>

## Proactive Memory Suggestions

<CodeGroup>
  ```bash CLI theme={null}
  memoclaw suggested --category stale --limit 5
  ```

  ```python Python theme={null}
  suggested = client.suggested(category="stale", limit=5)

  for memory in suggested.memories:
      print(f"- {memory.content[:50]}... (importance: {memory.importance})")
  ```

  ```bash curl theme={null}
  curl "https://api.memoclaw.com/v1/suggested?category=stale&limit=5" \
    -H "x-wallet-auth: $WALLET:$TIMESTAMP:$SIGNATURE"
  ```
</CodeGroup>

## Error Handling

<CodeGroup>
  ```bash CLI theme={null}
  # CLI exits with non-zero codes on errors
  memoclaw delete some-id || echo "Delete failed"
  ```

  ```python Python theme={null}
  from memoclaw import MemoClaw, NotFoundError, RateLimitError, ValidationError

  try:
      client.delete("some-id")
  except NotFoundError:
      print("Memory already deleted")
  except RateLimitError:
      print("Rate limited - wait and retry")
  except ValidationError as e:
      print(f"Validation error: {e.message}")
  ```

  ```bash curl theme={null}
  # Check HTTP status codes
  # 404 = not found, 429 = rate limited, 422 = validation error
  curl -s -o /dev/null -w "%{http_code}" \
    -X DELETE "https://api.memoclaw.com/v1/memories/some-id" \
    -H "x-wallet-auth: $WALLET:$TIMESTAMP:$SIGNATURE"
  ```
</CodeGroup>
