Skip to content

Commit eabced3

Browse files
authored
Add braintrust docs (#3628)
* Add braintrust docs * Add more things * fix eval command * Add missing crewai-tools import * Allow for dynamic inputs
1 parent b77074e commit eabced3

File tree

7 files changed

+713
-0
lines changed

7 files changed

+713
-0
lines changed

docs/docs.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@
248248
"pages": [
249249
"en/observability/overview",
250250
"en/observability/arize-phoenix",
251+
"en/observability/braintrust",
251252
"en/observability/langdb",
252253
"en/observability/langfuse",
253254
"en/observability/langtrace",
@@ -623,6 +624,7 @@
623624
"pages": [
624625
"pt-BR/observability/overview",
625626
"pt-BR/observability/arize-phoenix",
627+
"pt-BR/observability/braintrust",
626628
"pt-BR/observability/langdb",
627629
"pt-BR/observability/langfuse",
628630
"pt-BR/observability/langtrace",
@@ -1007,6 +1009,7 @@
10071009
"pages": [
10081010
"ko/observability/overview",
10091011
"ko/observability/arize-phoenix",
1012+
"ko/observability/braintrust",
10101013
"ko/observability/langdb",
10111014
"ko/observability/langfuse",
10121015
"ko/observability/langtrace",
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
---
2+
title: Braintrust
3+
description: Braintrust integration for CrewAI with OpenTelemetry tracing and evaluation
4+
icon: magnifying-glass-chart
5+
mode: "wide"
6+
---
7+
8+
# Braintrust Integration
9+
10+
This guide demonstrates how to integrate **Braintrust** with **CrewAI** using OpenTelemetry for comprehensive tracing and evaluation. By the end of this guide, you will be able to trace your CrewAI agents, monitor their performance, and evaluate their outputs using Braintrust's powerful observability platform.
11+
12+
> **What is Braintrust?** [Braintrust](https://www.braintrust.dev) is an AI evaluation and observability platform that provides comprehensive tracing, evaluation, and monitoring for AI applications with built-in experiment tracking and performance analytics.
13+
14+
## Get Started
15+
16+
We'll walk through a simple example of using CrewAI and integrating it with Braintrust via OpenTelemetry for comprehensive observability and evaluation.
17+
18+
### Step 1: Install Dependencies
19+
20+
```bash
21+
uv add braintrust[otel] crewai crewai-tools opentelemetry-instrumentation-openai opentelemetry-instrumentation-crewai python-dotenv
22+
```
23+
24+
### Step 2: Set Up Environment Variables
25+
26+
Setup Braintrust API keys and configure OpenTelemetry to send traces to Braintrust. You'll need a Braintrust API key and your OpenAI API key.
27+
28+
```python
29+
import os
30+
from getpass import getpass
31+
32+
# Get your Braintrust credentials
33+
BRAINTRUST_API_KEY = getpass("🔑 Enter your Braintrust API Key: ")
34+
35+
# Get API keys for services
36+
OPENAI_API_KEY = getpass("🔑 Enter your OpenAI API key: ")
37+
38+
# Set environment variables
39+
os.environ["BRAINTRUST_API_KEY"] = BRAINTRUST_API_KEY
40+
os.environ["BRAINTRUST_PARENT"] = "project_name:crewai-demo"
41+
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
42+
```
43+
44+
### Step 3: Initialize OpenTelemetry with Braintrust
45+
46+
Initialize the Braintrust OpenTelemetry instrumentation to start capturing traces and send them to Braintrust.
47+
48+
```python
49+
import os
50+
from typing import Any, Dict
51+
52+
from braintrust.otel import BraintrustSpanProcessor
53+
from crewai import Agent, Crew, Task
54+
from crewai.llm import LLM
55+
from opentelemetry import trace
56+
from opentelemetry.instrumentation.crewai import CrewAIInstrumentor
57+
from opentelemetry.instrumentation.openai import OpenAIInstrumentor
58+
from opentelemetry.sdk.trace import TracerProvider
59+
60+
def setup_tracing() -> None:
61+
"""Setup OpenTelemetry tracing with Braintrust."""
62+
current_provider = trace.get_tracer_provider()
63+
if isinstance(current_provider, TracerProvider):
64+
provider = current_provider
65+
else:
66+
provider = TracerProvider()
67+
trace.set_tracer_provider(provider)
68+
69+
provider.add_span_processor(BraintrustSpanProcessor())
70+
CrewAIInstrumentor().instrument(tracer_provider=provider)
71+
OpenAIInstrumentor().instrument(tracer_provider=provider)
72+
73+
74+
setup_tracing()
75+
```
76+
77+
### Step 4: Create a CrewAI Application
78+
79+
We'll create a CrewAI application where two agents collaborate to research and write a blog post about AI advancements, with comprehensive tracing enabled.
80+
81+
```python
82+
from crewai import Agent, Crew, Process, Task
83+
from crewai_tools import SerperDevTool
84+
85+
def create_crew() -> Crew:
86+
"""Create a crew with multiple agents for comprehensive tracing."""
87+
llm = LLM(model="gpt-4o-mini")
88+
search_tool = SerperDevTool()
89+
90+
# Define agents with specific roles
91+
researcher = Agent(
92+
role="Senior Research Analyst",
93+
goal="Uncover cutting-edge developments in AI and data science",
94+
backstory="""You work at a leading tech think tank.
95+
Your expertise lies in identifying emerging trends.
96+
You have a knack for dissecting complex data and presenting actionable insights.""",
97+
verbose=True,
98+
allow_delegation=False,
99+
llm=llm,
100+
tools=[search_tool],
101+
)
102+
103+
writer = Agent(
104+
role="Tech Content Strategist",
105+
goal="Craft compelling content on tech advancements",
106+
backstory="""You are a renowned Content Strategist, known for your insightful and engaging articles.
107+
You transform complex concepts into compelling narratives.""",
108+
verbose=True,
109+
allow_delegation=True,
110+
llm=llm,
111+
)
112+
113+
# Create tasks for your agents
114+
research_task = Task(
115+
description="""Conduct a comprehensive analysis of the latest advancements in {topic}.
116+
Identify key trends, breakthrough technologies, and potential industry impacts.""",
117+
expected_output="Full analysis report in bullet points",
118+
agent=researcher,
119+
)
120+
121+
writing_task = Task(
122+
description="""Using the insights provided, develop an engaging blog
123+
post that highlights the most significant {topic} advancements.
124+
Your post should be informative yet accessible, catering to a tech-savvy audience.
125+
Make it sound cool, avoid complex words so it doesn't sound like AI.""",
126+
expected_output="Full blog post of at least 4 paragraphs",
127+
agent=writer,
128+
context=[research_task],
129+
)
130+
131+
# Instantiate your crew with a sequential process
132+
crew = Crew(
133+
agents=[researcher, writer],
134+
tasks=[research_task, writing_task],
135+
verbose=True,
136+
process=Process.sequential
137+
)
138+
139+
return crew
140+
141+
def run_crew():
142+
"""Run the crew and return results."""
143+
crew = create_crew()
144+
result = crew.kickoff(inputs={"topic": "AI developments"})
145+
return result
146+
147+
# Run your crew
148+
if __name__ == "__main__":
149+
# Instrumentation is already initialized above in this module
150+
result = run_crew()
151+
print(result)
152+
```
153+
154+
### Step 5: View Traces in Braintrust
155+
156+
After running your crew, you can view comprehensive traces in Braintrust through different perspectives:
157+
158+
<Tabs>
159+
<Tab title="Trace">
160+
<Frame>
161+
<img src="/images/braintrust-trace-view.png" alt="Braintrust Trace View"/>
162+
</Frame>
163+
</Tab>
164+
165+
<Tab title="Timeline">
166+
<Frame>
167+
<img src="/images/braintrust-timeline-view.png" alt="Braintrust Timeline View"/>
168+
</Frame>
169+
</Tab>
170+
171+
<Tab title="Thread">
172+
<Frame>
173+
<img src="/images/braintrust-thread-view.png" alt="Braintrust Thread View"/>
174+
</Frame>
175+
</Tab>
176+
</Tabs>
177+
178+
### Step 6: Evaluate via SDK (Experiments)
179+
180+
You can also run evaluations using Braintrust's Eval SDK. This is useful for comparing versions or scoring outputs offline. Below is a Python example using the `Eval` class with the crew we created above:
181+
182+
```python
183+
# eval_crew.py
184+
from braintrust import Eval
185+
from autoevals import Levenshtein
186+
187+
def evaluate_crew_task(input_data):
188+
"""Task function that wraps our crew for evaluation."""
189+
crew = create_crew()
190+
result = crew.kickoff(inputs={"topic": input_data["topic"]})
191+
return str(result)
192+
193+
Eval(
194+
"AI Research Crew", # Project name
195+
{
196+
"data": lambda: [
197+
{"topic": "artificial intelligence trends 2024"},
198+
{"topic": "machine learning breakthroughs"},
199+
{"topic": "AI ethics and governance"},
200+
],
201+
"task": evaluate_crew_task,
202+
"scores": [Levenshtein],
203+
},
204+
)
205+
```
206+
207+
Setup your API key and run:
208+
209+
```bash
210+
export BRAINTRUST_API_KEY="YOUR_API_KEY"
211+
braintrust eval eval_crew.py
212+
```
213+
214+
See the [Braintrust Eval SDK guide](https://www.braintrust.dev/docs/start/eval-sdk) for more details.
215+
216+
### Key Features of Braintrust Integration
217+
218+
- **Comprehensive Tracing**: Track all agent interactions, tool usage, and LLM calls
219+
- **Performance Monitoring**: Monitor execution times, token usage, and success rates
220+
- **Experiment Tracking**: Compare different crew configurations and models
221+
- **Automated Evaluation**: Set up custom evaluation metrics for crew outputs
222+
- **Error Tracking**: Monitor and debug failures across your crew executions
223+
- **Cost Analysis**: Track token usage and associated costs
224+
225+
### Version Compatibility Information
226+
- Python 3.8+
227+
- CrewAI >= 0.86.0
228+
- Braintrust >= 0.1.0
229+
- OpenTelemetry SDK >= 1.31.0
230+
231+
### References
232+
- [Braintrust Documentation](https://www.braintrust.dev/docs) - Overview of the Braintrust platform
233+
- [Braintrust CrewAI Integration](https://www.braintrust.dev/docs/integrations/crew-ai) - Official CrewAI integration guide
234+
- [Braintrust Eval SDK](https://www.braintrust.dev/docs/start/eval-sdk) - Run experiments via the SDK
235+
- [CrewAI Documentation](https://docs.crewai.com/) - Overview of the CrewAI framework
236+
- [OpenTelemetry Docs](https://opentelemetry.io/docs/) - OpenTelemetry guide
237+
- [Braintrust GitHub](https://github.com/braintrustdata/braintrust) - Source code for Braintrust SDK
302 KB
Loading
151 KB
Loading
337 KB
Loading

0 commit comments

Comments
 (0)