Skip to main content
This example demonstrates how to use Agent as Judge evaluation to assess the main agent’s output as a background task. Unlike blocking validation, background evaluation:
  • Does NOT block the response to the user
  • Logs evaluation results for monitoring and analytics
  • Can trigger alerts or store metrics without affecting latency
Use cases:
  • Quality monitoring in production
  • Compliance auditing
  • Validating hallucinations or other inappropriate content
1

Create a Python file

background_output_evaluation.py
2

Set up your virtual environment

3

Install dependencies

4

Export your OpenAI API key

5

Run the server

6

Test the endpoint

The response will be returned immediately. The evaluation runs in the background and results are stored in the database.

What Happens

  1. User sends a request to the agent
  2. The agent processes and generates a response
  3. The response is sent to the user immediately
  4. Background evaluation runs:
    • AgentAsJudgeEval automatically evaluates the response against the criteria
    • Scores the response on a scale of 1-10
    • Stores results in the database

Production Extensions

In production, you could extend this pattern to:
ExtensionDescription
Database StorageStore evaluations for analytics dashboards
AlertingUse on_fail callback to send alerts when evaluations fail
ObservabilityLog to platforms like Datadog or OpenTelemetry
A/B TestingCompare response quality across model versions
Training DataBuild datasets for fine-tuning
Background evaluation is ideal for quality monitoring without impacting user experience. For scenarios where you need to block bad responses, use synchronous hooks instead.

Global Background Hooks

Run all hooks as background tasks

Per-Hook Background

Mix synchronous and background hooks