Metadata-Version: 2.4
Name: blockwatch
Version: 2.0.0
Summary: OpenTelemetry + OpenInference telemetry for multi-agent collaboration and orchestration
License: MIT
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: opentelemetry-sdk>=1.27.0
Requires-Dist: opentelemetry-exporter-otlp-proto-http>=1.27.0
Provides-Extra: bedrock
Requires-Dist: boto3>=1.26.0; extra == "bedrock"
Requires-Dist: openinference-instrumentation-bedrock>=0.1.0; extra == "bedrock"
Provides-Extra: langgraph
Requires-Dist: openinference-instrumentation-langchain>=0.1.0; extra == "langgraph"
Provides-Extra: crewai
Requires-Dist: openinference-instrumentation-crewai>=1.0.0; extra == "crewai"
Provides-Extra: autogen
Requires-Dist: openinference-instrumentation-ag2>=0.1.0; extra == "autogen"
Dynamic: description
Dynamic: description-content-type
Dynamic: license
Dynamic: license-file
Dynamic: provides-extra
Dynamic: requires-dist
Dynamic: requires-python
Dynamic: summary

# BlockWatch Python SDK

[![PyPI](https://img.shields.io/pypi/v/blockwatch.svg)](https://pypi.org/project/blockwatch/)
[![Python](https://img.shields.io/pypi/pyversions/blockwatch.svg)](https://pypi.org/project/blockwatch/)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)

**Agentic Trust Authority** — instrument multi-agent handoffs, detect runtime failures (loops, leaks, token burn), and stream session telemetry to BlockWatch without blocking your agents.

Built on **OpenTelemetry** and **OpenInference**: every framework integration is a standard OpenInference instrumentor, and every byte on the wire is a standard OTLP/HTTP span batch. BlockWatch does not invent a proprietary trace format — it is just one more OTLP-compliant destination for spans your agent framework already knows how to produce.

<!-- Replace with your dashboard asset:
![BlockWatch handoff graph](docs/assets/handoff-graph.gif)
-->

## Install

```bash
pip install blockwatch[bedrock]     # Amazon Bedrock Agents (boto3 + OpenInference Bedrock instrumentor)
pip install blockwatch[langgraph]   # LangGraph (OpenInference LangChain instrumentor)
pip install blockwatch[crewai]      # CrewAI (OpenInference CrewAI instrumentor)
pip install blockwatch[autogen]     # AutoGen / AG2 (OpenInference AG2 instrumentor)
```

Install the **agent framework** you use in the same environment (`langgraph`, `crewai`, `boto3`, `ag2`, etc.) if it is not already a dependency of your project. Each extra above only adds the matching OpenInference instrumentation package.

## Quick start (Bedrock)

```bash
pip install blockwatch[bedrock]
```

```python
import os
import blockwatch
import boto3

blockwatch.init(
    api_key=os.environ["BLOCKWATCH_API_KEY"],
    framework="bedrock",
)

client = boto3.client("bedrock-agent-runtime", region_name="us-east-1")

response = client.invoke_agent(
    agentId=os.environ["BEDROCK_AGENT_ID"],
    agentAliasId=os.environ["BEDROCK_AGENT_ALIAS_ID"],
    sessionId="your-session-id",
    inputText="Hello",
)

for event in response.get("completion", []):
    if "chunk" in event:
        print(event["chunk"]["bytes"].decode(), end="")
```

Call `init()` **once**, before creating the Bedrock client. The OpenInference Bedrock instrumentor patches boto3 client creation globally, so clients created afterward are automatically traced.

Copy [`.env.example`](.env.example) for local runs. Runnable samples: [`examples/`](examples/) (Bedrock, LangGraph, CrewAI, AutoGen/AG2).

## Core pillars

| Pillar | What BlockWatch does |
|--------|----------------------|
| **Privacy** | Captures OpenInference spans (`input.value` / `output.value` / tool call attributes), not raw application logs. Review what your spans contain before production; server-side masking runs on every ingested trace. |
| **Efficiency** | Surfaces loops, leaks, and stuck handoffs on the BlockWatch platform using the full span parent/child tree. The SDK batches up to **50 spans** or **1 second** per export (standard OTel `BatchSpanProcessor` behavior). |
| **Cost** | Correlates token usage and burn patterns from OpenInference LLM span attributes (`llm.token_count.*`) so you can spot runaway agents early. |

## Framework support

| Framework | SDK status | OpenInference instrumentor |
|-----------|------------|------------------------------|
| **Amazon Bedrock** (`bedrock-agent-runtime`) | **Supported** — install `blockwatch[bedrock]` | `openinference-instrumentation-bedrock` |
| **LangGraph** | **Supported** — install `blockwatch[langgraph]` + `langgraph` | `openinference-instrumentation-langchain` (LangGraph runs on LangChain's runnable/callback stack) |
| **CrewAI** | **Supported** — install `blockwatch[crewai]` + `crewai` | `openinference-instrumentation-crewai` |
| **AutoGen** | **Supported** — install `blockwatch[autogen]` + `ag2` | `openinference-instrumentation-ag2` (AutoGen was renamed/forked to AG2 upstream) |

See [ROADMAP.md](ROADMAP.md) for architecture details and backend normalization.

### LangGraph

```python
import os
import blockwatch

blockwatch.init(api_key=os.environ["BLOCKWATCH_API_KEY"], framework="langgraph")

# Call init() before compiling or invoking any graph.
for chunk in compiled.stream(input, stream_mode="updates"):
    ...
```

### CrewAI

```python
import os
import blockwatch

blockwatch.init(api_key=os.environ["BLOCKWATCH_API_KEY"], framework="crewai")

# Call init() before constructing Crew/Agent/Task objects.
result = crew.kickoff()
```

## How it works

- `blockwatch.init(api_key, framework)` builds one process-wide OpenTelemetry `TracerProvider` and registers the matching OpenInference instrumentor for `framework`.
- The instrumentor patches your framework's own entry points (boto3 client creation for Bedrock, LangChain's callback manager for LangGraph, `Crew.kickoff` for CrewAI, AG2's agent/chat hooks for AutoGen) so spans are produced automatically — there is no manual trace construction in this SDK.
- A standard `BatchSpanProcessor` batches up to 50 spans or 1 second of wall time, whichever comes first.
- A standard `OTLPSpanExporter` (`opentelemetry-exporter-otlp-proto-http`) gzip-compresses and POSTs OTLP/HTTP protobuf span batches to `https://api.blockwatch.ai/telemetry`.
- API key and framework identifier are sent as request headers (`X-BlockWatch-Api-Key`, `X-BlockWatch-Framework`), never in the payload or query string.
- `atexit` registers a best-effort flush (`TracerProvider.force_flush` + `shutdown()`, ~2s budget) on process exit so in-flight spans are not silently dropped.
- Every internal operation is defensive: `init()` failures, missing optional dependencies, and network errors are swallowed so BlockWatch can never crash or block your agent process.

## AWS Lambda

**`requirements.txt`**

```text
blockwatch[bedrock]>=2.0.0
```

```python
import os
import blockwatch

blockwatch.init(
    api_key=os.environ["BLOCKWATCH_API_KEY"],
    framework="bedrock",
)
```

Build the layer on Amazon Linux:

```bash
pip install -r requirements.txt -t python/
```

## API

| Function | Description |
|----------|-------------|
| `blockwatch.init(api_key, framework)` | Start telemetry. `framework`: `bedrock`, `langgraph`, `crewai`, or `autogen`. |

## Requirements

- Python **3.9+**
- Core dependencies: `opentelemetry-sdk`, `opentelemetry-exporter-otlp-proto-http` (always installed with `blockwatch`)
- **Bedrock:** `boto3` + `openinference-instrumentation-bedrock` via `pip install blockwatch[bedrock]`
- **LangGraph:** `langgraph` (install separately) + `openinference-instrumentation-langchain` via `pip install blockwatch[langgraph]`
- **CrewAI:** `crewai` (install separately) + `openinference-instrumentation-crewai` via `pip install blockwatch[crewai]`
- **AutoGen:** `ag2` (install separately) + `openinference-instrumentation-ag2` via `pip install blockwatch[autogen]`

## Contributing

1. Open an [issue](https://github.com/innogine/blockwatch-sdk/issues) or pull request.
2. Do **not** commit API keys, PyPI tokens, or customer IDs — use environment variables only.
3. Integration tests (optional): `pytest tests/ -m integration` with `BLOCKWATCH_API_KEY`, `BEDROCK_AGENT_ID`, and `BEDROCK_AGENT_ALIAS_ID` set.

## Security

Report vulnerabilities via GitHub **Security Advisories** (preferred) or a private channel to your BlockWatch contact. Rotate any key that may have been exposed in logs or commits.

## License

MIT — see [LICENSE](LICENSE). Copyright (c) 2026 INNOGINE.
