LLM-readable documentation index

Use Structured Memory with the API

Read a supplier profile during extraction and save the invoice to Structured Memory

Hand off to an LLM

Use Structured Memory to fill in a missing currency from a supplier profile and save the extracted invoice in the same bucket. Set BEM_API_KEY to an API key for the environment where you want to try this.

Create a bucket

Create one bucket for supplier supplier-1042.

curl -X POST https://api.bem.ai/v3/structured-memory/buckets \
  -H "Content-Type: application/json" \
  -H "x-api-key: $BEM_API_KEY" \
  -d '{"key": "supplier-1042", "displayName": "Supplier 1042"}'

A 201 response confirms the bucket was created.

Create the kinds

Create a supplier-profile kind for the facts you keep about the supplier.

curl -X POST https://api.bem.ai/v3/structured-memory/kinds \
  -H "Content-Type: application/json" \
  -H "x-api-key: $BEM_API_KEY" \
  -d '{
    "name": "supplier-profile",
    "attributesSchema": {
      "type": "object",
      "properties": {
        "supplierName": { "type": "string" },
        "currency": { "type": "string" },
        "paymentTermsDays": { "type": "integer" }
      },
      "required": ["supplierName", "currency", "paymentTermsDays"]
    }
  }'

Create an invoice kind for the invoices the workflow saves. Use the same schema for this kind and the Extract function.

curl -X POST https://api.bem.ai/v3/structured-memory/kinds \
  -H "Content-Type: application/json" \
  -H "x-api-key: $BEM_API_KEY" \
  -d '{
    "name": "invoice",
    "attributesSchema": {
      "type": "object",
      "properties": {
        "invoiceNumber": { "type": "string" },
        "invoiceDate": { "type": "string" },
        "supplierName": { "type": "string" },
        "currency": {
          "type": "string",
          "description": "ISO 4217 currency code. When the invoice states no currency, use the currency of the supplier profile."
        },
        "totalAmount": { "type": "number" }
      },
      "required": ["invoiceNumber", "invoiceDate", "supplierName", "currency", "totalAmount"]
    }
  }'

Each request returns 201 and creates version 1 of the kind.

Add the supplier profile

Save an entry with the supplier's default currency.

curl -X POST https://api.bem.ai/v3/structured-memory/buckets/supplier-1042/entries \
  -H "Content-Type: application/json" \
  -H "x-api-key: $BEM_API_KEY" \
  -d '{
    "kindName": "supplier-profile",
    "kindVersionNum": 1,
    "key": "profile",
    "attributes": {
      "supplierName": "Example Packaging Ltd.",
      "currency": "EUR",
      "paymentTermsDays": 30
    }
  }'

The response is 201 with the entry. If the attributes don't match the kind version's schema, the response is 400 with MEMORY_ATTRIBUTES_INVALID.

Create the functions

Create a Read Memory function. It has no configuration.

curl -X POST https://api.bem.ai/v3/functions \
  -H "Content-Type: application/json" \
  -H "x-api-key: $BEM_API_KEY" \
  -d '{"functionName": "supplier-memory-read", "type": "read_memory"}'

Create the Extract function with the invoice schema from step 2.

curl -X POST https://api.bem.ai/v3/functions \
  -H "Content-Type: application/json" \
  -H "x-api-key: $BEM_API_KEY" \
  -d '{
    "functionName": "invoice-extract",
    "type": "extract",
    "outputSchemaName": "invoice",
    "outputSchema": {
      "type": "object",
      "properties": {
        "invoiceNumber": { "type": "string" },
        "invoiceDate": { "type": "string" },
        "supplierName": { "type": "string" },
        "currency": {
          "type": "string",
          "description": "ISO 4217 currency code. When the invoice states no currency, use the currency of the supplier profile."
        },
        "totalAmount": { "type": "number" }
      },
      "required": ["invoiceNumber", "invoiceDate", "supplierName", "currency", "totalAmount"]
    }
  }'

Create a Write Memory function that saves entries of invoice version 1.

curl -X POST https://api.bem.ai/v3/functions \
  -H "Content-Type: application/json" \
  -H "x-api-key: $BEM_API_KEY" \
  -d '{
    "functionName": "invoice-memory-write",
    "type": "write_memory",
    "writeMemoryConfig": { "kindName": "invoice", "kindVersionNum": 1 }
  }'

Create the workflow

Set Extract as the main node. Connect Read Memory to Extract as context, then connect Extract to Write Memory.

curl -X POST https://api.bem.ai/v3/workflows \
  -H "Content-Type: application/json" \
  -H "x-api-key: $BEM_API_KEY" \
  -d '{
    "name": "supplier-invoices",
    "mainNodeName": "extract",
    "nodes": [
      { "name": "read", "function": { "name": "supplier-memory-read" } },
      { "name": "extract", "function": { "name": "invoice-extract" } },
      { "name": "write", "function": { "name": "invoice-memory-write" } }
    ],
    "edges": [
      { "sourceNodeName": "read", "destinationNodeName": "extract" },
      { "sourceNodeName": "extract", "destinationNodeName": "write" }
    ]
  }'

Optional webhook for saved entries

To receive each saved entry at your webhook URL, subscribe to the Write Memory function.

curl -X POST https://api.bem.ai/v3/subscriptions \
  -H "Content-Type: application/json" \
  -H "x-api-key: $BEM_API_KEY" \
  -d '{
    "name": "invoice-entries",
    "type": "write_memory",
    "functionName": "invoice-memory-write",
    "webhookURL": "https://your-app.example.com/webhooks/bem"
  }'

To verify deliveries, see Webhooks.

Call the workflow

Save this invoice as invoice.txt. It states no currency.

INVOICE

Invoice number: INV-2041
Invoice date: 2026-11-20
From: Example Packaging Ltd.
Bill to: Example Retail Inc.

Description              Qty   Unit price     Amount
Packing tape, 48 mm       40         3.25     130.00
Shipping boxes, large    200         5.60   1,120.00

Total                                       1,250.00

Call the workflow with the supplier profile in memory.read and the new invoice entry in memory.write.

curl -X POST https://api.bem.ai/v3/workflows/supplier-invoices/call \
  -H "x-api-key: $BEM_API_KEY" \
  -F "wait=true" \
  -F "callReferenceID=INV-2041" \
  -F "file=@invoice.txt" \
  --form-string 'memory={"read": ["bem://memory/buckets/supplier-1042?kind=supplier-profile"], "write": "bem://memory/buckets/supplier-1042/entries/INV-2041"}'

In a multipart call, memory is a JSON object string. In a JSON call, memory is an object in the body.

{
  "input": {
    "singleFile": {
      "inputType": "text",
      "inputContent": "<base64 of invoice.txt>"
    }
  },
  "memory": {
    "read": ["bem://memory/buckets/supplier-1042?kind=supplier-profile"],
    "write": "bem://memory/buckets/supplier-1042/entries/INV-2041"
  }
}

Open call.outputs[0] to find the write_memory event. The saved entry includes the currency from the supplier profile. The response below omits unrelated fields.

{
  "call": {
    "callID": "wc_3Mw7Wss4M3p3Jiro2tpmdJyn6dr",
    "status": "completed",
    "workflowName": "supplier-invoices",
    "callReferenceID": "INV-2041",
    "outputs": [
      {
        "eventID": "evt_3Mw7X9PAp6lflibSgPOKNfXaeQY",
        "eventType": "write_memory",
        "functionName": "invoice-memory-write",
        "callID": "wc_3Mw7Wss4M3p3Jiro2tpmdJyn6dr",
        "entry": {
          "id": "ment_3Mw7X8EzIrLiKFmYeb1aTczcTd3",
          "key": "INV-2041",
          "bucketKey": "supplier-1042",
          "kindName": "invoice",
          "kindVersionNum": 1,
          "attributes": {
            "currency": "EUR",
            "invoiceDate": "2026-11-20",
            "totalAmount": 1250,
            "supplierName": "Example Packaging Ltd.",
            "invoiceNumber": "INV-2041"
          },
          "producingCallID": "wc_3Mw7Wss4M3p3Jiro2tpmdJyn6dr"
        }
      }
    ],
    "errors": []
  }
}

When the memory input doesn't fit the workflow, the call returns 400 and runs no function. See Structured Memory error codes.

Read the saved entry

Fetch the invoice from the supplier bucket.

curl https://api.bem.ai/v3/structured-memory/buckets/supplier-1042/kinds/invoice/entries/INV-2041 \
  -H "x-api-key: $BEM_API_KEY"

The response is the entry, with its createdAt:

{
  "id": "ment_3Mw7X8EzIrLiKFmYeb1aTczcTd3",
  "key": "INV-2041",
  "bucketKey": "supplier-1042",
  "kindName": "invoice",
  "kindVersionNum": 1,
  "attributes": {
    "currency": "EUR",
    "invoiceDate": "2026-11-20",
    "totalAmount": 1250,
    "supplierName": "Example Packaging Ltd.",
    "invoiceNumber": "INV-2041"
  },
  "producingCallID": "wc_3Mw7Wss4M3p3Jiro2tpmdJyn6dr",
  "createdAt": "2026-12-02T22:16:52.536433Z"
}

Check which entries the call selected

Get the call trace and inspect trace.readCapture for the selectors and matching entries. The response below shows only readCapture.

curl https://api.bem.ai/v3/calls/wc_3Mw7Wss4M3p3Jiro2tpmdJyn6dr/trace \
  -H "x-api-key: $BEM_API_KEY"
{
  "trace": {
    "readCapture": {
      "selectors": ["bem://memory/buckets/supplier-1042?kind=supplier-profile"],
      "entries": [
        {
          "id": "ment_3Mw7U5psoXhQtQUZvUl091r8Dro",
          "key": "profile",
          "bucketKey": "supplier-1042",
          "kindName": "supplier-profile",
          "kindVersionNum": 1,
          "attributes": {
            "currency": "EUR",
            "supplierName": "Example Packaging Ltd.",
            "paymentTermsDays": 30
          }
        }
      ]
    }
  }
}

Verify webhook delivery

If you subscribed to the Write Memory function, compare the webhook body with outputs[0]. Both have the same eventID and entry. Use eventID to detect duplicate deliveries.

On this page