|
| 1 | +# Braintrust Python SDK Examples |
| 2 | + |
| 3 | +Each subdirectory in this folder is a self-contained [`uv`](https://docs.astral.sh/uv/) project demonstrating one Braintrust integration or feature. They are designed to be cloned, copied, or run as-is without affecting the rest of the repository. |
| 4 | + |
| 5 | +## Layout |
| 6 | + |
| 7 | +Every example has the same shape: |
| 8 | + |
| 9 | +``` |
| 10 | +examples/<name>/ |
| 11 | +├── pyproject.toml # declares deps + a local `path` source for braintrust |
| 12 | +├── README.md # what it shows + how to run it |
| 13 | +└── *.py # the example script(s) |
| 14 | +``` |
| 15 | + |
| 16 | +The `pyproject.toml` in each example pins `braintrust` to the local SDK checkout in `py/` via: |
| 17 | + |
| 18 | +```toml |
| 19 | +[tool.uv.sources] |
| 20 | +braintrust = { path = "../../py", editable = true } |
| 21 | +``` |
| 22 | + |
| 23 | +That means `uv sync` inside any example installs the version of the SDK currently on disk — edits to `py/src/braintrust/` are picked up immediately. |
| 24 | + |
| 25 | +## Running an example |
| 26 | + |
| 27 | +From the repo root: |
| 28 | + |
| 29 | +```bash |
| 30 | +cd examples/<name> |
| 31 | +uv sync |
| 32 | +uv run python <script>.py |
| 33 | +``` |
| 34 | + |
| 35 | +You will need a `BRAINTRUST_API_KEY` in your environment, plus whatever provider key the example uses (`OPENAI_API_KEY`, `ANTHROPIC_API_KEY`, etc.). Each example's README lists its requirements. |
| 36 | + |
| 37 | +## The default pattern: `auto_instrument()` |
| 38 | + |
| 39 | +Most provider/framework examples follow the same two-line setup: |
| 40 | + |
| 41 | +```python |
| 42 | +import braintrust |
| 43 | + |
| 44 | +braintrust.auto_instrument() |
| 45 | +braintrust.init_logger(project="example-<name>") |
| 46 | + |
| 47 | +# now import and use the library normally |
| 48 | +``` |
| 49 | + |
| 50 | +`braintrust.auto_instrument()` patches every supported integration that's installed in the current environment — there's no need to call individual `setup_*()` or `wrap_*()` helpers. The exceptions are examples that demonstrate non-tracing concerns (`evals/`, `langsmith/`, `otel/`, `temporal/`). |
| 51 | + |
| 52 | +If you want to see `auto_instrument()` light up multiple providers in one trace, the `openai/`, `anthropic/`, and any other provider example can be combined trivially — just install both packages in the same environment, call `auto_instrument()` once, and use both clients. |
| 53 | + |
| 54 | +## Available examples |
| 55 | + |
| 56 | +Unless noted otherwise, every example below uses `braintrust.auto_instrument()`. |
| 57 | + |
| 58 | +| Directory | What it shows | |
| 59 | +| --- | --- | |
| 60 | +| `adk/` | Google ADK weather agent with one tool | |
| 61 | +| `agentscope/` | AgentScope `ReActAgent` against `gpt-4o-mini` | |
| 62 | +| `agno/` | Agno agents and teams, sync + async, streaming + non-streaming | |
| 63 | +| `anthropic/` | Sync and async Anthropic clients | |
| 64 | +| `autogen/` | AutoGen `AssistantAgent` backed by `OpenAIChatCompletionClient` | |
| 65 | +| `claude_agent_sdk/` | Claude Agent SDK subprocess query | |
| 66 | +| `cohere/` | Cohere `ClientV2` chat call | |
| 67 | +| `crewai/` | CrewAI `Agent` + `Task` + `Crew` end-to-end | |
| 68 | +| `dspy/` | DSPy `ReAct` agent with two tools (LiteLLM token metrics propagate) | |
| 69 | +| `evals/` | The `Eval` framework — does **not** use `auto_instrument()` | |
| 70 | +| `google_genai/` | Google GenAI `generate_content` against Gemini | |
| 71 | +| `langchain/` | LangChain `prompt | model` chain — global handler installed by `auto_instrument()` | |
| 72 | +| `langsmith/` | Migration helper for projects coming from LangSmith — uses `setup_langsmith()` | |
| 73 | +| `litellm/` | LiteLLM `completion` | |
| 74 | +| `llamaindex/` | LlamaIndex LLM completion | |
| 75 | +| `mistral/` | Mistral chat completion | |
| 76 | +| `openai/` | OpenAI chat completion plus `@braintrust.traced` for the surrounding function | |
| 77 | +| `openai_agents/` | OpenAI Agents SDK `Runner` running an agent | |
| 78 | +| `openrouter/` | OpenRouter chat completion routed to OpenAI | |
| 79 | +| `otel/` | OpenTelemetry interop — `BraintrustSpanProcessor`, filtering, distributed tracing | |
| 80 | +| `pydantic_ai/` | Pydantic AI agent run inside a `start_span` for a permalink | |
| 81 | +| `strands/` | Strands `Agent` against `gpt-4o-mini` | |
| 82 | +| `temporal/` | Distributed Temporal workflow tracing via `BraintrustPlugin` | |
| 83 | + |
| 84 | +## Why no committed `uv.lock`? |
| 85 | + |
| 86 | +Examples are meant to demonstrate the **current** SDK against **current** provider releases, not snapshot a specific point in time. Committing lockfiles would create constant churn (a new lockfile for every transitive bump on PyPI) and would actively work against the demo goal — someone running an example next quarter wants it to work with whatever's on PyPI today, not a 3-month-old pin. Lockfiles are gitignored. |
| 87 | + |
| 88 | +If you want a reproducible snapshot for your own use, run `uv lock` locally; it just won't be checked in. |
| 89 | + |
| 90 | +## Adding a new example |
| 91 | + |
| 92 | +1. Create `examples/<name>/` with the layout above. |
| 93 | +2. In `pyproject.toml`, declare deps under `[project] dependencies` and add the local `braintrust` source under `[tool.uv.sources]` (see any existing example). |
| 94 | +3. Write a short README explaining required env vars and how to run it. |
| 95 | +4. Verify with `uv sync && uv run python <script>.py` from inside the example dir. |
| 96 | +5. The `pylint` nox session in `py/noxfile.py` automatically lints `examples/`, so any imports your example needs must be in the `lint` dependency group of `py/pyproject.toml`. |
0 commit comments