Skip to main content

Overview

This example demonstrates a production-ready contract analysis agent that extracts entities, identifies risks, and generates comprehensive reports. It showcases personas, contexts, anchors, custom types, tools, and multi-step flows.

Use Case

Analyze legal contracts to:
  • Extract parties, obligations, dates, and penalties
  • Identify ambiguous or risky clauses
  • Assess risk levels with quantified scores
  • Generate structured reports with citations
  • Prevent hallucination through hard constraints

Complete Code

contract_analyzer.axon

Key Components

Persona: LegalExpert

Defines a specialized legal expert with:
  • Domain expertise: Contract law, intellectual property, corporate law
  • Tone: Precise and formal
  • Confidence threshold: High (0.85) for legal accuracy
  • Citation requirement: All claims must cite sources

Context: LegalReview

Configures the execution environment:
  • Memory: Session-scoped (persists during execution)
  • Language: English
  • Depth: Exhaustive analysis
  • Max tokens: 4096 for detailed responses
  • Temperature: Low (0.3) for consistent, factual output

Anchor: NoHallucination

Enforces hard constraints:
  • Requires: Source citation for all claims
  • Confidence floor: Minimum 0.75 confidence
  • Unknown response: Explicit admission when uncertain
  • On violation: Raises error to trigger self-healing
Anchors are non-negotiable constraints. If violated, AXON’s self-healing runtime will retry with failure context. After max attempts, it raises AnchorBreachError.

Custom Types

RiskScore: Range-constrained float (0.0-1.0)
  • Compile-time validation ensures scores are always valid
  • Cannot be confused with arbitrary floats
Party: Structured type with epistemic fields
  • name and role must be FactualClaim (not opinions or speculation)
  • Prevents hallucinated party information
Risk: Combines quantitative and qualitative
  • score: Quantified risk level
  • mitigation: Optional opinion on how to address it
Use epistemic types (FactualClaim, Opinion) to separate facts from interpretations. This prevents the LLM from mixing subjective opinions into factual fields.

Tool: WebSearch

Defines external web search capability:
  • Provider: Brave Search API
  • Max results: Top 5 results
  • Timeout: 10 seconds to prevent hanging
This tool can be invoked with use WebSearch("legal precedents") to find relevant case law or regulations.

Flow: AnalyzeContract

Two-step cognitive pipeline: Step 1: Extract
  • Input: The contract document
  • Task: Extract structured entities
  • Output: EntityMap with parties, obligations, dates, penalties
Step 2: Assess
  • Input: Extracted entities from Step 1
  • Task: Identify risks and ambiguities
  • Output: RiskAnalysis with risk scores and descriptions
Steps execute sequentially, with outputs flowing from one to the next.

Run Statement

Executes the flow with:
  • as LegalExpert: Use the LegalExpert persona
  • within LegalReview: Use the LegalReview context
  • constrained_by: Apply NoHallucination anchor
  • on_failure: Retry with exponential backoff on errors
  • output_to: Save results to report.json
  • effort: High effort level for thorough analysis

Usage

Validate Syntax

Expected output:

Compile to IR

Generates contract_analyzer.ir.json — the intermediate representation that any backend can execute.

Execute with Tracing

Runs the analyzer with Claude and saves execution trace to contract_analyzer.trace.json.

View Trace

Displays detailed execution flow, model calls, and validation checkpoints.

Example Output

When analyzing a contract, the output might look like:

Advanced Enhancements

Add Precedent Research

Enhance the flow to search for legal precedents:

Add Validation

Ensure high-confidence results:

Add Deep Reasoning

Use explicit chain-of-thought reasoning:

Best Practices

1. Use Epistemic Types for Facts

2. Apply Anchors to Critical Steps

4. Require Source Citations

5. Handle Failures Gracefully

Sentiment Analysis

Analyze text sentiment with epistemic confidence

Data Extraction

Extract structured data from unstructured text

Multi-Step Reasoning

Complex reasoning with chain-of-thought
  • Persona — Define agent identities
  • Context — Configure execution environments
  • Anchor — Enforce hard constraints
  • Types — Epistemic type system
  • Tools — External capabilities
  • Flow — Cognitive pipelines