Agent Action Grounding and Verification: 6 Proven Strategies from the ai-agent-book

Agent action grounding and verification relies on token-level semantic overlap, configurable similarity thresholds, and deterministic evidence serialization to ensure every claim is traceable to observable evidence.

The bojieli/ai-agent-book repository implements a deterministic, heuristic framework for verifying that an autonomous agent’s actions, claims, and final conclusions are grounded in the evidence it has observed. Unlike black-box LLM evaluations, this system uses explicit token-level matching and structured violation reporting to create an auditable trail of agent reasoning.

Token-Level Semantic Grounding

The foundation of the verification system rests on semantic token overlap rather than brittle exact-string matching. This allows the framework to tolerate paraphrasing and surface-form variations while maintaining strict evidence requirements.

Significant Token Extraction

In chapter9/trajectory-verifier/consistency_checker.py, the _tokenize method normalizes text by lowercasing and extracting alphanumeric tokens with length greater than one. The _significant_tokens function then filters out stop-words and negation tokens (stored in _STOPWORDS and _NEGATION_TOKENS sets) to isolate the semantically meaningful content of both claims and evidence.

A claim is considered grounded when a sufficient proportion of its significant tokens overlap with any prior evidence string. The overlap ratio must exceed the configurable _GROUNDING_THRESHOLD = 0.5 (50%) as implemented in TrajectoryConsistencyChecker.check_claim_grounded.

Configurable Threshold Tuning

Both grounding and contradiction detection rely on class-level constants that can be adjusted for experimental sensitivity. The _GROUNDING_THRESHOLD and _CONTRADICTION_SIMILARITY parameters allow developers to tune the strictness of verification without modifying core logic, making the system adaptable to domains with varying noise tolerances.

Evidence Serialization and Chain Integrity

Reliable verification requires canonical representations of heterogeneous data types, from JSON tool outputs to natural language observations.

Canonical String Representation

The _serialize_evidence method (lines 20-30 in consistency_checker.py) normalizes tool results and observations into comparable strings. This deterministic serialization ensures that structured JSON objects, numeric values, and text observations occupy the same comparison space, preventing type-mismatch errors during grounding checks.

Evidence Accumulation for Conclusions

For final answer verification, the system evaluates grounding against the complete accumulated evidence chain—including all observations, tool results, and intermediate claims. If the conclusion fails this comprehensive check, the system records a VIOLATION_UNSUPPORTED violation, ensuring that agents cannot introduce unsupported information in their final outputs.

Multi-Dimensional Verification Architecture

The repository separates concerns into specialized checker classes to isolate failure modes and provide actionable diagnostics.

Contradiction Detection

The TrajectoryConsistencyChecker.find_contradictions method tokenizes claims, records negation polarity, and compares core tokens using Jaccard similarity with a threshold of _CONTRADICTION_SIMILARITY = 0.5. When semantically similar claims exhibit opposite polarity or disjoint numeric values, the system flags a VIOLATION_CONTRADICTION, preventing the agent from asserting mutually exclusive facts within the same trajectory.

Hallucination Prevention

Evidence-chain integrity checks verify that claimed tool results match the actual tool_result entries in the trajectory. Mismatches trigger VIOLATION_HALLUCINATED errors, catching cases where agents fabricate tool outputs or misattribute observations.

Policy Compliance and Privacy Boundaries

The ProcessVerifier class (in chapter9/trajectory-verifier/verifier.py) extends grounding checks to include policy compliance, privacy boundary protection, and promise-action consistency. The _promise_action method ensures that promised tool calls actually occurred before the associated claim was made, preventing agents from claiming future actions as completed facts.

Human-in-the-Loop Safety Gates

The verification pipeline includes explicit gating logic to escalate uncertain or high-risk trajectories for human review.

Risk-Based Review Triggers

In TrajectoryVerifier.review, the system combines dimension scores into an overall consistency score. If any high-risk dimensions—including rule_compliance, privacy_boundary, or promise_action_consistency—fail, or if any dimension reports low confidence, the system automatically requires human review. This fail-safe mechanism makes the framework suitable for iterative deployment in safety-critical environments.

Practical Implementation Examples

The following code demonstrates the core verification primitives used in the framework.

Grounding a Claim Against Evidence

from chapter9.trajectory_verifier.consistency_checker import TrajectoryConsistencyChecker

checker = TrajectoryConsistencyChecker()
claim = "The temperature is 22°C"
evidence = ["Observed sensor reading: 22°C", "Previous step said it's warm"]
is_grounded = checker.check_claim_grounded(claim, evidence)
print(is_grounded)   # → True (significant token overlap meets threshold)

Detecting Contradictions

from chapter9.trajectory_verifier.consistency_checker import TrajectoryConsistencyChecker

checker = TrajectoryConsistencyChecker()
claims = [
    (0, "The server is online"),
    (1, "The server is offline"),
]
violations = checker.find_contradictions(claims)
for v in violations:
    print(v.violation_type, v.description)

# → contradiction Claim at step 1 contradicts claim at step 0

Verifying Promised Actions

from chapter9.trajectory_verifier.verifier import ProcessVerifier

traj = {
    "tool_calls": [
        {"turn": 1, "name": "search", "result": {"success": True}},
    ],
    "promises": [
        {"turn": 2, "required_tool": "search", "text": "Will search for X"},
    ],
}
verifier = ProcessVerifier()
result = verifier._promise_action(traj)
print(result.verdict)   # → pass (the promised tool call succeeded before the claim)

Summary

  • Token-level grounding: Uses semantic overlap of significant tokens (excluding stop-words) with a configurable 50% threshold to verify claims against evidence.
  • Deterministic serialization: Converts heterogeneous tool outputs and observations into canonical strings via _serialize_evidence for reliable comparison.
  • Multi-layered verification: Separates concerns into TrajectoryConsistencyChecker for claims and contradictions, and ProcessVerifier for policy and promise compliance.
  • Explicit violation types: Categorizes failures as VIOLATION_CONTRADICTION, VIOLATION_HALLUCINATED, or VIOLATION_UNSUPPORTED to enable targeted remediation.
  • Human review gating: Automatically escalates trajectories with high-risk dimension failures or low confidence scores for human oversight.
  • Paraphrase tolerance: The token-based approach accepts semantic equivalence without requiring exact string matches, accommodating natural language variation.

Frequently Asked Questions

What is agent action grounding?

Agent action grounding is the process of verifying that an autonomous agent's claims, conclusions, and tool usage are based on actual observed evidence rather than hallucinated or inferred information. According to the ai-agent-book implementation, grounding requires that a significant proportion of a claim's tokens overlap with previously observed evidence strings, ensuring traceability from output to input.

How does token-level grounding differ from exact string matching?

Unlike exact string matching, which fails when an agent paraphrases "The temperature is 22°C" as "It is 22 degrees Celsius," token-level grounding extracts significant tokens (removing stop-words like "the" and "is") and checks for proportional overlap. This semantic approach, implemented in _significant_tokens and check_claim_grounded, tolerates surface-form variation while maintaining strict evidentiary requirements.

What triggers a human review in the verification pipeline?

Human review is triggered when TrajectoryVerifier.review detects failures in high-risk dimensions—specifically rule_compliance, privacy_boundary, or promise_action_consistency—or when any verification dimension reports low confidence. This ensures that ambiguous or potentially dangerous agent behaviors receive human oversight before deployment.

How does the system detect hallucinations in tool results?

The system detects hallucinations by comparing the claimed tool result against the actual tool_result entry in the trajectory data structure. When these values mismatch, as checked in the hallucination verification logic of TrajectoryConsistencyChecker, the system flags a VIOLATION_HALLUCINATED error, catching cases where agents fabricate tool outputs or misremember previous observations.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →