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
- 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
- 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
- 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
Custom Types
- Compile-time validation ensures scores are always valid
- Cannot be confused with arbitrary floats
nameandrolemust beFactualClaim(not opinions or speculation)- Prevents hallucinated party information
score: Quantified risk levelmitigation: Optional opinion on how to address it
Tool: WebSearch
- Provider: Brave Search API
- Max results: Top 5 results
- Timeout: 10 seconds to prevent hanging
use WebSearch("legal precedents") to find relevant case law or regulations.
Flow: AnalyzeContract
- Input: The contract document
- Task: Extract structured entities
- Output:
EntityMapwith parties, obligations, dates, penalties
- Input: Extracted entities from Step 1
- Task: Identify risks and ambiguities
- Output:
RiskAnalysiswith risk scores and descriptions
Run Statement
- 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
Compile to IR
contract_analyzer.ir.json — the intermediate representation that any backend can execute.
Execute with Tracing
contract_analyzer.trace.json.
View Trace
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
3. Use Low Temperature for Legal Work
4. Require Source Citations
5. Handle Failures Gracefully
Related Examples
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

