PT

Code, Cash Flow, and Forensic Accounting: Using AI Agents to Automate Financial Statement Analysis

Alisson Prata, CFP® ·

I've been testing artificial intelligence tools since the first language models became accessible. I've already tried applications focused on text, image, and audio automation. None, however, changed my experimentation routine as much as coding agents.

The idea is simple: you describe the logic and the agent produces functional code. This substantially shifts the boundary of what's possible for someone who understands the problem, but isn't a professional developer.


Integrating quantitative finance and AI

While studying Evaluating the Quality of Financial Reports in preparation for CFA Level II, I decided to combine two vectors:

  • Classic forensic accounting models

  • AI agents specialized in code automation

The question guiding the experiment was straightforward:

Given that the models, metrics, and validation criteria are known, to what extent can an AI agent build a functional system autonomously?


The prototype: Advisor-Brain-FSA

The result was Advisor-Brain-FSA, a prototype that automatically runs:

  • Beneish M-Score

  • Accruals Quality framework

Applied to public CVM data, with a full pipeline of ingestion, transformation, calculation, and visualization. The website represents only the visual product of the experiment.

Experiment output: https://evaluating-quality-of-financial-reports-production.up.railway.app

The main lesson was not in code generation, but in the points where the agent fails structurally without human intervention guided by technical domain knowledge.


What was automated and the limits encountered

Experiment premise: Implement the Beneish M-Score (1999) and accruals quality metrics using CVM data, letting the agent build the entire pipeline.

Objective: Identify quantitative signals of:

  • Inflated revenue recognition

  • Inappropriate expense capitalization

  • Divergence between accounting profit and operating cash flow

Three central limitations emerged.


1. Sector applicability of the model

The first failure was trying to apply the Beneish M-Score indiscriminately to the more than 420 listed companies.

The model assumes the existence of accounts such as:

  • Cost of Goods Sold

  • Inventories

These accounts don't exist in the accounting structure of banks, insurers, and financial institutions. Unrestricted application generates severe distortions and false positives.

The solution required a conceptual decision: instructing the agent to build a sector filter, excluding financial institutions based on the CVM registry and applying the model only to roughly 260 companies in the real sector.

The agent fully implemented the logic. Defining the scope was human.


2. Sign convention in CVM data

In CVM data, expense accounts — such as Depreciation — are provided with a negative sign. Without an explicit absolute value adjustment, the DEPI (Depreciation Index) ends up distorting the M-Score.

This is a relevant limit of current agents: they replicate formulas with syntactic precision, but they don't understand accounting conventions implicit in the data.

To identify the problem, an additional Year-over-Year visualization layer was needed, displaying the intermediate accounting values before the indices were calculated. This is domain knowledge that doesn't emerge from the formula.


3. Backtest with restated financial statements (CVCB3)

For validation, the engine was tested on CVC in 2019, the year in which fraud related to revenue recognition and expense capitalization was acknowledged.

The M-Score didn't flag a failure. The reason is structural: the data currently made available by CVM corresponds to the restated financial statements, already corrected after the audit. The original data is no longer available.

Even so, the model captured relevant changes in the Asset Quality Index (AQI) resulting from the restatement. This indicates that partial anomalies can be detected even in corrected datasets, but it imposes a clear limitation on any attempt at rigorous historical backtesting.


Applied test: Ambipar (AMBP3)

Forensic accounting isn't limited to fraud detection. It is, above all, a tool for assessing earnings quality.

In the test with Ambipar (2023), the system indicated:

  • M-Score below the formal "manipulator" threshold

  • Relevant alerts in the DSRI (receivables growing faster than revenue) and SGI indices

The automatic interpretation layer reclassified the result as Grade C – Questionable Quality, indicating the need for greater scrutiny of credit and growth policies.

This is the same signal that careful Equity Research work would raise, with a significant difference in time and operating cost.


Conclusions from the experiment

The prototype evolved from local execution to persistent cloud cache to address memory limitations. Not a single line of code was written directly by me:

  • The agent built the implementation

  • The architecture, validation, and conceptual corrections were human

The pattern observed is consistent:

  • Agents are highly effective at structure, syntax, and automation

  • They systematically fail at aspects requiring accounting, regulatory, and methodological context

The agent doesn't know that:

  • Banks don't have inventory

  • CVM uses a negative sign for expenses

  • Restated financial statements compromise historical backtests

For professionals with technical domain expertise, translating field knowledge into precise instructions for AI agents is today the most efficient way to understand where this technology actually adds value — and where it still depends entirely on human judgment.


Legal and Regulatory Disclaimer

Advisor-Brain-FSA is a research experiment in data automation and artificial intelligence, based on quantitative methodologies studied at the CFA Institute.

The results presented (M-Score, Accrual Ratio, and heuristic narratives):

  • Are derived from automated processing of public CVM data

  • Do not constitute investment recommendations, fiduciary advice, or an offer of securities

  • Do not replace independent analysis, audit, or professional due diligence

Quantitative indicators assess probabilities and statistical inconsistencies, and do not replace sound analytical judgment.

Originally published on LinkedIn on March 31, 2026. View on LinkedIn