One operation, three scans: capacity planning for an AI security layer
One customer message can become three security decisions. How we size Koreshield for that fan-out, why overload gets a fast, retryable 503, and what a load test must capture before it counts as evidence.
The arithmetic most capacity plans miss
A protected support operation is not one request to a security layer. It can be three: the customer's message is checked before the model reads it, the retrieved context is checked before it enters the prompt, and the proposed action is checked before anything executes.
So a burst of 500 customers writing in at once can become roughly 1,500 scan requests. Plan for 500 and the security layer becomes the bottleneck at exactly the moment the support team is busiest. That is why Koreshield plans capacity per protected operation, not per request.
We also say plainly where we started. Our first production baseline covered two requests per second, and an isolated burst test showed something worse than slowness: with the default database pool exhausted, the synchronous cleanup that returns connections could itself be starved, so the service struggled to recover. Everything below came out of fixing that.

A fast no beats a slow maybe
When a security layer is saturated, there are two ways to behave. It can queue requests and let them wait, or it can admit what it can serve and refuse the rest quickly. Koreshield refuses quickly.
Each API worker has a ceiling on in-flight scans. Above it, the scan is rejected immediately with a 503 and a Retry-After header, and the rejection is counted in our metrics. Database pool waits are short, three seconds by default, so a request that cannot get a connection fails early instead of hanging for tens of seconds.
There is one detail that is easy to get wrong. The server's own socket concurrency limit is set well above the application's scan ceiling. If it were lower, overload would show up as reset connections, which a client cannot tell apart from a network fault. With the order right, overload always reaches the response path and the client gets an explicit, retryable answer.
- Retryable means bounded. Our SDKs retry network errors and server errors once by default. On 429 and 503 they honour Retry-After, capped at five seconds.
- Authentication and validation errors are never retried, and they never fail open.
- If the retries run out, the workflow's approved fallback applies: by default, input and context checks continue marked degraded, and proposed actions are blocked marked degraded.
Workers, pools and connections are one budget
The API runs four Uvicorn worker processes by default. Each worker has its own bounded SQLAlchemy pool: ten connections plus five overflow by default. Multiply them out and the total must stay below what PostgreSQL is configured to accept.
Changing one of those numbers alone just moves the failure. More workers with the same pools can exhaust the database under load. Bigger pools with the same database produce the same failure later and less legibly. And every additional worker holds its own copy of the in-memory detection corpus, so it costs memory as well as connections.
Two smaller fixes mattered as much as the big numbers. Request sessions are now closed from an asynchronous finalizer, so cleanup cannot wait behind saturated worker threads. And API-key and trial rate-limit writes reuse the request's own session, so a request never holds one transaction while opening a second connection just to increment a limiter bucket.
The rule we hold ourselves to: worker count, pool sizes, PostgreSQL connections and load-test thresholds change together, or not at all.
What a load test must capture to count
Our capacity profile models the real fan-out. Each simulated client runs input, retrieved-context and proposed-action checks as one protected operation, with its own scoped key, because the per-key rate limit is part of the production contract. Sharing one key across every virtual user would measure the limiter, not the service.
The steady-state profile has explicit thresholds: individual requests at p95 under one second and p99 under two, the whole three-check operation at p95 under 2.5 seconds and p99 under four, and fewer than 1% failed or unexpected responses. A separate overload profile drives 500 concurrent clients and passes only if responses are successful decisions or well-formed 503s with Retry-After. Anything else, a reset socket or a hanging request, counts against a budget of less than 1%.
Latency alone is not evidence. A run counts only when application latency, error rate, database connections, host CPU, memory and I/O, and post-test recovery are captured together.
Tiers are earned, not inferred
A concurrency tier is approved only after load, spike, recovery and soak results meet the recorded thresholds in a production-shaped environment. Passing unit tests does not qualify a tier. Neither does a low-rate smoke test, however clean.
This is slower than quoting a big number, and it means saying "not yet" to some traffic levels. But a security layer that sits in the path of every customer conversation should be judged on its worst day, and the only honest way to describe that day is to have measured it.
What we deliberately did not do
We did not split Koreshield into microservices to scale it. One FastAPI service and one PostgreSQL database, with explicit module boundaries, keep the scan path short and keep rate limits and entitlement in a single consistency boundary. Extra network hops would have added latency to every scan for a team and a workload that do not need them.
The next infrastructure step is also written down: if host-failure tolerance becomes a contractual objective, a managed highly available database or separately operated replica comes before any service split.
Questions to ask any vendor
If you are putting a security check in the path of an AI agent, ask how many security requests one of your operations creates, what the service returns when it is saturated, which failures the client retries and for how long, and what evidence stands behind the concurrency they quote. Ask us too.