Agent as Judge as Post-Hook
Using Agent as Judge evaluation as a post-hook for automatic evaluation
This example demonstrates using Agent as Judge evaluation as a post-hook, automatically evaluating every agent response.
Add the following code to your Python file
1from kern.agent import Agent2from kern.db.sqlite import SqliteDb3from kern.eval.agent_as_judge import AgentAsJudgeEval4from kern.models.openai import OpenAIResponses56# Setup database to persist eval results7db = SqliteDb(db_file="tmp/agent_as_judge_post_hook.db")89# Eval runs as post-hook, results saved to database10agent_as_judge_eval = AgentAsJudgeEval(11 name="Response Quality Check",12 model=OpenAIResponses(id="gpt-5.2"),13 criteria="Response should be professional, well-structured, and provide balanced perspectives",14 scoring_strategy="numeric",15 threshold=7,16 db=db,17)1819agent = Agent(20 model=OpenAIResponses(id="gpt-5.2"),21 instructions="Provide professional and well-reasoned answers.",22 post_hooks=[agent_as_judge_eval],23 db=db,24)2526response = agent.run("What are the benefits of renewable energy?")27print(response.content)2829# Query database for eval results30print("Evaluation Results:")31eval_runs = db.get_eval_runs()32if eval_runs:33 latest = eval_runs[-1]34 if latest.eval_data and "results" in latest.eval_data:35 result = latest.eval_data["results"][0]36 print(f"Score: {result.get('score', 'N/A')}/10")37 print(f"Status: {'PASSED' if result.get('passed') else 'FAILED'}")38 print(f"Reason: {result.get('reason', 'N/A')[:200]}...")Set up your virtual environment
1uv venv --python 3.122source .venv/bin/activate1uv venv --python 3.122.venv\Scripts\activateInstall dependencies
1uv pip install -U kern-ai openaiExport your OpenAI API key
1export OPENAI_API_KEY="your_openai_api_key_here"1$Env:OPENAI_API_KEY="your_openai_api_key_here"Run the example
1python agent_as_judge_post_hook.py