# AI21

**Use Case Overview**

**Scenario**: Legal Document Processing Pipeline\
**Objective**:

* Process 10,000+ contracts/day with:
  * AI21’s high-accuracy text analysis
  * **AgentGPT Guardrails**: PII redaction, versioned audits
  * Multi-model failover (AI21 + GPT-4 + in-house legal AI)

***

#### **Prerequisites**

* AI21 Enterprise Account (`Jurassic-2 Ultra` access)
* AgentGPT ≥4.1 with `ai21-orchestrator` plugin
* **Secure Document Storage**: AWS S3/Microsoft Azure with client-specific encryption

***

#### **1. Architecting the Integrated System**

**AI21 Agent with Governance Layer:**

```python
from agentgpt import Agent, LegalValidator  
from ai21 import AI21Client  

class LegalProcessingAgent(Agent):  
    def __init__(self):  
        super().__init__(  
            name="LegalAnalyzer-Pro",  
            compliance_mode="gdpr+ccpa"  
        )  
        self.ai21 = AI21Client(  
            api_key=os.getenv("AI21_ENTERPRISE_KEY"),  
            model="j2-ultra-2025",  
            application_name="ContractAnalysis"  
        )  
        self.validator = LegalValidator(  
            rule_sets=["clm", "uben"]  # Contract Lifecycle Mgmt & UCC rules  
        )  

    async def analyze_contract(self, doc_path: str) -> dict:  
        content = self.security_layer.decrypt_s3(doc_path)  
        sanitized = self.redact_pii(content)  # Uses AgentGPT's PII engine  

        # AI21 Document Processing  
        ai21_response = self.ai21.summarize(  
            text=sanitized,  
            parameters={  
                "summary_length": "bullets",  
                "legal_terms": "highlight"  
            }  
        )  

        # Compliance Validation  
        audit_report = self.validator.validate(  
            summary=ai21_response["summary"],  
            original_hash=self.hash_content(content)  
        )  

        return {  
            "analysis": ai21_response,  
            "compliance": audit_report,  
            "version_id": self.create_immutable_record(ai21_response)  
        }  
```

***

#### **2. AI21 Configuration & Deployment**

**Integration Setup:**

```yaml
# ai21-integration.yaml  
ai21:  
  api_endpoint: "https://enterprise.ai21.com/v2/legal"  
  fallback_model: "j2-jumbo-instruct"  
  rate_limit:  
    rpm: 600  # Requests per minute  
    burst: 50  
  security:  
    input_sanitization: "strict"  
    output_encryption: "aes-256-gcm"  
agentgpt:  
  audit:  
    model_interactions: true  
    data_retention: "7y"  
```

Deploy with:

```bash
agentgpt integrations deploy ai21 -f ai21-integration.yaml --health-check  
```

***

#### **3. Responsible AI Implementation**

**Bias Mitigation Workflow:**

```python
from agentgpt.compliance import BiasDetector  

def safe_ai21_call(text: str) -> dict:  
    # Pre-check using AI21's built-in detectors  
    bias_score = AI21Client.detect_bias(text)["score"]  
    
    if bias_score > 0.7:  
        self.log_incident("HIGH_BIAS_RISK", text_hash=hash(text))  
        return {"error": "Content rejected by AI safety filters"}  
    
    # Proceed with validated input  
    return self.ai21.analyze(text)  
```

**Audit Log Example:**

```json
{  
  "timestamp": "2025-2-08T09:12:42Z",  
  "model": "j2-ultra-2025",  
  "input_sha256": "e3b0c44298fc1...",  
  "output_token_count": 1422,  
  "compliance_checks": {  
    "clm_violations": 0,  
    "uben_compliance": "passed",  
    "pii_leakage": false  
  },  
  "cost_breakdown": {  
    "ai21_tokens": 1892,  
    "agentgpt_units": 4.22  
  }  
}  
```

***

#### **4. Multi-Model Monitoring**

```bash
agentgpt monitor ai21 --detail  
```

**Output**:

```
AI21 STATUS: ✔️ Operational (99.95% uptime)  
LAST HOUR:  
- Requests: 2,114  | Avg Latency: 820ms  
- Tokens Processed: 291,492  | Cost: $48.22  
COMPLIANCE METRICS:  
- PII Redaction Efficiency: 99.8%  
- CLM Rule Violations: 0/10,000 docs  
FAILOVER ACTIVATIONS:  
- j2-ultra → j2-jumbo: 12 requests (0.5%)  
```

***

#### **5. Best Practices**

1. **Model Version Pinning**:

```bash
agentgpt integrations set-version ai21 --model j2-ultra-2025q3  
```

2. **Data Sanitation Protocol**:

```python
self.security_layer.enable(  
    sanitizers=["pii", "financial_terms", "ndata"],  
    encryption="FIPS_140_2"  
)  
```

3. **Smart Routing Logic**:

```python
def model_router(text: str) -> str:  
    if "NDA" in text:  
        return "j2-ultra"  
    elif len(text) < 2000:  
        return "gpt-4-legalsum"  
    else:  
        return "in-house-legal-ai"  
```

***

#### **6. Troubleshooting**

| **Issue**                   | **Resolution Protocol**                                                                 |
| --------------------------- | --------------------------------------------------------------------------------------- |
| `403 Forbidden` from AI21   | <p>1. Check IP whitelisting<br>2. Rotate API keys via AgentGPT CLI</p>                  |
| High Jurassic-2 latency     | <p>1. Activate local AI21 cache<br>2. Enable <code>intelligent\_chunking</code></p>     |
| `CLM Rule Violation` alerts | <p>1. Update legal rule sets<br>2. Manual audit with <code>agentgpt validate</code></p> |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://agent-gpt.gitbook.io/agent-gpt/integration-examples/ai21.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
