Pre-close accounting review
An assistant for Portuguese accounting firms that reads every entry in the ledger before the year is closed. It checks the account each entry was posted to, flags strange expenses, sizes them against materiality and cites the law. The rule that shapes it: numbers and detections come from code; the language model only explains.
The problem
Before closing, someone has to look at every entry.
An accountant closing a company's year reviews hundreds of entries: was the dinner posted as fuel? Is that invoice a duplicate? Was VAT deducted on a meal, which the law does not allow? Most entries are fine, and the few that are not are easy to miss when you look at all of them by hand.
A chatbot is the wrong tool on its own. Language models get arithmetic wrong, miss items when asked to "scan" a list, and invent article numbers with confidence. In accounting, each of those is a real error in a tax return.
How it works
Three layers, and the model is only in the middle.
- Read the booksImports the company's SAF-T (PT) file, the standard Portuguese accounting export, and an anonymiser strips personal data before anything else runs.
- Classify from the client's own historyFor each supplier (by tax number), the system looks at where the firm posted it before. "This supplier was posted 47 times, always to account 6211" is a rule the accountant can verify, not an opinion. An entry that disagrees with a consistent history is the most valuable finding; a new or mixed supplier goes to a human.
- Scan for anomaliesDeterministic scanners, built to miss nothing: account that does not match the description, wrong VAT rate, VAT that is not base × rate, probable duplicate invoices, expenses not deductible for corporate tax (IRC), project clusters, and amounts unusual for their account (median and MAD, robust to the outlier itself).
- Size by materialityComputes materiality and a working threshold in the spirit of ISA 320, and orders findings by money. Small findings below the threshold are also added up, because together they can matter.
- Explain, with citations that cannot be inventedThe model receives each finding already detected and writes the explanation. It may cite law only through IDs of articles retrieved from a database of 1,137 articles extracted from the official codes. A validator blocks anything else.
- Human gateThe model never writes to the books. It proposes; the accountant approves.
What the report looks like
A pre-close review the accountant can act on.
On a sample company (synthetic data, 25 entries, one year), the deterministic layer alone produced 16 findings, 9 of them above the working threshold. A few of them:
| Finding | Action | Basis |
|---|---|---|
| Possible duplicate: 1,230.00 € and 1,230.50 €, same supplier, 2 days apart | Confirm with supplier | scanner |
| Business lunch posted to 6221 Fuel | Reclassify to 6226 or 6265 | account rules |
| Fuel invoice at 13% VAT (expected 23%) | Correct the rate | CIVA art. 18 |
| VAT deducted on a client dinner | Reverse the deduction | CIVA art. 21 |
| Fine for late filing in expenses | Add back in Modelo 22, table 07 | CIRC art. 23-A |
12 of the 16 findings carry a legal basis fixed in the rules table, the same in every run. Even when the model server is down, the report is still produced; it just has no explanations.
Measured results
Training beat size.
For the explanation layer I fine-tuned small open models on curated examples whose legal citations were all validated; numbers were kept out of training, because they belong to the code. On a fixed benchmark of 30 scenarios:
| Model | F1 | Recall | Precision | Invented law |
|---|---|---|---|---|
| Qwen2.5-7B, no training (baseline) | 0.248 | 0.211 | 0.317 | 0 |
| Qwen2.5-14B, no training | 0.294 | 0.283 | 0.339 | 0 |
| Qwen2.5-7B, fine-tuned | 0.500 | – | – | 0 |
| Qwen3-8B, fine-tuned, 2 epochs | 0.659 | 0.739 | 0.622 | 0 |
The fine-tuned 8B model more than doubles the untrained 14B, and it fits on an 8 GB card that a small firm can run on its own premises, with client data never leaving the office.
The scanners are measured separately against planted errors: 11 of 11 caught, with 6 extra flags left for the model and the accountant to dismiss. High recall is deliberate; precision is the human's job.
Running it in an office
A private AI server on a 6 GB graphics card.
Client data should not leave the firm, so the assistant runs on one machine in the office and several people use it at the same time from their browsers. Packaging it taught me as much as building it:
- Memory that does not grow. Long conversations are compacted: older turns are condensed into a short summary and only the last six messages are kept word for word, so a conversation stays under about 2,500 tokens. Measured on real requests, GPU memory stays flat while it answers (about 5 GB of 6).
- Sizing before buying. A small calculator estimates the graphics memory needed from the model, the context length and the number of people using it at the same time, and recommends a card.
- One-command install on Linux. The installer checks the GPU, verifies the model file by SHA-256, compiles the inference engine with CUDA (falling back to CPU if it must), and sets up services that start with the machine.
- Locked down by default. The model only listens on the machine itself; the web app asks for a password on the local network, and remote access goes through a VPN instead of open ports.
- Law that stays current. A weekly job re-checks the official tax codes and flags the service as failed on purpose if a source changed, so someone reads the change before it is applied.
Auditing my own work
The tests were green. The validator still had holes.
In September the product path went through an independent audit that attacked what no test asked about. The citation test had been checking a copy of the logic inside the test file, not the validator used in production. Attacked with deliberately bad citations and controls, the real validator failed 5 of 10 checks, letting through abbreviated forms like "art. 32.º do CIVA" and whole families it did not know, ISA and NCRF references among them.
Across 11,539 model answers, 765 cited law in free text, and the old guard caught 2% of them. I rewrote the validator, tested it against the real code path, and every one of those forms is now blocked. The lesson I keep: a passing test only matters if it would fail when the thing it guards is broken.
Built with
- Python
- SAF-T (PT)
- SNC / IVA / IRC rules
- ISA 320 materiality
- Qwen3-8B fine-tuning (QLoRA, 4-bit)
- llama.cpp
- Retrieval with anchored citations
Data shown is synthetic. Rules follow Portuguese law as of 2026 and are not tax advice.