Skip to main content

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
Use advanced metrics for better results - Template variables provide crucial context for accurate evaluation.

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):
Resolves to formatted conversation:
Note: For long conversations, TurnWise uses rolling summaries to keep context manageable while preserving important information.

@GOAL

User’s overall goal/intent (extracted from conversation):
Resolves to extracted goal:
Note: Goals are automatically extracted from user messages using intent classification. The goal is cached per conversation for efficiency.

@LIST_AGENT

Available agents and their tools:
Resolves to formatted list:

@MESSAGES

All messages formatted:
Resolves to:

@USER_MESSAGES

User messages only:
Resolves to:

@ASSISTANT_MESSAGES

Assistant messages only:
Resolves to:

@FIRST_USER_MSG

First user message:
Resolves to:

@LAST_USER_MSG

Last user message:
Resolves to:

@LAST_ASSISTANT_MSG

Last assistant message:
Resolves to:

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:
Resolves to formatted message:

@CURRENT_MESSAGE.output

Current message content:
Resolves to:

@CURRENT_MESSAGE.role

Current message role:
Resolves to:

@CURRENT_STEPS

All steps in current message:
Resolves to formatted steps:

@CURRENT_STEPS_COUNT

Number of steps in message:
Resolves to:

Step-Level Variables

Includes all message-level variables plus:

@PREVIOUS_STEP

Previous step in the same message. Use with nested properties:
Resolves to formatted step:

@PREVIOUS_STEP.thinking

Previous step’s reasoning:
Resolves to:

@PREVIOUS_STEP.tool_call

Previous step’s tool call (JSON):
Resolves to:

@PREVIOUS_STEP.tool_result

Previous step’s tool result (JSON):
Resolves to:

@CURRENT_STEP

Current step being evaluated. Use with nested properties:
Resolves to formatted step:

@CURRENT_STEP.thinking

Current step’s reasoning:
Resolves to:

@CURRENT_STEP.tool_call

Current step’s tool call (JSON):
Resolves to:

@CURRENT_STEP.tool_result

Current step’s tool result (JSON):
Resolves to:

@CURRENT_STEP.output_content

Current step’s output text:
Resolves to:

@CURRENT_STEP.output_structured

Current step’s structured output (JSON):
Resolves to:

@STEP_NUMBER

Step position (1-indexed):
Resolves to:

Sequential Mode Variables

Available when using sequential execution mode (pipeline nodes with execution_mode: "sequential"):

@METRIC_PREVIOUS_RESULT

Previous evaluation result from the same pipeline execution. Only available in sequential mode:
Resolves to JSON of previous metric’s output:
Note: This variable is only available when:
  • Pipeline node has execution_mode: "sequential"
  • There is a previous metric result in the same execution
  • Evaluation level is message or step (not conversation)

Using Template Variables

Single Variable

Multiple Variables

Nested Context

JSON Schema for Structured Outputs

When using output_type: "json", define a JSON schema:

Basic Schema

Advanced Schema

Schema Best Practices

  1. Include Score Field: Always have a primary metric
  2. Add Reasoning: Include explanation field
  3. Use Enums: For categorical values
  4. Keep Simple: 2-5 fields typically sufficient
  5. Describe Fields: Clear descriptions help LLM

Context Resolution

TurnWise resolves template variables in this order:
  1. Fetch Conversation Data: Load from database
  2. Extract Goals: If @GOAL needed, extract user goals
  3. Create/Update Summary: If @HISTORY needed, manage rolling summary
  4. Resolve Variables: Replace @VARIABLE_NAME with actual data
  5. Build Prompt: Combine resolved variables with prompt text
  6. 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

  1. Reuse Variables: Multiple variables in one prompt = one resolution
  2. Cache Goals: Goals are cached per conversation
  3. Reuse Summaries: Summaries are reused across evaluations
  4. 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