# 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.

{% hint style="info" %}
This workflow is available as a ready-to-use template in Midio.
{% endhint %}

## **Prerequisites**

* [Midio](https://midio.com) account
* Google OAuth provider configured in Midio. Set one up following [this guide](https://docs.midio.com/midio-docs/guides/secrets-and-environment-variables/oauth-2.0-providers). 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

<figure><picture><source srcset="/files/DedE9f8AqEqUHqzPrzTZ" media="(prefers-color-scheme: dark)"><img src="/files/GCee0Itbi8I9ed9karZo" alt="A screenshot of the entire workflow"></picture><figcaption><p>The entire workflow</p></figcaption></figure>

## 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.

<figure><picture><source srcset="/files/hhEEEZWN5BKBhtpJ4AVd" media="(prefers-color-scheme: dark)"><img src="/files/ouYT668T7KK4gpB9ronF" alt="A screenshot of &#x60;Get documents&#x60; node"></picture><figcaption><p>Content of `Get documents` node</p></figcaption></figure>

## 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.

````json
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.

{% hint style="info" %}
We recommend using `gpt-4.1` for *optimal cross-checking accuracy*
{% endhint %}

<figure><picture><source srcset="/files/66QXihK0T6pZIiEJpqh0" media="(prefers-color-scheme: dark)"><img src="/files/J3UwhqkCR91u5VRz6MAG" alt="A screenshot of &#x60;Cross-check bank statement&#x60; node"></picture><figcaption><p>Content of `Cross-check bank statment`</p></figcaption></figure>

## 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.

<figure><picture><source srcset="/files/NOY4joJQvkQ1P2kjGac8" media="(prefers-color-scheme: dark)"><img src="/files/u1wnBOpNjgoe7ZXyz7rs" alt="A screenshot of &#x60;Create content in Google Document&#x60;"></picture><figcaption><p>Content of `Create content in Google Document`</p></figcaption></figure>

## 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](https://docs.midio.com/midio-docs/tutorials/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](https://docs.midio.com/midio-docs/slack-bot-that-summarizes-articles) for an example of how to integrate with Slack.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.midio.com/midio-docs/tutorials/bank-statement-cross-checker-agent.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
