- Home
- Skills
- Agents & Orchestration
- Safe Money Handling
More screenshots
Works with the AI Agents you already use
Safe Money Handling
A float turns 0.29 into 28.999…, a three-way split loses a cent, a tax line doesn't match the bank statement, a retried job posts the same interest twice.
$6.99
Safe Money Handling
Example session with this skill installed
Our app credits monthly interest on savings accounts (3% annual, 19% tax withheld at source). Users say the amounts are a few cents off from their bank statement, and last month a few accounts got October's interest twice. Here's the current code:
const avg = balances.reduce((a, b) => a + b, 0) / balances.length / 100
const interest = avg * 0.03 / 12
const net = interest * 0.81
await db.insert('transactions', { account_id, amount: net, date: firstOfMonth })
Fix it.
- Read your context and instructions
- Compiled the safe money handling
Diagnosis
Four money bugs are mixed together here
| # | Problem | Effect |
|---|---|---|
| 1 | avg × rate ÷ 12 treats every month as 1/12 of a year | A 31-day month earns too little and February too much; banks accrue daily (ACT/365) |
| 2 | Euros as floats (/ 100, * 0.81) | Drift such as 11.149999…; the stored value depends on float rounding |
| 3 | Net derived as interest × 0.81 | Tax is never rounded on its own, so gross ≠ tax + net and nothing reconciles with the bank |
| 4 | Insert without claiming the period | Two overlapping runs (page loads, cron, retries) both post October |
Fix
Integer cents end to end, rates as integers, one rounding point, tax rounded up, and the period claimed before writing.
// lib/money.ts — the only module that does money arithmetic
export function accruedInterest(dailyBalancesCents: number[], rateBasisPoints: number, dayCount = 365): number {
const sum = dailyBalancesCents.reduce((acc, b) => acc + BigInt(Math.max(b, 0)), 0n)
const denominator = 10_000n * BigInt(dayCount)
return Number((2n * sum * BigInt(rateBasisPoints) + denominator) / (2n * denominator)) // round half up, once
}
export function withholding(grossCents: number, ratePermille: number) {
if (grossCents <= 0) return { gross: 0, tax: 0, net: 0 }
const tax = Number((BigInt(grossCents) * BigInt(ratePermille) + 999n) / 1000n) // tax rounded up
return { gross: grossCents, tax, net: grossCents - tax } // gross === tax + net, always
}
// jobs/credit-interest.ts
const { rowCount } = await db.query(
`update accounts set interest_next_date = $1
where id = $2 and interest_next_date = $3`, // claim the period first
[nextDate, accountId, paymentDate]
)
if (rowCount === 0) return // another run already took this period
const gross = accruedInterest(dailyEndOfDayBalancesCents, 300) // 3.00 %
const { tax, net } = withholding(gross, 190) // 19 %
try {
await db.insert('transactions', {
account_id: accountId,
amount_cents: net,
date: paymentDate,
description: `October interest · ${fmt(gross)} gross − ${fmt(tax)} withholding (19 %)`,
})
} catch (e) {
await db.query('update accounts set interest_next_date = $1 where id = $2', [paymentDate, accountId]) // release
throw e
}
Tests
test('31 days at 5,405.75 and 3 % → 13.77 gross, 2.62 tax, 11.15 net', () => {
const gross = accruedInterest(Array(31).fill(540_575), 300)
expect(gross).toBe(1377)
expect(withholding(gross, 190)).toEqual({ gross: 1377, tax: 262, net: 1115 })
})
test('gross always equals tax + net', () => {
for (let g = 1; g < 5000; g++) {
const r = withholding(g, 190)
expect(r.tax + r.net).toBe(g)
}
})
test('overdrawn days earn nothing', () => {
expect(accruedInterest([100_000, -50_000], 365)).toBe(10)
})
Next steps
- Migrate
amounttoamount_cents bigintand addcheck (amount_cents <> 0). - Check one real statement against the new numbers. Some banks round tax differently, so adjust
withholdingif they do. - Existing credits are not recalculated. Correct the duplicated October rows by hand (or soft-delete them) after deploying.
Connects securely to your tools. The creator never sees your data.
About this skill
Money bugs are quiet. A float turns 0.29 into 28.999…, a three-way split loses a cent, a tax line doesn't match the bank statement, a retried job posts the same interest twice. Nobody notices until a user does. This skill gives your agent one clear set of rules for any code that touches money, plus tested reference code to copy.
What it does
- Integer cents everywhere: amounts stored as
bigintminor units, aggregated in SQL, BigInt for large intermediate products. - One conversion module: user input parsed from the string straight into cents (
1.234,56,1,234.56,12,50 €), rejecting extra decimals instead of rounding them away. - Validation in two places: schema at the edge, plus database constraints for clients that can reach your API directly.
- Server-side signs: users type positive amounts; the server signs them from the entry type. Transfers stay out of income and expense totals.
- One rounding policy: round once per calculation. Tax and fees reconcile exactly (gross = tax + net), and splits always add up to the total (largest remainder).
- Interest done like a bank: daily accrual with an explicit day-count convention and integer rates.
- Idempotent postings: recurring entries, interest and fees claim their period before writing, so overlapping runs never duplicate.
- No silent truncation: totals are never computed over a capped result set.
- Mandatory tests: a checklist of the edge cases every money change must cover, with a pinned timezone.
What's included
SKILL.md: the rules and a review checklist your agent applies while writing or reviewing code.reference/money.ts: dependency-free implementations (parseAmountToMinor,applySign,allocate,withholding,accruedInterest,exceedsPercent).reference/money.test.ts: tests you can run withnode --test. No install.
Who it's for
Anyone building a budgeting app, a SaaS with invoices or payouts, a marketplace that splits payments, or anything that must match a bank statement to the cent.
Extracted from a personal-finance app in production, where these rules were added one real bug at a time.
Guidance plus reference code, not a library: your agent adapts the functions to your codebase. No currency conversion or FX rates, and amounts in different currencies are never mixed. Reference parsing and the amount cap assume 2-decimal currencies (the parser takes a decimals parameter; adjust the cap for 0- or 3-decimal currencies). Tax rounding rules vary by country: the default (withholding rounded up) must be checked against your bank or tax authority. SQL examples use PostgreSQL syntax. Optional tests need Node.js 22.18+.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before any payment.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
Fresh listing
Recently published to Agensi
Money-back guarantee
Doesn't do what its page promises? You get your money back.
Trust & safety
Security scan
Security scan passed 11 days ago