A prompt can interpret which fields matter and explain a computed result, but application code should perform exact arithmetic when the operation is known. Parse units, currency, and missing values before calculation. The model receives the computed total and the source rows; it may explain the rule but cannot silently override the number. For money, use decimal arithmetic with an explicit rounding rule. A mismatch between model prose and the computed value is a validation failure, not an invitation to average both answers.
Numeric prompts: let code calculate and the model explain
Decision in practice
An invoice contains two valve assemblies at 47.25 each and three seals at 19.40 each. Code computes 152.70. The assistant explains that the total excludes tax because the input has no tax jurisdiction. A second invoice has an unknown quantity and is routed to review rather than inventing a number. The test checks the amount, row IDs, and uncertainty state. If the assistant writes 152.70 but cites only one row, the explanation fails the evidence check even though the arithmetic is correct.
from decimal import Decimal
line_items = [
{"sku": "VALVE-47", "quantity": 2, "unit_price": "47.25"},
{"sku": "SEAL-19", "quantity": 3, "unit_price": "19.40"},
]
invoice_total = sum(
(Decimal(item["unit_price"]) * item["quantity"] for item in line_items),
Decimal("0"),
)
print(invoice_total) # 152.70Performance and operating cost
A single pass over N line items costs O(N) time and O(1) extra space beyond the input list. Decimal arithmetic avoids binary floating-point surprises for this fixed-price example, though tax and rounding rules still need their own contract. A tool call has overhead, but a deterministic subtotal is cheaper to verify than repeated model reasoning. Test empty lists, negative adjustments, unknown quantities, and unit mismatches. Do not present an untaxed subtotal as a final payable amount.
Common Mistakes
- Do not ask a model to guess a missing quantity.
- Do not use binary floating point for a money contract without a rounding policy.
- Do not let prose overwrite the computed total.
Connected lessons
- Prompt Engineering
- Prompt patterns
- Prompt inputs: normalize records before asking for conclusions
- Prompt decomposition: split stages at verifiable handoffs
- Document chunks: preserve the clause and its governing exception
- Project: measure a retrieval-backed answer gate
- Prompt evidence and output decisions
Continue with: Program-aided prompting: make computation executable and bounded.
Continue with: Aggregation prompts: pin the denominator and recompute the rate.
Continue with: Study effects: calculate the measure outside the model.
Continue with: Capacity prompts: check units, headroom, and a failed worker.
Continue with: Location prompts: compute stop-window feasibility before recommending.
Continue with: Forecast prompts: label intervals and check coverage.
Continue with: Analytics prompts: review read-only queries and totals.
Continue with: Invoice prompts: recalculate typed charges.
