Advanced Metrics
Advanced metrics use template variables and structured outputs to create context-aware, powerful evaluations. This guide covers all advanced features.Basic vs Advanced Metrics
Basic Metrics
Simple prompts without template variables:- Direct evaluation
- No context resolution
- Faster execution
- Limited context awareness
Advanced Metrics
Prompts with template variables:- Context-aware evaluation
- Template variable resolution
- Richer context
- More accurate evaluations
Template Variables
Template variables inject conversation data into your prompts. They’re written as@VARIABLE_NAME.
Quick Reference
Levels:
- All = Available at conversation, message, and step levels
- Conversation = Only available at conversation level
- Message = Available at message and step levels
- Step = Only available at step level
Conversation-Level Variables
Available for conversation-level evaluations:@HISTORY
Full conversation history (or rolling summary if long):@GOAL
User’s overall goal/intent (extracted from conversation):@LIST_AGENT
Available agents and their tools:@MESSAGES
All messages formatted:@USER_MESSAGES
User messages only:@ASSISTANT_MESSAGES
Assistant messages only:@FIRST_USER_MSG
First user message:@LAST_USER_MSG
Last user message:@LAST_ASSISTANT_MSG
Last assistant message:Message-Level Variables
Includes all conversation-level variables plus:@PREVIOUS_USER_MSG
Previous user message:@PREVIOUS_ASSISTANT_MSG
Previous assistant message:@CURRENT_MESSAGE
Current message being evaluated. Use with nested properties:@CURRENT_MESSAGE.output
Current message content:@CURRENT_MESSAGE.role
Current message role:@CURRENT_STEPS
All steps in current message:@CURRENT_STEPS_COUNT
Number of steps in message:Step-Level Variables
Includes all message-level variables plus:@PREVIOUS_STEP
Previous step in the same message. Use with nested properties:@PREVIOUS_STEP.thinking
Previous step’s reasoning:@PREVIOUS_STEP.tool_call
Previous step’s tool call (JSON):@PREVIOUS_STEP.tool_result
Previous step’s tool result (JSON):@CURRENT_STEP
Current step being evaluated. Use with nested properties:@CURRENT_STEP.thinking
Current step’s reasoning:@CURRENT_STEP.tool_call
Current step’s tool call (JSON):@CURRENT_STEP.tool_result
Current step’s tool result (JSON):@CURRENT_STEP.output_content
Current step’s output text:@CURRENT_STEP.output_structured
Current step’s structured output (JSON):@STEP_NUMBER
Step position (1-indexed):Sequential Mode Variables
Available when using sequential execution mode (pipeline nodes withexecution_mode: "sequential"):
@METRIC_PREVIOUS_RESULT
Previous evaluation result from the same pipeline execution. Only available in sequential mode:- Pipeline node has
execution_mode: "sequential" - There is a previous metric result in the same execution
- Evaluation level is
messageorstep(notconversation)
Using Template Variables
Single Variable
Multiple Variables
Nested Context
JSON Schema for Structured Outputs
When usingoutput_type: "json", define a JSON schema:
Basic Schema
Advanced Schema
Schema Best Practices
- Include Score Field: Always have a primary metric
- Add Reasoning: Include explanation field
- Use Enums: For categorical values
- Keep Simple: 2-5 fields typically sufficient
- Describe Fields: Clear descriptions help LLM
Context Resolution
TurnWise resolves template variables in this order:- Fetch Conversation Data: Load from database
- Extract Goals: If @GOAL needed, extract user goals
- Create/Update Summary: If @HISTORY needed, manage rolling summary
- Resolve Variables: Replace @VARIABLE_NAME with actual data
- Build Prompt: Combine resolved variables with prompt text
- Execute: Send to LLM
Example Advanced Metrics
Example 1: Context-Aware Helpfulness
Example 2: Tool Chain Evaluation
Example 3: Multi-Dimensional Analysis
Performance Considerations
Variable Resolution Cost
Some variables require additional processing:- @GOAL: Requires goal extraction (cached per conversation)
- @HISTORY: May require summary creation/update
- @LIST_AGENT: Requires agent data loading
Optimization Tips
- Reuse Variables: Multiple variables in one prompt = one resolution
- Cache Goals: Goals are cached per conversation
- Reuse Summaries: Summaries are reused across evaluations
- Choose Right Level: Step-level is most granular (and most expensive)
Best Practices
Use Template Variables
Always use variables for context-aware evaluation
Be Specific
Specify what to evaluate and how
Provide Context
Include relevant context variables
Test Schemas
Test JSON schemas before running on all data
Next Steps
Creating Metrics
Learn the basics of metric creation
Running Evaluations
Run your advanced metrics