RedaktAIDocs

Anthropic

Using Rdakt AI with Anthropic's API

Anthropic's Python SDK uses httpx internally. Rdakt AI handles Anthropic-specific behaviors like gzip-compressed streaming automatically.

Setup

import httpx
from anthropic import AsyncAnthropic
from rdakt_ai import RdaktMiddleware

client = AsyncAnthropic(
    http_client=httpx.AsyncClient(
        transport=RdaktMiddleware(inner=httpx.AsyncHTTPTransport()),
    ),
)

Supported Models

All Anthropic chat models: Claude Sonnet 4, Claude Opus 4, Claude Haiku 3.5, and future models.

Message Format

Anthropic uses the messages[].content format (same structure as OpenAI):

{
  "model": "claude-sonnet-4-20250514",
  "max_tokens": 1024,
  "messages": [
    {"role": "user", "content": "Tell me about John Smith"}
  ]
}

Gzip Compression

Anthropic's API compresses streaming responses with gzip (Content-Encoding: gzip). Rdakt AI:

  1. Detects the gzip encoding from response headers
  2. Decompresses the stream on-the-fly
  3. Parses SSE events from the decompressed stream
  4. Deanonymizes tokens
  5. Re-emits the decompressed events

No configuration needed — this is handled transparently.

Streaming Format

Anthropic uses a different SSE event structure than OpenAI:

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}}

Rdakt AI extracts content from delta.text for content_block_delta events.

Streaming Example

async with client.messages.stream(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Write about Jane Doe (jane@acme.com)"}
    ],
) as stream:
    async for text in stream.text_stream:
        print(text, end="", flush=True)

Non-Streaming Example

response = await client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Summarize the case for John Smith (SSN 123-45-6789)"}
    ],
)
print(response.content[0].text)

On this page