Carrot LabsCarrot Docs
Python SDK

SDK Reference

Complete API reference for the carrot-ai Python SDK.

carrot_ai.init()

Initialize the SDK. Must be called before wrap() or patch_litellm() unless the CARROT_API_KEY environment variable is set.

carrot_ai.init(
    api_key="sk-...",
    base_url="https://api.carrotlabs.ai",
    flush_interval=5.0,
    batch_size=50,
)
ParameterTypeDefaultDescription
api_keystrCARROT_API_KEY env varYour Carrot API key
base_urlstrhttps://api.carrotlabs.aiAPI endpoint
flush_intervalfloat5.0Seconds between background flushes
batch_sizeint50Max traces per batch

carrot_ai.wrap()

Wrap an LLM client for automatic trace capture. Returns a proxy with the same interface.

client = carrot_ai.wrap(OpenAI(), tags=["production"])
ParameterTypeDefaultDescription
clientOpenAI | AnthropicrequiredThe LLM client to wrap
tagslist[str]NoneDefault tags for every trace from this client

Supported clients: OpenAI, AsyncOpenAI, Anthropic, AsyncAnthropic

carrot_ai.patch_litellm()

Enable automatic tracing for litellm.completion and litellm.acompletion.

carrot_ai.patch_litellm(tags=["production"])
ParameterTypeDefaultDescription
tagslist[str]NoneDefault tags for every litellm trace

Safe to call multiple times.

@carrot_ai.trace

Decorator for capturing multi-step functions as parent traces.

@carrot_ai.trace
def my_function():
    ...

@carrot_ai.trace("custom-name")
def my_function():
    ...

@carrot_ai.trace(name="custom-name", tags=["prod"], metadata={"version": "2"})
def my_function():
    ...
ParameterTypeDefaultDescription
namestrFunction nameTrace name shown in the dashboard
tagslist[str]NoneTags for filtering
metadatadictNoneArbitrary key-value metadata

Works with both sync and async functions.

carrot_ai.flush()

Force-flush all pending traces. Blocks until complete.

carrot_ai.flush()

Call this before process exit in short-lived scripts. The SDK also flushes automatically on normal shutdown.

Environment variables

VariableDescription
CARROT_API_KEYAPI key (used if not passed to init())
CARROT_BASE_URLAPI base URL override

On this page