Tracing API¶
The tracing module provides observability for agent execution.
Registry Functions¶
zap_ai.tracing.set_tracing_provider(provider)
¶
Set the global tracing provider for activities.
Called during worker initialization to provide activities access to the tracing provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
TracingProvider
|
TracingProvider instance. |
required |
Source code in src/zap_ai/tracing/__init__.py
42 43 44 45 46 47 48 49 50 51 52 53 | |
zap_ai.tracing.get_tracing_provider()
¶
Get the global tracing provider.
Returns NoOpTracingProvider if not configured.
Returns:
| Type | Description |
|---|---|
TracingProvider
|
TracingProvider instance. |
Source code in src/zap_ai/tracing/__init__.py
56 57 58 59 60 61 62 63 64 65 66 67 68 | |
zap_ai.tracing.reset_tracing_provider()
¶
Reset the global tracing provider to None.
Primarily used for testing to ensure clean state between tests.
Source code in src/zap_ai/tracing/__init__.py
71 72 73 74 75 76 77 78 | |
Protocol¶
zap_ai.tracing.protocol.TracingProvider
¶
Bases: Protocol
Protocol for tracing backends.
Implementations must be async-safe and handle context propagation across Temporal boundaries.
The provider is responsible for: - Creating traces (root spans) for tasks - Creating child observations for iterations, tool calls, etc. - Creating generation observations for LLM calls (with token usage) - Tracking errors and events - Flushing and cleanup
Source code in src/zap_ai/tracing/protocol.py
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 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 150 151 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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
start_trace(name, session_id=None, user_id=None, metadata=None, tags=None)
async
¶
Start a new trace (root observation).
Called at task start. Returns context for propagation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the trace (e.g., "task-AgentName-taskId"). |
required |
session_id
|
str | None
|
Optional session for grouping traces. |
None
|
user_id
|
str | None
|
Optional user identifier. |
None
|
metadata
|
dict[str, Any] | None
|
Additional metadata to attach. |
None
|
tags
|
list[str] | None
|
Optional tags for filtering. |
None
|
Yields:
| Type | Description |
|---|---|
AsyncIterator[TraceContext]
|
TraceContext for propagation to activities and child workflows. |
Source code in src/zap_ai/tracing/protocol.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
start_observation(name, observation_type, parent_context, metadata=None, input_data=None)
async
¶
Start a child observation within an existing trace.
Used for iterations, tool calls, sub-agent delegation, etc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the observation (e.g., "iteration-0", "tool-search"). |
required |
observation_type
|
ObservationType
|
Type of observation for categorization. |
required |
parent_context
|
TraceContext
|
Context from parent observation. |
required |
metadata
|
dict[str, Any] | None
|
Additional metadata to attach. |
None
|
input_data
|
Any | None
|
Input data for the observation. |
None
|
Yields:
| Type | Description |
|---|---|
AsyncIterator[TraceContext]
|
TraceContext for nested observations. |
Source code in src/zap_ai/tracing/protocol.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
start_generation(name, parent_context, model, input_messages, metadata=None)
async
¶
Start an LLM generation observation.
For tracking LLM calls with model info and usage. Must be explicitly ended with end_generation().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the generation (e.g., "inference-AgentName"). |
required |
parent_context
|
TraceContext
|
Context from parent observation. |
required |
model
|
str
|
LLM model identifier. |
required |
input_messages
|
list[dict[str, Any]]
|
Input messages sent to the LLM. |
required |
metadata
|
dict[str, Any] | None
|
Additional metadata. |
None
|
Returns:
| Type | Description |
|---|---|
TraceContext
|
TraceContext that must be passed to end_generation(). |
Source code in src/zap_ai/tracing/protocol.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
end_generation(context, output, usage=None)
async
¶
End an LLM generation observation with output and usage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
TraceContext
|
Context from start_generation(). |
required |
output
|
dict[str, Any]
|
LLM output (content, tool_calls, etc.). |
required |
usage
|
dict[str, int] | None
|
Token usage dict (prompt_tokens, completion_tokens, total_tokens). |
None
|
Source code in src/zap_ai/tracing/protocol.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
add_event(context, name, attributes=None)
async
¶
Add an event to the current observation.
Used for logging significant occurrences (status changes, etc.).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
TraceContext
|
Current trace context. |
required |
name
|
str
|
Event name. |
required |
attributes
|
dict[str, Any] | None
|
Event attributes. |
None
|
Source code in src/zap_ai/tracing/protocol.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
set_error(context, error)
async
¶
Mark the observation as errored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
TraceContext
|
Current trace context. |
required |
error
|
Exception
|
The exception that occurred. |
required |
Source code in src/zap_ai/tracing/protocol.py
194 195 196 197 198 199 200 201 202 203 204 205 206 | |
flush()
async
¶
Flush any pending trace data.
Source code in src/zap_ai/tracing/protocol.py
208 209 210 | |
shutdown()
async
¶
Cleanup tracing resources.
Source code in src/zap_ai/tracing/protocol.py
212 213 214 | |
Abstract Base Class¶
zap_ai.tracing.base.BaseTracingProvider
¶
Bases: ABC
Abstract base class for tracing providers.
Subclass this to implement custom tracing backends. You must implement: - _start_trace_impl(): Core trace creation logic - _start_observation_impl(): Core observation creation logic - start_generation(): LLM generation tracking - end_generation(): Complete LLM generation
Optional overrides (default to no-op): - add_event(): Add events to observations - set_error(): Mark observations as errored - flush(): Flush pending data - shutdown(): Cleanup resources
Example
class MyTracingProvider(BaseTracingProvider): async def _start_trace_impl(self, name, **kwargs): ctx = self._create_context() return ctx, None # No cleanup needed
async def _start_observation_impl(
self, name, observation_type, parent_context, **kwargs
):
ctx = self._create_child_context(parent_context)
return ctx, None
async def start_generation(
self, name, parent_context, model, input_messages, **kwargs
):
return self._create_child_context(parent_context)
async def end_generation(self, context, output, usage=None):
pass # No-op for simple implementation
Source code in src/zap_ai/tracing/base.py
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 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 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 150 151 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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 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 255 256 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 287 288 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 315 316 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 359 360 361 | |
start_trace(name, session_id=None, user_id=None, metadata=None, tags=None)
async
¶
Start a new trace (root observation).
Called at task start. Returns context for propagation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the trace. |
required |
session_id
|
str | None
|
Optional session for grouping traces. |
None
|
user_id
|
str | None
|
Optional user identifier. |
None
|
metadata
|
dict[str, Any] | None
|
Additional metadata to attach. |
None
|
tags
|
list[str] | None
|
Optional tags for filtering. |
None
|
Yields:
| Type | Description |
|---|---|
AsyncIterator[TraceContext]
|
TraceContext for propagation to activities and child workflows. |
Source code in src/zap_ai/tracing/base.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
start_observation(name, observation_type, parent_context, metadata=None, input_data=None)
async
¶
Start a child observation within an existing trace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the observation. |
required |
observation_type
|
ObservationType
|
Type of observation for categorization. |
required |
parent_context
|
TraceContext
|
Context from parent observation. |
required |
metadata
|
dict[str, Any] | None
|
Additional metadata to attach. |
None
|
input_data
|
Any | None
|
Input data for the observation. |
None
|
Yields:
| Type | Description |
|---|---|
AsyncIterator[TraceContext]
|
TraceContext for nested observations. |
Source code in src/zap_ai/tracing/base.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
start_generation(name, parent_context, model, input_messages, metadata=None)
abstractmethod
async
¶
Start an LLM generation observation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the generation. |
required |
parent_context
|
TraceContext
|
Context from parent observation. |
required |
model
|
str
|
LLM model identifier. |
required |
input_messages
|
list[dict[str, Any]]
|
Input messages sent to the LLM. |
required |
metadata
|
dict[str, Any] | None
|
Additional metadata. |
None
|
Returns:
| Type | Description |
|---|---|
TraceContext
|
TraceContext that must be passed to end_generation(). |
Source code in src/zap_ai/tracing/base.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | |
end_generation(context, output, usage=None)
abstractmethod
async
¶
End an LLM generation observation with output and usage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
TraceContext
|
Context from start_generation(). |
required |
output
|
dict[str, Any]
|
LLM output (content, tool_calls, etc.). |
required |
usage
|
dict[str, int] | None
|
Token usage dict (prompt_tokens, completion_tokens, total_tokens). |
None
|
Source code in src/zap_ai/tracing/base.py
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | |
add_event(context, name, attributes=None)
async
¶
Add an event to the current observation.
Default implementation does nothing. Override to implement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
TraceContext
|
Current trace context. |
required |
name
|
str
|
Event name. |
required |
attributes
|
dict[str, Any] | None
|
Event attributes. |
None
|
Source code in src/zap_ai/tracing/base.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
set_error(context, error)
async
¶
Mark the observation as errored.
Default implementation does nothing. Override to implement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
TraceContext
|
Current trace context. |
required |
error
|
Exception
|
The exception that occurred. |
required |
Source code in src/zap_ai/tracing/base.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
flush()
async
¶
Flush any pending trace data.
Default implementation does nothing. Override if buffering is used.
Source code in src/zap_ai/tracing/base.py
347 348 349 350 351 352 353 | |
shutdown()
async
¶
Cleanup tracing resources.
Default implementation does nothing. Override if cleanup is needed.
Source code in src/zap_ai/tracing/base.py
355 356 357 358 359 360 361 | |
zap_ai.tracing.protocol.TraceContext
dataclass
¶
Serializable trace context for propagation across Temporal boundaries.
This context is passed through activity inputs to maintain trace continuity across process boundaries. Must be JSON-serializable for Temporal.
Attributes:
| Name | Type | Description |
|---|---|---|
trace_id |
str
|
Unique identifier for the trace. |
span_id |
str
|
Unique identifier for the current span/observation. |
provider_data |
dict[str, Any] | None
|
Provider-specific data (e.g., Langfuse observation ID). |
Source code in src/zap_ai/tracing/protocol.py
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 | |
to_dict()
¶
Serialize for Temporal activity input.
Source code in src/zap_ai/tracing/protocol.py
48 49 50 51 52 53 54 | |
from_dict(data)
classmethod
¶
Deserialize from Temporal activity input.
Source code in src/zap_ai/tracing/protocol.py
56 57 58 59 60 61 62 63 | |
zap_ai.tracing.protocol.ObservationType
¶
Bases: str, Enum
Observation types aligned with Langfuse's native types.
These types provide semantic context for different kinds of operations being traced in the agent workflow.
Source code in src/zap_ai/tracing/protocol.py
16 17 18 19 20 21 22 23 24 25 26 27 | |
Providers¶
LangfuseTracingProvider¶
zap_ai.tracing.langfuse_provider.LangfuseTracingProvider
¶
Bases: BaseTracingProvider
Langfuse implementation of BaseTracingProvider.
Uses Langfuse's v3 SDK for async-compatible tracing with native observation types (generation, tool, agent, span).
Attributes:
| Name | Type | Description |
|---|---|---|
public_key |
Langfuse public key (or set LANGFUSE_PUBLIC_KEY env var). |
|
secret_key |
Langfuse secret key (or set LANGFUSE_SECRET_KEY env var). |
|
host |
Langfuse host URL (defaults to cloud). |
Source code in src/zap_ai/tracing/langfuse_provider.py
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 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 150 151 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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 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 255 256 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 287 288 289 290 291 292 293 | |
__init__(public_key=None, secret_key=None, host=None)
¶
Initialize Langfuse tracing provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
public_key
|
str | None
|
Langfuse public key (or use LANGFUSE_PUBLIC_KEY env var). |
None
|
secret_key
|
str | None
|
Langfuse secret key (or use LANGFUSE_SECRET_KEY env var). |
None
|
host
|
str | None
|
Langfuse host URL (defaults to https://cloud.langfuse.com). |
None
|
Source code in src/zap_ai/tracing/langfuse_provider.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
start_generation(name, parent_context, model, input_messages, metadata=None)
async
¶
Start a Langfuse generation for LLM calls.
Source code in src/zap_ai/tracing/langfuse_provider.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
end_generation(context, output, usage=None)
async
¶
End a Langfuse generation with output and usage.
Source code in src/zap_ai/tracing/langfuse_provider.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
add_event(context, name, attributes=None)
async
¶
Add an event to the Langfuse observation.
Source code in src/zap_ai/tracing/langfuse_provider.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
set_error(context, error)
async
¶
Mark the Langfuse observation as errored.
Source code in src/zap_ai/tracing/langfuse_provider.py
274 275 276 277 278 279 280 281 282 283 284 285 | |
flush()
async
¶
Flush pending Langfuse data.
Source code in src/zap_ai/tracing/langfuse_provider.py
287 288 289 | |
shutdown()
async
¶
Shutdown Langfuse client.
Source code in src/zap_ai/tracing/langfuse_provider.py
291 292 293 | |
NoOpTracingProvider¶
zap_ai.tracing.noop_provider.NoOpTracingProvider
¶
Bases: BaseTracingProvider
No-operation tracing provider.
Used when tracing is not configured. All operations are no-ops but return valid contexts for code compatibility.
Source code in src/zap_ai/tracing/noop_provider.py
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 | |
start_generation(name, parent_context, model, input_messages, metadata=None)
async
¶
Return a dummy context.
Source code in src/zap_ai/tracing/noop_provider.py
45 46 47 48 49 50 51 52 53 54 | |
end_generation(context, output, usage=None)
async
¶
No-op.
Source code in src/zap_ai/tracing/noop_provider.py
56 57 58 59 60 61 62 63 | |
Usage Example¶
from zap_ai.tracing import set_tracing_provider, reset_tracing_provider
from zap_ai.tracing.langfuse_provider import LangfuseTracingProvider
# Enable Langfuse tracing
provider = LangfuseTracingProvider()
set_tracing_provider(provider)
# ... run your agents ...
# Flush before shutdown
await provider.flush()
# Optionally reset to no-op
reset_tracing_provider()