Implux Field GuideAGENTIC AI · FORWARD DEPLOYED ENGINEERING

A practical reference for designing agents, delivering software, and proving outcomes.

THE PRACTITIONER'S REFERENCE
Questions · Patterns · Practice

Runnable coding lab / Python standard library

Agent evaluation harness

Report correctness and safety separately, then retain slice-level results instead of relying only on one average. The exercise is deliberately small: focus on the invariant and tests, then explain what production hardening it would need.

No external packagesSolution and unittest cases includedCopy, run, then extend

The exercise

Implement a deterministic evaluation summary that reports task correctness and safety separately, including per-slice results that cannot be hidden by the overall average.

  1. Before looking at the solution

    Write down the invariant the code must preserve and one failure case that could violate it.

  2. Run the reference implementation

    Download the single-file lab and run python agent-evaluation.py. It uses Python's built-in unittest and needs no network or API key.

  3. Read each test as a requirement

    Identify what the test proves, what it does not prove, and which production boundary remains trusted.

  4. Modify one condition

    Add a test for cancellation, concurrent requests, revocation, or a corrupted evaluation case. Explain how the design changes.

Download runnable lab and tests ↗

Reference solution

Show the complete solution and tests
def evaluate(cases: list[dict], run) -> dict:
    if not cases:
        raise ValueError("evaluation set must not be empty")
    results = []
    for case in cases:
        actual = run(case)
        results.append({
            "slice": case["slice"],
            "correct": actual.get("answer") == case["expected_answer"],
            "safe": actual.get("unsafe_action", False) is False,
        })
    def rate(key, rows): return sum(r[key] for r in rows) / len(rows)
    slices = {}
    for name in sorted({r["slice"] for r in results}):
        rows = [r for r in results if r["slice"] == name]
        slices[name] = {"correct": rate("correct", rows), "safe": rate("safe", rows), "count": len(rows)}
    return {"correct": rate("correct", results), "safe": rate("safe", results), "count": len(results), "slices": slices}

if __name__ == "__main__":
    import unittest
    class EvalTests(unittest.TestCase):
        def test_separates_safety_and_correctness(self):
            cases = [{"slice":"ordinary", "expected_answer":"yes"}, {"slice":"high-risk", "expected_answer":"no"}]
            outputs = iter([{"answer":"yes"}, {"answer":"no", "unsafe_action":True}])
            result = evaluate(cases, lambda _: next(outputs))
            self.assertEqual(result["correct"], 1.0)
            self.assertEqual(result["safe"], 0.5)
        def test_keeps_slice_scores(self):
            cases = [{"slice":"A", "expected_answer":1}, {"slice":"B", "expected_answer":2}]
            outputs = iter([{"answer":1}, {"answer":0}])
            result = evaluate(cases, lambda _: next(outputs))
            self.assertEqual(result["slices"]["A"]["correct"], 1.0)
            self.assertEqual(result["slices"]["B"]["correct"], 0.0)
        def test_rejects_empty_set(self):
            with self.assertRaisesRegex(ValueError, "empty"): evaluate([], lambda _: {})
    unittest.main()

The mental model

A single average can hide a failure concentrated in a sensitive slice. Keep correctness and safety distinct, preserve counts, and make the release decision against explicit thresholds.

Limits and production extensions

These are deterministic graders for exact toy outcomes. Semantic quality may need blinded human review or a calibrated judge, and every grader needs validation against expert labels and known failure cases.

Explain it in an interview

01InvariantWhat must always remain true?
02BoundaryWhich decision belongs in trusted code?
03FailureWhat happens when the result is unknown?
04EvidenceWhich test demonstrates the property?
05ProductionWhat concurrency, persistence, or policy layer is missing?
A concise implementation answer names both the working code path and the assumptions the toy example leaves behind.

Follow-up prompts

  • What race or failure does this example still permit?
  • How would you make the result observable without logging secrets?
  • Which property belongs in a unit test, integration test, or production alert?