trace_event
Decorator for tracking AI model interactions
The trace_event
decorators allow you to track and analyze interactions with AI models in your software. They come in two variants:
trace_event_sync
for synchronous functionstrace_event_async
for asynchronous functions
Requirements
The decorated method must belong to a class that defines:
system_name
: A string identifying the type of agent (e.g., “Math_Agent”, “Translation_Agent”)system_instance_id
: A UUID string identifying a specific instance of the agent
Note: The
system_instance_id
is particularly important when running multiple instances in parallel, as it helps differentiate between records from different runs. Thesystem_name
allows you to group and analyze data across all instances of the same type of agent.
Usage
The decorators can be used with either the Synth OpenAI or Anthropic clients, or in combination with track-messages.
Usage with Anthropic
The traced events can later be analyzed and uploaded using the Synth SDK’s dataset and upload functionality.
Usage with track_messages
You can combine trace_event
decorators with track-messages for more detailed tracking of model interactions. Here’s an example using the async variant:
This approach allows you to:
- Track the overall function execution with
trace_event_async
- Explicitly track the messages and model parameters with
track_messages_async
Parameters
event_type
(str, required): A descriptive name for the type of AI interaction being traced. This should reflect what is happening when the AI models are called (e.g., “math_solution”, “content_generation”, “translation”).
Example
Here’s a complete example showing how to use trace_event_sync
with an Anthropic client:
The traced events can later be analyzed and uploaded using the Synth SDK’s dataset and upload functionality.
Using with track_messages
You can combine trace_event
decorators with track-messages for more detailed tracking of model interactions. Here’s an example using the async variant:
This approach allows you to:
- Track the overall function execution with
trace_event_async
- Explicitly track the messages and model parameters with
track_messages_async