Skip to content

Python SDK

ctxflow-otel is an OpenTelemetry distribution for Python. On top of the OpenTelemetry SDK it adds:

  • identity from baggage on every span (conversation, user, turn), also across MCP calls;
  • retrieval identity (C4): the id and text hash of every retrieved item, on the retrieval span;
  • ingestion spans (ingest.*) for LlamaIndex, Unstructured and Docling;
  • a check command, ctxflow-otel check, for your key and endpoint.

Node.js and PHP have their own SDKs: JavaScript, PHP.

It depends on no instrumentation library: install the ones you need (OpenInference, OpenLLMetry…) yourself. Apache-2.0.

Python 3.12 or later.

Terminal window
pip install ctxflow-otel==0.2.2

The package installs ctxflow-semconv (the conventions) too. To use an extra, quote it: pip install "ctxflow-otel[mcp]==0.2.2".

Extra Adds
ctxflow-otel[mcp] Conversation and user across MCP calls, and tool text on mcp 2.x servers (mcp 1.24 or later below 3). See MCP
ctxflow-otel[llamaindex] LlamaIndex agent, LLM, tool and retriever spans (openinference-instrumentation-llama-index), and ingestion spans and C1 (llama-index-core 0.14.19 or later, below 0.15). See Integrations
ctxflow-otel[unstructured] Ingestion spans and C1 for Unstructured (unstructured[md] 0.27.x, unstructured-ingest 1.12.x)
ctxflow-otel[docling] ingest.parse and C1 for Docling (2.150 or later)
Terminal window
OTEL_SERVICE_NAME=my-agent ctxflow-instrument python app.py

ctxflow-instrument is opentelemetry-instrument with OTEL_PYTHON_DISTRO=ctxflow and OTEL_PYTHON_CONFIGURATOR=ctxflow. It loads every installed OpenTelemetry instrumentor, and it starts the LlamaIndex instrumentation when [llamaindex] and llama-index-core are installed. Setting either variable to another value disables the ctxflow pipeline (a warning is logged). With plain opentelemetry-instrument, the first installed distro wins, so select ctxflow explicitly.

Any program can follow it, not only python: ctxflow-instrument runs the command you give it with the instrumentation already installed.

Terminal window
OTEL_SERVICE_NAME=my-agent ctxflow-instrument uvicorn app:app --port 8000

With uvicorn, --reload and --workers N also export spans: the child processes inherit the instrumentation, and each process has its own exporter. Servers that fork after start-up, such as gunicorn --preload, are not covered here. Spans leave in batches, so a process that is killed loses the last ones: see Flush before exit.

ctxflow-instrument does not install instrumentation of third-party libraries on its own. It runs the OpenTelemetry instrumentors that are installed in the environment, whoever installed them. What ctxflow brings:

Library Comes with Spans
LlamaIndex extra [llamaindex] Agent and workflow, LLM, tool and retriever spans (OpenInference)
MCP Python SDK extra [mcp] Trace context and identity across calls; tool spans on mcp 2.x (MCP)
LlamaIndex, Docling, Unstructured ingestion the extras of the same names ingest.* spans (Ingestion)

Every other instrumentor you install runs too: the OpenInference ones (openinference-instrumentation-openai, -anthropic, -langchain…), OpenLLMetry, and the OpenTelemetry contrib ones (opentelemetry-instrumentation-fastapi, -httpx, -requests…). A model span with gen_ai.operation.name, model and tokens, for example for the Anthropic client, appears only if such an instrumentor is installed. To see what is installed:

Terminal window
python -c "from importlib.metadata import entry_points; print(sorted(e.name for e in entry_points(group='opentelemetry_instrumentor')))"

OTEL_PYTHON_DISABLED_INSTRUMENTATIONS=name1,name2 turns some off. With init() or attach() none of this runs: start the instrumentors you want yourself.

import ctxflow_otel
provider = ctxflow_otel.init() # idempotent; returns the TracerProvider

Same configuration as ctxflow-instrument, in process. It loads no instrumentation except MCP and the ingestion extras when they are installed: call your other instrumentors yourself, after init(). That includes the LlamaIndex instrumentor (how). If an SDK TracerProvider is already the global one, init() attaches to it (with a warning); a non-SDK provider is an error.

from opentelemetry.sdk.trace import TracerProvider
import ctxflow_otel
provider = TracerProvider()
ctxflow_otel.attach(provider) # idempotent per provider

For a provider you own. It adds the identity processor and an OTLP/HTTP exporter (from OTEL_EXPORTER_OTLP_*). It does not instrument MCP or the ingestion extras: call ctxflow_otel.mcp.instrument_mcp() and ctxflow_otel.ingest.instrument_ingest() yourself.

ctxflow-instrument init() attach(provider)
Creates the TracerProvider Yes Yes (or attaches to the SDK provider already set) No: yours
Identity from baggage on every span Yes Yes Yes
OTLP/HTTP exporter, item identity on retrieval spans Yes Yes Yes
Writes the exporter defaults to the environment Yes Only when it creates the provider No
telemetry.distro.* resource attributes Yes Only when it creates the provider No
MCP context and identity propagation ([mcp]) Yes Yes No: call ctxflow_otel.mcp.instrument_mcp()
Ingestion spans ([llamaindex], [unstructured], [docling]) Yes Yes No: call ctxflow_otel.ingest.instrument_ingest()
LlamaIndex agent, LLM, tool and retriever spans Yes No: call instrument_llamaindex() (how) No
Any other installed instrumentor (FastAPI, OpenAI, Anthropic…) Yes No: start them yourself No
Warning for an instrumentor that was installed but skipped Yes No No

When an SDK TracerProvider is already the global one, init() does what attach() does for it (identity and exporter, with a warning) and adds MCP and the ingestion extras. It sets no defaults and no resource attributes on a provider it did not create. If ctxflow-instrument already set up the provider, init() returns it unchanged.

  • A TracerProvider whose resource has service.name and telemetry.distro.name / telemetry.distro.version.
  • The identity processor (baggage to span attributes), and a batch span processor over the exporter.
  • Defaults written to the environment when you did not set them: OTEL_TRACES_EXPORTER=otlp_proto_http, OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf, OTEL_METRICS_EXPORTER=none, OTEL_LOGS_EXPORTER=none.

Only traces go through the ctxflow exporter. Text, ids and metadata are exported as they are. If you do not want content captured at all, set the flags of your instrumentation (OpenInference OPENINFERENCE_HIDE_*, Traceloop TRACELOOP_TRACE_CONTENT=false); the turn detail then shows no text.

OTEL_SDK_DISABLED=true makes the configurator do nothing. OTEL_CONFIG_FILE (declarative configuration) is not supported: init() raises, and ctxflow-instrument logs the error and runs the app without telemetry.

OpenTelemetry skips an instrumentor whose declared dependencies do not match your installed libraries, and says so only in a low-level log line. With ctxflow-instrument, ctxflow logs a WARNING for each known AI instrumentor (OpenInference LlamaIndex, MCP, OpenAI, Anthropic, LangChain) that is installed, whose library is installed too, and that did not run. The message names the instrumentor and the way out: fix the dependency, or set OTEL_PYTHON_DISABLED_INSTRUMENTATIONS=<name> to silence it.

Topology OTEL_EXPORTER_OTLP_ENDPOINT API key
Directly to ctxflow https://app.ctxflow.sh In the app: OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer%20<key>"
Through your collector (recommended for production) http://<collector-host>:4318 In the collector only

The SDK adds /v1/traces to OTEL_EXPORTER_OTLP_ENDPOINT. For the signal-specific OTEL_EXPORTER_OTLP_TRACES_ENDPOINT see Configuration.

Run ctxflow-otel check with the same variables as your app: it tests the key and the endpoint and says which environment the key sends to.

Spans are exported in batches. A short-lived process must call provider.shutdown() (or provider.force_flush()) before it exits, or the last spans are lost.