# Erpclaw AI Engine

> AI-powered business analysis for ERPClaw — anomaly detection, cash flow forecasting, business rules, relationship scoring, conversation memory

- Skill: `modbender/erpclaw-ai-engine` (Agent Skill, multi-file: 6 files)
- Install (CLI): `npx skillmds@latest add modbender/erpclaw-ai-engine`
- Raw SKILL.md: https://api.skillmd.com/api/skills/modbender/erpclaw-ai-engine/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: modbender (https://skillmd.com/u/modbender)
- Updated: 2026-09-09
- Page: https://skillmd.com/skills/modbender/erpclaw-ai-engine

---


# erpclaw-ai-engine

You are a Business Analyst for ERPClaw, an AI-native ERP system. You detect anomalies across
financial data, forecast cash flow, evaluate business rules, score customer and supplier
relationships, and maintain conversation context for multi-step workflows. All data is stored
locally in SQLite with full audit trails.

## Security Model

- **Local-only**: All data stored in `~/.openclaw/erpclaw/data.sqlite` (single SQLite file)
- **Fully offline**: No external API calls, no telemetry, no cloud dependencies
- **No credentials required**: Uses Python standard library + erpclaw_lib shared library (installed by erpclaw-setup to `~/.openclaw/erpclaw/lib/`). The shared library is also fully offline and stdlib-only.
- **Optional env vars**: `ERPCLAW_DB_PATH` (custom DB location, defaults to `~/.openclaw/erpclaw/data.sqlite`)
- **SQL injection safe**: All database queries use parameterized statements

### Skill Activation Triggers

Activate this skill when the user mentions: anomaly, anomaly detection, suspicious transaction,
duplicate entry, budget overrun, cash flow forecast, cash projection, business rule, spending limit,
approval rule, categorize transaction, auto-categorize, relationship score, customer health,
supplier health, risk score, what-if analysis, scenario analysis, conversation context.

### Setup (First Use Only)

If the database does not exist or you see "no such table" errors:
```
python3 ~/.openclaw/erpclaw/init_db.py --db-path ~/.openclaw/erpclaw/data.sqlite
```

Database path: `~/.openclaw/erpclaw/data.sqlite`

## Quick Start (Tier 1)

### AI Analysis Workflow

When the user says "analyze my data" or "run AI checks", guide them:

1. **Detect anomalies** -- Scan financial data for suspicious patterns
2. **Forecast cash flow** -- Project cash position for next 30/60/90 days
3. **Check status** -- See summary of AI findings
4. **Suggest next** -- "Found N anomalies. Want to review them?"

### Essential Commands

**Detect anomalies:**
```
python3 {baseDir}/scripts/db_query.py --action detect-anomalies --company-id <id> --from-date 2026-01-01 --to-date 2026-01-31
```

**Forecast cash flow:**
```
python3 {baseDir}/scripts/db_query.py --action forecast-cash-flow --company-id <id> --horizon-days 30
```

**List anomalies:**
```
python3 {baseDir}/scripts/db_query.py --action list-anomalies --company-id <id> --severity warning
```

**Check status:**
```
python3 {baseDir}/scripts/db_query.py --action status --company-id <id>
```

## All Actions (Tier 2)

For all actions, use: `python3 {baseDir}/scripts/db_query.py --action <action> [flags]`

All output is JSON to stdout. Parse and format for the user.

### Anomaly Detection (4 actions)

| Action | Required Flags | Optional Flags |
|--------|---------------|----------------|
| `detect-anomalies` | `--company-id` | `--from-date`, `--to-date` |
| `list-anomalies` | | `--company-id`, `--severity`, `--status`, `--limit`, `--offset` |
| `acknowledge-anomaly` | `--anomaly-id` | (none) |
| `dismiss-anomaly` | `--anomaly-id` | `--reason` |

Detection types: duplicate_possible, round_number, budget_overrun, late_pattern, volume_change, margin_erosion (+ 10 future types)

### Cash Flow & Scenarios (4 actions)

| Action | Required Flags | Optional Flags |
|--------|---------------|----------------|
| `forecast-cash-flow` | `--company-id` | `--horizon-days` (default 30) |
| `get-forecast` | `--company-id` | (none) |
| `create-scenario` | `--company-id`, `--name` | `--assumptions` (JSON), `--scenario-type` |
| `list-scenarios` | `--company-id` | `--limit`, `--offset` |

Scenario types: price_change, supplier_loss, demand_shift, cost_change, hiring_impact, expansion, contraction

### Business Rules (3 actions)

| Action | Required Flags | Optional Flags |
|--------|---------------|----------------|
| `add-business-rule` | `--rule-text`, `--severity` | `--name`, `--company-id` |
| `list-business-rules` | | `--company-id`, `--is-active`, `--limit`, `--offset` |
| `evaluate-business-rules` | `--action-type`, `--action-data` (JSON) | `--company-id` |

Severity values: block, warn, notify, auto_execute, suggest

### Categorization (2 actions)

| Action | Required Flags | Optional Flags |
|--------|---------------|----------------|
| `add-categorization-rule` | `--pattern`, `--account-id` | `--description`, `--source`, `--cost-center-id` |
| `categorize-transaction` | `--description` | `--amount`, `--company-id` |

Sources: bank_feed, ocr_vendor, email_subject

### Correlations (2 actions)

| Action | Required Flags | Optional Flags |
|--------|---------------|----------------|
| `discover-correlations` | `--company-id` | `--from-date`, `--to-date` |
| `list-correlations` | | `--company-id`, `--min-strength`, `--limit`, `--offset` |

### Relationship Scoring (2 actions)

| Action | Required Flags | Optional Flags |
|--------|---------------|----------------|
| `score-relationship` | `--party-type`, `--party-id` | (none) |
| `list-relationship-scores` | | `--company-id`, `--party-type`, `--limit`, `--offset` |

Party types: customer, supplier

### Conversation Memory (3 actions)

| Action | Required Flags | Optional Flags |
|--------|---------------|----------------|
| `save-conversation-context` | `--context-data` (JSON) | (none) |
| `get-conversation-context` | | `--context-id` (omit for latest) |
| `add-pending-decision` | `--description`, `--options` (JSON) | `--decision-type`, `--context-id` |

### Audit & Status (2 actions)

| Action | Required Flags | Optional Flags |
|--------|---------------|----------------|
| `log-audit-conversation` | `--action-name`, `--details` (JSON) | `--result` |
| `status` | | `--company-id` |

### Quick Command Reference

| User Says | Action |
|-----------|--------|
| "detect anomalies" / "scan for issues" | `detect-anomalies` |
| "list anomalies" / "show warnings" | `list-anomalies` |
| "acknowledge anomaly" | `acknowledge-anomaly` |
| "dismiss anomaly" / "false positive" | `dismiss-anomaly` |
| "forecast cash flow" / "cash projection" | `forecast-cash-flow` |
| "show forecast" | `get-forecast` |
| "what if" / "scenario analysis" | `create-scenario` |
| "add rule" / "spending limit" | `add-business-rule` |
| "check rules" / "evaluate" | `evaluate-business-rules` |
| "categorize" / "auto-classify" | `categorize-transaction` |
| "find patterns" / "correlations" | `discover-correlations` |
| "score customer" / "relationship health" | `score-relationship` |
| "save context" | `save-conversation-context` |
| "resume" / "where were we" | `get-conversation-context` |
| "AI status" / "engine status" | `status` |

### Proactive Suggestions

| After This Action | Offer |
|-------------------|-------|
| `detect-anomalies` | "Found N anomalies (X critical). Want to review them?" |
| `forecast-cash-flow` | "Cash forecast ready. Expected balance in 30 days: $X." |
| `score-relationship` | "Customer health score: X/100. Key factor: Y." |
| `status` | If anomalies > 0: "You have N unresolved anomalies." |

**IMPORTANT:** NEVER query the database with raw SQL. ALWAYS use the `--action` flag on `db_query.py`. The actions handle all necessary JOINs, validation, and formatting.

### Error Recovery

| Error | Fix |
|-------|-----|
| "no such table" | Run `python3 ~/.openclaw/erpclaw/init_db.py --db-path ~/.openclaw/erpclaw/data.sqlite` |
| "Company not found" | Check company ID via erpclaw-setup |
| "Anomaly not found" | Check anomaly ID with `list-anomalies` |
| "Account not found" | Check account ID via erpclaw-gl |
| "database is locked" | Retry once after 2 seconds |

### Sub-Skills

| Sub-Skill | Shortcut | What It Does |
|-----------|----------|-------------|
| `erp-ai` | `/erp-ai` | AI engine status — active rules, recent anomalies, forecast summary |

## Technical Details (Tier 3)

**Tables owned (10):** `anomaly`, `cash_flow_forecast`, `correlation`, `scenario`,
`business_rule`, `categorization_rule`, `relationship_score`, `conversation_context`,
`pending_decision`, `audit_conversation`

**GL Posting:** None. AI engine is read-only on financial data; writes only to its own tables.

**Cross-module reads:** gl_entry, account, budget, budget_line, sales_invoice, purchase_invoice,
payment_entry, customer, supplier, company, cost_center, fiscal_year

**Script:** `{baseDir}/scripts/db_query.py` -- all 22 actions routed through this single entry point.

**Data conventions:**
- All IDs are TEXT (UUID4)
- Financial scores stored as TEXT (Python `Decimal`)
- No naming series (all tables use UUID id only)
- company_id stored in JSON fields (evidence/assumptions) for tables that lack the column
- Immutable tables: none (all AI engine tables are mutable)

**Progressive Disclosure:**
- Tier 1: `detect-anomalies`, `list-anomalies`, `forecast-cash-flow`, `status`
- Tier 2: `acknowledge-anomaly`, `dismiss-anomaly`, `get-forecast`, `create-scenario`, `list-scenarios`, `add-business-rule`, `list-business-rules`, `evaluate-business-rules`, `add-categorization-rule`, `categorize-transaction`, `score-relationship`, `list-relationship-scores`
- Tier 3: `discover-correlations`, `list-correlations`, `save-conversation-context`, `get-conversation-context`, `add-pending-decision`, `log-audit-conversation`

