{ "cells": [ { "cell_type": "markdown", "id": "538c986e", "metadata": {}, "source": [ "# Evaluating Agent Reliability: Tool-Call Accuracy and Hallucination Rate\n", "\n", "Most write-ups on evaluating LLM agents measure one thing: did the final\n", "answer match the expected answer. That's necessary but not sufficient for\n", "an agent you're going to put in front of a customer or a regulated\n", "workflow. Two failures can hide behind a correct-looking final answer:\n", "\n", "1. **The agent called the wrong tool** but got lucky, or partially\n", " recovered.\n", "2. **The agent called the right tool but fabricated detail** beyond what\n", " the tool actually returned -- the answer reads as more confident and\n", " more specific than what it's actually grounded in.\n", "\n", "This notebook builds a small, generic evaluation harness that scores\n", "these two failure modes *separately*, on a synthetic research-assistant\n", "agent (a couple of documents, a calculator tool). It's deliberately not\n", "tied to any one company's data -- the point is the methodology, which is\n", "the same methodology I use for evaluating agents in production, just\n", "applied here to a toy domain I can share.\n", "\n", "Three mock agent policies are evaluated with the same harness, to show\n", "it actually discriminates between reliability profiles rather than just\n", "producing a single pass/fail number:\n", "\n", "- `GoodPolicy` -- calls the correct tool, states only what the tool returned.\n", "- `WrongToolPolicy` -- sometimes calls the wrong tool for the question type.\n", "- `HallucinatingPolicy` -- calls the correct tool but pads the final answer\n", " with invented specifics.\n", "\n", "Swapping in a real model (e.g. Claude with real tool-use) means replacing\n", "the `.answer()` method on a policy class with an actual API call -- the\n", "eval functions below don't change." ] }, { "cell_type": "markdown", "id": "18444f5a", "metadata": {}, "source": [ "## The toy environment\n", "\n", "A tiny \"knowledge base\" and two tools: `search_docs` (keyword-match\n", "retrieval) and `calculator`. Small on purpose, so every eval result below\n", "is easy to verify by hand -- an eval harness you can't manually audit on\n", "its own test set isn't trustworthy on a harder one." ] }, { "cell_type": "code", "execution_count": 1, "id": "c4e59731", "metadata": { "execution": { "iopub.execute_input": "2026-09-13T02:33:52.486933Z", "iopub.status.busy": "2026-09-13T02:33:52.486777Z", "iopub.status.idle": "2026-09-13T02:33:52.496477Z", "shell.execute_reply": "2026-09-13T02:33:52.495553Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "doc_founding: Anthropic was founded in 2021 by former OpenAI researchers.\n", "doc_capital: The capital of France is Paris.\n", "doc_mission: Anthropic's mission is to ensure AI systems are safe and beneficial.\n", "\n", "Q1: 'When was Anthropic founded?' (expects tool=search_docs)\n", "Q2: 'What is the capital of France?' (expects tool=search_docs)\n", "Q3: 'What is 12 * 7?' (expects tool=calculator)\n", "Q4: \"What is Anthropic's mission?\" (expects tool=search_docs)\n", "Q5: 'What is 100 / 4?' (expects tool=calculator)\n" ] } ], "source": [ "from reliability_eval import (\n", " DOCS, TEST_SET, GoodPolicy, WrongToolPolicy, HallucinatingPolicy,\n", " evaluate_policy, is_grounded,\n", ")\n", "\n", "for k, v in DOCS.items():\n", " print(f\"{k}: {v}\")\n", "\n", "print()\n", "for qa in TEST_SET:\n", " print(f\"{qa.qid}: {qa.question!r} (expects tool={qa.expected_tool})\")" ] }, { "cell_type": "markdown", "id": "e4a8dd65", "metadata": {}, "source": [ "## The two metrics\n", "\n", "**Tool-call accuracy** -- fraction of questions where the agent invoked\n", "the tool the question actually required. Independent of whether the\n", "final text is correct: a math question answered via `search_docs` is a\n", "tool-selection failure even if the retrieved snippet happens to look\n", "plausible.\n", "\n", "**Hallucination rate** -- fraction of answers containing content that\n", "isn't traceable to what the tool actually returned. The implementation\n", "here (`is_grounded`) is intentionally simple: it flags specific\n", "content (capitalized words, digits) in the final answer that doesn't\n", "appear in the tool's raw output. That's a cheap proxy for a real\n", "faithfulness check, and it has a known blind spot -- see the limitation\n", "called out after the results below." ] }, { "cell_type": "code", "execution_count": 2, "id": "7a89b95d", "metadata": { "execution": { "iopub.execute_input": "2026-09-13T02:33:52.498552Z", "iopub.status.busy": "2026-09-13T02:33:52.497913Z", "iopub.status.idle": "2026-09-13T02:33:52.503904Z", "shell.execute_reply": "2026-09-13T02:33:52.503031Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "policy | tool_call_accuracy | hallucination_rate\n", "--------------------------------------------------------------------\n", "good_policy | 1.00 | 0.00\n", "wrong_tool_policy | 0.20 | 0.00\n", "hallucinating_policy | 1.00 | 0.20\n" ] } ], "source": [ "results = []\n", "for cls in [GoodPolicy, WrongToolPolicy, HallucinatingPolicy]:\n", " results.append(evaluate_policy(cls()))\n", "\n", "print(f\"{'policy':<22} | {'tool_call_accuracy':>19} | {'hallucination_rate':>19}\")\n", "print(\"-\" * 68)\n", "for r in results:\n", " print(f\"{r['policy']:<22} | {r['tool_call_accuracy']:>19.2f} | {r['hallucination_rate']:>19.2f}\")" ] }, { "cell_type": "markdown", "id": "815a3b95", "metadata": {}, "source": [ "## Reading the results\n", "\n", "`GoodPolicy` scores 1.00 / 0.00 as expected -- it's the control.\n", "\n", "`WrongToolPolicy` drops to 0.20 tool-call accuracy but 0.00 hallucination\n", "rate. That combination matters: this agent isn't fabricating anything,\n", "it's asking the wrong tool the wrong question and returning whatever\n", "comes back. A single end-to-end accuracy number would likely have caught\n", "this policy failing outright (since answers to the wrong tool are mostly\n", "useless), but tool-call accuracy tells you specifically *where* it broke,\n", "which is what you need to fix it.\n", "\n", "`HallucinatingPolicy` keeps 1.00 tool-call accuracy but hallucination\n", "rate rises to 0.20. This is the dangerous profile in production: the\n", "agent looks like it's doing everything right (correct tool, plausible\n", "answer) while quietly adding unsupported specifics." ] }, { "cell_type": "code", "execution_count": 3, "id": "45760f72", "metadata": { "execution": { "iopub.execute_input": "2026-09-13T02:33:52.505794Z", "iopub.status.busy": "2026-09-13T02:33:52.505184Z", "iopub.status.idle": "2026-09-13T02:33:52.510815Z", "shell.execute_reply": "2026-09-13T02:33:52.509853Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Q1 grounded=False\n", " tool_output: 'Anthropic was founded in 2021 by former OpenAI researchers.'\n", " final_answer: 'Anthropic was founded in 2021 in San Francisco by 7 former OpenAI employees.'\n", "Q2 grounded=True\n", " tool_output: 'The capital of France is Paris.'\n", " final_answer: 'The capital of France is Paris.'\n", "Q3 grounded=True\n", " tool_output: '84'\n", " final_answer: '84, calculated using standard long multiplication in under a second.'\n", "Q4 grounded=True\n", " tool_output: \"Anthropic's mission is to ensure AI systems are safe and beneficial.\"\n", " final_answer: \"Anthropic's mission is to ensure AI systems are safe and beneficial.\"\n", "Q5 grounded=True\n", " tool_output: '25.0'\n", " final_answer: '25.0'\n" ] } ], "source": [ "hallucinating_records = evaluate_policy(HallucinatingPolicy())[\"records\"]\n", "for r in hallucinating_records:\n", " grounded = is_grounded(r, next(qa for qa in TEST_SET if qa.qid == r.qid))\n", " print(f\"{r.qid} grounded={grounded}\")\n", " print(f\" tool_output: {r.tool_output!r}\")\n", " print(f\" final_answer: {r.final_answer!r}\")" ] }, { "cell_type": "markdown", "id": "ad6af8c7", "metadata": {}, "source": [ "## A known limitation, left in on purpose\n", "\n", "Look at Q3 above: the tool returned `'84'`, and the agent's final answer\n", "was `'84, calculated using standard long multiplication in under a\n", "second.'` -- a fabricated method claim with no digits or proper nouns in\n", "it. The current `is_grounded` check misses this, because it only flags\n", "capitalized words and numbers as suspicious. It correctly caught Q1's\n", "fabricated headcount and location, but missed Q3's fabricated process\n", "description.\n", "\n", "I'm leaving this in rather than quietly fixing it, because it's the\n", "actual argument for why this harness needs to evolve past a\n", "string-matching heuristic: the next version should replace `is_grounded`\n", "with a proper entailment check -- either a small NLI model or an\n", "LLM-as-judge prompt that asks \"is every claim in this answer supported by\n", "this tool output, yes or no\" -- without changing anything else in the\n", "harness. That swap is the difference between a eval that catches the\n", "obvious hallucinations and one that catches the subtle ones, and it's\n", "exactly the kind of gap you find by actually reading your own eval's\n", "misses instead of trusting the aggregate number." ] }, { "cell_type": "markdown", "id": "cd881fd3", "metadata": {}, "source": [ "## Where this connects to real deployment work\n", "\n", "This is a toy environment, but the same two-axis evaluation (tool-call\n", "accuracy + groundedness, scored separately) is the methodology I use for\n", "evaluating agentic AI in regulated production workflows -- see\n", "[`auditable-agent-demo`](https://github.com/kernalseed/auditable-agent-demo)\n", "for the companion project, which takes the same \"don't trust an agent's\n", "output without checking how it got there\" idea and applies it to an\n", "auditable, spec-bound compliance-triage agent with a full tool-call and\n", "rule-citation audit trail.\n", "\n", "**Next steps for this notebook:**\n", "- Replace the synthetic `DOCS`/`calculator` tools with real API-backed\n", " tools and re-run the same harness unchanged.\n", "- Replace `is_grounded` with an LLM-as-judge or NLI-based entailment\n", " check, and compare its verdicts against this heuristic's on the same\n", " records to quantify how much the simple version was missing.\n", "- Add a third metric: *escalation appropriateness* -- did the agent\n", " correctly decline to answer when neither tool could resolve the\n", " question, rather than guessing." ] } ], "metadata": { "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }