# Failures and scaling

> What happens when a provider, the database, the ledger or a copy of the gateway fails, and how it grows.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://ai.docs.sypnia.com/architecture/failures-and-scaling/

For agents: Provider down/slow: route fallbacks; circuit breaker per provider+model (opens after 5 failures in 30 s, half-open after 30 s), health in GET /health and Telemetry. Postgres unavailable: calls with cached key/route continue for up to 5 min using local conservative limits (half of per-minute limits, caps checked against last known totals), records spooled locally/SQS and written after; browser tokens and end-user-limited routes fail closed (503). Ledger unavailable (Cloud): holds queued; calls allowed up to the organization's last known balance minus outstanding holds; never past spending limits. Usage queue failure: DLQ, replayed; usage never lost or double-counted (idempotent call id). Gateway instance dies mid-stream: client gets a cut stream; charged for written tokens; hold released by a sweeper after 15 min. Scaling: stateless gateway, Lambda concurrency; calls table partitioned by day.

| What fails                            | What happens                                                                                                                                                                                                                                                                                                                    |
| ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **A model or provider**               | the route's fallbacks take over. After 5 failures in 30 seconds the gateway stops sending to that model for 30 seconds, then tries again with one call. `GET /health` and Telemetry show it.                                                                                                                                    |
| **Postgres**                          | calls whose key and route are already cached keep going for up to 5 minutes, with stricter limits (half the per-minute limits; caps checked against the last known totals). Records wait and are written when it is back. Browser tokens and routes with per-person limits stop with `503` rather than run without their limit. |
| **The credits ledger** **\[Cloud]**   | holds wait in a queue; calls continue only up to your last known balance minus what is already held, and never past a spending limit                                                                                                                                                                                            |
| **The usage queue** **\[Cloud]**      | records go to a dead-letter queue and are replayed; each record carries its call id, so nothing is lost or counted twice                                                                                                                                                                                                        |
| **A copy of the gateway, mid-answer** | your app sees the stream end early; only the tokens written are charged, and a sweep releases the rest of the hold within 15 minutes                                                                                                                                                                                            |

## Growing

The gateway keeps no state between calls, so more traffic means more copies of it: in Sypnia Cloud, more Lambda instances. Call records are split by day, so dropping old days costs nothing, and usage totals are summed once a day.
