Skip to content
solvepass
Back to blog
BillingNovember 15, 20265 min

How captcha credits work: reserve, capture, release

Charging at task creation is simple and wrong. The hold model keeps concurrent tasks from overspending an account and makes failed solves free.

The problem with charging at creation

If you charge when a task is created, a failed solve costs the customer. If you charge when it finishes, concurrent tasks can overspend the balance before the first charge lands. Both are avoidable with a ledger that separates reservation from settlement.

Solvepass uses a hold. At creation, the price is reserved from the available balance. On success, the hold is captured. On failure, expiry, or cancel, the hold is released. The customer is charged only for a completed task.

The three states

A hold has one terminal state. The invariant is simple: every task has at most one hold, and every hold ends as captured, released, or expired. The organization balance and the reserved amount are updated in the same transaction as the ledger row.

  • Reserve: available = balance - reserved. Reject if available < price.
  • Capture: balance -= price; reserved -= price. Write a negative ledger row.
  • Release: reserved -= price. Write a zero-delta ledger row for audit.
reserve.sqlsql
BEGIN;
SELECT balance_micros, reserved_micros
FROM organizations
WHERE id = $org
FOR UPDATE;

-- available = balance_micros - reserved_micros
-- reject if available < $price

UPDATE organizations
SET reserved_micros = reserved_micros + $price
WHERE id = $org;

INSERT INTO credit_holds (org_id, task_id, amount_micros, status, expires_at)
VALUES ($org, $task, $price, 'held', now() + interval '2 minutes');
COMMIT;

Why the customer sees a reservation

The dashboard shows balance, reserved, and available. A task that is still pending is not a charge; it is a reservation. That distinction matters when a customer runs a burst of tasks and checks the balance before the first result arrives.

The ledger is append-only. A capture is a negative delta. A release is a zero delta with a reference to the hold, so support can reconstruct the story without guessing.

Idempotency is part of the billing model

A network timeout after task creation is common. If the client retries without an idempotency key, it creates a second task and a second hold. Solvepass accepts an Idempotency-Key header and returns the original task when the key and body match.

The same idea applies to payment webhooks. Provider event IDs are stored uniquely, so a duplicate delivery cannot credit the balance twice.

headershttp
POST /v1/tasks
Authorization: Bearer sp_live_...
Idempotency-Key: order-1042-attempt-1
Content-Type: application/json

What to test

Test the failure path, not just the happy path. Create a task with insufficient credits. Create two tasks that together exceed the balance. Expire a task and confirm the reservation is released. Send the same idempotency key twice and confirm one charge.

Related