Enhance Claude needle-in-a-haystack recall by pruning middle context fluff.
Optimize context density for Claude 3.5 Sonnet, Claude Opus, and Haiku models using structural XML boundary locking.
Install LLMSlim and the official Anthropic Claude SDK using your package manager:
pip install llmslim anthropic1. Prepare system instructions wrapped in Claude XML tags (<instructions>).
2. LLMSlim locks XML boundaries in Tier 4 and prunes repetitive narrative prose.
3. Dispatch compressed prompt payload via Anthropic Messages API.
Complete, runnable implementation wrapper for Anthropic Claude:
import anthropic
from llmslim import compress
client = anthropic.Anthropic()
raw_prompt = """
<instructions>
You MUST extract user entities strictly into <output> tags.
</instructions>
<context>
Verbose customer support transcript details and repetitive prose...
</context>
"""
slim_prompt = compress(raw_prompt, target_ratio=0.5, mode="xml").compressed_text
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[{"role": "user", "content": slim_prompt}]
)
print(message.content[0].text)| Metric Dimension | Uncompressed Payload | LLMSlim Compressed | Recorded Impact |
|---|---|---|---|
| Context Token Payload | 8,500 tokens | 3,400 tokens | 60% Payload Reduction |
No. The XML optimizer mode explicitly locks tags like <instructions>, <context>, and <examples>.