Skip to main content
Code-based evaluators are deterministic functions that check specific, objective criteria in your agent’s outputs. They work like unit tests - fast, reliable, and easy to debug.

When to Use Code-based Eval

Use code-based evaluators for:
  • Tool usage patterns - Did the agent call the right tools?
  • Output structure - Does the response match a required schema?
  • Keyword matching - Does the answer mention specific terms?
  • Workflow verification - Did the agent follow the expected sequence?
Code-based evaluators excel at objective, binary checks. For subjective criteria like “helpfulness” or “tone”, use LLM-as-judge evaluation.

Basic Evaluator

The simplest evaluator returns a boolean:
simple_evaluator.py
Use it in an experiment:
run_experiment.py

Evaluator Signatures

Evaluators can accept different parameters:

Outputs Only

Check the agent’s output:
outputs_only.py

Run Context

Access the full trace to inspect tool calls, intermediate steps:
run_context.py

With Expected Output

Compare against reference answers:
with_expected.py

Real-World Example: Schema Check

This evaluator from the OfficeFlow course checks that agents inspect database schema before querying:
eval_schema_check.py
This evaluator uses pattern matching to identify schema-inspection queries, then checks the order of tool calls to verify proper workflow.

Return Formats

Boolean

Simplest format - pass/fail:
boolean_return.py

Score Dictionary

Provide score and explanation:
score_dict.py

Numeric Score

For continuous metrics:
numeric_score.py

Common Patterns

Check Tool Usage

check_tools.py

Validate Output Structure

validate_structure.py

Check Answer Quality

check_quality.py

Verify Workflow Order

workflow_order.py

Testing Evaluators

Test your evaluators before running full experiments:
test_evaluator.py

Best Practices

Provide Clear Comments

Always explain why an evaluation passed or failed:
clear_comments.py

Handle Edge Cases

Make evaluators robust to unexpected inputs:
robust_evaluator.py

Name Evaluators Descriptively

descriptive_names.py

Limitations

Code-based evaluators struggle with:
  • Subjective qualities - “Is this response helpful?”
  • Semantic equivalence - Different phrasings with same meaning
  • Tone and style - Professional, friendly, empathetic
  • Nuanced reasoning - Complex multi-step logic
For these criteria, use LLM-as-judge evaluation.

Next Steps

LLM-as-Judge

Evaluate subjective criteria

Pairwise Eval

Compare two agent versions