Skip to main content
Every public symbol in the gatewaysdk tracing surface, with its exact signature, a parameter table, its return value, and a short runnable example. Signatures are verified against the SDK source. A captured session is the seed a world is built from, and a run against a world is traced like any other execution. The span and lifecycle helpers live on the tracing module. The identify helper lives at the package root. Every example on this page assumes the two imports below, and that tracing.init() has already run.

tracing.init()

Configure the OpenTelemetry tracer provider, connect to Surface Area, and instrument installed AI libraries. Call once at startup. All parameters are keyword-only.
Returns None.
With no credential parameters, init() reads GATEWAY_HOST, GATEWAY_PUBLIC_KEY, and GATEWAY_SECRET_KEY from the environment. When none of the three are set, spans are created but not exported.

tracing.shutdown() and tracing.flush()

Flush pending spans. Spans are batched and exported in the background, so a short-lived process must flush before exit or it drops its last traces.
shutdown() flushes and tears down the provider; it is safe to call more than once and returns None. flush() forces an export at a checkpoint without ending the session and returns True when the flush completed, False on timeout or error.

tracing.trace()

Create a root span. trace is a class that works both as a decorator (auto-captures function input and output) and as a context manager (explicit input and output). The first positional argument is either a span name or the function being decorated.
As a context manager it yields an OpenTelemetry Span. As a decorator it wraps sync and async functions unchanged.

tracing.span()

Create a child span under the current active span. Becomes a root span when none is active. Yields an OpenTelemetry Span.

tracing.tool()

Trace a tool or API call. Marks the span as a client call and sets gateway.observation.type to tool so the platform files it as a tool observation. Yields an OpenTelemetry Span.

tracing.atrace() and tracing.aspan()

Async variants of trace (root span) and span (child span). Both are async context managers with identical signatures and yield an OpenTelemetry Span.

tracing.get_current_span()

Return the currently active span, or a non-recording span when none is active. Use it to attach attributes from deep inside a call stack without threading the span through arguments.
Returns the active OpenTelemetry Span.

tracing.session()

Group every trace recorded inside the block under one session id. Propagates process-wide, so auto-instrumented LLM calls and tool spans inside the block are grouped too. When sessions nest, the innermost wins.
Yields None.
Every trace already belongs to an ambient session that resolves from GATEWAY_SESSION_ID, or is auto-generated per process. tracing.session() overrides that default for the duration of the block. Use it for a named, replayable session.
Inside a live world session, with open_session(...) as s: or with s.trace_context(): works the same way with the world session’s id, and adds world_session_id trace metadata. See Trace inside a world session.

tracing.tracing_context() and tracing.call_type_decorator()

Tag spans with a call type, tags, or an environment without touching each span. tracing_context is a context manager; call_type_decorator builds a reusable decorator that sets the call type for a function.
tracing_context yields None. call_type_decorator returns a decorator that wraps sync and async functions.
Three getters read the active context: tracing.get_current_call_type(), tracing.get_current_tags(), and tracing.get_current_environment(). Each returns the current value or None.

gatewaysdk.identify()

Attribute everything that follows (and the currently open span) to an end user. Sets the user for spans started in the calling context and stamps gateway.user.id on the in-flight span so the current trace is attributed immediately. Defined in gatewaysdk.tracing.identity and exported at the package root.
Returns None. When any of attributes, display_name, or email is provided, the user’s platform profile is updated on a background thread, so identify() never blocks the request. Profile updates need GATEWAY_HOST, GATEWAY_PUBLIC_KEY, and GATEWAY_SECRET_KEY; span attribution works without them.

Semantic conventions: tracing.semconv

Attribute-key constants for span.set_attribute(...). Set the gateway.* keys below to control how traces and observations appear in Surface Area. Reference a constant, not the raw string, so a key rename never breaks your code.
Trace-level attributes: set on the root span to shape the trace. User, agent, and session: who and what the trace belongs to. Run tracking: links traces to a Surface Area run. Set automatically when a run is active. Environment and version: resource-level filters, usually set through init(). Context Hub linkage: stamped automatically when content is pulled from the Context Hub, correlating a trace with the prompts, agents, skills, and memory it used. Observation-level attributes: set on a child span or generation. Cost and timing metrics: GatewaySDK custom measures. Two helper classes hold the valid values: semconv.ObservationType (SPAN, GENERATION, EVENT) and semconv.ObservationLevel (DEBUG, DEFAULT, WARNING, ERROR).
semconv also defines compatibility constants under the langfuse.*, gen_ai.*, and openinference.* namespaces. Auto-instrumentation and OTLP ingestion read those; you rarely set them by hand. Prefer the gateway.* keys above for your own spans.

tracing.gateway_exporter()

Build the Surface Area OTLP exporter explicitly, for passing to init(exporters=[...]) alongside your own exporters. init() creates one for you when credentials are present, so call this only when you need an explicit instance.
Returns a GatewayOTLPExporter. Raises ValueError when host, public key, or secret key is missing from both the arguments and the environment.

tracing.instrument() and the auto-instrumentation registry

Activate instrumentors by name. init(instrument_default=True) calls this with the default set; call it directly for manual control.
Returns None. An installed AI library whose instrumentor is missing produces a loud warning rather than silent zero-telemetry.
The registry ships the instrumentors below. Default set marks the seven enabled by instrument_default=True. An instrumentor activates only when its target library is importable.
The gatewaysdk[tracing] extra bundles only the OpenInference instrumentors for openai-agents, openai, anthropic, and litellm. It installs instrumentors only, never the libraries themselves (LiteLLM is traced once your app installs litellm). langchain and llama_index are in the default set but their instrumentor packages are not bundled: install openinference-instrumentation-langchain or openinference-instrumentation-llama-index for them to activate. Everything below the default set needs its own openinference-instrumentation-* package.
Setting OPENAI_AGENTS_DISABLE_TRACING=1 turns off the OpenAI Agents SDK tracing pipeline the openai_agents instrumentor rides on, so nothing from openai-agents reaches Surface Area. Leave it unset; the OpenAI trace-upload 401s it silences are harmless.