Skip to main content
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.
Claude responds particularly well to XML-tagged memory blocks. Use a custom format_memories to wrap memories in <memory_context> tags for best results.

Install

pip install 'thrindex[anthropic]'

Usage

Synchronous

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

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?"}],
)

Options

Same as the OpenAI integration — replace openai_client with anthropic_client in Python, and openai with anthropic in TypeScript.
Parameter (Python)Option (TypeScript)DefaultDescription
thrindex_clientthrindexrequiredA configured Thrindex client
anthropic_clientanthropicrequiredYour Anthropic client instance
agent_idagentIdrequiredAgent identifier for memory scoping
user_iduserIdrequiredUser identifier for memory scoping
inject_kinjectK5Number of memories to inject per call
injectinjecttrueWhether to inject memories before the call
capturecapturetrueWhether to store conversation turns after the call
capture_assistantcaptureAssistanttrueCapture both user message and assistant reply as a single turn
format_memoriesformatMemoriesbullet listCustom memory formatter

Claude’s instruction-following is strongest with XML structure. Use this formatter for best retrieval quality:
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,
)