Work effectively with AI coding agents. Understand them as language models running in a tool loop with limited context, which is why they guess, game the visible signal and report confidently. Recognise the shortcut and correctness tells in their code across Python, TypeScript/JavaScript, Rust, Java and Go, and know the damage each one does later. Verify changes instead of trusting them, review diffs fast for quality and security, judge when work is truly finished, and choose, design and steer tasks so agents can finish them well.
Retrying operations that aren't idempotent
card 4 of 6
BAD: a retry around a charge
Read the loop as the provider sees it. Attempt 1 times out after the charge went through. Attempt 2 is the same request again, with nothing that ties it to attempt 1. To the provider it is a brand-new payment. The final `raise` looks careful, but it is unsafe too. Three timeouts can mean three charges, and the customer is then told the payment failed.
// Python
--- a/billing/checkout.py
+++ b/billing/checkout.py
- return pay.charge(order.total, card)
+ for attempt in range(3):
+ try:
+ return pay.charge(order.total, card)
+ except Timeout:
+ time.sleep(2 ** attempt)
+ raise PaymentError("charge timed out")
Lesson goal: Spot retry loops the agent wrapped around payments, POSTs, emails or queue publishes. Explain how they cause duplicates when the first attempt actually succeeded but its response was lost, and how an idempotency key or deduplication makes the retry safe. ·
the whole lesson