Automating financial compliance checks using AI workflows is rapidly becoming a necessity for organizations navigating the complex regulatory landscape of 2026. This tutorial provides a deep, practical guide to building and deploying AI-powered compliance checks, ensuring your systems remain agile, auditable, and up-to-date with evolving standards.
As we covered in our complete guide to AI workflow automation in finance, automating compliance is both a technical and strategic imperative. Here, we’ll focus specifically on how to implement, test, and maintain these workflows—so you can move from policy to production with confidence.
Prerequisites
- Tools:
- Python 3.11+
- Jupyter Notebook or VSCode (for prototyping and testing)
- Docker 25.x
- AI workflow orchestrator (e.g., Apache Airflow 3.x, Prefect 3.x, or Temporal 2.x)
- OpenAI GPT-5 API or Azure OpenAI Service (for LLM-based checks)
- Financial data source (e.g., PostgreSQL 15+, Snowflake, or S3 bucket with CSV/Parquet files)
- Accounts/Access:
- API key for your chosen LLM provider
- Database credentials for your financial data source
- Knowledge:
- Basic Python scripting
- Familiarity with REST APIs
- Understanding of financial compliance concepts (e.g., AML, KYC, transaction monitoring)
- Comfort with Docker and workflow orchestration basics
1. Define Your Compliance Rules and Data Sources
-
List compliance requirements.
- Identify which regulations to automate (e.g., AML transaction thresholds, suspicious activity patterns, KYC completeness).
-
Map data sources.
- Document where relevant data lives (databases, files, APIs).
- Ensure access and permissions are in place.
-
Example: AML Transaction Monitoring Rule
RULES = [ {"type": "amount", "threshold": 10000}, {"type": "country", "high_risk_countries": ["IR", "KP", "SY", "SD", "CU"]} ]
2. Set Up Your AI Workflow Orchestrator
-
Install Docker (if not already installed):
sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io -
Deploy Apache Airflow via Docker Compose:
git clone https://github.com/apache/airflow.git cd airflow cp docker-compose.yaml docker-compose.override.yaml docker compose up -dAccess Airflow UI at
http://localhost:8080(default credentials:airflow/airflow). -
Install required Python packages in your Airflow container:
docker exec -it airflow-webserver bash pip install openai pandas sqlalchemy
3. Connect to Your Financial Data Source
-
Configure database connection in Airflow:
- In the Airflow UI, go to Admin > Connections.
- Add a new connection of type
Postgres(or your source), filling in host, schema, user, password, and port.
Screenshot description: Airflow Connections page with a new Postgres connection named
fin_data_db. -
Test your connection:
import sqlalchemy engine = sqlalchemy.create_engine('postgresql://user:password@host:5432/dbname') with engine.connect() as conn: result = conn.execute("SELECT COUNT(*) FROM transactions") print(result.fetchone())
4. Integrate LLM-Based Compliance Checks
-
Set up your OpenAI or Azure OpenAI API key as an environment variable:
export OPENAI_API_KEY="sk-..." -
Write a Python function to call the LLM for compliance reasoning:
import openai def check_transaction_with_llm(transaction, rules): prompt = f""" You are a financial compliance expert. Given the following transaction: {transaction} And these compliance rules: {rules} Does this transaction violate any rules? Respond YES or NO and explain. """ response = openai.ChatCompletion.create( model="gpt-5", messages=[{"role": "user", "content": prompt}] ) return response['choices'][0]['message']['content'] -
Test the function in your notebook or Airflow task:
sample_tx = {"amount": 15000, "country": "IR", "customer_id": 1234} result = check_transaction_with_llm(sample_tx, RULES) print(result)Screenshot description: Jupyter output showing the LLM response:
YES: This transaction exceeds $10,000 and involves a high-risk country (IR).
5. Build an End-to-End Compliance DAG (Airflow Example)
-
Create a new DAG file:
touch dags/compliance_check_dag.py -
Paste the following DAG code:
from airflow import DAG from airflow.operators.python import PythonOperator from datetime import datetime, timedelta import openai, sqlalchemy, pandas as pd def fetch_transactions(**context): engine = sqlalchemy.create_engine('postgresql://user:password@host:5432/dbname') df = pd.read_sql("SELECT * FROM transactions WHERE processed=false", engine) context['ti'].xcom_push(key='transactions', value=df.to_dict(orient='records')) def run_compliance_checks(**context): transactions = context['ti'].xcom_pull(key='transactions') flagged = [] for tx in transactions: result = check_transaction_with_llm(tx, RULES) if "YES" in result: flagged.append({"transaction": tx, "reason": result}) context['ti'].xcom_push(key='flagged', value=flagged) def store_flags(**context): flagged = context['ti'].xcom_pull(key='flagged') # Store flagged results (e.g., insert into alerts table) print("Flagged transactions:", flagged) default_args = { 'owner': 'airflow', 'start_date': datetime(2026, 1, 1), 'retries': 1, 'retry_delay': timedelta(minutes=5), } with DAG( 'compliance_check', default_args=default_args, schedule_interval='@hourly', catchup=False, ) as dag: fetch = PythonOperator(task_id='fetch_transactions', python_callable=fetch_transactions, provide_context=True) check = PythonOperator(task_id='run_compliance_checks', python_callable=run_compliance_checks, provide_context=True) store = PythonOperator(task_id='store_flags', python_callable=store_flags, provide_context=True) fetch >> check >> store -
Trigger the DAG manually in Airflow UI and monitor execution logs.
Screenshot description: Airflow DAG run details showing task statuses: fetch_transactions (success), run_compliance_checks (success), store_flags (success).
6. Add Audit Logging and Human Review Workflow
-
Modify
store_flagsto log flagged transactions:def store_flags(**context): flagged = context['ti'].xcom_pull(key='flagged') import json with open('/data/compliance_audit_log.json', 'a') as f: for item in flagged: f.write(json.dumps(item) + "\n") # Optionally, send flagged transactions to a review queue (e.g., email, Slack, or ticketing system) -
Set up notifications for human review:
from airflow.operators.email import EmailOperator notify = EmailOperator( task_id='notify_compliance_team', to='compliance@example.com', subject='Flagged Transactions Alert', html_content='New flagged transactions require review.', files=['/data/compliance_audit_log.json'] ) store >> notify -
Test the audit log and notification process:
- Check the
/data/compliance_audit_log.jsonfile for correct entries. - Ensure emails are received by the compliance team.
- Check the
7. Monitor, Retrain, and Update Compliance Logic
-
Track false positives and negatives:
- Have reviewers annotate flagged transactions as true/false positives.
- Log these outcomes for model improvement.
-
Retrain LLM prompts or fine-tune if required:
def update_prompt_with_feedback(transaction, rules, feedback): prompt = f""" Transaction: {transaction} Rules: {rules} Reviewer Feedback: {feedback} Based on feedback, should the compliance rule be adjusted? """ # Call LLM as before -
Schedule regular reviews of compliance rules in your workflow orchestrator.
- Use Airflow’s
@monthlyschedule to trigger policy review tasks.
- Use Airflow’s
Common Issues & Troubleshooting
-
Issue: LLM API rate limits or timeouts.
Solution: Implement retry logic and exponential backoff. Consider batching requests where possible. -
Issue: Incomplete or inconsistent data.
Solution: Add data validation steps before passing transactions to the LLM. Log and skip invalid records. -
Issue: Too many false positives.
Solution: Refine rules and prompts. Use reviewer feedback to improve accuracy. See The Hidden Costs of AI Workflow Automation for more on model tuning and operational overhead. -
Issue: Workflow orchestrator tasks fail intermittently.
Solution: Check container logs for Python errors, memory limits, or network issues. Use Airflow’s retry and alerting features.
Next Steps
- Expand your compliance ruleset to cover additional regulations (e.g., GDPR, sanctions screening).
- Integrate with real-time transaction streams for proactive compliance monitoring.
- Explore advanced workflow features like branching, parallelism, and escalation paths.
- Read our tutorial on automating SLA monitoring with AI workflows for more orchestration techniques.
- For customer feedback compliance, see hands-on sentiment analysis automation.
- For a broader strategic view, revisit our PILLAR: The 2026 Playbook for AI Workflow Automation in Finance.