
The fastest way to make an LLM agent unsafe is to let it decide what happens next. The fastest way to make it safe is to give it one job, a strict contract and a process that decides everything else.
That is the idea behind running agents inside BPMN service tasks. The engine owns sequence, approvals, timers and history; the agent does bounded work inside one step. Here is the pattern, using invoice coding as the example. For the wider case for BPM in software and how it compares with suites and low-code tools, read the full guide to BPM in software.
The example flow
Supplier invoices need a general ledger code and a purchase order match. The target flow:
-
Service task: extract invoice fields and propose a GL code (agent).
-
Service task: validate the output deterministically.
-
Business rule task: decide the route using confidence, amount and vendor risk.
-
Exclusive gateway: auto-post, or create a user task for review.
-
Service task: post to the ERP.
The agent never posts anything. It proposes.
Rule 1: the agent step has a contract, not a prompt
Treat the agent like any other service. Define inputs and a typed output, and reject anything that does not conform before the process sees it.
{
"_comment": "Illustrative output schema for the extract-and-code step",
"type": "object",
"required": ["invoiceNumber", "vendorId", "total", "currency", "glCode", "confidence", "evidence"],
"properties": {
"invoiceNumber": { "type": "string" },
"vendorId": { "type": "string" },
"total": { "type": "number" },
"currency": { "type": "string", "pattern": "^[A-Z]{3}$" },
"glCode": { "type": "string" },
"confidence": { "type": "number", "minimum": 0, "maximum": 1 },
"evidence": { "type": "array", "items": { "type": "string" } }
}
}
evidence matters. It holds the snippets the model relied on, so a reviewer can check the reasoning against the source document rather than trusting a number.
A few worker-side guardrails, in illustrative pseudo-code:
# Illustrative worker for the "ai-code-invoice" job type
def handle(job):
doc = fetch_document(job.vars["documentId"])
result = llm.call(
prompt_id="invoice-coding",
prompt_version=PROMPT_VERSION,
input=doc,
tools=["lookup_vendor", "lookup_po"], # read-only tools only
timeout_s=30,
)
parsed = validate_against_schema(result) # raise on failure
if parsed.glCode not in allowed_gl_codes(parsed.vendorId):
raise BpmnError("GL_CODE_NOT_ALLOWED")
return {**parsed, "modelId": result.model_id, "promptVersion": PROMPT_VERSION}
Note the read-only tool list. An agent inside a service task should not have write access to the systems the process itself will update later.
Rule 2: confidence is an input to a rule, not a decision
Models report confidence badly, and a raw self-reported score is not a probability. Use it as one signal among several, and put the thresholds in a versioned DMN table, never in the prompt.
Illustrative DMN: InvoiceRoute (hit policy: FIRST)
| vendorRisk | amount | confidence | validationFlags | -> route |
|------------|-----------|------------|-----------------|-------------------|
| high | any | any | any | senior-review |
| any | > 25000 | any | any | senior-review |
| any | any | any | not empty | clerk-review |
| low | <= 5000 | >= 0.9 | empty | auto-post |
| any | any | any | any | clerk-review |
Calibrate thresholds from real outcomes. Run in shadow mode first: the agent proposes, humans still do the work, and you compare. Only enable auto-post for the segment where agreement is consistently high, and widen it gradually.

Rule 3: every failure has a modelled path
LLM steps fail in more ways than normal services. Model each one explicitly with boundary events instead of hoping retries fix it:
-
Timeout or provider error: engine retries with backoff. After retries, route to a fallback: a smaller model, a different provider, or straight to the review task.
-
Schema violation: retry once with the validation error appended, then route to review. Do not loop.
-
Business rule violation (disallowed GL code): throw a BPMN error caught by a boundary event leading to review.
-
Downstream failure after posting: compensation handler reverses the ERP entry.
An agent failure should never stall silently. It should land in a human queue with context.
Rule 4: the reviewer sees what the agent saw
The user task form should show the source document, extracted fields, proposed code, evidence snippets and the reason it was routed for review. The reviewer can accept, edit or reject.
Record the outcome as process variables: reviewAction, finalGlCode, overrideReason. Overrides become the evaluation set for the next prompt or model version, and a rising override rate signals drift.
Rule 5: audit by construction
Because every agent call is a step in a process instance, the history already captures timing and path. Add the variables that let you reconstruct any decision later:
-
process and decision model versions
-
modelIdandpromptVersion -
input document reference and a hash of the input
-
raw output (or a reference to where it is stored) and parsed output
-
route chosen, reviewer identity, action and timestamps
Regulations such as the EU AI Act emphasise documentation, human oversight and traceability for higher-risk uses. Take specific legal advice on your obligations, but this structure gives you evidence without retrofitting logging later.
Why this beats an autonomous agent loop
An agent that plans its own steps is hard to predict, test and explain. Here, flow is deterministic and covered by ordinary process tests, and the agent is swappable: change provider or prompt version, rerun the shadow comparison, and nothing else moves.
Getting the contracts, evaluations and fallbacks right is most of the work, which is why teams often bring in help with LLM integration for the first governed flow and then reuse the pattern.
Frequently Asked Questions
Why not let the agent decide the whole workflow?
Autonomous sequencing is non-deterministic and hard to test or audit. Keeping control flow in BPMN makes behaviour predictable while the agent handles bounded tasks.
Should confidence thresholds live in the prompt?
No. Put them in a versioned decision table so business owners can change them, and every instance records which version applied.
How do I choose an initial confidence threshold?
Run the agent in shadow mode against real cases, compare with human outcomes, and enable automation only where agreement is consistently high.
What should happen when the model returns invalid output?
Validate against a schema, retry once with the error, then route to a human review task. Never pass unvalidated output downstream.
What should be logged for AI steps?
Model and prompt versions, input references, raw and parsed outputs, route taken, reviewer action and timestamps, stored as process variables.
Can Camunda or Flowable run this pattern?
Yes. Both support service tasks, DMN decision tables, boundary events, user tasks and compensation, which are the building blocks this pattern needs.