> For the complete documentation index, see [llms.txt](https://docs.maiagent.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.maiagent.ai/maiagent-user-guide/en/work/image-vision/invoice-recognition.md).

# Invoice Recognition

Invoice recognition example: use a multimodal Agent to recognize triplicate, duplicate, electronic, and cash-register invoices, extract fields, and output JSON for ERP or accounting system integration

Corporate accounting teams handle a large number of purchase and sales invoices every month. In the past, they had to read each paper invoice and type the invoice number, date, buyer and seller tax IDs, line items, and amounts into the system one by one, which is time-consuming and prone to errors in repetitive work. Taiwan invoices also come in triplicate, duplicate, electronic, and cash-register formats, each with a different layout, so traditional OCR requires a separate template for each format.

With MaiAgent's multimodal Agent, you simply upload an invoice image in a conversation, and the AI automatically determines the invoice type, extracts each field, and outputs structured JSON that can be connected directly to an ERP or accounting system. The following explains how to use role instructions to define an Agent as an "invoice document recognition API."

## Processing Multiple Invoices at Once <a href="#multiple-invoices" id="multiple-invoices"></a>

Recognition is performed per message. There is no separate batch OCR feature, and there are three approaches:

* **Attach multiple images to one message**: A single message can include multiple invoice images, which are passed to the model together. The maximum number of images per message is set by the platform (default: 20). Add "Output one record per image, and return everything as a JSON array in attachment order" to the role instructions; otherwise the model may merge several images into one record.
* **Multiple invoices in one image**: The model can recognize them, but accuracy is lower than with one invoice per image. In the instructions, require that "if one image contains multiple invoices, output each separately," and try to keep every invoice fully visible and non-overlapping.
* **Large volumes of invoices**: Use the [Conversation API](https://docs.maiagent.ai/tech/api-integration/conversations-and-messages) to send them message by message, each with a batch of images, and have your program collect the returned JSON. If an invoice fails to be recognized, resend just that one.

{% hint style="warning" %}
Uploading invoice images to a knowledge base only stores the files and builds an index. It does not run recognition or produce JSON. Recognition always happens in a conversation.
{% endhint %}

## OCR (Optical Character Recognition) <a href="#ocr-optical-character-recognition" id="ocr-optical-character-recognition"></a>

Role instructions for the "invoice document recognition API"

````yaml
# Persona
You are an invoice document recognition API

# Task
## Input
The user will upload an image of an invoice document

## Output
Extract the text from the invoice document and output it in JSON format. Output the JSON directly.

### Field descriptions
- invoice_type: triplicate/duplicate/electronic/cash register
- For fields that cannot be recognized, keep the field and use null as the value

### Output example
<example>
```json
{
  "status": "success",
  "data": {
     "invoide_type": "三聯式"
     "invoice_number": "AB-12345678",
     "invoice_date": "112/11/17",
     "invoice_time": "14:30:00",
     "seller": {
       "tax_id": "12345678",
       "company_name": "好吃餐廳股份有限公司",
       "address": "台北市信義區信義路五段100號"
     },
     "buyer": {
       "tax_id": "87654321",
       "company_name": "測試科技股份有限公司"
     },
     "items": [
       {
         "description": "商業午餐A餐",
         "quantity": 2,
         "unit": "份",
         "unit_price": 120,
         "amount": 240
       },
       {
         "description": "紅茶",
         "quantity": 2,
         "unit": "杯",
         "unit_price": 30,
         "amount": 60
       }
     ],
     "amounts": {
       "sales_amount": 300,
       "tax_amount": 15,
       "total_amount": 315
     },
   }
}
```
</example>

> Failure example
<example>
```json
{
  "status": "failed",
  "error": {
    "code": "IMAGE_ANALYSIS_ERROR",
    "message": "Unable to correctly recognize the business card image",
    "details": "Image quality is insufficient or the format does not meet requirements"
  }
}
```
</example>
````


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.maiagent.ai/maiagent-user-guide/en/work/image-vision/invoice-recognition.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
