0%
booting mobile runtime
All Projects
·14 min read

Agent Referee: The Senior Engineer Your AI Agent Never Had

Your agent is a confident intern with no supervisor. It will answer things it has no business answering and nobody is checking its work unless you build that yourself. Agent Referee is a one-decorator Python library that plugs into any agent, in any framework, and gives it a bouncer, a report card, and a flight recorder, while actually explaining what it's doing instead of just linking you a doc and vanishing.

PythonLLM ObservabilityGuardrailsEvaluationOpenTelemetryCLIOpen SourceAI Agents
View on GitHub

Okay but what even is this

Your AI agent is, structurally, a very confident intern who has never once been told "actually, no." It will answer a question it has no business answering. It will occasionally make something up with the full conviction of a guy who definitely read the whole report. And unless you personally sat down and built the thing that checks its work, nothing is checking its work. It just ships. It looks fine in the five conversations you tried by hand. Then it's live, and it's somebody else's problem.

Agent Referee is the senior engineer you wish was reviewing that intern's work. Not the kind who drops a link to a 40-page doc and disappears. The kind who sits next to you, watches what just happened, and tells you exactly what it means while it's happening. One pip install. One decorator on top of the function you already wrote. It doesn't touch your agent's code, it just starts watching, and it explains every single thing it catches instead of assuming you already speak the vocabulary.

pip install agent-referee
referee demo

That's it. That's the whole first move. No API key, no config file, no real agent involved yet, it's running against a tiny fake pizza-shop bot bundled into the package just so you can watch the machinery work before you trust it with anything that matters.

Agent Referee
Whatever you built
Bouncerat the door
Report cardgraded later
Flight recorderalways on

Why I actually built this

Nobody's AI agent comes with a check-engine light. It answers questions, it looks basically fine in the handful of conversations you personally tried, and then it's live. If it leaks a phone number back into a chat log, or somebody figures out the exact phrase that makes it forget its own instructions, or its answers just quietly get worse after you tweak a prompt three weeks from now, there's no alarm. There's just a worse product, and no diff to blame.

The concepts that fix this already exist, evaluation, guardrails, observability are not new ideas. Every serious production AI system already has some version of them. The problem is that wiring up any one of them from scratch is its own entire afternoon, and most people building their first agent have never had a reason to learn what a "guardrail" is, let alone what an OpenTelemetry span is. So it gets skipped. Everybody finds out the hard way instead.

I wanted something that teaches you as it protects you. Not a wizard that silently does seventeen things and hands you a green checkmark you don't understand. Something that holds your hand through the actual concept the first time it matters, then gets out of your way once you've got it. That's the whole design brief, honestly. A guide first, a tool second.

Here's it actually running, word for word

Not a mockup. This is the literal, unedited output of typing referee demo into a fresh terminal with nothing installed and no API key anywhere near it.

Agent Referee demo, no config file, no API key, nothing installed beyond this package.

Meet the demo agent: a pretend pizza-shop assistant, built into this package just for
this walkthrough. It's not a real AI, just a few lines of if/else, the point isn't the
agent, it's watching Agent Referee work around it. Three short scenes:

── SCENE 1, a normal question ──────────────────────────────────────

customer> What time do you close?

(behind the scenes, this is the real trace, printed live as it happens:)
  · input_guardrail   (1.0ms)   guardrail.allowed = True
  · agent_call        (0.0ms)
  · output_guardrail  (0.1ms)   guardrail.allowed = True

agent> We're open 11 AM to 11 PM, every day.

── SCENE 2, someone pastes personal info by accident ───────────────

customer> call me back at 9876543210 please

(the input guardrail catches this BEFORE the agent function ever runs:)
  · input_guardrail  (0.1ms)   guardrail.allowed = False
      block_reason = Blocked: message looks like it contains a phone.
      Plain regex check, no AI call involved.

agent> I can't help with that. (Blocked: message looks like it contains a phone.)

── SCENE 3, grading the answer from Scene 1 ─────────────────────────

A golden dataset says a correct answer must mention '11 AM' or '11 PM'.
Checking the real answer from Scene 1 against that:

[PASS] Looked for any of ['11 AM', '11 PM'] in the response, found one.

That's the whole loop: a guardrail blocking bad input before your agent ever sees it,
a trace recording every step as it happens, and an evaluator grading the answer after
the fact. Zero setup, zero API key, all three pillars.

→ NEXT: referee init

Thirty seconds, start to finish, and you've already watched all three pillars work without risking a single real API call.

The whole shape of it, so you don't have to hold it in your head

Message comes in

Whatever your user just typed, unfiltered.

Guardrails check it

Cheapest checks first, so nothing expensive runs on an obvious no.

rate limitPIIinjectionscope

Your agent runs

Completely untouched. Same code, any framework.

Answer gets checked

Leaked PII, toxicity, does it even match reality.

PII leaktoxicitygrounded?

Back to your user

Only if everything above said yes.

Blocked at either checkpoint, your agent never runs, your user gets told why
Every step above gets taped

A real OpenTelemetry trace, printed to your terminal as it happens. Always on.

referee eval run grades it later

Separate command, your own schedule. Offline report card, not a live gate.

That's the entire mental model. Everything live wraps around your function automatically the second the decorator's in place. Evaluation is the one deliberately separate piece, a command you run on your own schedule, because grading whether an answer was good is a completely different job from deciding whether a message was safe enough to let through right now, and mixing those two up is the single most common way people misunderstand this kind of tooling.

The three things it's actually doing for you

It's a bouncer. A guardrail doesn't care if an answer is good, it only cares if a message is safe and on-topic enough to let through the door at all. Cheapest checks run first so an obviously bad message never triggers an expensive one: a plain counter for rate limiting, regex for PII and prompt injection, and only then, if nothing else already said no, one real LLM call to check whether the message is even on-topic for your agent. Output gets the same treatment in reverse before your user ever sees it.

It's a report card. Completely separate job, completely separate timing. You write down a list of questions and what a correct answer should mention, called a golden dataset, which is just a fancy name for a list you already know how to write. Then referee eval run asks your real agent every single one of them and checks the real answers against your list automatically, instead of you reading every response by hand forever.

It's a flight recorder. This one doesn't change your agent's behavior at all, it just writes down what actually happened, in enough detail that a human can debug it later. Real OpenTelemetry, the same open standard Google Cloud and AWS run on, not a reinvented wheel. Every guardrail check and your own agent call gets wrapped in a timed, nested span, so a trace of one request shows you exactly where the time went and exactly which check, if any, pulled the plug.

A guardrail can't tell you if an answer that got through was actually any good. A grading run can't stop a bad message from reaching your agent, it only finds out afterward. And neither one tells you why one particular call took forty seconds instead of four. That's three different jobs wearing three different hats, on purpose.

The one guardrail you still wire by hand

Everything above happens automatically once the decorator's on your function, except one thing, for an honest reason, not laziness. If your agent calls a tool, actually placing an order, actually sending an email, the decorator physically cannot see inside that decision, it only wraps the outer function. Blocking a specific tool call needs one line, right before the tool actually fires:

from referee.guardrails.execution_layer import check_tool_call

result = check_tool_call(tool_name, tool_args, session_id, config)
if not result["allowed"]:
    return result["reason"]
# only now does your tool actually run

That's the only hand-wired part anywhere in the whole project.

Evaluation, by the numbers

Five real scoring methods live behind referee eval run, and they trade off speed, cost, and how much slack they give you for different phrasing:

MethodWhat it's actually doing
Exact matchDoes the real answer contain one of your required phrases. Free, instant, and it will absolutely fail you for saying the right thing in the wrong words.
Refusal checkSame idea, but checking your agent actually said no when it was supposed to say no.
ROUGE overlapCounts shared words against a reference answer. More forgiving than exact match, still has no idea what the words mean.
Embedding similarityCompares meaning, not wording, using a local sentence-embedding model, no API call. Catches a right answer phrased completely differently. Needs the [embedding] extra, since it drags in PyTorch to do it.
LLM-as-judgeA second model grades the real answer against a rubric you write and hands back a score with a reason. The most flexible one, and the only one that spends a real API call per question.

Every run saves a full report, prints a plain-English reason for each pass or fail, and exits with a non-zero status if anything marked critical failed, specifically so this exact command can block a bad deploy in CI without anyone having to remember to check a dashboard first.

You can also attack your own guardrails on purpose, for free: referee guardrails test fires eight fixed adversarial strings straight at the checking functions themselves, zero calls to your real agent, and prints a colored pass or fail table. A guardrail you've never actually tried to break is a guardrail you're just hoping works.

Agent Referee's trace output, live in a terminal

Actually setting it up, click by click

This is the "teach first, then do" flow, the thing it's named referee init and not referee setup for. Every step tells you what's happening before it hands you a command.

  1. Virtual environment, then install. python3 -m venv .venv && source .venv/bin/activate, then pip install agent-referee. It's fast on purpose, no PyTorch dragged in just to say hello.
  2. referee init. Three questions, never a password: where your agent function lives, one sentence describing what it's allowed to talk about, and which of Gemini, OpenAI, or Anthropic you're using. Writes a plain-text referee.yaml and makes sure your .env is in .gitignore for you, since your actual key lives in a file on your own machine, read the same boring way the official SDKs already read it, not typed into a wizard anywhere.
  3. One decorator, on your existing function. @referee.protect(config="referee.yaml"), directly above the function that takes a question and returns an answer. Nothing else about your code changes. If you built with Google's ADK, the shape's a little different since it speaks in async events instead of a plain function, there's a complete working adapter for that in examples/adk_example.py.
  4. referee try "a real question". Call your actual agent once, by hand, right there in the terminal. You'll probably see guardrail and trace lines stack up above a clearly boxed final answer, something close to what Scene 1 showed above but against your real code this time. If it doesn't look right, better to catch it here than three steps later.
  5. referee dataset new. A fully offline, zero-API-key wizard that walks you through writing your first golden dataset, question by question.
  6. referee eval run. The report card moment. Every question gets asked, every answer gets graded, a full report lands in reports/.
  7. referee guardrails test. Throw the eight built-in attacks at your own defenses and watch the scoreboard.
  8. referee dashboard, if you want it. pip install "agent-referee[dashboard]" first, this is the one genuinely optional extra, and it's optional for an honest reason: it's built on Streamlit, which pulls in roughly 180MB of its own dependencies for a table you'll probably glance at twice a day. That weight lands inside your own project's virtual environment, not anywhere permanent, and it's explained here instead of hidden in a footnote.

Here's what that dashboard actually looked like on a real run, not a staged one:

The Agent Referee dashboard showing a real 67% pass rate and two scope-check attacks that got through while PII and injection were both caught

Sixty-seven percent, not a hundred. Two of the scope attacks slipped past while every PII and injection attempt got caught cold. Left in deliberately, because a tool that only ever shows you green checkmarks hasn't actually taught you anything about what a real failure looks like.

Plays well with whatever you already built

Agent Referee never imports LangGraph, CrewAI, ADK, or any provider's SDK. It only ever sees one shape: a function that takes a string in and hands a string back. That's the whole integration surface, which is also the entire reason it'll keep working with whatever framework shows up next year without anyone touching this library. Four complete, runnable examples ship in the repo, not snippets:

  • Plain Python, any provider. Wrap the function that calls the SDK directly. The simplest possible version.
  • LangGraph. The decorator sits on the one function that calls graph.invoke() and hands back the text. Whatever looping happens inside the graph stays completely invisible to Agent Referee, and that's the point.
  • CrewAI. Same idea around a function that spins up a one-task crew and returns its raw result.
  • Google ADK. The one genuinely different shape. ADK's Runner speaks in async events instead of a plain call, so a small async adapter awaits the run, pulls the final text out of the event stream, and a synchronous wrapper hands that to the decorator like normal.

What's actually under the hood

Core languagePython 3.9+

No surprise dependencies, no compiled extensions you need a C toolchain for.

CLIClick

Every command above, from referee demo to referee dashboard, is a Click command.

Configreferee.yaml + python-dotenv

Plain YAML you can read and commit, plus a .env that referee init keeps out of git automatically.

TracingOpenTelemetry SDK

Console exporter by default, or point it at any OTLP backend, Langfuse Cloud's free tier for instance, for permanent searchable history.

Guardrail regexStandard library re

PII and injection checks run on zero dependencies and zero API calls.

Text-overlap scoringrouge-score

ROUGE-1 recall, the fast, cheap half of the evaluation story.

Semantic scoringsentence-transformers + PyTorch

Optional. This is the extra that actually costs you disk space, over five times the base install's size.

LLM callsreferee/providers.py

One thin layer routing to Gemini, OpenAI, or Anthropic official SDKs, whichever you picked.

DashboardStreamlit

Optional, roughly double the base install's size, opt-in for exactly that reason.

Packagingpyproject.toml, on PyPI as agent-referee

pip install agent-referee, nothing fancier.

All three provider SDKs ship in the base install already, they add up to roughly 75MB combined, comfortably under the two optional extras above. That number wasn't a guess, it came from actually checking install sizes before deciding what gets to be optional. Weight only where weight is earned.

What building this actually taught me

Teaching and tooling pull in opposite directions by default, and you have to fight for the teaching on purpose. The lazy version of this project prints a clean trace and moves on with its life. The useful version stops you the first time something real happens and explains, once, what a span even is, with a dotfile quietly remembering it already told you so it never repeats itself. That one-time explanation took more rewrites than the actual tracing code sitting underneath it.

"Fails open" is a decision, not an accident. Every check that spends a real API call, the scope check, the toxicity check, explicitly lets a message through if that call errors out or comes back unparseable. The alternative is a guardrail that turns a slow network into a full outage, which is a worse failure than the one it was supposed to prevent. The free local checks get to be strict, because they can never go down. The paid ones get to be forgiving, for the same reason.

Keeping the three pillars structurally separate was the actual hard part. It would've been so easy to bolt "is this good" onto the same code path as "is this safe" and call it one feature with a progress bar. They answer different questions on different timescales, and collapsing them would've meant quietly lying to anyone using this about what a passing check actually proves.

If any of this sounds like something you've been meaning to set up for your own agent and never quite got around to, that's genuinely the entire reason this exists. pip install agent-referee, run the demo, see what it catches. The full source, the complete guided flow, and all four framework examples are sitting right there on GitHub, waiting for someone to go poke at them.