Tracing and Debugging
LLMKit provides powerful tracing and debugging tools to help you understand how your prompts are processed, identify issues, and optimize performance. This guide explains how to use these tools effectively.
Trace Overview
Every LLM call in LLMKit generates a detailed trace that captures the entire lifecycle of the request, from template rendering to final response. Traces provide visibility into:
- Template variable resolution
- Conditional logic evaluation
- Prompt rendering
- Token usage
- Response generation
- Error handling
Accessing Traces
Viewing Recent Traces
To view recent traces:
- Navigate to the "Traces" section in the LLMKit interface
- Browse the list of recent traces
- Click on a trace to view its details
Finding Specific Traces
You can search for traces using various filters:
- By prompt ID
- By time range
- By status (success, error)
- By containing specific text
- By user or client application
Trace Export
For detailed analysis, you can export traces in various formats:
llmkit trace export --id=trace-123abc --format=json
Supported formats include JSON, CSV, and OpenTelemetry.
Trace Details
A typical trace contains the following sections:
1. Request Summary
Overview information about the request:
- Timestamp
- Prompt ID and version
- Execution time
- Status
- Token usage
- Client information
2. Template Processing
Details about how the template was processed:
- Template variables and their values
- Conditional branch evaluation
- Loop iterations
- Rendered template (final prompt sent to the model)
3. LLM Request
Information about the request to the LLM provider:
- Provider and model
- Request parameters (temperature, top_p, etc.)
- Request headers
- Token count
4. LLM Response
Details about the response from the LLM:
- Raw response
- Completion tokens
- Response time
- Token usage
5. Errors and Warnings
Any issues that occurred during processing:
- Template rendering errors
- Variable validation errors
- Provider API errors
- Token limit warnings
Debugging with Traces
Traces are invaluable for debugging prompt issues:
Template Rendering Issues
If your prompt isn't rendering correctly:
- Check the "Template Processing" section
- Look for undefined variables or syntax errors
- Examine the conditional branch evaluation
- Verify the final rendered template
Unexpected Responses
If your LLM responses aren't what you expected:
- Check the rendered template to ensure it contains what you think
- Review the request parameters (temperature, etc.)
- Look at token usage to see if you're hitting limits
- Check for any provider-specific warnings
Performance Issues
If your prompts are slow:
- Check the execution time breakdown
- Look for slow template rendering (complex conditionals)
- Check provider response time
- Review token usage (larger prompts take longer)
Live Debugging
LLMKit provides tools for live debugging:
Debug Mode
Enable debug mode for a prompt:
- Navigate to the prompt's detail page
- Click the "Settings" tab
- Enable "Debug Mode"
- Click "Save"
In debug mode, LLMKit provides extra information in traces and can log debug messages.
Debug Console
Use the debug console for real-time insights:
- Navigate to "Developer" > "Debug Console"
- Watch live traces as they occur
- Filter for specific prompts or clients
- Set breakpoints to pause execution
Template Preview
For template debugging:
- Navigate to a prompt's detail page
- Click the "Template" tab
- Enter test values for variables
- Click "Preview" to see how the template renders
Trace Analytics
LLMKit provides analytics based on trace data:
Performance Dashboard
Monitor overall performance:
- Navigate to "Analytics" > "Performance"
- View metrics like:
- Average response time
- Token usage trends
- Error rates
- Requests per minute
Prompt Performance
Analyze specific prompt performance:
- Navigate to a prompt's detail page
- Click the "Analytics" tab
- View prompt-specific metrics:
- Usage frequency
- Average response time
- Token efficiency
- Error rates
Custom Reports
Create custom reports based on trace data:
- Navigate to "Analytics" > "Custom Reports"
- Click "New Report"
- Select dimensions and metrics
- Choose visualization type
- Click "Generate Report"
Optimizing with Trace Data
Use trace data to optimize your prompts:
Token Usage Optimization
Reduce token usage and costs:
- Identify high-token prompts in the analytics dashboard
- Review trace details to find optimization opportunities
- Consider:
- Shortening instructions
- Removing redundant context
- Streamlining examples
- Using more efficient variable formats
Performance Tuning
Improve response times:
- Identify slow prompts in the analytics dashboard
- Check trace execution breakdowns
- Consider:
- Simplifying complex template logic
- Caching static content
- Using shorter prompts
- Switching to faster models
Error Reduction
Minimize errors:
- Review error trends in the analytics dashboard
- Examine traces for common error patterns
- Implement:
- Better variable validation
- Fallback mechanisms
- Token limit management
- Retry logic
Integrating with External Systems
LLMKit's tracing system integrates with external monitoring and observability platforms:
OpenTelemetry Export
Export trace data to OpenTelemetry-compatible systems:
llmkit configure --tracing.exporter=otlp --tracing.endpoint=https://your-collector:4317
Prometheus Metrics
Export metrics to Prometheus:
llmkit configure --metrics.exporter=prometheus --metrics.endpoint=:9090
Logging Integration
Send trace logs to external logging systems:
llmkit configure --logging.exporter=fluentbit --logging.endpoint=http://your-fluentbit:8888
Best Practices
For effective tracing and debugging:
- Enable Comprehensive Tracing: In development environments, enable detailed tracing
- Use Structured Logging: Add structured context to your traces
- Sample in Production: In high-volume production, use sampling to reduce overhead
- Set Up Alerts: Configure alerts for unusual patterns or errors
- Regularly Review Analytics: Schedule time to review trace analytics for optimization opportunities
Example: Debugging Workflow
Here's an example of a complete debugging workflow:
- Identify the Issue:
- Notice unexpected responses in a specific scenario
- Locate relevant traces using search filters
- Analyze Traces:
- Examine the rendered template
- Check variable values
- Review model parameters
- Look for warnings or errors
- Test Hypotheses:
- Modify prompt in the template preview
- Adjust variables or parameters
- Test different inputs
- Implement Solution:
- Update prompt template
- Add input validation
- Adjust model parameters
- Create a new prompt version
- Verify Fix:
- Run tests with problematic inputs
- Check new traces to confirm resolution
- Monitor analytics to ensure the fix holds
By following this workflow and leveraging LLMKit's tracing tools, you can efficiently identify and resolve issues with your prompts.
Conclusion
Effective tracing and debugging are essential for developing reliable, high-quality LLM applications. LLMKit's comprehensive tracing system provides the visibility and tools you need to understand, debug, and optimize your prompts at every stage of the development lifecycle.