Carrot LabsCarrot Docs
Python SDK

Anthropic

Automatic tracing for Anthropic messages and streaming.

Setup

import carrot_ai
from anthropic import Anthropic

carrot_ai.init(api_key="sk-...")
client = carrot_ai.wrap(Anthropic())

Messages

response = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Explain quantum computing in simple terms."},
    ],
)

print(response.content[0].text)

Streaming

stream = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Tell me a story"}],
    stream=True,
)

for event in stream:
    if hasattr(event, 'delta') and hasattr(event.delta, 'text'):
        print(event.delta.text, end="", flush=True)

System messages

response = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    system="You are a helpful coding assistant.",
    messages=[
        {"role": "user", "content": "Write a Python fibonacci function."},
    ],
)

Async client

from anthropic import AsyncAnthropic

client = carrot_ai.wrap(AsyncAnthropic())


async def main():
    response = await client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Hello!"}],
    )
    print(response.content[0].text)

With @trace for pipelines

@carrot_ai.trace("research-pipeline")
def research(topic: str) -> str:
    outline = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=1024,
        messages=[{"role": "user", "content": f"Create an outline for: {topic}"}],
    ).content[0].text

    article = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=4096,
        messages=[
            {"role": "user", "content": f"Write an article following this outline:\n{outline}"},
        ],
    ).content[0].text

    return article

On this page