Setup
Install the Braintrust SDK and LiteLLM, then set your API keys for the providers you use. The examples below use OpenAI.1
Install packages
2
Set environment variables
.env
Auto-instrumentation
To trace LiteLLM without modifying your application code, callbraintrust.auto_instrument() before importing LiteLLM. This patches LiteLLM at startup, so calls are traced without per-call wiring.Trace only LiteLLM
Trace only LiteLLM
To trace LiteLLM without auto-instrumenting other libraries, use
patch_litellm() instead of braintrust.auto_instrument().Manual instrumentation
To trace a specific LiteLLM module instance manually, wrap it yourself withwrap_litellm(). Use this when you want to instrument a particular module reference rather than patching the globally-imported litellm.What Braintrust traces
Braintrust patches LiteLLM’s top-level call entry points and creates an LLM-typed span per call:- Completion spans (
Completion) forlitellm.completion,litellm.acompletion,litellm.text_completion, andlitellm.atext_completion, with messages or prompt, model, and request parameters; response choices, token usage, and time-to-first-token for streaming. - Responses API spans (
Response) forlitellm.responses/litellm.aresponses, with input and request parameters; response output, token usage, and time-to-first-token for streaming. - Image generation spans (
Image Generation) forlitellm.image_generation/litellm.aimage_generation, with prompt and request parameters; output capturing per-image data (attachment for base64 responses or URL reference for URL responses) plus metadata like output format, size, quality, and image count, plus timing and token usage when reported. - Embedding spans (
Embedding) forlitellm.embedding/litellm.aembedding, with input text and request parameters; output summarized as the embedding vector dimension (length of the first embedding), plus token usage. - Moderation spans (
Moderation) forlitellm.moderation/litellm.amoderation, with input and request parameters; classification results and token usage when reported. - Speech spans (
Speech) forlitellm.speech/litellm.aspeech, with text input and request parameters; generated audio captured as an attachment, plus timing. - Transcription spans (
Transcription) forlitellm.transcription/litellm.atranscription, with the input audio captured as an attachment plus model and request parameters; transcribed text and token usage. - Rerank spans (
Rerank) forlitellm.rerank/litellm.arerank, with query, documents, and request parameters (plus auto-deriveddocument_count); results as a list of{index, relevance_score}items (capped at 100, with documents intentionally dropped); token metrics (prompt, completion, total), plus Cohere-style billed-unit metrics (search units, classifications) when the response includes them. - Token usage metrics (prompt, completion, total, plus cached and reasoning tokens when the provider reports them).
- Errors captured on every call.
Resources
- LiteLLM documentation
- LiteLLM supported providers
- DSPy integration, which combines LiteLLM tracing with DSPy-specific callbacks