Skip to main content

What is Semantic Validation?

Semantic validation means checking if a response is correct in meaning, not just in exact wording.

The Problem

The Solution: LLMJudge

LLMJudge uses OpenAI models to evaluate whether responses meet your semantic requirements:
Output:

When to Use LLMJudge

Non-deterministic text content:
  • AI chat responses
  • Generated summaries
  • Creative content
  • Explanations
  • Natural language descriptions
Example:

Setup

1. Get OpenAI API Key

Sign up at platform.openai.com and create an API key.

2. Set Environment Variable

Or use a .env file:
Cost: LLMJudge uses GPT-4o-mini by default (~0.0001perevaluation).Atypicaltestwith10AIresponsescosts 0.0001 per evaluation). A typical test with 10 AI responses costs ~0.001.

Basic Usage

Simple Validation

Expected Behavior Format

Be Specific

Too vague - what makes a response “good”?

Include Key Details

Use Action Verbs

Good verbs: confirms, explains, lists, mentions, asks, provides, includes, summarizes

Custom Scoring Criteria

Define your own scoring dimensions:
Output:
Weights must sum to 1.0

Available Criteria

LLMJudge supports three scoring criteria. You can adjust their weights:
Weights must sum to 1.0. The three criteria are hardcoded and cannot be customized, but you can adjust their relative importance by changing the weights.

Example Weight Adjustments

Use when correctness is most important (e.g., factual questions).

Model Selection

Choose the right model for your use case:

Model Comparison

ModelSpeedCostAccuracyUse Case
gpt-4o-mini⚡ Fast💰 $0.0001⭐⭐⭐ GoodRecommended - Fast, cheap, accurate
gpt-4o⚡ Fast💰💰 $0.0025⭐⭐⭐⭐ BetterComplex evaluations
gpt-4-turbo🐌 Slower💰💰💰 $0.01⭐⭐⭐⭐⭐ BestCritical validations
Start with gpt-4o-mini. It’s 25x cheaper than gpt-4-turbo and handles 95% of use cases well.

Real-World Examples

1. Testing ChatGPT-Style Interface

2. Testing Content Summarization

3. Testing Code Generation

Cost Optimization

LLMJudge makes API calls to OpenAI. Here’s how to minimize costs:

1. Use gpt-4o-mini

Savings: ~0.99per100evaluationsvs0.99 per 100 evaluations vs 25 with gpt-4-turbo

2. Combine with Traditional Validation

Don’t use LLMJudge for everything:

3. Cache Common Validations

If testing same AI responses repeatedly during development:

4. Test Samples in CI/CD

Run full suite less frequently (nightly builds).

Best Practices

For conversations, pass history:

Troubleshooting

Low Scores Despite Good Responses

Problem: judgement.score is 0.6 but response looks good Solutions:
  1. Make expectedBehavior more specific
  2. Lower score threshold: { "gte": 0.7 } instead of { "gte": 0.9 }
  3. Check if response actually meets expectations

High Costs

Problem: Test suite costs $5+ per run Solutions:
  1. Switch to gpt-4o-mini
  2. Use traditional validation where possible
  3. Test fewer examples in CI
  4. Cache results during development

Inconsistent Scores

Problem: Same response gets different scores (0.85, then 0.78, then 0.92) Solution: This is normal with AI. Set threshold ranges:

Missing Context

Problem: LLMJudge doesn’t understand multi-turn conversation Solution: Pass conversation history:

Next Steps

Tool Call Validation

Learn to validate AI tool/function calls

Streaming Responses

Parse and test SSE streams from AI APIs

LLMJudge Block Reference

Complete LLMJudge documentation

AI Chat Example

Full example testing OpenAI chat API