Bank Statement Cross-checker Agent

In this guide we will create a Bank statement cross-checker agent. This agent will:

  • Fetch a bank statement and receipts from Google Drive

  • Performs cross-checking and creates a summary using Chat Complete API

  • Updates a Google Document with the summary

We make use of libraries like google-drive, google-docs, mistral and open-ai provided in Midio to achieve this.

This workflow is available as a ready-to-use template in Midio.

Prerequisites

  • Midio account

  • Google OAuth provider configured in Midio. Set one up following this guide. Make sure to create an access token called GOOGLE_WS in the Secrets panel.

  • Mistral API key. You can create one for free at https://mistral.ai. Store it as a secret with the name MISTRAL_AI_API_KEY.

Setting up Google Drive

This is the structure in Google Drive the workflow assumes:

  • A folder containing bank statements in PDF.

  • A folder containing all receipts in PDF.

  • A document containing result of the cross-checking

High-level view

The workflow is split into four parts:

  1. Fetching the relevant documents from Google Drive

  2. Feed the documents to Chat Complete to perform the cross-checking

  3. Creating a Markdown summary of the cross-checking using Chat Complete

  4. Updating the Google Document

A screenshot of the entire workflow
The entire workflow

1. Fetching from Google Drive

First, the relevant documents are fetched from Google using Get Files in Folder, which is provided by the google-docs library. Since we only want the PDF files in the folders, we also use a Filter function from the Midio standard library, and filter out using mimeType which is provided in the data returned from the Google API call. This functionality is packed away in the function Get PDF Files in Folder.

Next we transform the PDF to markdown by using OCR on File, provided by the mistral library. For each receipt in the folder, a new process is spawned to transform the PDF. This was not necessary for the bank statement, since the workflow only fetches the first file returned by Get PDF Files in Folder. The first file will be the newest file crated, because the call to Get Files in Folder sorts the documents by the creation time descendingly.

A screenshot of `Get documents` node
Content of `Get documents` node

2. Cross-check bank statement

To perform the cross-checking, we provide this system prompt to the Chat Complete node. It includes formats for successful matches, missing receipts and unmatched receipts. This standardization helps downstream nodes generate a consistent Markdown summary.

You are an expert cross-checker of bank statements.
You will be given one bank statement and a list of receipts. These documents will be given to you in Markdown format.

How to cross-check a bank entry:
- Find the receipt where the total sum is the same as the outgoing amount in the entry.
- If a receipt matches the sum:
  - Check that the dates are the same.
  - A bank entry might contain a description. Use this description to verify that you have found the correct receipt.

A successful validation is where every step of the cross-checking is successful. If every step is not successful, you must consider the validation as failed or missing.
- A successful validation creates an output that follows the objects described under header "Successful validation".
- A failed or missing validation creates an output that follows the objects described under header "Failed or missing validation".

Every bank entry and receipt *must* be accounted for in the output.

Your final output will be a JSON array of objects with the following format.
```json
{"values": []}
```
The format the JSON objects can be as described below.

## JSON object formats

### Successful validation
1. For each entry in the bank statement append the following to the output:
```json
{
  "validation": "success",
  "entry": <BANK STATEMENT DATA>,
  "receipt_id": <RECEIPT ID>,
  "receipt_content": <RECEIPT SUMMARY MAXIMUM 10 WORDS. MUST CONTAIN TOTAL SUM AND SHOP NAME>
}
```

### Failed or missing validation
1. For each entry in the bank statement that is missing a receipt append the following to the output:
```json
{
  "validation": "missing_receipt",
  "entry": <BANK STATEMENT DATA>
}
```

2. For each receipt that is missing an entry in the bank statement append the following to the output:
```json
{
  "validation": "missing_entry",
  "receipt_id": <RECEIPT ID>,
  "receipt_content": <RECEIPT SUMMARY MAXIMUM 10 WORDS. MUST CONTAIN TOTAL SUM AND SHOP NAME>
}
```

The user message follows this format (written in Midio expression language):

"<BANK STATEMENT>
${bankStatement}
</BANK STATEMENT>

<RECEIPT LIST>
${receipts}
</RECEIPT LIST>"

where ${bankStatement} is the Markdown content of the PDF file and ${receipts} follows this format:

<RECEIPT ID: 0>
markdown content of receipt
</RECEIPT ID: 0>
<RECEIPT ID: 1>
markdown content of receipt
</RECEIPT ID: 1>
... and so on

The mapping of the list of receipts is done using the Map function from the Midio standard library.

We recommend using gpt-4.1 for optimal cross-checking accuracy

A screenshot of `Cross-check bank statement` node
Content of `Cross-check bank statment`

3. Creating Markdown summary

Another Chat Complete node receives a JSON list of objects representing the cross-checking of each entry in the bank statement and the receipts, and is prompted to use this to create a summary:

You are an agent expert in transforming JSON objects into Markdown content.
You will receive a list of JSON objects.

The JSON objects have one key `validation` that you can use as a discriminator.
Each type of discriminator corresponds to a header. Your task is to collect each object under their corresponding header.
The output format must be Markdown.
The headers must be uppercased.
Make sure the content is structured in a way that makes it easy to verify the content.

4. Updating Google Document

The last node is provided the summary content and a document id to add the content to. To achieve this we use the Update Google Document node in the google-docs library.

A screenshot of `Create content in Google Document`
Content of `Create content in Google Document`

Next steps and enhancements

This agent is available as a template. Insert your ids for folders and summary document, and click the Play button in the toolbar at the bottom. As you build upon this agent, here are some enhancements you can consider:

  • Support multiple bank statements by modifying the workflow to only select receipts that would be relevant for a given bank statement. You might have to change the structure the Google Drive setup to achieve this.

  • Expose this workflow as a REST API by attaching an Endpoint event node from the Midio standard library. You can find an example of this in the guide Concurrent Research Agent#exposing-the-agent-as-a-rest-api

  • Add Slack bot to notify users if mismatches are found. This can be done using Send Slack message to Channel or User node in the slack library. See the guide Slack Bot That Summarizes Article for an example of how to integrate with Slack.

Last updated

Was this helpful?