Tracing module
TracingProcessor
Bases: ABC
Interface for processing spans.
Source code in src/cai/sdk/agents/tracing/processor_interface.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
on_trace_start
abstractmethod
on_trace_start(trace: Trace) -> None
Called when a trace is started.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trace
|
Trace
|
The trace that started. |
required |
Source code in src/cai/sdk/agents/tracing/processor_interface.py
12 13 14 15 16 17 18 19 |
|
on_trace_end
abstractmethod
on_trace_end(trace: Trace) -> None
Called when a trace is finished.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trace
|
Trace
|
The trace that started. |
required |
Source code in src/cai/sdk/agents/tracing/processor_interface.py
21 22 23 24 25 26 27 28 |
|
on_span_start
abstractmethod
on_span_start(span: Span[Any]) -> None
Called when a span is started.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
span
|
Span[Any]
|
The span that started. |
required |
Source code in src/cai/sdk/agents/tracing/processor_interface.py
30 31 32 33 34 35 36 37 |
|
on_span_end
abstractmethod
on_span_end(span: Span[Any]) -> None
Called when a span is finished. Should not block or raise exceptions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
span
|
Span[Any]
|
The span that finished. |
required |
Source code in src/cai/sdk/agents/tracing/processor_interface.py
39 40 41 42 43 44 45 46 |
|
shutdown
abstractmethod
shutdown() -> None
Called when the application stops.
Source code in src/cai/sdk/agents/tracing/processor_interface.py
48 49 50 51 |
|
force_flush
abstractmethod
force_flush() -> None
Forces an immediate flush of all queued spans/traces.
Source code in src/cai/sdk/agents/tracing/processor_interface.py
53 54 55 56 |
|
Span
Bases: ABC
, Generic[TSpanData]
Source code in src/cai/sdk/agents/tracing/spans.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
start
abstractmethod
start(mark_as_current: bool = False)
Start the span.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mark_as_current
|
bool
|
If true, the span will be marked as the current span. |
False
|
Source code in src/cai/sdk/agents/tracing/spans.py
39 40 41 42 43 44 45 46 47 |
|
finish
abstractmethod
finish(reset_current: bool = False) -> None
Finish the span.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reset_current
|
bool
|
If true, the span will be reset as the current span. |
False
|
Source code in src/cai/sdk/agents/tracing/spans.py
49 50 51 52 53 54 55 56 57 |
|
Trace
A trace is the root level object that tracing creates. It represents a logical "workflow".
Source code in src/cai/sdk/agents/tracing/traces.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
trace_id
abstractmethod
property
trace_id: str
The trace ID.
name
abstractmethod
property
name: str
The name of the workflow being traced.
start
abstractmethod
start(mark_as_current: bool = False)
Start the trace.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mark_as_current
|
bool
|
If true, the trace will be marked as the current trace. |
False
|
Source code in src/cai/sdk/agents/tracing/traces.py
26 27 28 29 30 31 32 33 34 |
|
finish
abstractmethod
finish(reset_current: bool = False)
Finish the trace.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reset_current
|
bool
|
If true, the trace will be reset as the current trace. |
False
|
Source code in src/cai/sdk/agents/tracing/traces.py
36 37 38 39 40 41 42 43 44 |
|
export
abstractmethod
export() -> dict[str, Any] | None
Export the trace as a dictionary.
Source code in src/cai/sdk/agents/tracing/traces.py
62 63 64 65 66 67 |
|
agent_span
agent_span(
name: str,
handoffs: list[str] | None = None,
tools: list[str] | None = None,
output_type: str | None = None,
span_id: str | None = None,
parent: Trace | Span[Any] | None = None,
disabled: bool = False,
) -> Span[AgentSpanData]
Create a new agent span. The span will not be started automatically, you should either do
with agent_span() ...
or call span.start()
+ span.finish()
manually.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the agent. |
required |
handoffs
|
list[str] | None
|
Optional list of agent names to which this agent could hand off control. |
None
|
tools
|
list[str] | None
|
Optional list of tool names available to this agent. |
None
|
output_type
|
str | None
|
Optional name of the output type produced by the agent. |
None
|
span_id
|
str | None
|
The ID of the span. Optional. If not provided, we will generate an ID. We
recommend using |
None
|
parent
|
Trace | Span[Any] | None
|
The parent span or trace. If not provided, we will automatically use the current trace/span as the parent. |
None
|
disabled
|
bool
|
If True, we will return a Span but the Span will not be recorded. |
False
|
Returns:
Type | Description |
---|---|
Span[AgentSpanData]
|
The newly created agent span. |
Source code in src/cai/sdk/agents/tracing/create.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
custom_span
custom_span(
name: str,
data: dict[str, Any] | None = None,
span_id: str | None = None,
parent: Trace | Span[Any] | None = None,
disabled: bool = False,
) -> Span[CustomSpanData]
Create a new custom span, to which you can add your own metadata. The span will not be
started automatically, you should either do with custom_span() ...
or call
span.start()
+ span.finish()
manually.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the custom span. |
required |
data
|
dict[str, Any] | None
|
Arbitrary structured data to associate with the span. |
None
|
span_id
|
str | None
|
The ID of the span. Optional. If not provided, we will generate an ID. We
recommend using |
None
|
parent
|
Trace | Span[Any] | None
|
The parent span or trace. If not provided, we will automatically use the current trace/span as the parent. |
None
|
disabled
|
bool
|
If True, we will return a Span but the Span will not be recorded. |
False
|
Returns:
Type | Description |
---|---|
Span[CustomSpanData]
|
The newly created custom span. |
Source code in src/cai/sdk/agents/tracing/create.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
|
function_span
function_span(
name: str,
input: str | None = None,
output: str | None = None,
span_id: str | None = None,
parent: Trace | Span[Any] | None = None,
disabled: bool = False,
) -> Span[FunctionSpanData]
Create a new function span. The span will not be started automatically, you should either do
with function_span() ...
or call span.start()
+ span.finish()
manually.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the function. |
required |
input
|
str | None
|
The input to the function. |
None
|
output
|
str | None
|
The output of the function. |
None
|
span_id
|
str | None
|
The ID of the span. Optional. If not provided, we will generate an ID. We
recommend using |
None
|
parent
|
Trace | Span[Any] | None
|
The parent span or trace. If not provided, we will automatically use the current trace/span as the parent. |
None
|
disabled
|
bool
|
If True, we will return a Span but the Span will not be recorded. |
False
|
Returns:
Type | Description |
---|---|
Span[FunctionSpanData]
|
The newly created function span. |
Source code in src/cai/sdk/agents/tracing/create.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
|
generation_span
generation_span(
input: Sequence[Mapping[str, Any]] | None = None,
output: Sequence[Mapping[str, Any]] | None = None,
model: str | None = None,
model_config: Mapping[str, Any] | None = None,
usage: dict[str, Any] | None = None,
span_id: str | None = None,
parent: Trace | Span[Any] | None = None,
disabled: bool = False,
) -> Span[GenerationSpanData]
Create a new generation span. The span will not be started automatically, you should either
do with generation_span() ...
or call span.start()
+ span.finish()
manually.
This span captures the details of a model generation, including the
input message sequence, any generated outputs, the model name and
configuration, and usage data. If you only need to capture a model
response identifier, use response_span()
instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input
|
Sequence[Mapping[str, Any]] | None
|
The sequence of input messages sent to the model. |
None
|
output
|
Sequence[Mapping[str, Any]] | None
|
The sequence of output messages received from the model. |
None
|
model
|
str | None
|
The model identifier used for the generation. |
None
|
model_config
|
Mapping[str, Any] | None
|
The model configuration (hyperparameters) used. |
None
|
usage
|
dict[str, Any] | None
|
A dictionary of usage information (input tokens, output tokens, etc.). |
None
|
span_id
|
str | None
|
The ID of the span. Optional. If not provided, we will generate an ID. We
recommend using |
None
|
parent
|
Trace | Span[Any] | None
|
The parent span or trace. If not provided, we will automatically use the current trace/span as the parent. |
None
|
disabled
|
bool
|
If True, we will return a Span but the Span will not be recorded. |
False
|
Returns:
Type | Description |
---|---|
Span[GenerationSpanData]
|
The newly created generation span. |
Source code in src/cai/sdk/agents/tracing/create.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
get_current_span
get_current_span() -> Span[Any] | None
Returns the currently active span, if present.
Source code in src/cai/sdk/agents/tracing/create.py
79 80 81 |
|
get_current_trace
get_current_trace() -> Trace | None
Returns the currently active trace, if present.
Source code in src/cai/sdk/agents/tracing/create.py
74 75 76 |
|
guardrail_span
guardrail_span(
name: str,
triggered: bool = False,
span_id: str | None = None,
parent: Trace | Span[Any] | None = None,
disabled: bool = False,
) -> Span[GuardrailSpanData]
Create a new guardrail span. The span will not be started automatically, you should either
do with guardrail_span() ...
or call span.start()
+ span.finish()
manually.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the guardrail. |
required |
triggered
|
bool
|
Whether the guardrail was triggered. |
False
|
span_id
|
str | None
|
The ID of the span. Optional. If not provided, we will generate an ID. We
recommend using |
None
|
parent
|
Trace | Span[Any] | None
|
The parent span or trace. If not provided, we will automatically use the current trace/span as the parent. |
None
|
disabled
|
bool
|
If True, we will return a Span but the Span will not be recorded. |
False
|
Source code in src/cai/sdk/agents/tracing/create.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
|
handoff_span
handoff_span(
from_agent: str | None = None,
to_agent: str | None = None,
span_id: str | None = None,
parent: Trace | Span[Any] | None = None,
disabled: bool = False,
) -> Span[HandoffSpanData]
Create a new handoff span. The span will not be started automatically, you should either do
with handoff_span() ...
or call span.start()
+ span.finish()
manually.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
from_agent
|
str | None
|
The name of the agent that is handing off. |
None
|
to_agent
|
str | None
|
The name of the agent that is receiving the handoff. |
None
|
span_id
|
str | None
|
The ID of the span. Optional. If not provided, we will generate an ID. We
recommend using |
None
|
parent
|
Trace | Span[Any] | None
|
The parent span or trace. If not provided, we will automatically use the current trace/span as the parent. |
None
|
disabled
|
bool
|
If True, we will return a Span but the Span will not be recorded. |
False
|
Returns:
Type | Description |
---|---|
Span[HandoffSpanData]
|
The newly created handoff span. |
Source code in src/cai/sdk/agents/tracing/create.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
|
mcp_tools_span
mcp_tools_span(
server: str | None = None,
result: list[str] | None = None,
span_id: str | None = None,
parent: Trace | Span[Any] | None = None,
disabled: bool = False,
) -> Span[MCPListToolsSpanData]
Create a new MCP list tools span. The span will not be started automatically, you should
either do with mcp_tools_span() ...
or call span.start()
+ span.finish()
manually.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
server
|
str | None
|
The name of the MCP server. |
None
|
result
|
list[str] | None
|
The result of the MCP list tools call. |
None
|
span_id
|
str | None
|
The ID of the span. Optional. If not provided, we will generate an ID. We
recommend using |
None
|
parent
|
Trace | Span[Any] | None
|
The parent span or trace. If not provided, we will automatically use the current trace/span as the parent. |
None
|
disabled
|
bool
|
If True, we will return a Span but the Span will not be recorded. |
False
|
Source code in src/cai/sdk/agents/tracing/create.py
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 |
|
response_span
response_span(
response: Response | None = None,
span_id: str | None = None,
parent: Trace | Span[Any] | None = None,
disabled: bool = False,
) -> Span[ResponseSpanData]
Create a new response span. The span will not be started automatically, you should either do
with response_span() ...
or call span.start()
+ span.finish()
manually.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response | None
|
The OpenAI Response object. |
None
|
span_id
|
str | None
|
The ID of the span. Optional. If not provided, we will generate an ID. We
recommend using |
None
|
parent
|
Trace | Span[Any] | None
|
The parent span or trace. If not provided, we will automatically use the current trace/span as the parent. |
None
|
disabled
|
bool
|
If True, we will return a Span but the Span will not be recorded. |
False
|
Source code in src/cai/sdk/agents/tracing/create.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
|
speech_group_span
speech_group_span(
input: str | None = None,
span_id: str | None = None,
parent: Trace | Span[Any] | None = None,
disabled: bool = False,
) -> Span[SpeechGroupSpanData]
Create a new speech group span. The span will not be started automatically, you should
either do with speech_group_span() ...
or call span.start()
+ span.finish()
manually.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input
|
str | None
|
The input text used for the speech request. |
None
|
span_id
|
str | None
|
The ID of the span. Optional. If not provided, we will generate an ID. We
recommend using |
None
|
parent
|
Trace | Span[Any] | None
|
The parent span or trace. If not provided, we will automatically use the current trace/span as the parent. |
None
|
disabled
|
bool
|
If True, we will return a Span but the Span will not be recorded. |
False
|
Source code in src/cai/sdk/agents/tracing/create.py
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
|
speech_span
speech_span(
model: str | None = None,
input: str | None = None,
output: str | None = None,
output_format: str | None = "pcm",
model_config: Mapping[str, Any] | None = None,
first_content_at: str | None = None,
span_id: str | None = None,
parent: Trace | Span[Any] | None = None,
disabled: bool = False,
) -> Span[SpeechSpanData]
Create a new speech span. The span will not be started automatically, you should either do
with speech_span() ...
or call span.start()
+ span.finish()
manually.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
str | None
|
The name of the model used for the text-to-speech. |
None
|
input
|
str | None
|
The text input of the text-to-speech. |
None
|
output
|
str | None
|
The audio output of the text-to-speech as base64 encoded string of PCM audio bytes. |
None
|
output_format
|
str | None
|
The format of the audio output (defaults to "pcm"). |
'pcm'
|
model_config
|
Mapping[str, Any] | None
|
The model configuration (hyperparameters) used. |
None
|
first_content_at
|
str | None
|
The time of the first byte of the audio output. |
None
|
span_id
|
str | None
|
The ID of the span. Optional. If not provided, we will generate an ID. We
recommend using |
None
|
parent
|
Trace | Span[Any] | None
|
The parent span or trace. If not provided, we will automatically use the current trace/span as the parent. |
None
|
disabled
|
bool
|
If True, we will return a Span but the Span will not be recorded. |
False
|
Source code in src/cai/sdk/agents/tracing/create.py
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
|
trace
trace(
workflow_name: str,
trace_id: str | None = None,
group_id: str | None = None,
metadata: dict[str, Any] | None = None,
disabled: bool = False,
) -> Trace
Create a new trace. The trace will not be started automatically; you should either use
it as a context manager (with trace(...):
) or call trace.start()
+ trace.finish()
manually.
In addition to the workflow name and optional grouping identifier, you can provide an arbitrary metadata dictionary to attach additional user-defined information to the trace.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
workflow_name
|
str
|
The name of the logical app or workflow. For example, you might provide "code_bot" for a coding agent, or "customer_support_agent" for a customer support agent. |
required |
trace_id
|
str | None
|
The ID of the trace. Optional. If not provided, we will generate an ID. We
recommend using |
None
|
group_id
|
str | None
|
Optional grouping identifier to link multiple traces from the same conversation or process. For instance, you might use a chat thread ID. |
None
|
metadata
|
dict[str, Any] | None
|
Optional dictionary of additional metadata to attach to the trace. |
None
|
disabled
|
bool
|
If True, we will return a Trace but the Trace will not be recorded. This will
not be checked if there's an existing trace and |
False
|
Returns:
Type | Description |
---|---|
Trace
|
The newly created trace object. |
Source code in src/cai/sdk/agents/tracing/create.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|
transcription_span
transcription_span(
model: str | None = None,
input: str | None = None,
input_format: str | None = "pcm",
output: str | None = None,
model_config: Mapping[str, Any] | None = None,
span_id: str | None = None,
parent: Trace | Span[Any] | None = None,
disabled: bool = False,
) -> Span[TranscriptionSpanData]
Create a new transcription span. The span will not be started automatically, you should
either do with transcription_span() ...
or call span.start()
+ span.finish()
manually.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
str | None
|
The name of the model used for the speech-to-text. |
None
|
input
|
str | None
|
The audio input of the speech-to-text transcription, as a base64 encoded string of audio bytes. |
None
|
input_format
|
str | None
|
The format of the audio input (defaults to "pcm"). |
'pcm'
|
output
|
str | None
|
The output of the speech-to-text transcription. |
None
|
model_config
|
Mapping[str, Any] | None
|
The model configuration (hyperparameters) used. |
None
|
span_id
|
str | None
|
The ID of the span. Optional. If not provided, we will generate an ID. We
recommend using |
None
|
parent
|
Trace | Span[Any] | None
|
The parent span or trace. If not provided, we will automatically use the current trace/span as the parent. |
None
|
disabled
|
bool
|
If True, we will return a Span but the Span will not be recorded. |
False
|
Returns:
Type | Description |
---|---|
Span[TranscriptionSpanData]
|
The newly created speech-to-text span. |
Source code in src/cai/sdk/agents/tracing/create.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
|
gen_span_id
gen_span_id() -> str
Generates a new span ID.
Source code in src/cai/sdk/agents/tracing/util.py
15 16 17 |
|
gen_trace_id
gen_trace_id() -> str
Generates a new trace ID.
Source code in src/cai/sdk/agents/tracing/util.py
10 11 12 |
|
add_trace_processor
add_trace_processor(
span_processor: TracingProcessor,
) -> None
Adds a new trace processor. This processor will receive all traces/spans.
Source code in src/cai/sdk/agents/tracing/__init__.py
79 80 81 82 83 |
|
set_trace_processors
set_trace_processors(
processors: list[TracingProcessor],
) -> None
Set the list of trace processors. This will replace the current list of processors.
Source code in src/cai/sdk/agents/tracing/__init__.py
86 87 88 89 90 |
|
set_tracing_disabled
set_tracing_disabled(disabled: bool) -> None
Set whether tracing is globally disabled.
Source code in src/cai/sdk/agents/tracing/__init__.py
93 94 95 96 97 |
|
set_tracing_export_api_key
set_tracing_export_api_key(api_key: str) -> None
Set the OpenAI API key for the backend exporter.
Source code in src/cai/sdk/agents/tracing/__init__.py
100 101 102 103 104 |
|