Temporal
Trace OpenAI Agents SDK workflows on Temporal with cetra from Python, through Temporal’s OpenAI Agents plugin.
Install
Section titled “Install”pip install cetra-sdk "temporalio-openai-agents[openinference]" temporalioSet up
Section titled “Set up”import asyncio
from opentelemetry import tracefrom temporalio.client import Clientfrom temporalio.contrib.opentelemetry import OpenTelemetryPlugin, create_tracer_providerfrom temporalio.openai_agents import OpenAIAgentsPlugin
import cetra
provider = create_tracer_provider()trace.set_tracer_provider(provider)cetra.init(use_global_provider=True)
async def main() -> None: client = await Client.connect( "localhost:7233", plugins=[ OpenAIAgentsPlugin(use_otel_instrumentation=True), OpenTelemetryPlugin(add_temporal_spans=True), ], ) # Run workers and workflows as usual.
asyncio.run(main())Set the tracer provider before cetra.init(): with use_global_provider=True, cetra adds its exporter to the provider you set instead of building its own.
OpenAIAgentsPlugin(use_otel_instrumentation=True) instruments the OpenAI Agents SDK itself. OpenTelemetryPlugin(add_temporal_spans=True) adds Temporal’s own workflow and activity spans. Define your workflows in their own module, not in the one you run: the worker re-imports it, and a workflow defined in __main__ fails to validate.
For tests and local runs, WorkflowEnvironment.start_local() starts the dev server; pass dev_server_existing_path if you want it to use a Temporal CLI binary you already have, because otherwise the SDK downloads one at runtime.
What you get
Section titled “What you get”A workflow that runs an agent with one activity tool produces:
- an
agentspan for the workflow and one for the agent; - one
chainspan per turn, and one pair oftemporal:startActivityandtemporal:executeActivitychainspans around each model call and each tool; - an
llmspan for each model call, with the provider, the token counts, the cost computed from the prices, and the messages, with the tool call and the tool result on the turns that have them; - a
toolspan for the activity tool, with its input and output.
The span shapes are those of the OpenAI Agents page. With OpenTelemetryPlugin(add_temporal_spans=True), Temporal’s own StartWorkflow, RunWorkflow, StartActivity and RunActivity spans arrive too, as plain spans that carry no convention.
Limits
Section titled “Limits”- The fixture was captured with
temporalio1.36.1,temporalio-openai-agents2.0.0,openai-agents0.24.0, OpenInference 2.7.0 and the Temporal CLI 1.10.1. temporalio-openai-agents2.0.0 is the version foropenai-agents0.24.x; 1.1.0 needs 0.22.x.- Do not also pass
instrument=["openai-agents"]: the plugin instruments the OpenAI Agents SDK itself, and the calls would be recorded twice. - With
use_global_provider=True, cetra’s default export filter keeps only spans of LLM instrumentations, so Temporal’s own workflow and activity spans are not sent. Theexport_filteroption keeps them; see Python OpenTelemetry. - If a field is missing, look at the span’s Attributes tab and compare it with Span attributes.
- Traces: read the trace as a tree and a timeline.
- Cost and tokens: how cost is computed from the token counts.
- Online rules: score these traces automatically.