# Architecture

> How Sypnia AI is built: the gateway's parts, where Rust runs, and how they connect.

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

For agents: AI architecture: one Rust binary, the gateway (axum, tokio, reqwest; streaming proxy): API (OpenAI-compatible + Sypnia endpoints), router (routes, fallbacks), limiter (token buckets with leases from Postgres) and meter (reservations, charges), safety (redaction, moderation), provider adapters (OpenRouter, Bedrock via aws-sdk, OpenAI-compatible), OTLP exporter, MCP server. Postgres for routes, keys, limits, call records, usage; object storage only for opted-in bodies and the response cache. Cloud adds the credits ledger (shared with the suite), the console (TypeScript, private) and Sypnia's provider accounts. @sypnia/ai TypeScript MIT.

**Figure**: Sypnia AI: your app and Sypnia's products call the gateway; routes choose the model, limits and the meter guard the credits, and every call becomes a span.

1. Your app, or a Sypnia product, calls the API with a task.
2. The router reads the task's route: model, fallbacks, instructions, options.
3. Limits are checked and the most the call can cost is reserved; after the call, the real cost is charged.
4. The call goes to the provider, and the answer streams back.
5. The call becomes a span in Telemetry; its record and usage go to Postgres and, in Sypnia Cloud, to your credits.

The gateway is one Rust binary with the API, the router, the limiter and meter, the safety checks, the provider adapters, the OpenTelemetry exporter and the MCP server. It keeps routes, keys, limits, call records and usage in Postgres. Object storage is used only for what a route chose to keep and for the response cache.

| Part                                                                              | Language                                             | Open source   |
| --------------------------------------------------------------------------------- | ---------------------------------------------------- | ------------- |
| Gateway: API, router, limiter and meter, safety, provider adapters, exporter, MCP | Rust (axum, tokio, reqwest; the AWS SDK for Bedrock) | yes, AGPL-3.0 |
| `@sypnia/ai` and the `ai` command                                                 | TypeScript                                           | yes, MIT      |
| Credits ledger and billing                                                        | the suite's shared ledger                            | no            |
| Console (`ai.app.sypnia.com`)                                                     | TypeScript (React)                                   | no            |

## Why Rust

Every model call of the suite passes through the gateway, and many are long streams. Rust keeps the time the gateway adds to a call to a few milliseconds, holds thousands of open streams in little memory, and makes the code that counts credits hard to get wrong.

- [Request paths](/architecture/request-paths/): What happens inside the gateway for a call, a streamed call, a browser call and a route change.
- [Data model](/architecture/data-model/): The tables of the gateway's Postgres schema and what each holds.
- [Reference implementation on AWS](/architecture/reference-implementation-aws/): Every AWS service Sypnia AI uses in Sypnia Cloud, and how.
- [Failures and scaling](/architecture/failures-and-scaling/): What happens when a provider, the database, the ledger or a copy of the gateway fails, and how it grows.
- [Codebase](/architecture/codebase/): How the open-source repository is organized.
