Async Agent as Judge
Asynchronous evaluation with Agent as Judge
This example demonstrates an asynchronous Agent as Judge evaluation with async callbacks.
Add the following code to your Python file
1import asyncio23from kern.agent import Agent4from kern.db.sqlite import AsyncSqliteDb5from kern.eval.agent_as_judge import AgentAsJudgeEval, AgentAsJudgeEvaluation6from kern.models.openai import OpenAIResponses789async def on_evaluation_failure(evaluation: AgentAsJudgeEvaluation):10 """Async callback triggered when evaluation fails (score < threshold)."""11 print(f"Evaluation failed - Score: {evaluation.score}/10")12 print(f"Reason: {evaluation.reason}")131415async def main():16 # Setup database to persist eval results17 db = AsyncSqliteDb(db_file="tmp/agent_as_judge_async.db")1819 agent = Agent(20 model=OpenAIResponses(id="gpt-5.2"),21 instructions="Provide helpful and informative answers.",22 db=db,23 )2425 response = await agent.arun("Explain machine learning in simple terms")2627 evaluation = AgentAsJudgeEval(28 name="ML Explanation Quality",29 model=OpenAIResponses(id="gpt-5.2"),30 criteria="Explanation should be clear, beginner-friendly, and avoid jargon",31 scoring_strategy="numeric",32 threshold=9,33 on_fail=on_evaluation_failure,34 db=db,35 )3637 result = await evaluation.arun(38 input="Explain machine learning in simple terms",39 output=str(response.content),40 print_results=True,41 print_summary=True,42 )434445if __name__ == "__main__":46 asyncio.run(main())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_async.py