|
2 | 2 |
|
3 | 3 | [](https://github.com/botanu-ai/botanu-sdk-python/actions/workflows/ci.yml) |
4 | 4 | [](https://pypi.org/project/botanu/) |
| 5 | +[](https://pypi.org/project/botanu/) |
5 | 6 | [](https://scorecard.dev/viewer/?uri=github.com/botanu-ai/botanu-sdk-python) |
6 | 7 | [](LICENSE) |
7 | 8 |
|
8 | 9 | OpenTelemetry-native **run-level cost attribution** for AI workflows. |
9 | 10 |
|
10 | 11 | ## Overview |
11 | 12 |
|
12 | | -Botanu adds **runs** on top of distributed tracing. A run represents a single business execution that may span multiple traces, retries, and services. By correlating all spans to a stable `run_id`, you get accurate cost attribution without sampling artifacts. |
| 13 | +Botanu adds **runs** on top of distributed tracing. A run represents a single business transaction that may span multiple LLM calls, database queries, and services. By correlating all operations to a stable `run_id`, you get accurate cost attribution without sampling artifacts. |
| 14 | + |
| 15 | +**Key features:** |
| 16 | +- 🎯 **Run-level attribution** — Link all costs to business outcomes |
| 17 | +- 🔗 **Cross-service correlation** — W3C Baggage propagation |
| 18 | +- 📊 **OTel-native** — Works with any OpenTelemetry-compatible backend |
| 19 | +- ⚡ **Minimal overhead** — < 0.5ms per request |
| 20 | +- 🤖 **GenAI support** — OpenAI, Anthropic, Vertex AI, and more |
13 | 21 |
|
14 | 22 | ## Quick Start |
15 | 23 |
|
16 | 24 | ```python |
17 | 25 | from botanu import enable, botanu_use_case, emit_outcome |
18 | 26 |
|
| 27 | +# Initialize at startup |
19 | 28 | enable(service_name="my-app") |
20 | 29 |
|
21 | 30 | @botanu_use_case(name="Customer Support") |
22 | 31 | async def handle_ticket(ticket_id: str): |
23 | | - result = await process_ticket(ticket_id) |
| 32 | + # All operations inside get the same run_id |
| 33 | + context = await fetch_context(ticket_id) |
| 34 | + response = await generate_response(context) |
| 35 | + |
| 36 | + # Record the business outcome |
24 | 37 | emit_outcome("success", value_type="tickets_resolved", value_amount=1) |
25 | | - return result |
| 38 | + return response |
26 | 39 | ``` |
27 | 40 |
|
28 | 41 | ## Installation |
29 | 42 |
|
30 | 43 | ```bash |
31 | | -pip install botanu # Core (opentelemetry-api only) |
32 | | -pip install botanu[sdk] # + OTel SDK + OTLP exporter |
33 | | -pip install botanu[all] # Everything including GenAI instrumentation |
| 44 | +# Core SDK (opentelemetry-api only, ~50KB) |
| 45 | +pip install botanu |
| 46 | + |
| 47 | +# With OTel SDK + OTLP exporter (for standalone use) |
| 48 | +pip install "botanu[sdk]" |
| 49 | + |
| 50 | +# With GenAI provider instrumentation |
| 51 | +pip install "botanu[genai]" |
| 52 | + |
| 53 | +# Everything included |
| 54 | +pip install "botanu[all]" |
| 55 | +``` |
| 56 | + |
| 57 | +### Extras |
| 58 | + |
| 59 | +| Extra | Description | |
| 60 | +|-------|-------------| |
| 61 | +| `sdk` | OpenTelemetry SDK + OTLP HTTP exporter | |
| 62 | +| `instruments` | Auto-instrumentation for HTTP, databases, etc. | |
| 63 | +| `genai` | GenAI provider instrumentation (OpenAI, Anthropic, etc.) | |
| 64 | +| `carriers` | Cross-service propagation helpers (Celery, Kafka) | |
| 65 | +| `all` | All of the above | |
| 66 | +| `dev` | Development and testing tools | |
| 67 | + |
| 68 | +## LLM Tracking |
| 69 | + |
| 70 | +Track LLM calls with full cost attribution: |
| 71 | + |
| 72 | +```python |
| 73 | +from botanu.tracking.llm import track_llm_call |
| 74 | + |
| 75 | +with track_llm_call(provider="openai", model="gpt-4") as tracker: |
| 76 | + response = await openai.chat.completions.create( |
| 77 | + model="gpt-4", |
| 78 | + messages=[{"role": "user", "content": "Hello"}] |
| 79 | + ) |
| 80 | + tracker.set_tokens( |
| 81 | + input_tokens=response.usage.prompt_tokens, |
| 82 | + output_tokens=response.usage.completion_tokens, |
| 83 | + ) |
| 84 | +``` |
| 85 | + |
| 86 | +## Data Tracking |
| 87 | + |
| 88 | +Track database and storage operations: |
| 89 | + |
| 90 | +```python |
| 91 | +from botanu.tracking.data import track_db_operation, track_storage_operation |
| 92 | + |
| 93 | +# Database |
| 94 | +with track_db_operation(system="postgresql", operation="SELECT") as db: |
| 95 | + result = await cursor.execute(query) |
| 96 | + db.set_result(rows_returned=len(result)) |
| 97 | + |
| 98 | +# Storage |
| 99 | +with track_storage_operation(system="s3", operation="PUT") as storage: |
| 100 | + await s3.put_object(Bucket="bucket", Key="key", Body=data) |
| 101 | + storage.set_result(bytes_written=len(data)) |
| 102 | +``` |
| 103 | + |
| 104 | +## Architecture |
| 105 | + |
| 106 | +``` |
| 107 | +┌──────────────────────────────────────────────────────────────┐ |
| 108 | +│ Your Application │ |
| 109 | +│ │ |
| 110 | +│ @botanu_use_case track_llm_call() track_db_operation()│ |
| 111 | +│ │ │ │ │ |
| 112 | +│ └───────────────────┴────────────────────┘ │ |
| 113 | +│ │ │ |
| 114 | +│ Botanu SDK (thin) │ |
| 115 | +│ - Generate run_id (UUIDv7) │ |
| 116 | +│ - Set W3C Baggage │ |
| 117 | +│ - Record spans │ |
| 118 | +└─────────────────────────────┬─────────────────────────────────┘ |
| 119 | + │ OTLP |
| 120 | + ▼ |
| 121 | +┌──────────────────────────────────────────────────────────────┐ |
| 122 | +│ OpenTelemetry Collector │ |
| 123 | +│ │ |
| 124 | +│ - PII redaction - Cost calculation │ |
| 125 | +│ - Vendor normalization - Cardinality management │ |
| 126 | +└──────────────────────────────────────────────────────────────┘ |
34 | 127 | ``` |
35 | 128 |
|
36 | 129 | ## Documentation |
37 | 130 |
|
38 | | -Full documentation is available at [docs.botanu.ai](https://docs.botanu.ai) and in the [`docs/`](./docs/) folder. |
| 131 | +Full documentation is available at [docs.botanu.ai](https://docs.botanu.ai) and in the [`docs/`](./docs/) folder: |
| 132 | + |
| 133 | +- [Getting Started](./docs/getting-started/) |
| 134 | +- [Concepts](./docs/concepts/) |
| 135 | +- [Tracking Guides](./docs/tracking/) |
| 136 | +- [Integration](./docs/integration/) |
| 137 | +- [API Reference](./docs/api/) |
| 138 | + |
| 139 | +## Requirements |
| 140 | + |
| 141 | +- Python 3.9+ |
| 142 | +- OpenTelemetry Collector (for production use) |
39 | 143 |
|
40 | 144 | ## Contributing |
41 | 145 |
|
42 | 146 | See [CONTRIBUTING.md](./CONTRIBUTING.md). This project uses [DCO](./DCO) sign-off. |
43 | 147 |
|
| 148 | +```bash |
| 149 | +git commit -s -m "Your commit message" |
| 150 | +``` |
| 151 | + |
44 | 152 | ## License |
45 | 153 |
|
46 | 154 | [Apache-2.0](./LICENSE) — see [NOTICE](./NOTICE) for attribution. |
| 155 | + |
| 156 | +This project is an [LF AI & Data Foundation](https://lfaidata.foundation/) project. |
0 commit comments