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

# Anthropic

> Drop-in wrapper for the Anthropic client that automatically injects memories into every messages.create call and captures conversation turns.

The `ThrindexAnthropic` wrapper replaces your `Anthropic` instance. Every `messages.create` call automatically retrieves relevant memories and injects them into the system prompt before the call, then captures the conversation turn as a new memory after it.

Memory retrieval failures are always silent — if Thrindex is unreachable, the underlying Anthropic call proceeds unaffected.

<Tip>
  Claude responds particularly well to XML-tagged memory blocks. Use a custom `format_memories` to wrap memories in `<memory_context>` tags for best results.
</Tip>

***

## Install

<Tabs>
  <Tab title="Python">
    ```bash theme={null}
    pip install 'thrindex[anthropic]'
    ```
  </Tab>

  <Tab title="TypeScript">
    ```bash theme={null}
    npm install thrindex @anthropic-ai/sdk
    ```
  </Tab>
</Tabs>

***

## Usage

<Tabs>
  <Tab title="Python">
    ### Synchronous

    ```python theme={null}
    from anthropic import Anthropic
    from thrindex import Thrindex
    from thrindex.integrations.anthropic import ThrindexAnthropic

    client = ThrindexAnthropic(
        thrindex_client=Thrindex(api_key="th_live_..."),
        anthropic_client=Anthropic(),
        agent_id="support-agent",
        user_id="user-42",
    )

    response = client.messages.create(
        model="claude-opus-4-5",
        max_tokens=1024,
        messages=[{"role": "user", "content": "What display theme do I prefer?"}],
    )
    ```

    ### Asynchronous

    ```python theme={null}
    from anthropic import AsyncAnthropic
    from thrindex import AsyncThrindex
    from thrindex.integrations.anthropic import ThrindexAsyncAnthropic

    client = ThrindexAsyncAnthropic(
        thrindex_client=AsyncThrindex(api_key="th_live_..."),
        anthropic_client=AsyncAnthropic(),
        agent_id="support-agent",
        user_id="user-42",
    )

    response = await client.messages.create(
        model="claude-opus-4-5",
        max_tokens=1024,
        messages=[{"role": "user", "content": "What display theme do I prefer?"}],
    )
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    import Anthropic from '@anthropic-ai/sdk';
    import { ThrindexClient } from 'thrindex';
    import { ThrindexAnthropic } from 'thrindex/integrations/anthropic';

    const client = new ThrindexAnthropic({
      thrindex: new ThrindexClient({ apiKey: 'th_live_...' }),
      anthropic: new Anthropic(),
      agentId: 'support-agent',
      userId: 'user-42',
    });

    const response = await client.messages.create({
      model: 'claude-opus-4-5',
      max_tokens: 1024,
      messages: [{ role: 'user', content: 'What display theme do I prefer?' }],
    });
    ```
  </Tab>
</Tabs>

***

## Options

Same as the [OpenAI integration](/sdks/integrations/openai) — replace `openai_client` with `anthropic_client` in Python, and `openai` with `anthropic` in TypeScript.

| Parameter (Python)  | Option (TypeScript) | Default     | Description                                                    |
| ------------------- | ------------------- | ----------- | -------------------------------------------------------------- |
| `thrindex_client`   | `thrindex`          | required    | A configured Thrindex client                                   |
| `anthropic_client`  | `anthropic`         | required    | Your Anthropic client instance                                 |
| `agent_id`          | `agentId`           | required    | Agent identifier for memory scoping                            |
| `user_id`           | `userId`            | required    | User identifier for memory scoping                             |
| `inject_k`          | `injectK`           | `5`         | Number of memories to inject per call                          |
| `inject`            | `inject`            | `true`      | Whether to inject memories before the call                     |
| `capture`           | `capture`           | `true`      | Whether to store conversation turns after the call             |
| `capture_assistant` | `captureAssistant`  | `true`      | Capture both user message and assistant reply as a single turn |
| `format_memories`   | `formatMemories`    | bullet list | Custom memory formatter                                        |

***

## Custom memory format (recommended for Claude)

Claude's instruction-following is strongest with XML structure. Use this formatter for best retrieval quality:

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    def xml_formatter(hits):
        lines = ["<memory_context>"]
        for hit in hits:
            lines.append(f"  <memory importance='{hit.importance:.2f}'>{hit.content}</memory>")
        lines.append("</memory_context>")
        return "\n".join(lines)

    client = ThrindexAnthropic(
        thrindex_client=Thrindex(api_key="th_live_..."),
        anthropic_client=Anthropic(),
        agent_id="support-agent",
        user_id="user-42",
        format_memories=xml_formatter,
    )
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    const client = new ThrindexAnthropic({
      thrindex: new ThrindexClient({ apiKey: 'th_live_...' }),
      anthropic: new Anthropic(),
      agentId: 'support-agent',
      userId: 'user-42',
      formatMemories: (hits) =>
        `<memory_context>\n${hits.map(h => `  <memory importance="${h.importance.toFixed(2)}">${h.content}</memory>`).join('\n')}\n</memory_context>`,
    });
    ```
  </Tab>
</Tabs>
