The reference implementation.
pip install spanforge — zero required dependencies, all 15 spanforge namespaces, a full CLI, and integrations for OpenAI, Anthropic, LangChain, LangGraph, LlamaIndex, CrewAI, Datadog, and more.
Get started in one command.
Install the base SDK with no required dependencies. Add optional extras for the integrations and export targets you need.
Verify the installation:
spanforge --version # spanforge 1.0.4
Optional extras
| Extra | What it adds |
|---|---|
| spanforge[jsonschema] | Enables runtime schema validation of every emitted event against the published JSON Schema. |
| spanforge[identity] | SFIdentityClient RS256 JWT support via cryptography; required when connecting to a remote sf-identity service (local mode uses stdlib HS256). |
| spanforge[openai] | Auto-instrumentation for the openai Python client — wraps chat completions, embeddings, and responses. Includes unified pricing table covering OpenAI, Anthropic, Groq, and Together AI. |
| spanforge[anthropic] | Anthropic Claude SDK integration — instrumented Claude calls with cost normalisation and cross-provider trace context. |
| spanforge[gemini] | Google Gemini SDK integration — instrumented Gemini calls with full trace context. |
| spanforge[bedrock] | AWS Bedrock integration — instrumented Bedrock model calls. |
| spanforge[ollama] | Ollama local model integration — instrumented local inference calls. |
| spanforge[groq] | Groq inference integration — high-speed inference tracing with cost normalisation. |
| spanforge[together] | Together AI integration — instrumented Together AI model calls. |
| spanforge[http] | OTLP/HTTP export and webhook export targets. |
| spanforge[pydantic] | Pydantic v2 model support for typed payload validation. |
| spanforge[otel] | Full OpenTelemetry SDK integration — exports spans as OTLP proto via gRPC or HTTP. |
| spanforge[kafka] | Apache Kafka export backend for streaming event pipelines. |
| spanforge[langchain] | LangChain callback handler — instruments chain, tool, and LLM calls automatically. |
| spanforge[llamaindex] | LlamaIndex event handler — instruments query, retrieval, and LLM calls. |
| spanforge[crewai] | CrewAI task-and-agent lifecycle instrumentation. |
| spanforge[presidio] | Presidio NLP PII backend — 15 entity types (SSN, email, phone, AADHAAR, PAN, UK NI, credit card, IBAN, and more) with ≥ 95% true-positive rate and < 0.5% false-positive rate. Requires python -m spacy download en_core_web_lg. |
| spanforge[redis] | RedisExporter and RedisBackend for semantic cache. |
| spanforge[compliance] | Extended compliance mapping dependencies for framework clause engines. |
| spanforge[worm-s3] | Append-only S3 export backend with Object Lock (WORM) for tamper-proof audit storage. |
| spanforge[worm-gcs] | Append-only GCS export backend (WORM) for tamper-proof audit storage. |
| spanforge[datadog] | Datadog exporter — ships events to DD APM as custom spans. |
| spanforge[all] | Installs all optional extras in a single command. |
Your first event.
Every event in the spanforge standard is an Event object with three required arguments: event_type, source, and payload.
from spanforge import Event, EventType
# Minimal event
event = Event(
event_type=EventType.TRACE_SPAN_COMPLETED,
source="my-service@0.1.0",
payload={"span_name": "summarise", "status": "ok"},
)
# Event is ready to emit or inspect
print(event.event_id) # ULID e.g. 01HZQSN7PQVR2K4M0BXJD3GSTA
print(event.timestamp) # ISO-8601 UTC
print(event.trace_id) # W3C-compatible 128-bit hexTyped namespace payloads
Use the typed payload classes from each namespace for full schema validation and IDE autocompletion. The classes map 1-to-1 to the published JSON Schema.
from spanforge import Event, EventType
from spanforge.namespaces.trace import SpanPayload, TokenUsage, ModelInfo, GenAISystem
event = Event(
event_type=EventType.TRACE_SPAN_COMPLETED,
source="spanforge@1.0.0",
payload=SpanPayload(
span_name="summarise_document",
span_kind="LLM",
status="ok",
duration_ms=830,
token_usage=TokenUsage(input_tokens=411, output_tokens=128, total_tokens=539),
model_info=ModelInfo(system=GenAISystem.OPENAI, name="gpt-4o"),
).to_dict(),
tags=["prod", "summarisation"],
)
# Export to OTLP
from spanforge.export import OTLPExporter
exporter = OTLPExporter(endpoint="http://localhost:4317")
exporter.export(event)Works with your existing stack.
Install the relevant extra and add one line of code. spanforge events are emitted automatically — you retain full compliance instrumentation without rewriting your existing code.
OpenAI
One-line auto-instrumentation for the openai Python client. Wraps chat completions, embeddings, and the Responses API. Emits llm.trace.span and llm.cost.* events automatically.
Anthropic
Anthropic Claude SDK integration with cross-provider cost normalisation and full trace context propagation.
Azure OpenAI
Instance-level instrumentation for Azure-hosted OpenAI clients via spanforge.integrations.azure_openai — token, model, and cost telemetry attached automatically to active spans.
LangChain
Drop-in callback handler. Instruments chain invocations, tool calls, and LLM calls. Compatible with LangChain v0.1 and v0.2.
LangGraph
Governance-aware agent graph instrumentation — node-level scope, RBAC, and lineage coverage for agentic workflow tracing.
LlamaIndex
Event handler for the LlamaIndex instrumentation API. Captures query, retrieval, embedding, and LLM events with full trace context.
CrewAI
Task and agent lifecycle instrumentation for CrewAI. Captures agent run start/end, task assignment, and tool delegation events.
Datadog
Export spanforge span events as custom Datadog APM spans. Compatible with the Datadog Agent and the dd-trace-py client.
OpenTelemetry
Full OTLP bridge. Converts spanforge span events to OpenTelemetry proto-compatible dicts and exports via gRPC or HTTP. Works with any OTEL-compatible backend.
Operational tooling included.
The spanforge CLI is installed automatically with the SDK. Use it for health checks, schema validation, audit-chain verification, compliance evidence generation, and PII scanning.
# End-to-end health check spanforge check # [1/5] Config ............. OK # [2/5] Event creation ..... OK # [3/5] Schema validation .. OK # [4/5] Export pipeline .... OK # [5/5] Trace store ........ OK # All checks passed. # Validate a JSONL event stream spanforge validate production-events.jsonl # Scan for PII before export spanforge scan production-events.jsonl # Generate compliance evidence package spanforge compliance production-events.jsonl --framework eu-ai-act
SDK instrumentation linter.
The built-in linter parses your Python source with ast and runs five checks to catch common instrumentation mistakes before they reach production. Integrates natively with flake8 and ruff.
Inspect. Validate. Ship.
Use SpanForge Debug to inspect and replay compliance traces, and SpanForge Validate to enforce schema compliance in your CI pipeline.