While basic monitoring is designed for simple prompt-to-response interactions, tracing is ideal for complex workflows involving multiple steps, parallel processes, and branching logic. Tracing gives you deeper visibility into your AI application’s behavior and performance.
Create a Trace
A trace represents a complete user interaction or process flow and serves as the top-level container for all monitoring activities. To create a trace, you’ll use thecreateTrace
method:
feature-slug
is a unique identifier that helps you categorize and group related traces in your analytics dashboard. The trace becomes the parent container for all logs and generations in your workflow.
It’s essential to call the
end()
method on your trace when the workflow is complete. Without calling end()
, the trace data will not be sent to Basalt for analysis. This applies even if an error occurs in your workflow.User and Organization Identification
You can associate a trace with specific users and organizations to enable more powerful analytics, filtering, and user-specific insights:identify
method allows you to add or update user and organization information at any point during the trace’s lifecycle. This is particularly useful when user details become available partway through a workflow.
Adding Metadata
Metadata provides additional context that helps you understand what happened during your workflow. You can add metadata when creating components or update it later:Create Logs (Spans)
Logs represent discrete operations or steps within your workflow. In tracing terminology, these are often called “spans”, and Basalt supports several specific types of logs for different purposes.Available Log Types
Thetype
parameter defines the nature of the operation being logged:
span
: A general-purpose operation or step in your workflowfunction
: A specific function call or computationtool
: External tool usage (like a database query or API call)retrieval
: Data retrieval operations (like RAG or knowledge base lookups)event
: Notable events or state changes in your application
Basic Log Creation
Log Lifecycle Methods
Logs have methods to track their lifecycle:Creating Nested Logs
You can create hierarchical structures by nesting logs:Create Generations
Generations represent AI model completions within your workflow. The Basalt SDK provides several ways to create and manage generations.Basic Generation Creation
Prompt Parameters
When using Basalt-managed prompts, you can reference them directly:Variables and Metadata
You can include variables for prompt templates and additional metadata:Ending Generations
There are multiple ways to end a generation depending on what information you have available:Nesting Logs and Generations
Basalt’s tracing system allows you to create hierarchical structures that accurately represent the flow of your application.Creating Nested Structures
Appending Existing Generations
You can append existing generations (such as those fromprompt.get()
) to logs or traces:
append
method is particularly useful when integrating Basalt-managed prompts into your tracing workflow. When you append a generation to a log or trace, the generation is properly associated with its new parent, maintaining the hierarchical structure of your workflow.
Using Evaluators
Evaluators automatically assess the quality of your traces, generations or logs based on predefined criteria. This provides objective metrics about your AI outputs.sampleRate
parameter controls how often evaluations are run, which can help manage costs for large-scale applications.
For more information on available evaluators and how to create custom ones, see the Evaluation documentation.
Complete Example
Here’s a complete example of using tracing to monitor a content processing workflow:- Creating a main trace for the entire workflow
- Using specific log types for different operations
- Appending generations from
prompt.get()
- Parallel processing with multiple logs
- Different ways to end generations (with and without token/cost data)
- Proper error handling and ensuring the trace is always ended
- Recording rich metadata at each step
Best Practices
For effective tracing:- Always end your traces: Make sure to call
end()
for all traces, even in error cases, or the data won’t be sent to Basalt. - Choose meaningful names: Use descriptive names that clearly communicate the purpose of each component.
- Add relevant metadata: Include information that will help with debugging and analysis later.
- Use the right log types: Choose the appropriate type (
span
,function
,tool
, etc.) for each operation to improve analytics. - Structure nested components logically: Create a hierarchy that reflects the logical flow of your application.
- Handle errors properly: Record errors in your trace metadata and ensure traces are ended even when errors occur.
- Consider evaluation needs: Apply evaluators strategically to assess the quality of critical generations.
- Optimize sample rates: Adjust evaluation sample rates based on your volume and budget.
Advanced Topics
For more advanced tracing capabilities, explore:- Evaluation - Assess the quality of your AI outputs
- Experiments - Compare different workflow versions